Skip to content
Advertisement

How to Detect collisions in PyQt5

Iam making a simple game using PyQt, but i don’t know how i can detect the collisions between enemy and bullets, there is a C++ implementation, but i dont know how i can do that in PyQt. and it should be done in Bullet.py file. these are the files.

Window.py

JavaScript

Player.py

JavaScript

Enemy.py

JavaScript

Bullet.py

JavaScript

Score.py from PyQt6.QtWidgets import QGraphicsTextItem from PyQt6.QtCore import Qt

JavaScript

This is the C++ code, i want similiar of that for Python.

JavaScript

Advertisement

Answer

QGraphicsItem has collidingItems method that use QGraphicsItem.boundingRect() to detect collisions. It is implemented for QGraphicsRectItem. So you only need to call it and iterate over items.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement