文章目录
- 首先,我们需要导入 Pygame 模块并初始化它: import pygame pygame.init()
- 接下来,我们创建一个窗口并设置窗口的标题: # 设置窗口尺寸 window_size = (800, 600) screen = pygame.display.set_mode(window_size) # 设置窗口标题 pygame.display.set_caption(“My First Pygame Window”)
- Pygame 需要一个主循环来保持窗口的打开状态,并处理各种事件。以下是一个简单的主循环示例: # 主循环标志 running = True # 主循环 while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 退出 Pygame pygame.quit()
- import pygame # 初始化 Pygame pygame.init() # 设置窗口尺寸 window_size = (800, 600) screen = pygame.display.set_mode(window_size) # 设置窗口标题 pygame.display.set_caption(“My First Pygame Window”) # 主循环标志 running = True # 主循环 while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 退出 Pygame pygame.quit() 运行这段代码,你会看到一个简单的 Pygame 窗口,点击关闭按钮可以退出窗口。
- Pygame 使用 RGB 颜色值来设置颜色。以下是一些常见颜色的定义: WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255)
- 我们可以使用 pygame.draw.rect 来绘制矩形: pygame.draw.rect(screen, RED, (100, 100, 50, 50))
- 我们可以使用 pygame.draw.circle 来绘制圆形: pygame.draw.circle(screen, BLUE, (200, 200), 30)
- 绘制完图形后,需要更新屏幕以显示图形: pygame.display.flip()
- import pygame # 初始化 Pygame pygame.init() # 设置窗口尺寸 window_size = (800, 600) screen = pygame.display.set_mode(window_size) # 设置窗口标题 pygame.display.set_caption(“Drawing Shapes”) # 定义颜色 WHITE = (255, 255, 255) RED = (255, 0, 0) BLUE = (0, 0, 255) # 主循环标志 running = True # 主循环 while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 填充背景色 screen.fill(WHITE) # 绘制矩形 pygame.draw.rect(screen, RED, (100, 100, 50, 50)) # 绘制圆形 pygame.draw.circle(screen, BLUE, (200, 200), 30) # 更新屏幕 pygame.display.flip() # 退出 Pygame pygame.quit() 运行这段代码,你会看到一个窗口,里面绘制了一个红色的矩形和一个蓝色的圆形。
- 在游戏开发中,经常需要加载和显示图像。Pygame 提供了简单的接口来加载图像。首先,确保你有一个图像文件(例如 player.png)保存在你的项目目录中。 使用 pygame.image.load 函数加载图像: player_image = pygame.image.load(‘player.png’)
- 加载图像后,可以使用 blit 方法将图像绘制到屏幕上: screen.blit(player_image, (100, 100))
- 以下是一个完整的示例代码,演示如何加载和显示图像: import pygame # 初始化 Pygame pygame.init() # 设置窗口尺寸 window_size = (800, 600) screen = pygame.display.set_mode(window_size) # 设置窗口标题 pygame.display.set_caption(“Display Image”) # 加载图像 player_image = pygame.image.load(‘player.png’) # 主循环标志 running = True # 主循环 while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 填充背景色 screen.fill((255, 255, 255)) # 显示图像 screen.blit(player_image, (100, 100)) # 更新屏幕 pygame.display.flip() # 退出 Pygame pygame.quit() 运行这段代码,你会看到一个窗口,里面显示了加载的图像。
- Pygame 提供了简单的接口来处理键盘输入。你可以在主循环中检测按键事件: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: print(“Left arrow key pressed”) elif event.key == pygame.K_RIGHT: print(“Right arrow key pressed”)
- 你也可以处理鼠标事件,例如检测鼠标点击: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: print(“Mouse button pressed at”, event.pos)
- 以下是一个完整的示例代码,演示如何处理键盘和鼠标输入: import pygame # 初始化 Pygame pygame.init() # 设置窗口尺寸 window_size = (800, 600) screen = pygame.display.set_mode(window_size) # 设置窗口标题 pygame.display.set_caption(“Handle Input”) # 加载图像 player_image = pygame.image.load(‘player.png’) # 主循环标志 running = True # 主循环 while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: print(“Left arrow key pressed”) elif event.key == pygame.K_RIGHT: print(“Right arrow key pressed”) elif event.type == pygame.MOUSEBUTTONDOWN: print(“Mouse button pressed at”, event.pos) # 填充背景色 screen.fill((255, 255, 255)) # 显示图像 screen.blit(player_image, (100, 100)) # 更新屏幕 pygame.display.flip() # 退出 Pygame pygame.quit() 运行这段代码,你可以在控制台看到键盘和鼠标事件的输出。
- 为了让图像在屏幕上移动,我们需要更新图像的位置。可以使用变量来存储图像的位置,并在主循环中更新这些变量: player_x = 100 player_y = 100 # 移动图像 player_x += 1
- 以下是一个完整的示例代码,演示如何在屏幕上移动图像: import pygame # 初始化 Pygame pygame.init() # 设置窗口尺寸 window_size = (800, 600) screen = pygame.display.set_mode(window_size) # 设置窗口标题 pygame.display.set_caption(“Move Image”) # 加载图像 player_image = pygame.image.load(‘player.png’) # 初始化图像位置 player_x = 100 player_y = 100 # 主循环标志 running = True # 主循环 while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: player_x -= 5 elif event.key == pygame.K_RIGHT: player_x += 5 elif event.key == pygame.K_UP: player_y -= 5 elif event.key == pygame.K_DOWN: player_y += 5 # 填充背景色 screen.fill((255, 255, 255)) # 显示图像 screen.blit(player_image, (player_x, player_y)) # 更新屏幕 pygame.display.flip() # 退出 Pygame pygame.quit() 运行这段代码,你可以使用箭头键移动图像。
- 在 Pygame 中,可以使用 pygame.mixer 模块来处理声音。首先,需要初始化混音器: pygame.mixer.init()
- 可以使用 pygame.mixer.music 模块加载和播放背景音乐: pygame.mixer.music.load(‘background.mp3’) pygame.mixer.music.play(-1) # -1 表示循环播放
- 可以使用 pygame.mixer.Sound 类加载和播放音效: collision_sound = pygame.mixer.Sound(‘collision.wav’) collision_sound.play()
- 以下是一个完整的示例代码,演示如何添加背景音乐和音效: import pygame # 初始化 Pygame pygame.init() # 初始化混音器 pygame.mixer.init() # 设置窗口尺寸 window_size = (800, 600) screen = pygame.display.set_mode(window_size) # 设置窗口标题 pygame.display.set_caption(“Music and Sound Effects”) # 加载图像 player_image = pygame.image.load(‘player.png’) # 初始化图像位置 player_x = 100 player_y = 100 # 加载背景音乐和音效 pygame.mixer.music.load(‘background.mp3’) pygame.mixer.music.play(-1) # -1 表示循环播放 collision_sound = pygame.mixer.Sound(‘collision.wav’) # 主循环标志 running = True # 主循环 while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: player_x -= 5 elif event.key == pygame.K_RIGHT: player_x += 5 elif event.key == pygame.K_UP: player_y -= 5 elif event.key == pygame.K_DOWN: player_y += 5 elif event.key == pygame.K_SPACE: collision_sound.play() # 按下空格键播放音效 # 填充背景色 screen.fill((255, 255, 255)) # 显示图像 screen.blit(player_image, (player_x, player_y)) # 更新屏幕 pygame.display.flip() # 退出 Pygame pygame.quit() 运行这段代码,背景音乐会开始播放,并且按下空格键时会播放音效。
- Pygame 提供了多种方法来检测对象之间的碰撞。我们可以使用 pygame.Rect 对象来定义矩形区域,并使用 colliderect 方法来检测两个矩形是否碰撞。
- import pygame # 初始化 Pygame pygame.init() # 设置窗口尺寸 window_size = (800, 600) screen = pygame.display.set_mode(window_size) # 设置窗口标题 pygame.display.set_caption(“Collision Detection”) # 定义颜色 WHITE = (255, 255, 255) RED = (255, 0, 0) BLUE = (0, 0, 255) # 初始化矩形位置 rect1 = pygame.Rect(100, 100, 50, 50) rect2 = pygame.Rect(300, 300, 50, 50) # 主循环标志 running = True # 主循环 while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: rect1.x -= 5 elif event.key == pygame.K_RIGHT: rect1.x += 5 elif event.key == pygame.K_UP: rect1.y -= 5 elif event.key == pygame.K_DOWN: rect1.y += 5 # 填充背景色 screen.fill(WHITE) # 绘制矩形 pygame.draw.rect(screen, RED, rect1) pygame.draw.rect(screen, BLUE, rect2) # 碰撞检测 if rect1.colliderect(rect2): print(“Collision detected!”) # 更新屏幕 pygame.display.flip() # 退出 Pygame pygame.quit() 运行这段代码,你可以使用箭头键移动红色矩形,当红色矩形与蓝色矩形碰撞时,控制台会输出 “Collision detected!”。
- 为了控制游戏的帧率,我们可以使用 pygame.time.Clock 对象。它允许我们设置每秒的帧数(FPS),从而控制游戏的速度。 clock = pygame.time.Clock() 在主循环的每一帧末尾,调用 clock.tick(fps) 来限制帧率: clock.tick(60) # 设置帧率为 60 FPS
- 以下是一个简单的动画示例,演示如何使用 pygame.time.Clock 控制帧率: import pygame # 初始化 Pygame pygame.init() # 设置窗口尺寸 window_size = (800, 600) screen = pygame.display.set_mode(window_size) # 设置窗口标题 pygame.display.set_caption(“Animation with Frame Rate Control”) # 定义颜色 WHITE = (255, 255, 255) RED = (255, 0, 0) # 初始化矩形位置和速度 rect = pygame.Rect(100, 100, 50, 50) rect_speed = [2, 2] # 创建时钟对象 clock = pygame.time.Clock() # 主循环标志 running = True # 主循环 while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 移动矩形 rect.x += rect_speed[0] rect.y += rect_speed[1] # 碰到边界反弹 if rect.left < 0 or rect.right > window_size[0]: rect_speed[0] = -rect_speed[0] if rect.top < 0 or rect.bottom > window_size[1]: rect_speed[1] = -rect_speed[1] # 填充背景色 screen.fill(WHITE) # 绘制矩形 pygame.draw.rect(screen, RED, rect) # 更新屏幕 pygame.display.flip() # 控制帧率 clock.tick(60) # 设置帧率为 60 FPS # 退出 Pygame pygame.quit() 运行这段代码,你会看到一个红色矩形在窗口中移动并反弹。
目录
- 前言
- 安装 Pygame
- 1.1 使用 pip 安装
- 1.2 检查安装
- 创建一个简单的 Pygame 窗口
- 2.1 导入 Pygame 模块
- 2.2 创建一个窗口
- 2.3 主循环
- 2.4 完整代码
- 绘制图形
- 3.1 设置颜色
- 3.2 绘制矩形
- 3.3 绘制圆形
- 3.4 更新屏幕
- 3.5 完整代码
- 加载和显示图像
- 4.1 加载图像
- 4.2 显示图像
- 4.3 完整代码
- 处理用户输入
- 5.1 处理键盘输入
- 5.2 处理鼠标输入
- 5.3 完整代码
- 移动图像
- 6.1 更新图像位置
- 6.2 完整代码
- 添加背景音乐和音效
- 7.1 初始化混音器
- 7.2 加载和播放背景音乐
- 7.3 加载和播放音效
- 7.4 完整代码
- 处理碰撞检测
- 8.1 碰撞检测基础
- 8.2 示例:简单的碰撞检测
- 动画与帧率控制
- 9.1 使用pygame.time.Clock控制帧率
- 9.2 动画示例
- 总结
Pygame 是一个非常流行的 Python 库,用于编写视频游戏。它建立在 SDL(Simple DirectMedia Layer)库之上,提供了简单的接口来处理图形、声音和输入设备。本文将提供一个非常详细的 Pygame 教程,帮助你从零开始编写一个简单的游戏。
你可以使用 pip 来安装 Pygame:
pip install pygame
安装完成后,可以通过以下命令检查是否安装成功:
python -m pygame.examples.aliens
如果看到一个简单的游戏窗口,说明 Pygame 安装成功。
首先,我们需要导入 Pygame 模块并初始化它:
import pygame pygame.init()
接下来,我们创建一个窗口并设置窗口的标题:
# 设置窗口尺寸
window_size = (800, 600)
screen = pygame.display.set_mode(window_size)
# 设置窗口标题
pygame.display.set_caption("My First Pygame Window")
Pygame 需要一个主循环来保持窗口的打开状态,并处理各种事件。以下是一个简单的主循环示例:
# 主循环标志
running = True
# 主循环
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 退出 Pygame
pygame.quit()
import pygame
# 初始化 Pygame
pygame.init()
# 设置窗口尺寸
window_size = (800, 600)
screen = pygame.display.set_mode(window_size)
# 设置窗口标题
pygame.display.set_caption("My First Pygame Window")
# 主循环标志
running = True
# 主循环
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 退出 Pygame
pygame.quit()
运行这段代码,你会看到一个简单的 Pygame 窗口,点击关闭按钮可以退出窗口。
Pygame 使用 RGB 颜色值来设置颜色。以下是一些常见颜色的定义:
WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255)
我们可以使用 pygame.draw.rect 来绘制矩形:
pygame.draw.rect(screen, RED, (100, 100, 50, 50))
我们可以使用 pygame.draw.circle 来绘制圆形:
pygame.draw.circle(screen, BLUE, (200, 200), 30)
绘制完图形后,需要更新屏幕以显示图形:
pygame.display.flip()
import pygame
# 初始化 Pygame
pygame.init()
# 设置窗口尺寸
window_size = (800, 600)
screen = pygame.display.set_mode(window_size)
# 设置窗口标题
pygame.display.set_caption("Drawing Shapes")
# 定义颜色
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# 主循环标志
running = True
# 主循环
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 填充背景色
screen.fill(WHITE)
# 绘制矩形
pygame.draw.rect(screen, RED, (100, 100, 50, 50))
# 绘制圆形
pygame.draw.circle(screen, BLUE, (200, 200), 30)
# 更新屏幕
pygame.display.flip()
# 退出 Pygame
pygame.quit()
运行这段代码,你会看到一个窗口,里面绘制了一个红色的矩形和一个蓝色的圆形。
在游戏开发中,经常需要加载和显示图像。Pygame 提供了简单的接口来加载图像。首先,确保你有一个图像文件(例如 player.png)保存在你的项目目录中。
使用 pygame.image.load 函数加载图像:
player_image = pygame.image.load('player.png')
加载图像后,可以使用 blit 方法将图像绘制到屏幕上:
screen.blit(player_image, (100, 100))
以下是一个完整的示例代码,演示如何加载和显示图像:
import pygame
# 初始化 Pygame
pygame.init()
# 设置窗口尺寸
window_size = (800, 600)
screen = pygame.display.set_mode(window_size)
# 设置窗口标题
pygame.display.set_caption("Display Image")
# 加载图像
player_image = pygame.image.load('player.png')
# 主循环标志
running = True
# 主循环
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 填充背景色
screen.fill((255, 255, 255))
# 显示图像
screen.blit(player_image, (100, 100))
# 更新屏幕
pygame.display.flip()
# 退出 Pygame
pygame.quit()
运行这段代码,你会看到一个窗口,里面显示了加载的图像。
Pygame 提供了简单的接口来处理键盘输入。你可以在主循环中检测按键事件:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
print("Left arrow key pressed")
elif event.key == pygame.K_RIGHT:
print("Right arrow key pressed")
你也可以处理鼠标事件,例如检测鼠标点击:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
print("Mouse button pressed at", event.pos)
以下是一个完整的示例代码,演示如何处理键盘和鼠标输入:
import pygame
# 初始化 Pygame
pygame.init()
# 设置窗口尺寸
window_size = (800, 600)
screen = pygame.display.set_mode(window_size)
# 设置窗口标题
pygame.display.set_caption("Handle Input")
# 加载图像
player_image = pygame.image.load('player.png')
# 主循环标志
running = True
# 主循环
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
print("Left arrow key pressed")
elif event.key == pygame.K_RIGHT:
print("Right arrow key pressed")
elif event.type == pygame.MOUSEBUTTONDOWN:
print("Mouse button pressed at", event.pos)
# 填充背景色
screen.fill((255, 255, 255))
# 显示图像
screen.blit(player_image, (100, 100))
# 更新屏幕
pygame.display.flip()
# 退出 Pygame
pygame.quit()
运行这段代码,你可以在控制台看到键盘和鼠标事件的输出。
为了让图像在屏幕上移动,我们需要更新图像的位置。可以使用变量来存储图像的位置,并在主循环中更新这些变量:
player_x = 100 player_y = 100 # 移动图像 player_x += 1
以下是一个完整的示例代码,演示如何在屏幕上移动图像:
import pygame
# 初始化 Pygame
pygame.init()
# 设置窗口尺寸
window_size = (800, 600)
screen = pygame.display.set_mode(window_size)
# 设置窗口标题
pygame.display.set_caption("Move Image")
# 加载图像
player_image = pygame.image.load('player.png')
# 初始化图像位置
player_x = 100
player_y = 100
# 主循环标志
running = True
# 主循环
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_x -= 5
elif event.key == pygame.K_RIGHT:
player_x += 5
elif event.key == pygame.K_UP:
player_y -= 5
elif event.key == pygame.K_DOWN:
player_y += 5
# 填充背景色
screen.fill((255, 255, 255))
# 显示图像
screen.blit(player_image, (player_x, player_y))
# 更新屏幕
pygame.display.flip()
# 退出 Pygame
pygame.quit()
运行这段代码,你可以使用箭头键移动图像。
在 Pygame 中,可以使用 pygame.mixer 模块来处理声音。首先,需要初始化混音器:
pygame.mixer.init()
可以使用 pygame.mixer.music 模块加载和播放背景音乐:
pygame.mixer.music.load('background.mp3')
pygame.mixer.music.play(-1) # -1 表示循环播放
可以使用 pygame.mixer.Sound 类加载和播放音效:
collision_sound = pygame.mixer.Sound('collision.wav')
collision_sound.play()
以下是一个完整的示例代码,演示如何添加背景音乐和音效:
import pygame
# 初始化 Pygame
pygame.init()
# 初始化混音器
pygame.mixer.init()
# 设置窗口尺寸
window_size = (800, 600)
screen = pygame.display.set_mode(window_size)
# 设置窗口标题
pygame.display.set_caption("Music and Sound Effects")
# 加载图像
player_image = pygame.image.load('player.png')
# 初始化图像位置
player_x = 100
player_y = 100
# 加载背景音乐和音效
pygame.mixer.music.load('background.mp3')
pygame.mixer.music.play(-1) # -1 表示循环播放
collision_sound = pygame.mixer.Sound('collision.wav')
# 主循环标志
running = True
# 主循环
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_x -= 5
elif event.key == pygame.K_RIGHT:
player_x += 5
elif event.key == pygame.K_UP:
player_y -= 5
elif event.key == pygame.K_DOWN:
player_y += 5
elif event.key == pygame.K_SPACE:
collision_sound.play() # 按下空格键播放音效
# 填充背景色
screen.fill((255, 255, 255))
# 显示图像
screen.blit(player_image, (player_x, player_y))
# 更新屏幕
pygame.display.flip()
# 退出 Pygame
pygame.quit()
运行这段代码,背景音乐会开始播放,并且按下空格键时会播放音效。
Pygame 提供了多种方法来检测对象之间的碰撞。我们可以使用 pygame.Rect 对象来定义矩形区域,并使用 colliderect 方法来检测两个矩形是否碰撞。
import pygame
# 初始化 Pygame
pygame.init()
# 设置窗口尺寸
window_size = (800, 600)
screen = pygame.display.set_mode(window_size)
# 设置窗口标题
pygame.display.set_caption("Collision Detection")
# 定义颜色
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# 初始化矩形位置
rect1 = pygame.Rect(100, 100, 50, 50)
rect2 = pygame.Rect(300, 300, 50, 50)
# 主循环标志
running = True
# 主循环
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
rect1.x -= 5
elif event.key == pygame.K_RIGHT:
rect1.x += 5
elif event.key == pygame.K_UP:
rect1.y -= 5
elif event.key == pygame.K_DOWN:
rect1.y += 5
# 填充背景色
screen.fill(WHITE)
# 绘制矩形
pygame.draw.rect(screen, RED, rect1)
pygame.draw.rect(screen, BLUE, rect2)
# 碰撞检测
if rect1.colliderect(rect2):
print("Collision detected!")
# 更新屏幕
pygame.display.flip()
# 退出 Pygame
pygame.quit()
运行这段代码,你可以使用箭头键移动红色矩形,当红色矩形与蓝色矩形碰撞时,控制台会输出 “Collision detected!”。
为了控制游戏的帧率,我们可以使用 pygame.time.Clock 对象。它允许我们设置每秒的帧数(FPS),从而控制游戏的速度。
clock = pygame.time.Clock()
在主循环的每一帧末尾,调用 clock.tick(fps) 来限制帧率:
clock.tick(60) # 设置帧率为 60 FPS
以下是一个简单的动画示例,演示如何使用 pygame.time.Clock 控制帧率:
import pygame
# 初始化 Pygame
pygame.init()
# 设置窗口尺寸
window_size = (800, 600)
screen = pygame.display.set_mode(window_size)
# 设置窗口标题
pygame.display.set_caption("Animation with Frame Rate Control")
# 定义颜色
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# 初始化矩形位置和速度
rect = pygame.Rect(100, 100, 50, 50)
rect_speed = [2, 2]
# 创建时钟对象
clock = pygame.time.Clock()
# 主循环标志
running = True
# 主循环
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 移动矩形
rect.x += rect_speed[0]
rect.y += rect_speed[1]
# 碰到边界反弹
if rect.left < 0 or rect.right > window_size[0]:
rect_speed[0] = -rect_speed[0]
if rect.top < 0 or rect.bottom > window_size[1]:
rect_speed[1] = -rect_speed[1]
# 填充背景色
screen.fill(WHITE)
# 绘制矩形
pygame.draw.rect(screen, RED, rect)
# 更新屏幕
pygame.display.flip()
# 控制帧率
clock.tick(60) # 设置帧率为 60 FPS
# 退出 Pygame
pygame.quit()
运行这段代码,你会看到一个红色矩形在窗口中移动并反弹。
通过这篇详细的 Pygame 教程,你已经学会了如何:
- 安装 Pygame。
- 创建一个简单的 Pygame 窗口。
- 绘制图形。
- 加载和显示图像。
- 处理用户输入(键盘和鼠标)。
- 移动图像。
- 添加背景音乐和音效。
- 处理碰撞检测。
- 实现动画并控制帧率。
这些基础知识将帮助你开始使用 Pygame 开发自己的游戏。
到此这篇关于Python Pygame教程的文章就介绍到这了,更多相关Python Pygame教程内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!
您可能感兴趣的文章:
- python中pygame安装过程(超级详细)
- python Pygame的具体使用讲解
- Python中pygame安装方法图文详解
- Python的pygame安装教程详解
- Python中Pygame模块的详细安装过程
- python pygame实现五子棋小游戏
- python实现飞机大战游戏(pygame版)
- python使用pygame模块实现坦克大战游戏
- python实现五子棋游戏(pygame版)