2017-06-21 261 views
1

我正在pygame库做一个简单的游戏。我注意到一个奇怪的错误,如果我点击其中一个方向键来移动我的角色,它会将它向一个方向移动。按下键会在您按下的方向上触发正向加速,然后向上键将加速度重置为零。检查这些值后,尽管按键发生,但加速度仍保持为正值。查看所有的事件,它注册一个键和键,但pygame不运行与该键关联事件关联的代码。这可能是我键盘的一个信号问题,但它似乎可能是pygame中的事件处理。这里是我的代码:pygame失败检测键盘

import pygame 

pygame.init() 

display_x = 1024 
display_y = 512 

game_display = pygame.display.set_mode((display_x, display_y)) 
clock = pygame.time.Clock() 

fps = 30 
font = pygame.font.SysFont(None, 25) 

max_velocity = 16 
acceleration = 4 


def print_to_screen(text, color, x, y): 
    screen_text = font.render(text, True, color) 
    game_display.blit(screen_text, [x, y]) 


def game_loop(): 
    game_exit = False 

    lead_x = display_x/2 
    lead_y = display_y/2 
    vel_x = 0 
    vel_y = 0 
    acc_x = 0 
    acc_y = 0 
    tile_size = 32 

    while not game_exit: 
     for event in pygame.event.get(): 
      print(event) 
      if event.type == pygame.QUIT: 
       game_exit = True 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_LEFT: 
        acc_x = -acceleration 
       if event.key == pygame.K_RIGHT: 
        acc_x = acceleration 
       if event.key == pygame.K_UP: 
        acc_y = -acceleration 
       if event.key == pygame.K_DOWN: 
        acc_y = acceleration 
      if event.type == pygame.KEYUP: 
       if event.key == pygame.K_LEFT and vel_x < 0: 
        acc_x = 0 
       if event.key == pygame.K_RIGHT and vel_x > 0: 
        acc_x = 0 
       if event.key == pygame.K_UP and vel_y < 0: 
        acc_y = 0 
       if event.key == pygame.K_DOWN and vel_y > 0: 
        acc_y = 0 

     if -max_velocity < vel_x < max_velocity: # if velocity isn't max, adds acceleration to velocity 
      vel_x += acc_x 
     if -max_velocity < vel_y < max_velocity: 
      vel_y += acc_y 
     if vel_x < 0: # velocity decay, if vel is less than 0, adds one. no input = slowed down cube 
      vel_x += 1 
     elif vel_x > 0: 
      vel_x -= 1 
     if vel_y < 0: 
      vel_y += 1 
     elif vel_y > 0: 
      vel_y -= 1 

     if (lead_x + vel_x) < 0: # looks into future: if past border in next frame, sets x coord to 0, stops motion 
      vel_x = 0 
      acc_x = 0 
      lead_x = 0 
     elif (lead_x + vel_x) > (display_x - tile_size): 
      vel_x = 0 
      acc_x = 0 
      lead_x = (display_x - tile_size) 
     if (lead_y + vel_y) < 0: 
      vel_y = 0 
      acc_y = 0 
      lead_y = 0 
     elif (lead_y + vel_y) > (display_y - tile_size): 
      vel_y = 0 
      acc_y = 0 
      lead_y = (display_y - tile_size) 

     lead_x += vel_x 
     lead_y += vel_y 

     game_display.fill((255, 255, 255)) 
     pygame.draw.rect(game_display, (255, 0, 0), [lead_x, lead_y, tile_size, tile_size]) 
     print_to_screen(str(int(lead_x)) + ", " + str(int(lead_y)), (0, 0, 0), 0, 0) 
     if vel_x > max_velocity or vel_y > max_velocity: 
      print_to_screen(str(int(vel_x)) + ", " + str(int(vel_y)), (255, 0, 0), 0, 16) 
     else: 
      print_to_screen(str(int(vel_x)) + ", " + str(int(vel_y)), (0, 0, 0), 0, 16) 
     if acc_x > acceleration or acc_y > acceleration: 
      print_to_screen(str(int(acc_x)) + ", " + str(int(acc_y)), (255, 0, 0), 0, 32) 
     else: 
      print_to_screen(str(int(acc_x)) + ", " + str(int(acc_y)), (0, 0, 0), 0, 32) 

     pygame.display.update() 
     clock.tick(fps) 

game_loop() 
pygame.quit() 
quit() 

下面是从快速点击的事件数据:

<Event(2-KeyDown {'unicode': '', 'key': 276, 'mod': 0, 'scancode': 75})> 
<Event(3-KeyUp {'key': 276, 'mod': 0, 'scancode': 75})> 

它显示了一个关键的向上和向下的关键事件,不知道为什么关键行动将无法注册。

回答

2

的问题是,你是否vel_x是与KEYUP事件结合小于或大于0。想想看,当我们向左移动(vel_x < 0),并迅速按下并释放K_RIGHT会发生什么,然后vel_x仍然是负的,因此if event.key == pygame.K_RIGHT and vel_x > 0:False,KeyUp事件被忽略,acc_x不会被重置为0。

你应该检查是否acc_xvel_x大于0

if event.type == pygame.KEYUP: 
    if event.key == pygame.K_LEFT and acc_x < 0: 
     acc_x = 0 
    if event.key == pygame.K_RIGHT and acc_x > 0: 
     acc_x = 0 

同样为UP和DOWN键和vel_y,acc_y。

+0

谢谢!不知道为什么我列入,但解决了这个问题。 –

+1

嘿,我可以告诉你为什么你添加了这个条件。 :P如果你快速按下'vel_x> 0'(或者在你的情况下为'acc_x')左右按压,则可能发生播放器完全停止,你必须释放键并再次按下才能再次移动精灵。我认为在重设“vel”之前检查球员是否正朝着特定方向移动通常是一个好主意。 – skrx

+0

是的,这可能是为什么。似乎现在工作,从那时起我改变了一些其他的代码。测试一下你说的话,如果我真的快点击左右快速的加速度会再次卡住,就像你快速点击一个方向一样。奇怪你的修复没有解决这个问题。 –

0

删除KEYUP块,并添加这KEYDOWN块:

if event.type == pygame.KEYDOWN: 
    if event.key == pygame.K_LEFT: 
    acc_x = -acceleration 
    acc_y = 0 
if event.key == pygame.K_RIGHT: 
    acc_x = acceleration 
    acc_y = 0 
if event.key == pygame.K_UP: 
    acc_y = -acceleration 
    acc_x = 0 
if event.key == pygame.K_DOWN: 
    acc_y = acceleration 
    acc_x = 0 
+0

谢谢,但那不是我正在寻找的东西。运动如此复杂的原因是因为我想对角地加速和移动。您的代码将删除此内容,并且在您释放所有密钥时播放器不会停止移动。 –

+0

哦,好的。感谢您的承认:) – sukhdeep