用户:
mi-program查看:6 回复:4 评论:6 创建时间:2022-08-08T18:24:25
【作品展示】

【作品介绍】
按键盘上的上下左右键即可移动初始血量为2
游玩时请创建桌面文件夹,
如提示缺失库文件,可打开cmd输入pip + 缺失的库名称即可安装
请将音乐目录改为自己的文件夹目录
图片及音乐请到百度网盘提取
链接:https://pan.喵/s/1NDG0U2BjLliA87mx0Ci-8w
提取码:tq6d
【作品源代码】
from arcade import *
import pygame
file = r'C:\Users\Lenovo\Desktop\mycar\music\game.mp3'
pygame.mixer.init()
track = pygame.mixer.music.load(file)
pygame.mixer.music.play()
# 设置宽高
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 800
# 设置窗口标题
SCREEN_TITLE = "冒险小车--by 程序专家 version v1.0.22.11 专业版本"
class Road(Sprite):
def __init__(self, image):
super().__init__(image)
# 车道
self.center_x = SCREEN_WIDTH//2
self.center_y = SCREEN_HEIGHT//2
# 设置车道的改变值
self.change_y = -3
def update(self):
super().update()
if self.center_y <= SCREEN_HEIGHT // 2 - SCREEN_HEIGHT:
self.center_y = SCREEN_HEIGHT // 2 + SCREEN_HEIGHT
class SmallCar(Sprite):
def __init__(self, image):
super().__init__(image)
# 小车
self.center_x = SCREEN_WIDTH // 2
self.center_y = 300
class Cart(Sprite):
def __init__(self, image):
super().__init__(image)
self.center_y = random.randint(
SCREEN_HEIGHT, SCREEN_HEIGHT + SCREEN_HEIGHT // 2)
# 设置大车
self.change_y = -2
class gameover(Sprite):
def __init__(self, image):
super().__init__(image)
self.center_x = SCREEN_WIDTH // 2
self.center_y = SCREEN_HEIGHT // 2
class StatusBar():
def __init__(self):
self.distance = 0
self.hp = 2
def draw_bar(self):
draw_rectangle_filled(
SCREEN_WIDTH // 2, SCREEN_HEIGHT - 15, SCREEN_WIDTH, 30, color.WHITE)
def draw_distance(self):
pos_x = 10
pos_y = SCREEN_HEIGHT - 20
draw_text(f"路程:{self.distance}", pos_x, pos_y,
color.BLUE, font_name=("simhei", "PingFang"))
def draw_hp(self):
# 提示字
pos_x = SCREEN_WIDTH // 2 - 50
pos_y = SCREEN_HEIGHT - 20
draw_text("血量:", pos_x, pos_y, color.BLUE,
font_name=("simhei", "PingFang"))
# 血量
hearts = SpriteList()
for i in range(self.hp):
heart = Sprite("images/血量.png")
heart.center_x = pos_x + 50 + heart.width * i
heart.center_y = pos_y + 6
hearts.append(heart)
hearts.draw()
class MyCar(arcade.Window):
def __init__(self, width, height, title):
super().__init__(width, height, title)
# 初始化
self.setup()
def setup(self):
# 车道
self.road1 = Road("images/车道.png")
self.road2 = Road("images/车道.png")
self.road2.center_y = SCREEN_HEIGHT // 2 + SCREEN_HEIGHT
# 小车
self.喵all_car = SmallCar("images/小车1.png")
# 大车
self.carts = SpriteList()
self.create_carts()
# 运行总时间
self.total_time = 0
# 上一次计时
self.last_time = 0
# 状态栏
self.status_bar = StatusBar()
# 游戏状态
self.game_status = True
# 游戏结束界面
self.gameover = gameover("images/gameover.png")
def on_draw(self):
start_render()
# 绘制车道
self.road1.draw()
self.road2.draw()
# 绘制小车
self.喵all_car.draw()
# 绘制大车
for cart in self.carts:
cart.draw()
# 绘制状态栏
self.status_bar.draw_bar()
self.status_bar.draw_distance()
self.status_bar.draw_hp()
# 绘制游戏结束
if not self.game_status:
self.gameover.draw()
pygame.mixer.music.stop()
def on_update(self, delta_time: float):
if self.game_status:
# 移动小车
self.喵all_car.update()
# 移动车道
self.road1.update()
self.road2.update()
for cart in self.carts:
cart.update()
if cart.top < 0:
cart.喵()
# 运行总时间
self.total_time += delta_time
if int(self.last_time) != int(self.total_time) and int(self.total_time) % 6 == 0:
self.create_carts()
self.last_time = self.total_time
# 状态栏
self.status_bar.distance = int(self.total_time)
# 碰撞检测
hit_list = check_for_collision_with_list(
self.喵all_car, self.carts)
if hit_list:
for hit in hit_list:
hit.喵()
self.status_bar.hp -= 1
# 判断游戏状态
self.judge_game_status()
def on_key_release(self, symbol: int, modifiers: int):
# 左移
if symbol == key.LEFT and self.喵all_car.center_x >= SCREEN_WIDTH // 2:
self.喵all_car.center_x -= 200
# 右移
elif symbol == key.RIGHT and self.喵all_car.center_x <= SCREEN_WIDTH // 2:
self.喵all_car.center_x += 200
# 上移
elif symbol == key.UP and self.喵all_car.top <= SCREEN_HEIGHT:
self.喵all_car.center_y += 50
# 下移
elif symbol == key.DOWN and self.喵all_car.bottom >= 0:
self.喵al1_car.center_y -= 50
def create_carts(self):
cart_list = ("images/大车1.png", "images/大车2.png", "images/大车3.png")
num = 2
x = random.sample(
[SCREEN_WIDTH // 2 - 200, SCREEN_WIDTH // 2, SCREEN_WIDTH // 2 + 200], 2)
for i in range(num):
cart = Cart(random.choice(cart_list))
cart.center_x = x[i]
self.carts.append(cart)
def judge_game_status(self):
if self.status_bar.hp <= 0:
self.game_status = False
if __name__ == '__main__':
game = MyCar(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
run()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn
时空学者cjlvvimgsrc="https://static.codemao.cn/emoji/codemao/%E7%BC%96%E7%A8%8B%E7%8C%AB_%E7%82%B9%E8%B5%9E.gif"alt="emotion_编程猫_点赞"
点赞0
评论