0

First of all I know this question in general have been asked and solved. I have read the complete post here: PyQt4 - Drag and Drop but my question is a little bit more specific.

What I am creating is the game Draught, but rather simplified. What I currently need help with is moving the pieces. I've managed to create a button which can be moved by holding down the left mouse button. The hard part is implementing this functionality for the button to a piece. The game is using two separate files, one called GUI-game-template and the other Game_Model.

My code so far:

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):
        super(MyGame, self).__init__()

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

        self.timer = QBasicTimer()

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

        self.setAcceptDrops(True)

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

        #Hole 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)

        self.button = Button('Button', self)
        self.button.move(100, 65)

        #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 timerEvent(self,event):
        if (event.timerId() == self.timer.timerId()):
            self.gui_board.enemy_x += 1
            self.gui_board.enemy_y += 1
            self.update()

    def dragEnterEvent(self, e):

        e.accept()

    def dropEvent(self, e):

        position = e.pos()
        self.button.move(position)

        e.setDropAction(QtCore.Qt.MoveAction)
        e.accept()

    @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_())

class Button(QtGui.QPushButton):

    def __init__(self, title, parent):
        super(Button,self).__init__(title, parent)

    def mouseMoveEvent(self, e):

        if e.buttons() != Qt.LeftButton:
            return

        mimeData = QMimeData()

        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)
        drag.setHotSpot(e.pos() - self.rect().topLeft())

        dropAction = drag.start(QtCore.Qt.MoveAction)

#Creates an instance and runs it
MyGame().run()

Next file:

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.hero_x = 2
        self.hero_y = 2

        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 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)
Community
  • 1
  • 1
Martin
  • 13
  • 5
  • So what is your question? – three_pineapples May 03 '15 at 06:37
  • @three_pineapples How do I drag and drop the red circles? – Martin May 04 '15 at 05:13
  • To be honest, your question is unclear. However, from the looks of it you don't really want to be implementing proper drag/drop but rather something using mouse click and mouse move events. This is because your circles are not actual things, but just drawings made by the call to `paintEvent()`. – three_pineapples May 04 '15 at 05:30
  • @three_pineapples That sounds reasonable. I'll try doing something like that instead. I really thought my circles were real things since they were defined in a function. – Martin May 04 '15 at 18:53
  • I should have been a little more clear (I wasn't really thinking coherently at the time). Drag/Drop if typically used when you want to move information from one widget to another (perhaps even between programs). All of your red circles are drawn in a single widget. Putting the drawing code in separate methods does not change that. – three_pineapples May 04 '15 at 22:32
  • @three_pineapples So to solve this do I need to create a new class or a new function? Or can I implement it in another way? – Martin May 05 '15 at 10:29

0 Answers0