pygame之键盘控制方块移动方法

1、**pygame.KEYDOW ------ **检测按键瞬间动作

‌检测按键被按下的瞬间事件:pygame.KEYDOWN‌ # 有延迟

获取按键状态 (实现平滑移动) : pygame.key.get_pressed()

键盘上的左箭头:‌pygame.K_LEFT

键盘上的右箭头:pygame.K_RIGHT

python 复制代码
if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
        player_rect.x -= 10  # 直接修改 x 坐标
    elif event.key == pygame.K_RIGHT:
        player_rect.x += 10  # 直接修改 x 坐标

2、pygame.key.get_pressed() ------ 持续获取按键状态

注意:如果使用 get_pressed,不需要放在 if event.type == KEYDOWN 块中

python 复制代码
# 在 while 循环内部,事件处理之后
keys = pygame.key.get_pressed()

if keys[pygame.K_LEFT]:
    player_rect.x -= 5  # 每帧移动 5 像素,速度更平滑
if keys[pygame.K_RIGHT]:
    player_rect.x += 5

3、边界检查

python 复制代码
keys = pygame.key.get_pressed()

if keys[pygame.K_LEFT] and player_rect.left > 0:
    player_rect.x -= 5
if keys[pygame.K_RIGHT] and player_rect.right < screen_width:  # screen_width 是窗口宽度
    player_rect.x += 5

4、按键控制方块移动(完整代码)

python 复制代码
import pygame
import sys

# 初始化
pygame.init()

# 设置窗口
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("键盘控制方块移动")

# 创建玩家矩形 (x, y, width, height)
player_rect = pygame.Rect(350, 250, 50, 50)
player_color = (255, 0, 0)  # 红色

# 时钟对象,用于控制帧率
clock = pygame.time.Clock()

running = True
while running:
    # 1. 事件处理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 2. 获取按键状态 (实现平滑移动)
    keys = pygame.key.get_pressed()
    
    if keys[pygame.K_LEFT]:
        player_rect.x -= 5
    if keys[pygame.K_RIGHT]:
        player_rect.x += 5
    
    # 可选:添加上下移动
    if keys[pygame.K_UP]:
        player_rect.y -= 5
    if keys[pygame.K_DOWN]:
        player_rect.y += 5

    # 3. 边界检查 (防止移出屏幕)
    if player_rect.left < 0:
        player_rect.left = 0
    if player_rect.right > 800:
        player_rect.right = 800
    if player_rect.top < 0:
        player_rect.top = 0
    if player_rect.bottom > 600:
        player_rect.bottom = 600

    # 4. 绘制
    screen.fill((0, 0, 0))  # 黑色背景
    pygame.draw.rect(screen, player_color, player_rect)  # 绘制玩家
    
    # 5. 更新显示
    pygame.display.flip()
    
    # 控制帧率为 60 FPS
    clock.tick(60)

pygame.quit()
sys.exit()

5、补充知识点:创建矩形

创建玩家矩形 (x, y, width, height)

player_rect = pygame.Rect(350, 250, 50, 50) #x,y,宽,高

player_color = (255, 0, 0) # 红色

pygame.draw.rect(screen, player_color, player_rect)
player_rect = pygame.Rect(350, 250, 50, 50)

player_rect.x -= 5 x坐标-5

相关推荐
卷无止境38 分钟前
SALib敏感性分析的python库学习笔记
后端·python
duansamve2 小时前
Python3+Flask+PostgreSQL18+Vue3项目生产环境用docker如何部署?
python·docker·flask
特立独行的猫A2 小时前
少 3 个环节、少 1 次 ABI、少 3500 行代码:cj-tauri 与 Ionic/Capacitor 的鸿蒙桥接机制对比
java·后端·python
特立独行的猫A2 小时前
cj-tauri vs Rust/Tauri 同款播放器实测:编译 21 秒 vs 11 分钟,磁盘 12 MB vs 1.2 GB
java·后端·python
duansamve3 小时前
详解Flask中Session和Cookie
python·flask
威哥爱编程4 小时前
模拟面试智能体开发实战 04:面试聊到哪算哪?用状态机把整场面试编排起来
python·agent·harmonyos
威哥爱编程4 小时前
模拟面试智能体开发实战 03:AI 面试官太客气?人设 Prompt 让它像个真考官
python·agent·harmonyos
量化分析码农4 小时前
股票数据接口返回字段逐项解析:从在线验证到 Python 数据校验
python
Dmz丶5 小时前
前端转 AI 100 天 Day 42:跨模型对比实战——用数据选模型,不靠“听说”
python
路溪非溪5 小时前
python标准库使用总结
开发语言·python