pygame跑酷小游戏代码,要求:1分钟内获得一千积分游戏通关否则游戏失败
时间: 2023-08-24 11:16:14 浏览: 109
pygame实现雷电游戏雏形开发
下面是一个实现要求的pygame跑酷小游戏代码:
```python
import pygame
import random
pygame.init()
# 游戏窗口大小
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 创建游戏窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('跑酷小游戏')
# 加载玩家图片
player_img = pygame.image.load('player.png')
player_width = 50
player_height = 50
# 加载障碍物图片
obstacle_img = pygame.image.load('obstacle.png')
obstacle_width = 50
obstacle_height = 50
# 加载背景图片
bg_img = pygame.image.load('bg.png')
# 设置字体
font = pygame.font.SysFont(None, 50)
# 定义玩家类
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = player_img
self.rect = self.image.get_rect()
self.rect.x = SCREEN_WIDTH // 2
self.rect.y = SCREEN_HEIGHT - player_height - 10
self.speed_x = 0
self.speed_y = 0
def update(self):
# 移动玩家
self.rect.x += self.speed_x
self.rect.y += self.speed_y
# 碰到边界停止移动
if self.rect.x < 0:
self.rect.x = 0
elif self.rect.x > SCREEN_WIDTH - player_width:
self.rect.x = SCREEN_WIDTH - player_width
if self.rect.y < 0:
self.rect.y = 0
elif self.rect.y > SCREEN_HEIGHT - player_height:
self.rect.y = SCREEN_HEIGHT - player_height
# 定义障碍物类
class Obstacle(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = obstacle_img
self.rect = self.image.get_rect()
self.rect.x = random.randint(0, SCREEN_WIDTH - obstacle_width)
self.rect.y = -obstacle_height
self.speed_y = random.randint(4, 10)
def update(self):
# 移动障碍物
self.rect.y += self.speed_y
# 创建精灵组
all_sprites = pygame.sprite.Group()
obstacles = pygame.sprite.Group()
# 创建玩家和障碍物
player = Player()
all_sprites.add(player)
for i in range(10):
obstacle = Obstacle()
all_sprites.add(obstacle)
obstacles.add(obstacle)
# 游戏主循环
game_over = False
score = 0
clock = pygame.time.Clock()
while not game_over:
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.speed_x = -5
elif event.key == pygame.K_RIGHT:
player.speed_x = 5
elif event.key == pygame.K_UP:
player.speed_y = -5
elif event.key == pygame.K_DOWN:
player.speed_y = 5
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
player.speed_x = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
player.speed_y = 0
# 更新精灵组
all_sprites.update()
# 绘制背景
screen.blit(bg_img, (0, 0))
# 绘制精灵组
all_sprites.draw(screen)
# 检测碰撞
hits = pygame.sprite.spritecollide(player, obstacles, False)
if hits:
game_over = True
# 绘制得分
score += 1
text = font.render('得分:{}'.format(score), True, WHITE)
screen.blit(text, (10, 10))
# 更新屏幕
pygame.display.update()
# 控制帧率
clock.tick(60)
# 判断游戏是否通关
if score >= 1000:
game_over = True
# 游戏结束,显示通关或失败信息
if score >= 1000:
text = font.render('恭喜通关!', True, GREEN)
else:
text = font.render('很遗憾,游戏失败!', True, RED)
screen.blit(text, (SCREEN_WIDTH // 2 - 150, SCREEN_HEIGHT // 2 - 25))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
```
该代码实现了玩家控制一个小人在随机生成的障碍物中跑酷,并计算得分,当玩家获得一千积分时游戏通关,否则游戏失败。你可以根据自己的需要修改代码并添加更多的游戏元素。
阅读全文