|
|
发表于 2023-5-2 18:37:54
|
显示全部楼层
使用pygame.key.set_mods(0) 可以解决这个问题。在检查按键之前添加这一行,它会禁用所有修饰符(如shift,ctrl等)的影响。在你的代码中,在 keys = pygame.key.get_pressed() 前添加 pygame.key.set_mods(0) :
- import pygame,sys
- pygame.init()
- screen = pygame.display.set_mode((1280,720))
- image = pygame.Surface((32,64))
- image.fill('green')
- rect = image.get_rect(center = (640,360))
- direction = pygame.math.Vector2()
- pos = pygame.math.Vector2(rect.center)
- speed = 2
- clock = pygame.time.Clock()
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- pygame.key.set_mods(0) # 添加这一行
- keys = pygame.key.get_pressed()
- if keys[pygame.K_UP]:
- direction.y = -1
- elif keys[pygame.K_DOWN]:
- direction.y = 1
- else:
- direction.y = 0
- if keys[pygame.K_LEFT]:
- direction.x = -1
- elif keys[pygame.K_RIGHT]:
- direction.x = 1
- else:
- direction.x = 0
- if keys[pygame.K_q]:
- print('这边是重点')
- if direction.magnitude() > 0:
- direction = direction.normalize()
- pos.x += direction.x * speed
- rect.centerx = pos.x
- pos.y += direction.y * speed
- rect.centery = pos.y
- screen.fill((0,0,0))
- screen.blit(image,rect)
- pygame.display.flip()
- clock.tick(60)
复制代码
现在当你按下 q 键时,上下左右键不会失效。这是因为在pygame 2.3.0中,默认的键盘修饰符行为已经发生了变化。将其设置为0可禁用这些修饰符,使其恢复到预期的行为。 |
|