我有一个快速的问题要问 - 为什么即使不再按下键,矩形仍然在移动?(我是pygame的新手,请耐心等待:D)谢谢
import pygame as pg
pg.init()
clock = pg.time.Clock()
clock.tick(30)
HEIGHT, WIDTH = 400, 400
class Clovik(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((20, 20))
self.image.fill((162, 38, 0))
self.rect = self.image.get_rect()
self.rect.center = ((HEIGHT / 2, WIDTH / 2))
def update(self):
keys = pg.key.get_pressed()
if keys[pg.K_DOWN]:
self.rect.y += 2
screen = pg.display.set_mode((HEIGHT, WIDTH))
running = True
all_sprites = pg.sprite.Group()
p = Clovik()
all_sprites.add(p)
while running:
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
all_sprites.update()
screen.fill((100, 250, 0))
all_sprites.draw(screen)
pg.display.flip()