用python写一个手机的赛车游戏
时间: 2024-05-29 20:03:27 浏览: 168
好的,我可以给你提供一个简单的思路和代码实现。
思路:
1. 导入 pygame 模块,创建窗口和游戏界面;
2. 加载赛车、障碍物和背景音乐;
3. 控制赛车左右移动和加速,随机生成障碍物;
4. 判断赛车是否与障碍物相撞或是否超过边界;
5. 计分并显示在界面上;
6. 添加游戏结束的提示和重新开始按钮。
代码实现:
```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("赛车游戏")
# 加载赛车和障碍物图片
car_img = pygame.image.load("car.png")
obstacle_img = pygame.image.load("obstacle.png")
# 加载背景音乐
pygame.mixer.music.load("background_music.mp3")
pygame.mixer.music.play(-1)
# 创建赛车和障碍物的类
class Car:
def __init__(self):
self.image = car_img
self.width = self.image.get_width()
self.height = self.image.get_height()
self.x = (screen_width - self.width) // 2
self.y = screen_height - self.height - 50
self.speed = 0
def move_left(self):
if self.x > 0:
self.x -= 10
def move_right(self):
if self.x + self.width < screen_width:
self.x += 10
def accelerate(self):
self.speed += 1
if self.speed > 5:
self.speed = 5
def decelerate(self):
self.speed -= 1
if self.speed < 0:
self.speed = 0
def draw(self):
screen.blit(self.image, (self.x, self.y))
class Obstacle:
def __init__(self):
self.image = obstacle_img
self.width = self.image.get_width()
self.height = self.image.get_height()
self.x = random.randint(0, screen_width - self.width)
self.y = 0
self.speed = random.randint(1, 3)
def move(self):
self.y += self.speed
def draw(self):
screen.blit(self.image, (self.x, self.y))
# 创建赛车和障碍物的实例
car = Car()
obstacles = []
# 设置游戏结束的标志
game_over = False
# 设置得分和字体
score = 0
font = pygame.font.Font(None, 36)
# 游戏循环
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:
car.move_left()
elif event.key == pygame.K_RIGHT:
car.move_right()
elif event.key == pygame.K_UP:
car.accelerate()
elif event.key == pygame.K_DOWN:
car.decelerate()
# 清空屏幕
screen.fill((255, 255, 255))
# 绘制赛车和障碍物
car.draw()
for obstacle in obstacles:
obstacle.move()
obstacle.draw()
# 删除超出屏幕的障碍物
for obstacle in obstacles:
if obstacle.y > screen_height:
obstacles.remove(obstacle)
# 随机生成障碍物
if random.randint(1, 100) == 1:
obstacles.append(Obstacle())
# 判断赛车是否与障碍物相撞
for obstacle in obstacles:
if car.x + car.width > obstacle.x and car.x < obstacle.x + obstacle.width and car.y + car.height > obstacle.y and car.y < obstacle.y + obstacle.height:
game_over = True
# 判断赛车是否超过边界
if car.x < 0 or car.x + car.width > screen_width:
game_over = True
# 计分并显示在界面上
score += 1
score_text = font.render("得分:" + str(score), True, (0, 0, 0))
screen.blit(score_text, (10, 10))
# 更新屏幕
pygame.display.update()
# 游戏结束后显示提示和重新开始按钮
font = pygame.font.Font(None, 72)
game_over_text = font.render("游戏结束", True, (255, 0, 0))
restart_text = font.render("重新开始", True, (0, 0, 255))
game_over_rect = game_over_text.get_rect(center=(screen_width//2, screen_height//2-50))
restart_rect = restart_text.get_rect(center=(screen_width//2, screen_height//2+50))
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
if restart_rect.collidepoint(mouse_pos):
game_over = False
score = 0
car = Car()
obstacles = []
# 清空屏幕
screen.fill((255, 255, 255))
# 显示提示和重新开始按钮
screen.blit(game_over_text, game_over_rect)
screen.blit(restart_text, restart_rect)
pygame.display.update()
```
这是一个简单的赛车游戏,你可以根据自己的需求进行修改和扩展。
阅读全文