How do I animate an object on the canvas with a bind with an animation already "rotating"

0

I was making a game copy of Space Invaders but when I went to do the "shot" of the ship, I came across a problem:

AsseenintheGif,whenIhitthekey,everythingonthecanvas"freezes" until the shot reaches the end, how do I solve it? Thank you in advance.

Code snippet:

def Update (self):
    while True :
        self.Move_nave()
        self.root.update()
        self.root.after(30)

def Shot (self, event):
    self.a_0 , self.a_2 = int(self.c.coords (self.bola)[0]), int(self.c.coords (self.bola)[2])
    self.a_1, self.a_3 = int (self.c.coords(self.bola)[1]), int (self.c.coords(self.bola)[3])
    self.tiro = self.c.create_rectangle(self.a_0 + 10, self.a_1, self.a_2 - 10, self.a_3, fill = 'blue')
    for c in range (10):
        self.c.move(self.tiro, 0, -10)
        self.root.after(40)
        self.root.update_idletasks()
    
asked by anonymous 01.10.2018 / 04:51

1 answer

0

Need a makeover in your code.

When firing the shot, you can not fully animate the shot to the end. Instead, you should create an object that stores the position of the shot, you can use a class for it or just a simple structure like a tuple.

Next you need to add this structure that represents the shot to a list, you can call tiros_atuais that will contain all the shots that are currently active.

At a single point in your code, you need to update all elements of the game. Update the ship's position as well as all shots that are in the tiros_atuais list. So you will have a .after() only in your code - and it will do all updates that depend on the time of your game.

Finally, if the shot has reached its destination, remove it from the tiros_atuais list so that it stops being updated.

    
01.10.2018 / 23:49