pygame 库从零开始开发一个简单的 2D 平台跳跃游戏。首先,确保安装了 pygame:
pip install pygame创建项目文件结构:
jump_game/
├── jump_game.py # 主程序文件
└── README.md # 项目说明文档以下是游戏的逐步实现代码。
import pygame
import random
import sys
# 初始化 Pygame
pygame.init()
# 窗口设置
WIDTH, HEIGHT = 400, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("2D 平台跳跃游戏")
# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 游戏时钟
clock = pygame.time.Clock()
FPS = 60角色需要具有以下功能:
class Player:
def __init__(self):
self.width, self.height = 30, 30
self.x = WIDTH // 2
self.y = HEIGHT - self.height
self.color = BLUE
self.velocity_y = 0 # 垂直速度
self.jump_power = -15 # 跳跃初速度
self.gravity = 0.8 # 重力加速度
def move(self, keys):
if keys[pygame.K_LEFT]:
self.x -= 5
if keys[pygame.K_RIGHT]:
self.x += 5
# 边界限制
if self.x < 0:
self.x = 0
if self.x + self.width > WIDTH:
self.x = WIDTH - self.width
def jump(self):
self.velocity_y = self.jump_power
def apply_gravity(self):
self.velocity_y += self.gravity
self.y += self.velocity_y
def draw(self):
pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))平台的功能:
class Platform:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = GREEN
def draw(self):
pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))在游戏主函数中初始化玩家和平台对象。
def main():
player = Player()
platforms = [Platform(random.randint(0, WIDTH - 80), random.randint(100, HEIGHT), 80, 10)]
score = 0
while True:
screen.fill(WHITE) # 清空屏幕
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 获取按键状态
keys = pygame.key.get_pressed()
player.move(keys)
# 应用重力
player.apply_gravity()
# 碰撞检测
for platform in platforms:
if (player.y + player.height >= platform.y and
player.y + player.height - player.velocity_y <= platform.y and
platform.x < player.x < platform.x + platform.width):
player.jump()
# 绘制角色和平台
player.draw()
for platform in platforms:
platform.draw()
# 显示分数
font = pygame.font.SysFont("Arial", 24)
score_surface = font.render(f"Score: {score}", True, BLACK)
screen.blit(score_surface, (10, 10))
pygame.display.flip() # 更新屏幕
clock.tick(FPS)让平台随着玩家的跳跃动态生成,并增加分数。
def main():
player = Player()
platforms = [Platform(random.randint(0, WIDTH - 80), HEIGHT - i * 100, 80, 10) for i in range(6)]
score = 0
while True:
screen.fill(WHITE)
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
player.move(keys)
player.apply_gravity()
# 碰撞检测
for platform in platforms:
if (player.y + player.height >= platform.y and
player.y + player.height - player.velocity_y <= platform.y and
platform.x < player.x < platform.x + platform.width):
player.jump()
score += 1
# 更新平台位置
if player.y < HEIGHT // 2:
player.y += 5
for platform in platforms:
platform.y += 5
# 删除离开屏幕的平台,生成新平台
platforms = [platform for platform in platforms if platform.y < HEIGHT]
while len(platforms) < 6:
platforms.append(Platform(random.randint(0, WIDTH - 80), random.randint(-50, 0), 80, 10))
# 绘制
player.draw()
for platform in platforms:
platform.draw()
# 显示分数
font = pygame.font.SysFont("Arial", 24)
score_surface = font.render(f"Score: {score}", True, BLACK)
screen.blit(score_surface, (10, 10))
pygame.display.flip()
clock.tick(FPS)通过本文,我们使用 Python 和 pygame 从零开发了一个简单的 2D 平台跳跃游戏,涵盖了角色移动、平台生成、碰撞检测和动态得分等核心功能。这个项目是学习游戏开发的一个很好起点,能够帮助你掌握 pygame 的基本用法,并为构建更复杂的游戏奠定基础。
如果你有其他功能扩展的想法或改进建议,欢迎留言讨论!一起用 Python 创造更多有趣的游戏吧!🎮
如果需要进一步优化或添加新的功能,随时告诉我!
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。