0

我一直在研究这个,我不知道为什么它不能正常移动。只有当我按 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()

有可能是硬件错误吗?

4

1 回答 1

0

看起来问题是缩进

        x = x + movex
        y = y+movex

它只会在 KEYUP 和 K_d 时运行。尝试取消缩进以使其在每次按键后运行。它应该与if语句处于同一级别。

于 2013-08-01T06:32:50.263 回答