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

相关推荐
Ponp_21 小时前
用 StyleGAN2 做图像数据扩充实战
python·深度学习·生成对抗网络
富贵儿9981 天前
NL2SQL 实践手记 08:SQL 没报错,答案为什么不对——从失败复盘到 Workflow 修复
数据库·python·sql·ai·自然语言处理·langchain
无巧不成书02181 天前
最新 PyPI可信发布完整教程 GitHub Actions+OIDC零Token实现Python包自动化发布
python·自动化·github·pypi
东莞市云毅网络有限公司1 天前
用 Node 做收录快照的周对比:差集、阈值与告警
python·sqlite·自动化运维·geo·数据监测
山楂树の1 天前
Python Pandas 数据处理:数据读取、筛选、清洗、排序与分组
开发语言·python·pandas
Logintern091 天前
langgraph 图编译的聊天机器人
python·langgraph
xurileidian1 天前
第 22 篇 · 边玩边学Python · 双等号与 is 不是一回事
开发语言·python·spring
钱栈up1 天前
GitHub热榜实测|Sherlock:一个用户名搜遍全网
python·开源·github
geovindu1 天前
python: Gradient Boosting
开发语言·后端·python·算法·boosting