[PyQt] QGraphicsItem collidingItems dont work

leo kirotawa kirotawa at gmail.com
Sat Oct 23 21:09:10 BST 2010


Hi,

I'm trying make a detection collision between graphicsitem, but when I use
the method collidingItems it show collisions with all object in the scene.
My scene have 800, 600 and my boundingRect to meu player object have the
same size. What I thinking is happend is that my method shape are not
override this way it just call the boundingRect and make the collision traet
with it.

A piece of my code;

BoundingRect = QtCore.QRectF(-20 - adjust, -22 - adjust,
                                 800 + adjust, 600 + adjust)

def boundingRect(self):
return Monster.BoundingRect
def shape(self):
        path = QtGui.QPainterPath()
        path.addRect(0, 0, 150, 150) #determina o shape, que sera base para
detectar colissoes.
         return path
by the way I am send all code to be easy see the problem. Just replace the
images.
[]'s


import sys
import math
from PyQt4 import QtCore, QtGui

class Monster(QtGui.QGraphicsItem):
adjust = 0.5
     BoundingRect = QtCore.QRectF(-20 - adjust, -22 - adjust,
                                 800 + adjust, 600 + adjust)
speed = 8

def __init__(self):
         QtGui.QGraphicsItem.__init__(self)
self.image = QtGui.QImage()
self.image.load("monstrinhofree.png")
self.image = self.image.scaled(100, 100, QtCore.Qt.KeepAspectRatio)
self.image2 = QtGui.QImage()
self.image2.load("monstrinhofree1.png")
self.image2 = self.image2.scaled(100,100, QtCore.Qt.KeepAspectRatio)
self.xpos, self.ypos = 344,520
self.frameControl = True
self.timer = QtCore.QTimer()
QtCore.QObject.connect(self.timer, QtCore.SIGNAL('timeout()'),
                               self.timerEvent)
self.timer.start(150)
 def boundingRect(self):
return Monster.BoundingRect
 def shape(self):
        path = QtGui.QPainterPath()
        path.addRect(0, 0, 150, 150) #determina o shape, que sera base para
detectar colissoes.
         return path


def paint(self,painter, option, widget):
 #nuvem = QtGui.QImage()
#nuvem.load("images/nuven.png")
#nuvem2 = QtGui.QImage()
#nuvem2.load("images/nuven2.png")
if self.frameControl:
painter.drawImage(self.xpos,self.ypos,self.image)
self.frameControl = False
else:
painter.drawImage(self.xpos,self.ypos,self.image2)
self.frameControl = True
#painter.drawImage(360,24,nuvem)
#painter.drawImage(504, 90,nuvem2)
#print self.xpos, self.ypos
#print dir(self.shape())
#items = self.scene().items(self.shape(), QtCore.Qt.IntersectsItemShape)
#print items
if self.collidingItems():
print self.collidingItems()[0]
#print "chocou"
control = False
#def update(self):
def timerEvent(self):
self.update()
 def keyPressEvent(self,event):
 key = event.key()
         if key == QtCore.Qt.Key_Left:
if self.xpos > 0:
         self.xpos -= Monster.speed
         elif key == QtCore.Qt.Key_Right:
if self.xpos < 680:
self.xpos += Monster.speed
elif key == QtCore.Qt.Key_Down:
if self.ypos < 520:
self.ypos += Monster.speed
elif key == QtCore.Qt.Key_Up:
if self.ypos > 0:
self.ypos -= Monster.speed
#else:
# QtGui.QGraphicsItem.keyPressEvent(self, event)
def mousePressEvent(self, event):
print "mouse funfa"


class Clouds(QtGui.QGraphicsItem):
adjust = 0.5
     BoundingRect = QtCore.QRectF(-20 - adjust, -22 - adjust,
                                 100 + adjust, 100 + adjust)
def __init__(self):
         QtGui.QGraphicsItem.__init__(self)
self.cloud1 = QtGui.QImage()
self.cloud1.load("images/nuven.png")
self.cloud2 = QtGui.QImage()
self.cloud2.load("images/nuven2.png")

def boundingRect(self):
return Clouds.BoundingRect
def shape(self):
        path = QtGui.QPainterPath()
        path.addRect(-10, -20, 228, 97) #determina o shape, que sera base
para detectar colissoes.
         return path
def paint(self,painter, option, widget):
painter.drawImage(360,24,self.cloud1)
painter.drawImage(504, 90,self.cloud2)
#if self.scene().collidingItems(self):
#print self.scene().collidingItems(self)[0]
class Scene(QtGui.QGraphicsScene):
def __init__(self):
QtGui.QGraphicsScene.__init__(self)
self.fun = None
def keyPressEvent(self,event):
self.func(event)
def setKeyPressEventItem(self,func):
self.func = func


class View(QtGui.QGraphicsView):
def __init__(self):
QtGui.QGraphicsView.__init__(self)
self.setCacheMode(QtGui.QGraphicsView.CacheBackground)
         self.setRenderHints(QtGui.QPainter.Antialiasing)
def drawBackground(self, painter, rect):
pixmap = QtGui.QImage("images/background3.png")
 #print pixmap
srect = self.sceneRect()
painter.drawImage(-5,-5,pixmap)
#print "draw"


if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    QtCore.qsrand(QtCore.QTime(0,0,0).secsTo(QtCore.QTime.currentTime()))

    scene = Scene()#QtGui.QGraphicsScene()
    scene.setSceneRect(0,0, 790, 590)
    scene.setItemIndexMethod(QtGui.QGraphicsScene.NoIndex)
    monster = Monster()
    scene.setKeyPressEventItem(monster.keyPressEvent)
    scene.addItem(monster)
    clouds = Clouds()
    scene.addItem(clouds)
    view = View()#QtGui.QGraphicsView(scene)
    view.setScene(scene)
    #view.setRenderHint(QtGui.QPainter.Antialiasing)
    #view.setBackgroundBrush(QtGui.QColor(230, 200, 167))
    #view.setRenderHints(QtGui.QPainter.Antialiasing)
    #pixmap = QtGui.QImage("images/background.png")


   # view.setBackgroundBrush(QtGui.QBrush(pixmap))
    #view.setCacheMode(QtGui.QGraphicsView.CacheBackground)
    view.setWindowTitle("Ensl Game")
    #view.setWindowTitle(QtCore.QT_TRANSLATE_NOOP(QtGui.QGraphicsView,
"Colliding Mice"))
    view.resize(800, 600)
    view.show()

-- 
Leônidas S. Barbosa (Kirotawa)
[DesenvolvedorWeb/CEFET/RN]
[Ciências da Computação/UFRN]
[pós-graduando em Inteligência Computacional/Processamento Gráfico /UFRN
[Estudante de japonês nível Intermediário I  - Japanese Student]
[Desenvolvedor em python, PyGame]
blog nerd: corecode.wordpress.com/
blog music: essenaomanja.blogspot.com
blog tirinhas: elminiche.wordpress.com/

"Mais sábio é aquele que sabe que não sabe" (Sócrates)

日本語の学生です。
コンピュータサイエンスの学位.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20101023/81a99b3c/attachment-0001.html>


More information about the PyQt mailing list