May 2019
Intermediate to advanced
542 pages
13h 37m
English
Bullets don't do much good if nothing happens when they hit the target. To make something happen when a bullet hits a tank, we need to implement collision detection. We will implement this in the Bullet class by asking it to check whether it has hit anything whenever it moves.
Start by creating a method in Bullet called check_colllision():
def check_collision(self): colliding_items = self.collidingItems() if colliding_items: self.scene().removeItem(self) for item in colliding_items: if type(item).__name__ == 'Tank': self.hit.emit()
QGraphicsObject.collidingItems() returns a list of any QGraphicsItem objects whose bounding rectangles overlap with this item. This includes not only our Tank objects, but also the floor and ...
Read now
Unlock full access