【问题标题】:TKinter multiple keypressTKinter 多次按键
【发布时间】:2020-12-18 14:08:15
【问题描述】:

我正在尝试创建一个基于 TKinter 的 Space Invaders,但我在键盘输入方面遇到了麻烦。

确实,我有 3 个“绑定”:

  • 如果按下右箭头,“字符”会向右移动 10px
  • 与左侧相同
  • 最后,如果按下空格键,则会创建一个镜头(现在它只是“字符”上方的一个白色矩形

但是问题来了,如果我在按住箭头的同时按空格键,船会停下来,我不能让它在移动时射击。

我已经在这里找到了一些关于这个问题的帖子(包括一个说这可能是由于系统如何处理按键,他给出了解决方案但我无法使用它)但我无法申请他们的解决方案,因为我的英语和 Python 知识很短(我第一次使用 OOP 和类)。

所以我尝试创建一种新类型的绑定:在按下箭头时开始不间断地向右移动,并在释放时停止它,左侧和双重按住的条件相同,但我没有能够编码。它甚至会解决问题还是我需要找到其他东西?

这是我的代码(法语笔记,如果需要,请询问):

from tkinter import Tk, Canvas, Button, Label


class SpaceInvaders():
    def __init__(self):
        self.window = Tk()
        self.window.geometry("1200x900")
    
        self.label_score = Label(self.window, text="Score : ")
        self.label_score.place(x=10, y=10)
    
        self.label_vies = Label(self.window, text="Vies :")
        self.label_vies.place(x=1130, y=10)
    
        self.button_new = Button(self.window, text="New Game", width=15)
        self.button_new.place(x=400, y=860)
    
        self.button_quit = Button(self.window, text="Quit", width=15, command = lambda:self.window.destroy())
        self.button_quit.place(x=680, y=860)
    
        self.canvas = Canvas(self.window, height = "800", width = "1200", bg='black')
        self.canvas.pack(expand=True)
    
        #Vaisseau et 1er alien
        self.vaisseau = Vaisseau(self.canvas)
        self.alien = Alien(self.canvas,self.window)

class Alien():
    def __init__(self,canvas,window):
        self.canvas = canvas
        self.window = window
    
        self.alien_x = 0
        self.alien_y = 0
        self.direction = 1
        self.alien = self.canvas.create_rectangle(self.alien_x,self.alien_y,self.alien_x+100,self.alien_y+20, fill='white')

    def run(self):
        direction = self.direction
        if direction==1:
            if self.alien_x<1100:
                self.alien_x += 5
            else:
                self.direction = -1
        elif direction == -1:
            if self.alien_x>0:
                self.alien_x -= 5
            else:
                self.direction = 1
        self.canvas.coords(self.alien,self.alien_x,self.alien_y,self.alien_x+100,self.alien_y+20)
        self.window.after(20,self.run)  #La méthode after semble retourner une erreur à 13 chiffres


class Vaisseau():
    def __init__(self,canvas):
    
        self.canvas = canvas
    
        self.player_x = 0
        self.player = self.canvas.create_rectangle(self.player_x,780,self.player_x+60,802,fill='white')
    
    def event_handler(self,event):
        if event.keysym == 'Right':
            self.move(True)
        elif event.keysym == 'Left':
            self.move(False)
        elif event.keysym == "space":
            self.tir()
    
    def move(self,right):
        if right and self.player_x<1140:
            self.player_x += 10
            self.canvas.coords(self.player,self.player_x,780,self.player_x+60,802)
        elif not right and self.player_x>0:
            self.player_x -= 10
            self.canvas.coords(self.player,self.player_x,780,self.player_x+60,802)
        self.right_hold = False
    
    def tir(self):
        self.canvas.create_rectangle(self.player_x+27,760,self.player_x+33,775,fill="white")
 
        
game = SpaceInvaders()
game.alien.run()
game.window.bind("<KeyPress>",game.vaisseau.event_handler)
game.window.mainloop()

【问题讨论】:

标签: python tkinter


【解决方案1】:

如果您希望同时执行操作,则需要在后台执行这些操作。您的代码,就像现在一样,正在以串行方式处理每个操作。例如,如果您点击“左”按钮,您的代码将在其他任何事情发生之前将您的角色向左移动。要解决这个问题,您可以使用 threadedmultiprocess 模块之类的东西。它们都可以帮助您引导您的操作,以便它们可以同时发生。他们的文档非常具体地说明了如何使用它们。

线程模块:https://pypi.org/project/threaded/

多进程模块:https://pypi.org/project/multiprocess/

【讨论】:

  • 谢谢,但遗憾的是我不应该使用外部模块(这是一个学校项目)。
猜你喜欢
  • 2014-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多