bg = "lv1.jpg"
ch = "char.png"
import pygame, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640, 400), 0, 32)
background = pygame.image.load(bg).convert()
char = pygame.image.load(ch).convert_alpha()
clock = pygame.time.Clock()
charspeed = 0
charx = 100
chary = 200
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
pygame.quit()
sys.exit()
if event.type== KEYDOWN:
if event.type == K_LEFT:
charspeed = -100
elif event.type == K_RIGHT:
charspeed = +100
if event.type== KEYUP:
if event.key==K_LEFT:
charspeed=0
elif event.key==K_RIGHT:
charspeed=0
screen.blit(background, (0,0))
milli = clock.tick()
seconds = milli/1000.
chardm = seconds*charspeed
charx += chardm
screen.blit(char, (charx, chary))
pygame.display.update()
出于某种原因,当我按下右键时,charspeed不会增加100。我正在尝试用时钟制作游戏,但它似乎不起作用。我对pygame很新,因为你可以看到Plz的帮助!
答案 0 :(得分:1)
问题在于:
if event.type== KEYDOWN:
if event.type == K_LEFT:
charspeed = -100
elif event.type == K_RIGHT:
charspeed = +100
event.type
为KEYDOWN
,因此不能K_RIGHT
。
你想要的是event.key == K_RIGHT
。
有关详细信息,请参阅pygame.event
文档。