pygame05.01—— 键盘交互 与 键盘控制正方形运动案例 |
|
原来是稀琳
L7
• 2024-10-17 • 回复 0 • 只看楼主
• 举报
|
我是一个python入门的小白,做这个系列即是为了巩固pygame的相关知识,也是为了考验自己坚持做一件事。
另外如果以下内容有错误或不规范的请各位大佬积极指出,谢谢大家
获取键盘事件
在Pygame中,键盘事件主要包括KEYDOWN
和KEYUP
两种类型。KEYDOWN
事件表示某个键被按下,而KEYUP
事件表示某个键被释放。 可以通过 pygame.event.get()
函数获取这些事件。
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
# 键盘按下事件处理
pass
elif event.type == pygame.KEYUP:
# 键盘释放事件处理
pass
在这里我给出一个按下键盘的案例 —— 移动正方形:
'''
用键盘移动正方形移动
'''
import pygame
import sys
#初始化pygame
from pygame.locals import*
pygame.init()
#创建游戏窗口
width =640
height = 480
screen = pygame.display.set_mode((width,height))
#给游戏窗口命名
pygame.display.set_caption("原来是稀琳")
#正方形变量
rect_x = width / 2
rect_y = height / 2
rect_width = 20
rect_height =20
rect_color = (0,225,0)
rect_size = 20
rect_speed = 20
rect_direction = 'RIGHT'
#控制帧率
game_clock = pygame.time.Clock()
runing = True
while runing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
runing = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and event.key != 'DOWN':
rect_direction = 'UP'
elif event.key == pygame.K_DOWN and rect_direction != 'UP':
rect_direction = 'DOWN'
elif event.key == pygame.K_LEFT and rect_direction != 'RIGHT':
rect_direction = 'LEFT'
elif event.key == pygame.K_RIGHT and rect_direction != 'LEFT':
rect_direction = 'RIGHT'
if rect_direction == 'UP':
rect_y -= rect_speed
elif rect_direction == 'DOWN':
rect_y += rect_speed
elif rect_direction == 'LEFT':
rect_x -= rect_speed
elif rect_direction == 'RIGHT':
rect_x += rect_speed
if rect_x < 0:
rect_direction = 'RIGHT'
elif rect_x > width:
rect_direction = 'LEFT'
elif rect_y < 0:
rect_direction = 'DOWN'
elif rect_y > height:
rect_direction = 'UP'
#更新画面
screen.fill((0,0,0))
#画出正方形
pygame.draw.rect(screen,rect_color,(rect_x,rect_y,rect_width,rect_height),rect_size)
# 更新显示
pygame.display.flip()
# 控制帧率
pygame.time.Clock().tick(10)
#刷新游戏窗口
pygame.display.update()
#关闭游戏窗口
pygame.quit()
sys.exit()
接下来我会详细的讲解上述代码的意思:
1、画出正方形
2、定义正方形的速度和初始运动变量
rect_speed = 20
rect_direction = 'RIGHT'
3、键盘交互与一些基本运动逻辑
for event in pygame.event.get():
if event.type == pygame.QUIT:
runing = False
elif event.type == pygame.KEYDOWN:
'''为了让正方形在按键时合理运动'''
if event.key == pygame.K_UP and event.key != 'DOWN': # 向上运动时不能控制它向下运动
rect_direction = 'UP'
elif event.key == pygame.K_DOWN and rect_direction != 'UP':
rect_direction = 'DOWN'
elif event.key == pygame.K_LEFT and rect_direction != 'RIGHT':
rect_direction = 'LEFT'
elif event.key == pygame.K_RIGHT and rect_direction != 'LEFT':
rect_direction = 'RIGHT'
''' 键盘控制它持续运动的原因 '''
if rect_direction == 'UP':
rect_y -= rect_speed
elif rect_direction == 'DOWN':
rect_y += rect_speed
elif rect_direction == 'LEFT':
rect_x -= rect_speed
elif rect_direction == 'RIGHT':
rect_x += rect_speed
4、如果正方形撞到窗口外怎么办?以下是我给出的方案,即让正方形返回运动:
if rect_x < 0:
rect_direction = 'RIGHT'
elif rect_x > width:
rect_direction = 'LEFT'
elif rect_y < 0:
rect_direction = 'DOWN'
elif rect_y > height:
rect_direction = 'UP'
5、为了让正方形运动时不显示出运动的轨迹和运动的速度不太快,一下的代码就是重中之重:
#更新画面
screen.fill((0,0,0))
#画出正方形
pygame.draw.rect(screen,rect_color,(rect_x,rect_y,rect_width,rect_height),rect_size)
# 更新显示
pygame.display.flip()
# 控制帧率
pygame.time.Clock().tick(10)
#刷新游戏窗口
pygame.display.update()
6、关闭窗口(常规操作)
总而言之,整体难度还是比较大的,主要难点是运动的逻辑,这个需要自己在坐标轴上去推理。真的希望我能坚持下去,能够把既定的目标完成,真的。
与君共勉,下期再见