Hi,<div><br></div><div>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.</div><div>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. </div>
<div><br></div><div>A piece of my code;</div><div><br></div><div><div>BoundingRect = QtCore.QRectF(-20 - adjust, -22 - adjust,</div><div>                                 800 + adjust, 600 + adjust)</div><div><br></div><div>
<div>def boundingRect(self):</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>return Monster.BoundingRect</div><div>def shape(self):</div><div><span class="Apple-tab-span" style="white-space:pre">   </span>        path = QtGui.QPainterPath()</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>        path.addRect(0, 0, 150, 150) #determina o shape, que sera base para detectar colissoes.</div><div>        <span class="Apple-tab-span" style="white-space:pre">     </span>return path </div>
</div>by the way I am send all code to be easy see the problem. Just replace the images.</div><div>[]'s</div><div><br></div><div><br></div><div><div>import sys</div><div>import math</div><div>from PyQt4 import QtCore, QtGui</div>
<div><br></div><div>class Monster(QtGui.QGraphicsItem):</div><div><span class="Apple-tab-span" style="white-space:pre">   </span>adjust = 0.5</div><div>    <span class="Apple-tab-span" style="white-space:pre">  </span>BoundingRect = QtCore.QRectF(-20 - adjust, -22 - adjust,</div>
<div>                                 800 + adjust, 600 + adjust)</div><div><span class="Apple-tab-span" style="white-space:pre">  </span>speed = 8</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">   </span>def __init__(self):</div>
<div>        <span class="Apple-tab-span" style="white-space:pre"> </span>QtGui.QGraphicsItem.__init__(self)</div><div><span class="Apple-tab-span" style="white-space:pre">           </span>self.image = QtGui.QImage()</div><div><span class="Apple-tab-span" style="white-space:pre">          </span>self.image.load("monstrinhofree.png")</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>self.image = self.image.scaled(100, 100, QtCore.Qt.KeepAspectRatio)</div><div><span class="Apple-tab-span" style="white-space:pre">          </span>self.image2 = QtGui.QImage()</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>self.image2.load("monstrinhofree1.png")</div><div><span class="Apple-tab-span" style="white-space:pre">            </span>self.image2 = self.image2.scaled(100,100, QtCore.Qt.KeepAspectRatio)</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>self.xpos, self.ypos = 344,520</div><div><span class="Apple-tab-span" style="white-space:pre">               </span>self.frameControl = True<span class="Apple-tab-span" style="white-space:pre">            </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>self.timer = QtCore.QTimer()</div><div><span class="Apple-tab-span" style="white-space:pre">         </span>QtCore.QObject.connect(self.timer, QtCore.SIGNAL('timeout()'),</div>
<div>                               self.timerEvent)</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>self.timer.start(150)</div><div><span class="Apple-tab-span" style="white-space:pre">                </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>def boundingRect(self):</div><div><span class="Apple-tab-span" style="white-space:pre">              </span>return Monster.BoundingRect</div><div><span class="Apple-tab-span" style="white-space:pre">          </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>def shape(self):</div><div><span class="Apple-tab-span" style="white-space:pre">     </span>        path = QtGui.QPainterPath()</div><div><span class="Apple-tab-span" style="white-space:pre">      </span>        path.addRect(0, 0, 150, 150) #determina o shape, que sera base para detectar colissoes.</div>
<div>        <span class="Apple-tab-span" style="white-space:pre"> </span>return path </div><div><br></div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">      </span>def paint(self,painter, option, widget):</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span></div><div><span class="Apple-tab-span" style="white-space:pre">             </span>#nuvem = QtGui.QImage()</div><div><span class="Apple-tab-span" style="white-space:pre">              </span>#nuvem.load("images/nuven.png")</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>#nuvem2 = QtGui.QImage()</div><div><span class="Apple-tab-span" style="white-space:pre">             </span>#nuvem2.load("images/nuven2.png")</div><div><span class="Apple-tab-span" style="white-space:pre">          </span>if self.frameControl:</div>
<div><span class="Apple-tab-span" style="white-space:pre">                      </span>painter.drawImage(self.xpos,self.ypos,self.image)</div><div><span class="Apple-tab-span" style="white-space:pre">                    </span>self.frameControl = False</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>else:</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>painter.drawImage(self.xpos,self.ypos,self.image2)</div><div><span class="Apple-tab-span" style="white-space:pre">                   </span>self.frameControl = True</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>#painter.drawImage(360,24,nuvem)</div><div><span class="Apple-tab-span" style="white-space:pre">             </span>#painter.drawImage(504, 90,nuvem2)</div><div><span class="Apple-tab-span" style="white-space:pre">           </span>#print self.xpos, self.ypos</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>#print dir(self.shape())</div><div><span class="Apple-tab-span" style="white-space:pre">             </span>#items = self.scene().items(self.shape(), QtCore.Qt.IntersectsItemShape)<span class="Apple-tab-span" style="white-space:pre">    </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>#print items<span class="Apple-tab-span" style="white-space:pre">        </span></div><div><span class="Apple-tab-span" style="white-space:pre">             </span>if self.collidingItems():</div>
<div><span class="Apple-tab-span" style="white-space:pre">                      </span>print self.collidingItems()[0]</div><div><span class="Apple-tab-span" style="white-space:pre">                       </span>#print "chocou"</div><div><span class="Apple-tab-span" style="white-space:pre">                    </span>control = False</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>#def update(self):</div><div><span class="Apple-tab-span" style="white-space:pre">   </span>def timerEvent(self):</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>self.update()</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span></div><div><span class="Apple-tab-span" style="white-space:pre">     </span>def keyPressEvent(self,event):</div><div><span class="Apple-tab-span" style="white-space:pre">               </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>key = event.key()</div><div><span class="Apple-tab-span" style="white-space:pre">            </span></div><div>        <span class="Apple-tab-span" style="white-space:pre">        </span>if key == QtCore.Qt.Key_Left:</div>
<div><span class="Apple-tab-span" style="white-space:pre">                      </span>if self.xpos > 0:</div><div>        <span class="Apple-tab-span" style="white-space:pre">                    </span>self.xpos -= Monster.speed</div><div>        <span class="Apple-tab-span" style="white-space:pre">      </span>elif key == QtCore.Qt.Key_Right:</div>
<div><span class="Apple-tab-span" style="white-space:pre">                      </span>if self.xpos < 680:<span class="Apple-tab-span" style="white-space:pre">              </span></div><div><span class="Apple-tab-span" style="white-space:pre">                             </span>self.xpos += Monster.speed    </div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>elif key == QtCore.Qt.Key_Down:</div><div><span class="Apple-tab-span" style="white-space:pre">                      </span>if self.ypos < 520:<span class="Apple-tab-span" style="white-space:pre">              </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">                              </span>self.ypos += Monster.speed</div><div><span class="Apple-tab-span" style="white-space:pre">           </span>elif key == QtCore.Qt.Key_Up:</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>if self.ypos > 0:</div>
<div><span class="Apple-tab-span" style="white-space:pre">                              </span>self.ypos -= Monster.speed</div><div><span class="Apple-tab-span" style="white-space:pre">           </span>#else:</div><div><span class="Apple-tab-span" style="white-space:pre">               </span>#<span class="Apple-tab-span" style="white-space:pre">   </span>QtGui.QGraphicsItem.keyPressEvent(self, event)</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>def mousePressEvent(self, event):</div><div><span class="Apple-tab-span" style="white-space:pre">            </span>print "mouse funfa"</div><div><br></div>
<div><br></div><div>class Clouds(QtGui.QGraphicsItem):</div><div><span class="Apple-tab-span" style="white-space:pre">    </span>adjust = 0.5</div><div>    <span class="Apple-tab-span" style="white-space:pre">  </span>BoundingRect = QtCore.QRectF(-20 - adjust, -22 - adjust,</div>
<div>                                 100 + adjust, 100 + adjust)</div><div><span class="Apple-tab-span" style="white-space:pre">  </span>def __init__(self):</div><div>        <span class="Apple-tab-span" style="white-space:pre">     </span>QtGui.QGraphicsItem.__init__(self)</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>self.cloud1 = QtGui.QImage()</div><div><span class="Apple-tab-span" style="white-space:pre">         </span>self.cloud1.load("images/nuven.png")</div><div>
<span class="Apple-tab-span" style="white-space:pre">         </span>self.cloud2 = QtGui.QImage()</div><div><span class="Apple-tab-span" style="white-space:pre">         </span>self.cloud2.load("images/nuven2.png")</div><div>
<br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>def boundingRect(self):</div><div><span class="Apple-tab-span" style="white-space:pre">              </span>return Clouds.BoundingRect</div><div><span class="Apple-tab-span" style="white-space:pre">   </span>def shape(self):</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>        path = QtGui.QPainterPath()</div><div><span class="Apple-tab-span" style="white-space:pre">      </span>        path.addRect(-10, -20, 228, 97) #determina o shape, que sera base para detectar colissoes.</div>
<div>        <span class="Apple-tab-span" style="white-space:pre"> </span>return path </div><div><span class="Apple-tab-span" style="white-space:pre">        </span>def paint(self,painter, option, widget):</div><div><span class="Apple-tab-span" style="white-space:pre">             </span>painter.drawImage(360,24,self.cloud1)</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>painter.drawImage(504, 90,self.cloud2)</div><div><span class="Apple-tab-span" style="white-space:pre">               </span>#if self.scene().collidingItems(self):</div>
<div><span class="Apple-tab-span" style="white-space:pre">                      </span>#print self.scene().collidingItems(self)[0]</div><div>class Scene(QtGui.QGraphicsScene):</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>def __init__(self):</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>QtGui.QGraphicsScene.__init__(self)</div><div><span class="Apple-tab-span" style="white-space:pre">          </span>self.fun = None</div><div><span class="Apple-tab-span" style="white-space:pre">      </span>def keyPressEvent(self,event):</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>self.func(event)</div><div><span class="Apple-tab-span" style="white-space:pre">     </span>def setKeyPressEventItem(self,func):</div><div><span class="Apple-tab-span" style="white-space:pre">         </span>self.func = func</div>
<div><br></div><div><br></div><div>class View(QtGui.QGraphicsView):</div><div><span class="Apple-tab-span" style="white-space:pre">     </span>def __init__(self):</div><div><span class="Apple-tab-span" style="white-space:pre">          </span>QtGui.QGraphicsView.__init__(self)</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>self.setCacheMode(QtGui.QGraphicsView.CacheBackground)</div><div>        <span class="Apple-tab-span" style="white-space:pre">  </span>self.setRenderHints(QtGui.QPainter.Antialiasing)</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>def drawBackground(self, painter, rect):</div><div><span class="Apple-tab-span" style="white-space:pre">             </span>pixmap = QtGui.QImage("images/background3.png")</div>
<div><span class="Apple-tab-span" style="white-space:pre">                      </span></div><div><span class="Apple-tab-span" style="white-space:pre">             </span>#print pixmap<span class="Apple-tab-span" style="white-space:pre">               </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>srect = self.sceneRect()<span class="Apple-tab-span" style="white-space:pre">            </span></div><div><span class="Apple-tab-span" style="white-space:pre">             </span>painter.drawImage(-5,-5,pixmap)</div>
<div><span class="Apple-tab-span" style="white-space:pre">              </span>#print "draw"</div><div><br></div><div><br></div><div>if __name__ == "__main__":</div><div>    </div><div>    app = QtGui.QApplication(sys.argv)</div>
<div>    QtCore.qsrand(QtCore.QTime(0,0,0).secsTo(QtCore.QTime.currentTime()))</div><div><br></div><div>    scene = Scene()#QtGui.QGraphicsScene()</div><div>    scene.setSceneRect(0,0, 790, 590)</div><div>    scene.setItemIndexMethod(QtGui.QGraphicsScene.NoIndex)</div>
<div>    monster = Monster()</div><div>    scene.setKeyPressEventItem(monster.keyPressEvent)</div><div>    scene.addItem(monster)</div><div>    clouds = Clouds()</div><div>    scene.addItem(clouds)</div><div>    view = View()#QtGui.QGraphicsView(scene)</div>
<div>    view.setScene(scene)</div><div>    #view.setRenderHint(QtGui.QPainter.Antialiasing)</div><div>    #view.setBackgroundBrush(QtGui.QColor(230, 200, 167))  </div><div>    #view.setRenderHints(QtGui.QPainter.Antialiasing)</div>
<div>    #pixmap = QtGui.QImage("images/background.png")</div><div>    </div><div>  </div><div>   # view.setBackgroundBrush(QtGui.QBrush(pixmap))</div><div>    #view.setCacheMode(QtGui.QGraphicsView.CacheBackground)</div>
<div>    view.setWindowTitle("Ensl Game")</div><div>    #view.setWindowTitle(QtCore.QT_TRANSLATE_NOOP(QtGui.QGraphicsView, "Colliding Mice"))</div><div>    view.resize(800, 600)</div><div>    view.show()</div>
<div><br></div>-- <br>Leônidas S. Barbosa (Kirotawa)<br>[DesenvolvedorWeb/CEFET/RN]<br>[Ciências da Computação/UFRN]<br>[pós-graduando em Inteligência Computacional/Processamento Gráfico /UFRN<br>[Estudante de japonês nível Intermediário I  - Japanese Student]<br>
[Desenvolvedor em python, PyGame]<br>blog nerd: <a href="http://corecode.wordpress.com/" target="_blank">corecode.wordpress.com/</a><br>blog music: <a href="http://essenaomanja.blogspot.com" target="_blank">essenaomanja.blogspot.com</a><div>
blog tirinhas: <a href="http://elminiche.wordpress.com/" target="_blank">elminiche.wordpress.com/</a><br><br>"Mais sábio é aquele que sabe que não sabe" (Sócrates)<br><br>日本語の学生です。<br>コンピュータサイエンスの学位.<br></div><br>

</div>