2

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?

tkiral
  • 49
  • 1
  • 9
Filip Filipovic
  • 410
  • 3
  • 13
  • You say "does not work." What does that mean? What happens? Do you get a traceback? I see that after you execute self.timer.stop() you will immediately create a new Bullet, since there is no return after self.timer.stop(). – Paul Cornelius Jan 02 '19 at 22:19
  • When i add this line in my code "self.timer.disconnect(self.timerCallback)" i get traceback saying BrokenPipeError: [WinError 109] The pipe has been ended. Without that line function continue working in background and when i fire a few bullets program start slowing – Filip Filipovic Jan 02 '19 at 22:26
  • You explained about the broken pipe error when you call disconnect. However I was asking about the call to stop() and del self.bull. You didn't explain what actually happens at that point. Also, executing those lines will NOT cause a decrease in the number of Bullet instances since you will immediately initialize another one in the same function. Did you consider that? – Paul Cornelius Jan 03 '19 at 02:48
  • When i call stop and del self.bull, function still continue to work, and if i fire second bullet there will be two instances of the same function. I try to create simple program with QTimer and when i call stop in him function stop working i don`t understand why in this case function continue to work – Filip Filipovic Jan 03 '19 at 19:58
  • As I've said twice now, every time you stop the timer and delete the Bullet, you immediately create another Bullet and start another timer. That's why the function still "works" (I assume you mean the function named "bullet"). Perhaps you need a return statement after the `del self.bull` line. I'm not sure. I am having a hard time following your code since you have only shown pieces of it.. – Paul Cornelius Jan 05 '19 at 06:24

0 Answers0