I'm trying to create two circles, which are moving around randomly within an area and changing their color if they collide.
There are many examples of creating circles and moving them around by mouse dragging, but I didn't find anything, where the object has to move on its own.
This is, what I already have:
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QGraphicsView, QGraphicsScene, QGraphicsEllipseItem
import sys, random
class Circle (QGraphicsEllipseItem):
def __init__(self, x, y, r, condition):
super().__init__(0,0,r,r)
self.setPos(x,y)
self.condition = condition
def setColor(self):
if self.condition == "changable":
self.setBrush(Qt.blue)
elif self.condition == "notChangable":
self.setBrush(Qt.black)
class GraphicView(QGraphicsView):
def __init__(self):
super().__init__()
self.scene = QGraphicsScene()
self.setScene(self.scene)
self.setSceneRect(0, 0, 600, 600)
self.circle1 = Circle(random.randint(0,600), random.randint(0,600), 20, "changable")
self.circle2 = Circle(random.randint(0,600), random.randint(0,600), 20, "notChangable")
self.circle1.setColor()
self.circle2.setColor()
self.scene.addItem(self.circle1)
self.scene.addItem(self.circle2)
app = QApplication(sys.argv)
view = GraphicView()
view.show()
sys.exit(app.exec_())
The "changeable"-circle shall update its condition to "not changeable" if it collides with the "not changeable"-circle.
So, how can I make these circles move randomly within the area and let them change their condition?