简体   繁体   English

Pygame中方向相反时,玩家移动停止

[英]Player movement stops when direction reverses in Pygame

I am messing around with pygame and I am eventually working towards a pong clone. 我在和pygame玩弄,最后我正在努力开发pong克隆。 I implemented player movement with the arrow keys and when ever I switch from going up to immediately going down, my player freezes and won't move again until I press that direction key again. 我使用箭头键实现了玩家的移动,一旦我从上升切换到立即下降,我的播放器就会死机,直到再次按下该方向键后才会移动。 Here is my code: 这是我的代码:

import sys, pygame

pygame.init()

display_width = 640
display_height = 480

display = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Test Game")

black = (0,0,0)
white = (255,255,255)

clock = pygame.time.Clock()

running = True

class Player:

    def __init__(self,x,y,hspd,vspd,color,screen):
        self.x = x
        self.y = y
        self.hspd = hspd
        self.vspd = vspd
        self.color = color
        self.screen = screen

    def draw(self):
        pygame.draw.rect(self.screen,self.color,(self.x,self.y,32,32))

    def move(self):
        self.x += self.hspd
        self.y += self.vspd

player = Player(0,0,0,0,black,display)

while running:

    clock.tick(60)

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                player.hspd = 0
            if event.key == pygame.K_LEFT:
                    player.hspd = 0
            if event.key == pygame.K_UP:
                player.vspd = 0
            if event.key == pygame.K_DOWN:
                player.vspd = 0

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                player.hspd = 4
            if event.key == pygame.K_LEFT:
                player.hspd = -4
            if event.key == pygame.K_UP:
                player.vspd = -4
            if event.key == pygame.K_DOWN:
                player.vspd = 4


    #Clear the screen
    display.fill(white)

    #Move objects
    player.move()
    #Draw objects
    player.draw()

    #Update the screen
    pygame.display.flip()

print "I made it!"
pygame.quit()
sys.exit()

I suggest you work with key.get_pressed() to check for the current set of pressed keys. 我建议您使用key.get_pressed()来检查当前按下的键集。

In your scenario - when you press down and release up (in that order) - the speed is set to 0, so you need to inspect the keys pressed not just by the current event. 在您的场景中-当您按下并释放(按此顺序)时-速度设置为0,因此您需要检查的按键不仅限于当前事件。

Here is a working version of the relevant part: 这是相关部分的工作版本:

def current_speed():
    # uses the fact that true = 1 and false = 0
    currently_pressed = pygame.key.get_pressed()
    hdir = currently_pressed[pygame.K_RIGHT] - currently_pressed[pygame.K_LEFT]
    vdir = currently_pressed[pygame.K_DOWN] - currently_pressed[pygame.K_UP]
    return hdir * 4, vdir * 4

while running:

    clock.tick(60)

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

        player.hspd, player.vspd = current_speed()

    #Clear the screen
    display.fill(white)

    #Move objects
    player.move()
    #Draw objects
    player.draw()

    #Update the screen
    pygame.display.flip()

your problem is here: 您的问题在这里:

if event.type == pygame.KEYUP:
        if event.key == pygame.K_RIGHT:
            player.hspd = 0
        if event.key == pygame.K_LEFT:
                player.hspd = 0
        if event.key == pygame.K_UP:
            player.vspd = 0
        if event.key == pygame.K_DOWN:
            player.vspd = 0

if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_RIGHT:
            player.hspd = 4
        if event.key == pygame.K_LEFT:
            player.hspd = -4
        if event.key == pygame.K_UP:
            player.vspd = -4
        if event.key == pygame.K_DOWN:
            player.vspd = 4  

I am guessing that your key event down is still consumed when u switch the keys immediately, meaning no other key down event is getting triggered as long as the first event didn't fire its key up event yet. 我猜想您在立即切换键时仍会消耗掉按键事件,这意味着只要第一个事件尚未触发其按键向上事件,就不会触发其他按键按下事件。

EDIT: maybe its better to check if the player is moving and if so just reverse speed . 编辑:也许最好检查玩家是否在移动,如果只是在反向移动。 Then you would only need to check the down event. 然后,您只需要检查down事件。 Otherwise your event will be consumed and not checked properly. 否则,您的活动将被使用,并且无法正确检查。 For your method you would need to store the occurred key events since the last frame and check that list. 对于您的方法,您需要存储自上一帧以来发生的键事件,并检查该列表。

To expand on LPK's answer, your key down (for event.key == pygame.K_DOWN) is likely being processed before your key up (from event.key == pygame.K_UP) is processed. 为了扩大LPK的答案,可能在处理您的向上键(来自event.key == pygame.K_UP)之前先处理您的向下键(对于event.key == pygame.K_DOWN)。 So while both are down (and you can confirm this), you may experience movement, until you release the up key. 因此,当两者都按下时(您可以确认这一点),您可能会感觉到运动,直到松开向上键。

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

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