I have two classes: class Bullet and Main Class. I need to the create movement of bullet and I did that, but when bullet moves out of screen I don`t know how to stop function for the moving bullet. Because of that, the program with time gets slower.
import multiprocessing as mp
from PyQt5.QtCore import Qt, QTimer
def bullet(self):
if self.bull_on[0] is None:
if self.bull is not None:
self.timer.stop()
del self.bull
self.bull = Bullet(self, self.player.geometry(), self.pipeB1)
self.bull_on[0] = 1
if self.player.pix1.position == 'up':
self.timerCallback = functools.partial(self.bull.move_up, isOn=self.bull_on)
self.timer.timeout.connect(self.timerCallback)
self.timer.start(10)
Above code creates instance of class Bullet and start funcion for moving bullet Code below is funcion that starts
def move_up(self, isOn):
send = [self.rec1.x(), self.rec1.y() - self.counter * 2, 'u']
self.pipe.send(send)
val = self.pipe.recv()
if val > -1:
self.bullet1 = QPixmap('Images\\Bullet_up.png')
self.setGeometry(self.rec1.x() + 16, self.rec1.y() - 20 - self.counter*2, self.rec1.width(), self.rec1.height())
self.setPixmap(self.bullet1)
self.show()
self.counter += 1
else:
isOn[0] = None
self.hide()
I use
self.timer.stop()
and
del self.bull
but this does not work. I tried
self.timer.disconnect(self.timerCallback)
but that throws errors saying that pipe is not closed properly.
My question is how to stop the function in the background?