PyGame中键盘输入错误

时间:2013-08-01 06:27:37

标签: python pygame

我一直在努力解决这个问题,我无法弄清楚为什么它不能正常运转。当我按D代码时它只向左移动。请帮帮我

bif = "bif.jpg"
mif = "games.png"
import pygame, sys
from pygame.locals import *

pygame.init()
screen=pygame.display.set_mode((640,360),0,32)

background=pygame.image.load(bif).convert()
cursor=pygame.image.load(mif).convert_alpha()
x,y=4,4
movex,movey=0,0

while True:
for event in pygame.event.get():
    if event.type == QUIT:
        pygame.quit()

    if event.type == KEYDOWN:
        if event.key==K_a:
            movex=-3
        elif event.key==K_d:
            movex=+3
        elif event.key==K_s:
            movey=+3
        elif event.key==K_w:
            movey= -3
    if event.type == KEYUP:
        if event.key==K_a:
                    movex=0
        elif event.key==K_w:
            movex=0
        elif event.key==K_s:
            movey=0
        elif event.key==K_d:
            movey= 0

            x = x + movex
            y = y+movex

screen.blit(background, (0,0))

screen.blit(cursor, (x,y))
pygame.display.update()

是否可能是硬件错误?

1 个答案:

答案 0 :(得分:0)

看起来问题是

的缩进
        x = x + movex
        y = y+movex

只有在KEYUP和K_d时才会运行。尝试unindenting它让它在每次按键后运行。它应该与if语句处于同一级别。