mardi 4 août 2015

How to use multiprocessing or threading with tkinter

I am trying to make a simple space invaders game and a problem I have run into is getting things t happen at the same time. I have binded the shooting action to the canvas of the game so that when you click a function is called. I would like it so that this function can be called multiple times at once so that multiple "lasers/bullets" can be seen on the screen at any one time. At the minute when you click and a "laser/bullet" is already on screen, the previous one disappears and a new one appears. CODE:

class Game1():

def __init__(self, xcoord1=380, ycoord1=550, xcoord2=400, ycoord2=570):
    self.Master = Master
    self.Master.geometry("800x600+300+150")
    Game1Canvas = Canvas(self.Master, bg="black", height=600, width=800)
    Game1Canvas.place(x=0, y=0)
    self.Canvas = Game1Canvas
    self.Canvas.bind("<Button-1>", self.Shoot)
    self.Ship = self.Canvas.create_rectangle(self.xcoord1, self.ycoord1, self.xcoord2, self.ycoord2, fill = "red")

def Shoot(self):
    self.LaserLocation = 0
    for self.LaserLocation in range(0 , 112):
        Master.after(1, self.Canvas.create_rectangle(self.xcoord1, self.ycoord1 - (self.LaserLocation * 5), self.xcoord2 - 5, self.ycoord2 - (self.LaserLocation * 5), fill = "pink", tag=str(CurrentTag)))
        Master.update()
        self.Canvas.delete(str(CurrentTag))

This is a much more "dumbed" down version of the code at the minute because I've been trying a bunch of different ways to get this working and it's a mess. I am aware of the multiprocessing and threading imports and I have tried them both but am unable to get them working for my code. If someone could reply back with a solution I would be very grateful. Cheers.

Aucun commentaire:

Enregistrer un commentaire