【发布时间】:2017-01-28 13:04:09
【问题描述】:
我已经开始在 pygame 上制作东西,但是在向左或向右移动时遇到了问题。如果我快速从按右箭头键变为按左箭头键并放开右箭头键,则该块停止移动。这是我的代码
bg = "sky.jpg"
ms = "ms.png"
import pygame, sys
from pygame.locals import *
x,y = 0,0
movex,movey=0,0
pygame.init()
screen=pygame.display.set_mode((664,385),0,32)
background=pygame.image.load(bg).convert()
mouse_c=pygame.image.load(ms).convert_alpha()
m = 0
pygame.event.pump()
while 1:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type==KEYDOWN:
if event.key==K_LEFT:
movex =-0.5
m = m + 1
if event.key==K_RIGHT:
movex=+0.5
m = m + 1
elif event.type == KEYUP:
if event.key==K_LEFT and not event.key==K_RIGHT:
movex = 0
if event.key==K_RIGHT and not event.key==K_LEFT:
movex =0
x+=movex
y=200
screen.blit(background, (0,0))
screen.blit(mouse_c,(x,y))
pygame.display.update()
有没有办法可以改变这一点,如果按下右箭头键并释放左箭头键,它会向右而不是停止? 附言 我仍在学习 pygame 并且对该模块非常陌生。很抱歉,这似乎是一个愚蠢的问题,但我找不到任何答案。
【问题讨论】:
-
ms.png 是块
-
event.key只能保留一个值,因此当not event.key==K_RIGHT:已经是event.key==K_LEFT时,检查它是没有意义的
标签: python python-2.7 pygame 2d