pygame跑酷小游戏代码
时间: 2023-09-25 13:07:09 浏览: 71
以下是一个简单的pygame跑酷小游戏代码,包括角色移动、障碍物生成、碰撞检测等基本功能:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 设置游戏窗口大小和标题
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Pygame Runner")
# 加载角色和障碍物图片
player_img = pygame.image.load("player.png")
obstacle_img = pygame.image.load("obstacle.png")
# 定义角色和障碍物类
class Player:
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 64
self.height = 64
self.vel = 5
self.is_jump = False
self.jump_count = 10
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
def draw(self, screen):
screen.blit(player_img, (self.x, self.y))
def move(self, direction):
if direction == "left":
self.x -= self.vel
elif direction == "right":
self.x += self.vel
# 限制角色在窗口内移动
if self.x < 0:
self.x = 0
elif self.x + self.width > screen_width:
self.x = screen_width - self.width
def jump(self):
if self.jump_count >= -10:
if self.jump_count < 0:
self.y += (self.jump_count ** 2) / 2
else:
self.y -= (self.jump_count ** 2) / 2
self.jump_count -= 1
else:
self.is_jump = False
self.jump_count = 10
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
class Obstacle:
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 32
self.height = 32
self.vel = 5
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
def draw(self, screen):
screen.blit(obstacle_img, (self.x, self.y))
def move(self):
self.x -= self.vel
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
# 定义生成障碍物函数
def generate_obstacle(obstacles):
gap = 150
min_height = 100
max_height = screen_height - gap - min_height
height = random.randint(min_height, max_height)
obstacles.append(Obstacle(screen_width, height)))
# 定义碰撞检测函数
def collision(player, obstacles):
for obstacle in obstacles:
if player.rect.colliderect(obstacle.rect):
return True
return False
# 创建角色和障碍物对象
player = Player(100, screen_height - 64)
obstacles = []
# 游戏循环
clock = pygame.time.Clock()
score = 0
font = pygame.font.Font(None, 30)
run = True
while run:
# 设置帧率
clock.tick(60)
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# 按键处理
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.move("left")
if keys[pygame.K_RIGHT]:
player.move("right")
if keys[pygame.K_SPACE] and not player.is_jump:
player.is_jump = True
# 角色跳跃
if player.is_jump:
player.jump()
# 生成障碍物
if len(obstacles) == 0 or obstacles[-1].x < screen_width - 200:
generate_obstacle(obstacles)
# 移动障碍物
for obstacle in obstacles:
obstacle.move()
if obstacle.x < -obstacle.width:
obstacles.remove(obstacle)
score += 1
# 碰撞检测
if collision(player, obstacles):
run = False
# 绘制游戏界面
screen.fill((255, 255, 255))
player.draw(screen)
for obstacle in obstacles:
obstacle.draw(screen)
score_text = font.render("Score: " + str(score), True, (0, 0, 0))
screen.blit(score_text, (10, 10))
pygame.display.update()
# 退出游戏
pygame.quit()
```
注:该代码仅为示例,仍有改进空间,如优化障碍物生成和移动的算法,增加音效和更精美的画面等。
阅读全文