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

相关推荐
涛声依旧-底层原理研究所20 分钟前
残差连接与层归一化通俗易懂的详解
人工智能·python·神经网络·transformer
csdn_aspnet27 分钟前
Python 算法快闪 LeetCode 编号 70 - 爬楼梯
python·算法·leetcode·职场和发展
fantasy_arch1 小时前
pytorch人脸匹配模型
人工智能·pytorch·python
熊猫_豆豆1 小时前
广义相对论水星近日点进动完整详细数学推导
python·天体·广义相对论
web3.08889991 小时前
1688 图搜接口(item_search_img / 拍立淘) 接入方法
开发语言·python
AI算法沐枫2 小时前
深度学习python代码处理科研测序数据
数据结构·人工智能·python·深度学习·决策树·机器学习·线性回归
X1A0RAN3 小时前
解决Pycharm中部分文件或文件夹被隐藏不展示问题
ide·python·pycharm
MomentYY3 小时前
第 3 篇:让 Agent 学会分工,LangGraph 构建多 Agent系统
人工智能·python·agent
程序员Jelena3 小时前
Python 代码是什么?—— 从字节到执行的完整解析
python
测试员周周3 小时前
【Appium 系列】第13节-混合测试执行器 — API + UI 的协同执行
开发语言·人工智能·python·功能测试·ui·appium·pytest