当Pygame中的方向反转时,玩家移动停止

时间:2016-02-19 21:31:57

标签: python pygame

我正在搞乱pygame,我最终正朝着一个pong克隆努力。我用箭头键实现了玩家动作,当我从上升到立即下降时,我的玩家冻结并再次不动,直到我再次按下方向键。这是我的代码:

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()

3 个答案:

答案 0 :(得分:1)

我建议您使用key.get_pressed()来检查当前的按键组。

在您的场景中 - 按下并释放(按此顺序) - 速度设置为0,因此您需要检查按下的按键,而不仅仅是当前事件。

以下是相关部分的工作版本:

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()

答案 1 :(得分:0)

你的问题在这里:

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  

我猜你的关键事件在你立刻切换时仍然消耗掉,这意味着只要第一个事件没有触发其关键事件,就不会触发其他按键事件。

编辑:也许最好检查一下玩家是否正在移动,如果是这样,只是反向速度。然后你只需要检查down事件。 否则,您的活动将被消费,而不是正确检查。 对于您的方法,您需要存储自上一帧以来发生的键事件并检查该列表。

答案 2 :(得分:0)

为了扩展LPK的答案,你的密钥关闭(对于event.key == pygame.K_DOWN)很可能在处理你的密钥(来自event.key == pygame.K_UP)之前被处理。因此,虽然两者都已关闭(并且您可以确认这一点),但您可能会遇到移动,直到您释放向上键。