Dawnbox 0 Newbie Poster

I'm currently creating the game checkers(draughts) and have problem moving the pieces on the board. So far I have all the pieces on one side of the board ready where each piece is it's own function. For the pieces to move i've created a function called mousePressEvent. It takes the x and y coordinates for the mouse click and adds that to an empty list. Then i've created a function which checks what the first coordinates were and then checks what the next are though I get the error "list index out of range" since the coordinates for the second click haven't been added to the list yet. The game consists of two files. While I fiddled around I put the mousePressEvent in the file called Game_Model and there I could move the pieces to some coordinates that I chose myself but it didn't work with mouse clikcs. Here's my code so far for the two files:

#First file called GUI-game-template

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import Game_Model

from PyQt4 import QtCore, QtGui

app = QApplication(sys.argv)

class MyGame(QMainWindow):

    def __init__(self, parent=None):
        """Specifies the name of the application and initializes the UI"""
        super(MyGame, self).__init__()

        self.setWindowTitle("Game template") 
        self.initUI()

        self.saved_xpos = []
        self.saved_ypos = []

        self.allowed_enemy_x = [1,2,2,0,0,3,4,4,5,6,6,7]
        self.allowed_enemy_y = [6,5,7,7,5,6,5,7,6,5,7,6]

        self.timer = QBasicTimer()

    def initUI(self):
        """Initializes all components"""

        self.setAcceptDrops(True)

        self.frame = QWidget(self)
        self.setCentralWidget(self.frame)

        #Complete window
        self.layout = QVBoxLayout()

        #Widgets
        self.gui_board = Game_Model.Game_Model(self)
        self.gui_board.setLineWidth(3)
        self.gui_board.setFrameStyle(QFrame.Plain)
        self.gui_board.setMinimumSize(400,400)
        self.layout.addWidget(self.gui_board)

        self.gui_board.setFocus()

        #Buttons
        self.button_box = QHBoxLayout()
        self.button_box.addStretch(1)

        self.btn_start = QPushButton('Start', self)
        self.btn_start.clicked.connect(self.btn_start_action)
        self.button_box.addWidget( self.btn_start )

        self.btn_restart = QPushButton('Restart', self)
        self.btn_restart.clicked.connect(self.btn_restart_action)
        self.layout.addLayout(self.button_box)
        self.button_box.addWidget( self.btn_restart )
        self.button_box.addStretch(1)

        #Add layouts to widget
        self.layout.addLayout(self.button_box)
        self.frame.setLayout(self.layout)

        #Statusbar    
        self.statusBar().showMessage('Ready')

    def keyPressEvent(self, event):

        key = Qt.MouseButton()
        if(key == Qt.Key_Up):
            self.gui_board.hero_y -= 1
        if(key == Qt.Key_Down):
            self.gui_board.hero_y += 1
        if(key == Qt.Key_Left):
            self.gui_board.hero_x -= 1
        if(key == Qt.Key_Right):
            self.gui_board.hero_x += 1
        self.update()

    def mousePressEvent(self, QMouseEvent):
        # Gets the coordinates for where you click with the mouse
        xpos = int(QMouseEvent.x()/50)
        ypos = int(QMouseEvent.y()/50)
        # Adds the coordinates to an empty list
        self.saved_xpos.append(xpos)
        self.saved_ypos.append(ypos)
        print(self.saved_xpos, self.saved_ypos)
        # Updates the window  
        self.update()

    def timerEvent(self,event):
        if (event.timerId() == self.timer.timerId()):
            self.gui_board.enemy_x += 1
            self.gui_board.enemy_y += 1
            self.update()

    @pyqtSlot()
    def btn_start_action(self):
        self.timer.start(500,self)

    @pyqtSlot()
    def btn_restart_action(self):
        pass

    def run(self):
        self.show()
        sys.exit(app.exec_())
#Creates an instans and runs it
MyGame().run()

#Second file called Game_Model

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Game_Model(QFrame):

    Boardwidth = 8
    Boardheight = 8

    def __init__(self,parent):
        super(Game_Model, self).__init__()

        self.setFocusPolicy(Qt.StrongFocus)

        self.saved_xpos = []
        self.saved_ypos = []

        self.hero_x = 2
        self.hero_y = 2
        # All pieces are created
        self.enemy_x = 1
        self.enemy_y = 6

        self.enemy_x2 = 2
        self.enemy_y2 = 5

        self.enemy_x3 = 2
        self.enemy_y3 = 7

        self.enemy_x4 = 0
        self.enemy_y4 = 7

        self.enemy_x5 = 0
        self.enemy_y5 = 5

        self.enemy_x6 = 3
        self.enemy_y6 = 6

        self.enemy_x7 = 4
        self.enemy_y7 = 5

        self.enemy_x8 = 4
        self.enemy_y8 = 7

        self.enemy_x9 = 5
        self.enemy_y9 = 6

        self.enemy_x10 = 6
        self.enemy_y10 = 5

        self.enemy_x11 = 6
        self.enemy_y11 = 7

        self.enemy_x12 = 7
        self.enemy_y12 = 6

    def paintEvent(self, event):
        qp = QPainter()

        width = self.size().width() - 1
        height = self.size().height() - 1
        sq_w = width/Game_Model.Boardwidth
        sq_h = height/Game_Model.Boardheight        

        qp.begin(self)
        self.draw_grid(qp, width, height, sq_w, sq_h)
        self.draw_hero(qp, sq_w, sq_h)
        self.draw_enemy(qp, sq_w, sq_h)
        self.draw_enemy2(qp, sq_w, sq_h)
        self.draw_enemy3(qp, sq_w, sq_h)
        self.draw_enemy4(qp, sq_w, sq_h)
        self.draw_enemy5(qp, sq_w, sq_h)
        self.draw_enemy6(qp, sq_w, sq_h)
        self.draw_enemy7(qp, sq_w, sq_h)
        self.draw_enemy8(qp, sq_w, sq_h)
        self.draw_enemy9(qp, sq_w, sq_h)
        self.draw_enemy10(qp, sq_w, sq_h)
        self.draw_enemy11(qp, sq_w, sq_h)
        self.draw_enemy12(qp, sq_w, sq_h)
        qp.end()

    def mousePressEvent(self, QMouseEvent):
        # This is the same function as before since i tried moving it here and it worked better.
        # Gets the coordinates for where you click with the mouse
        xpos = int(QMouseEvent.x()/50)
        ypos = int(QMouseEvent.y()/50)
        # Add the coordinates to an empty list
        self.saved_xpos.append(xpos)
        self.saved_ypos.append(ypos)
        print(self.saved_xpos, self.saved_ypos)

        if self.saved_xpos[0] == 2 and self.saved_ypos[0] == 2:
                self.hero_x = int(self.saved_xpos[0])
                self.hero_y = int(self.saved_ypos[0])
        # Updates the window   

    def draw_grid(self, qp, width, height, sq_w, sq_h):
        for n in range(0,Game_Model.Boardheight + 1):
            qp.drawLine(0,sq_h * n, width, sq_h * n)
        for n in range(0,Game_Model.Boardwidth + 1):
            qp.drawLine(sq_w * n, 0, sq_w * n, height)

    def draw_hero(self, qp, sq_w, sq_h):
        color = QColor(0x20FA48)
        qp.fillRect( sq_w * self.hero_x + 7, sq_h * self.hero_y + 7, 
                     sq_w * 0.7, sq_h * 0.7, color)
        qp.drawRect( sq_w * self.hero_x + 7, sq_h * self.hero_y + 7, 
                             sq_w * 0.7, sq_h * 0.7)

    def draw_enemy(self,qp, sq_w, sq_h):
        qp.setBrush(QBrush(QColor('red'),Qt.SolidPattern))
        qp.drawEllipse( sq_w * self.enemy_x + 7, sq_h * self.enemy_y + 7, 
                        sq_w * 0.7, sq_h * 0.7)

    def draw_enemy2(self,qp, sq_w, sq_h):
        qp.setBrush(QBrush(QColor('red'),Qt.SolidPattern))
        qp.drawEllipse( sq_w * self.enemy_x2 + 7, sq_h * self.enemy_y2 + 7,
                        sq_w * 0.7, sq_h * 0.7)

    def draw_enemy3(self,qp, sq_w, sq_h):
        qp.setBrush(QBrush(QColor('red'),Qt.SolidPattern))
        qp.drawEllipse( sq_w * self.enemy_x3 + 7, sq_h * self.enemy_y3 + 7,
                        sq_w * 0.7, sq_h * 0.7)

    def draw_enemy4(self,qp, sq_w, sq_h):
        qp.setBrush(QBrush(QColor('red'),Qt.SolidPattern))
        qp.drawEllipse( sq_w * self.enemy_x4 + 7, sq_h * self.enemy_y4 + 7,
                        sq_w * 0.7, sq_h * 0.7)

    def draw_enemy5(self,qp, sq_w, sq_h):
        qp.setBrush(QBrush(QColor('red'),Qt.SolidPattern))
        qp.drawEllipse( sq_w * self.enemy_x5 + 7, sq_h * self.enemy_y5 + 7,
                        sq_w * 0.7, sq_h * 0.7)

    def draw_enemy6(self,qp, sq_w, sq_h):
        qp.setBrush(QBrush(QColor('red'),Qt.SolidPattern))
        qp.drawEllipse( sq_w * self.enemy_x6 + 7, sq_h * self.enemy_y6 + 7,
                        sq_w * 0.7, sq_h * 0.7)

    def draw_enemy7(self,qp, sq_w, sq_h):
        qp.setBrush(QBrush(QColor('red'),Qt.SolidPattern))
        qp.drawEllipse( sq_w * self.enemy_x7 + 7, sq_h * self.enemy_y7 + 7,
                        sq_w * 0.7, sq_h * 0.7)

    def draw_enemy8(self,qp, sq_w, sq_h):
        qp.setBrush(QBrush(QColor('red'),Qt.SolidPattern))
        qp.drawEllipse( sq_w * self.enemy_x8 + 7, sq_h * self.enemy_y8 + 7,
                        sq_w * 0.7, sq_h * 0.7)

    def draw_enemy9(self,qp, sq_w, sq_h):
        qp.setBrush(QBrush(QColor('red'),Qt.SolidPattern))
        qp.drawEllipse( sq_w * self.enemy_x9 + 7, sq_h * self.enemy_y9 + 7,
                        sq_w * 0.7, sq_h * 0.7)

    def draw_enemy10(self,qp, sq_w, sq_h):
        qp.setBrush(QBrush(QColor('red'),Qt.SolidPattern))
        qp.drawEllipse( sq_w * self.enemy_x10 + 7, sq_h * self.enemy_y10 + 7,
                        sq_w * 0.7, sq_h * 0.7)

    def draw_enemy11(self,qp, sq_w, sq_h):
        qp.setBrush(QBrush(QColor('red'),Qt.SolidPattern))
        qp.drawEllipse( sq_w * self.enemy_x11 + 7, sq_h * self.enemy_y11 + 7,
                        sq_w * 0.7, sq_h * 0.7)

    def draw_enemy12(self,qp, sq_w, sq_h):
        qp.setBrush(QBrush(QColor('red'),Qt.SolidPattern))
        qp.drawEllipse( sq_w * self.enemy_x12 + 7, sq_h * self.enemy_y12 + 7,
                        sq_w * 0.7, sq_h * 0.7)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.