鱼C论坛

 找回密码
 立即注册
查看: 3364|回复: 3

pygame2.3.0中pygame.key.get_pressed()问题

[复制链接]
发表于 2023-5-8 11:31:09 | 显示全部楼层 |阅读模式
5鱼币
pygame2.0之前的代码没有问题。但是最近用了pygame2.3.0就有问题了。
问题就是pygame.key.get_pressed() 在获取键盘字母的时候 ,无效。下面代码我设置了上下左右键,使用的时候没有任何问题,但是按下q键的时候上下左右都会失效。


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()
    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)

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-5-8 11:35:49 | 显示全部楼层
在pygame2.3.0中, pygame.key.get_pressed() 的行为可能已经发生了变化。你的代码中的问题可能是由于按下q键时,方向键的条件被覆盖了。为了解决这个问题,可以尝试将 elif 替换为 if ,这样即使按下q键,上下左右键也会继续生效。

修改后的代码如下:

  1. import pygame,sys

  2. pygame.init()

  3. screen = pygame.display.set_mode((1280,720))

  4. image = pygame.Surface((32,64))
  5. image.fill('green')

  6. rect = image.get_rect(center = (640,360))

  7. direction = pygame.math.Vector2()
  8. pos = pygame.math.Vector2(rect.center)
  9. speed = 2

  10. clock = pygame.time.Clock()

  11. while True:
  12.     for event in pygame.event.get():
  13.         if event.type == pygame.QUIT:
  14.             pygame.quit()
  15.             sys.exit()
  16.     keys = pygame.key.get_pressed()

  17.     if keys[pygame.K_UP]:
  18.         direction.y = -1
  19.     if keys[pygame.K_DOWN]:
  20.         direction.y = 1
  21.     if not keys[pygame.K_UP] and not keys[pygame.K_DOWN]:
  22.         direction.y = 0

  23.     if keys[pygame.K_LEFT]:
  24.         direction.x = -1
  25.     if keys[pygame.K_RIGHT]:
  26.         direction.x = 1
  27.     if not keys[pygame.K_LEFT] and not keys[pygame.K_RIGHT]:
  28.         direction.x = 0

  29.     if keys[pygame.K_q]:
  30.         print('这边是重点')

  31.     if direction.magnitude() > 0:
  32.         direction = direction.normalize()

  33.     pos.x += direction.x * speed
  34.     rect.centerx = pos.x

  35.     pos.y += direction.y * speed
  36.     rect.centery = pos.y

  37.     screen.fill((0,0,0))

  38.     screen.blit(image,rect)  

  39.     pygame.display.flip()

  40.     clock.tick(60)
复制代码


这样,按下q键时,上下左右键仍然会生效。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-5-8 15:13:19 | 显示全部楼层
isdkz 发表于 2023-5-8 11:35
在pygame2.3.0中, pygame.key.get_pressed() 的行为可能已经发生了变化。你的代码中的问题可能是由于按下q ...

试过了没用!》
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-5-8 18:04:54 | 显示全部楼层
根据您的问题描述,我认为可能是由于 `pygame.key.get_pressed()` 方法在检测键盘事件时存在一些问题。对于这个问题,可以尝试使用 `pygame.event.get()` 方法来获取键盘按下/松开事件并在循环中处理。

以下是修改后的代码片段:


  1. while True:
  2.     for event in pygame.event.get():
  3.         if event.type == pygame.QUIT:
  4.             pygame.quit()
  5.             sys.exit()

  6.         # 检查是否有按键按下或松开
  7.         if event.type == pygame.KEYDOWN:
  8.             if event.key == pygame.K_UP:
  9.                 direction.y = -1
  10.             elif event.key == pygame.K_DOWN:
  11.                 direction.y = 1
  12.             elif event.key == pygame.K_LEFT:
  13.                 direction.x = -1
  14.             elif event.key == pygame.K_RIGHT:
  15.                 direction.x = 1
  16.             elif event.key == pygame.K_q:
  17.                 print('这边是重点')

  18.         elif event.type == pygame.KEYUP:
  19.             if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
  20.                 direction.y = 0
  21.             elif event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
  22.                 direction.x = 0
复制代码

通过捕获 `pygame.KEYDOWN` 和 `pygame.KEYUP` 事件,我们可以检测到所需的按键状态,并相应地更新方向向量。然后,您可以从键盘控制 `rect` 的移动和其他操作。

希望这能解决您的问题。如果这仍然无法解决问题,请提供更多详细信息或错误消息以进行进一步排查。
有用请设置最佳答案!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-29 02:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表