|
求教大佬,这个很简单的用WASD控制角色八方向移动,但是长时间(一般几秒)按住一个键,程序就会大概率出现严重卡顿,必须放开按键缓一会程序才能恢复过来。
改成用上下左右方向键控制移动,卡顿现象就少了很多。这是为啥啊。。。?
import pygame
import sys
import traceback
from pygame.locals import *
pygame.init()
bg_size = width, height = 600, 600
screen = pygame.display.set_mode(bg_size)
def main():
hero = pygame.image.load("images/hero0.png").convert_alpha()
x, y = 300, 300
running = True
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# 检测键盘操作
keys_pressed = pygame.key.get_pressed()
if keys_pressed[K_w]:
y -= 3
if keys_pressed[K_s]:
y += 3
if keys_pressed[K_a]:
x -= 3
if keys_pressed[K_d]:
x += 3
# 绘制
screen.blit(hero, (x, y))
pygame.display.flip()
clock.tick(60)
|
|