在另一个按键tkinter之后继续运行按键功能

时间:2017-04-04 13:58:54

标签: python tkinter tkinter-canvas

我有一个游戏,其中一个方块可以四处移动并跳跃,但每当它跳跃时,它会停止所有水平移动,直到你再次按下左或右键,我想知道是否有一种方法可以在击中后继续水平移动跳起来的关键?我的代码是

import Tkinter as tkinter
def is_even(n):
    if n % 2 == 0:
        retVal = True
    else:
        retVal = False
    return retVal
class gameScreen:
    def move(self, event):
        d = self.getdir(event.keysym)
        self.canvas.move(self.char, d[0], d[1])

        if self.collision():
            self.canvas.move(self.char, -d[0], -d[1])
    def jump(self):
        j = 0
        from time import sleep
        jump = [4, 4, 2, 1, 0, -1, -2, -4, -4]
        for i in range(0, (len(jump)*2)):
            x = jump[j]
            print x
            self.canvas.move(self.char, 0, -x)
            if not  is_even(i):
                j +=1
            self.canvas.update()
            sleep(.05)

    def touchingGround(self, event):
        self.canvas.move(self.char, 0, 5)

        if self.collision():
            retVal = True
        else:
            retVal = False
        self.canvas.move(self.char, 0, -5)
        return retVal

    def onKey(self, event):
        keys = ["Left", "Right"]
        if event.keysym == "Up":
            if self.touchingGround(event):
                self.jump()
        else:
            if event.keysym in keys:
                self.move(event)

    def collision(self):
        coords = self.canvas.coords(self.char)
        collision =  len(self.canvas.find_overlapping(coords[0]-1,
                         coords[1]-1, coords[2]-1, coords[3]-1)) >=2
        return collision
    def getdir(self, s):

        direction = {"Left":[-5, 0], "Right":[5, 0]}

        try:
            retVal = direction[s]
        except KeyError:
            retVal =False
        return retVal




    def __init__(self, master):
        self.objects = []
        self.master = master
        master.title("Game")
        self.img = tkinter.PhotoImage(file = "platformer.gif")
        self.canvas = tkinter.Canvas(master, width=500, height=500)

        self.canvas.pack(expand="YES",fill="both")

        self.objects.append(self.canvas.create_rectangle(2, 500, 500, 400,
                            fill="grey"))
        self.char = self.canvas.create_rectangle(2, 400, 52, 350, 
                                                 fill="red")
        self.x = 0
        self.y = 0
        master.bind("<Key>", self.onKey)        

root = tkinter.Tk()
my_gui = gameScreen(root)
root.mainloop()

0 个答案:

没有答案