简体   繁体   English

仅当按下Python键时,才如何移动字符。

[英]How to move character only when key is pressed down Python.

I have been working through a book called python for kids. 我一直在研究一本名为python的儿童书籍。 The last project in the book is about a platforming game. 本书的最后一个项目是有关平台游戏的。 The game is called Mr Stick Man Races for the Exit. 该游戏称为“ Stick Man先生竞选出口”。 The way you move the character (a stickman) is you press left or right and he will move left or right. 移动角色(火柴人)的方式是按向左或向右,然后角色将向左或向右移动。 But unlike most games, if you let go of the key, he keeps moving. 但是与大多数游戏不同,如果您放开钥匙,他会不断前进。 How do you make it so he will stop when the key is released? 您如何做到这一点,以便释放钥匙时他会停下来?

Here is a link to a download of the code: https://www.nostarch.com/pythonforkids 这是代码下载的链接: https : //www.nostarch.com/pythonforkids

If you press download sample code from the book, the program is "stickmangame7" in the chapter 18 folder. 如果按此书中的下载示例代码,则该程序将位于第18章文件夹中的“ stickmangame7”。 I have included this link in case I embedded the wrong bit on the code. 如果我在代码中嵌入了错误的位,我已经包含了此链接。

Here is some embedded code which may be the correct bit: 这是一些嵌入式代码,可能是正确的位:

class StickFigureSprite(Sprite):
def __init__(self, game):
    Sprite.__init__(self, game)
    self.images_left = [
        PhotoImage(file="stick-L1.gif"),
        PhotoImage(file="stick-L2.gif"),
        PhotoImage(file="stick-L3.gif")
    ]
    self.images_right = [
        PhotoImage(file="stick-R1.gif"),
        PhotoImage(file="stick-R2.gif"),
        PhotoImage(file="stick-R3.gif")
    ]
    self.image = game.canvas.create_image(200, 470, image=self.images_left[0], anchor='nw')
    self.x = -2
    self.y = 0
    self.current_image = 0
    self.current_image_add = 1
    self.jump_count = 0
    self.last_time = time.time()
    self.coordinates = Coords()
    game.canvas.bind_all('<KeyPress-Left>', self.turn_left)
    game.canvas.bind_all('<KeyPress-Right>', self.turn_right)
    game.canvas.bind_all('<space>', self.jump)

Also: 也:

 def turn_left(self, evt):
    if self.y == 0:
        self.x = -2

def turn_right(self, evt):
    if self.y == 0:
        self.x = 2

PS I know you can do this using pygame, but this is not using pygame for the rest, so I don't think that will work. PS:我知道您可以使用pygame做到这一点,但是其余的则不使用pygame,所以我认为这行不通。

Without looking at the rest of the code, given that turn_left() and turn_right() methods are modifying self.x I would assume that in some event loop this value is used to calculate the movement of your 'stick' on the x axis. 在不看代码其余部分的情况下,考虑到turn_left()turn_right()方法正在修改self.x我假设在某些事件循环中,该值用于计算“棒”在x轴上的运动。 You probably want to reset it to 0 when a key gets released, so create an additional method for example: 您可能想在释放键时将其重置为0 ,因此,请创建其他方法,例如:

def stop_movement(self, evt):
    self.x = 0

And when binding your KeyPress events, bind also KeyRelease events to that method, eg: 并且在绑定KeyPress事件时,还将KeyRelease事件也绑定到该方法,例如:

game.canvas.bind_all('<KeyRelease-Left>', self.stop_movement)
game.canvas.bind_all('<KeyRelease-Right>', self.stop_movement)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM