猫史档案馆


【Python作品分享】冒险小车【作品秀】

用户:疯狂的石头162疯狂的石头162查看:0 回复:0 评论:0 创建时间:2020-08-09T11:06:13


【作品展示】

center_image

 

【作品介绍】

一个简单的游戏←,→ 或 A, D建控制移动。

 

【作品源代码】

from arcade import *
# 设置窗体宽和高
SCREEN_WIDTH = 600
SCREEN_HIGHT = 690
# 设置窗体标题
SCREEN_TITLE = "冒险小车"
# 警告
print("警告,请注意!!")
print("严禁抄袭、抄写、复制等一系列操作,否则:后果自负,后果自负,后果自负!!!!!!!!!!")
print("石头制作。")
print("")


class Road(Sprite):
    def __init__(self, image):
        super().__init__(image)
        # 车道
        self.center_x = SCREEN_WIDTH // 2
        self.center_y = SCREEN_HIGHT // 2
        self.change_y = -6


    def update(self):
        super().update()
        if self.center_y <= SCREEN_HIGHT // 2 - SCREEN_HIGHT:
            self.center_y = SCREEN_HIGHT // 2 + SCREEN_HIGHT


class SmallCar(Sprite):
    def __init__(self, image):
        super().__init__(image)
        # 小车
        self.center_x = SCREEN_WIDTH // 2
        self.center_y = 70


class Cart(Sprite):
    def __init__(self, image):
        super().__init__(image)
        self.center_y = random.randint(SCREEN_HIGHT, SCREEN_HIGHT + SCREEN_HIGHT//2)
        # 大车移动
        self.change_y = -5


class GameOver(Sprite):
    def __init__(self, image):
        super().__init__(image)
        # 小车
        self.center_x = SCREEN_WIDTH // 2
        self.center_y = SCREEN_HIGHT // 2


class StatusBar():
    def __init__(self):
        self.distance = 0
        self.hp = 5


    def draw_bar(self):
        draw_rectangle_filled(SCREEN_WIDTH // 2, SCREEN_HIGHT - 15, SCREEN_WIDTH, 30, color.WHITE)


    def draw_disyance(self):
            pos_x = 10
            pos_y = SCREEN_HIGHT - 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_HIGHT - 20
        draw_text("血量:", pos_x, pos_y, color.RED, 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 + 5
            hearts.append(heart)
        hearts.draw()
        

class MyCar(arcade.Window):
    # 绘制
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        self.steup()


    def steup(self):
        # 车道
        self.road1 = Road("images/车道.png")
        self.road2 = Road("images/车道.png")
        self.road2.center_y = SCREEN_HIGHT//2 + SCREEN_HIGHT
        # 小车
        self.SmallCar = 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.SmallCar.draw()
        # 绘制大车
        for card in self.carts:
            card.draw()
        # 绘制状态栏
        self.status_bar.draw_bar()
        self.status_bar.draw_disyance()
        self.status_bar.draw_hp()
        # 绘制游戏结束
        if not self.game_status:
            self.GameOver.draw()


    def on_update(self, delta_time: float):
        # 判断游戏
        if self.game_status:
            #移动小车
            self.SmallCar.update()
            # 移动车道
            self.road1.update()
            self.road2.update()
            # 移动大车
            for card in self.carts:
                card.update()
                if card.top < 0:
                    card.喵()
            # 程序运行总时间
            self.total_time += delta_time
            if int(self.last_time) != int(self.total_time) and int(self.total_time) % 3 == 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.SmallCar, 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, modifiers):
        # 判断游戏
        if self.game_status:
            # 左移
            if symbol == key.A and self.SmallCar.center_x >= SCREEN_WIDTH//2:
                self.SmallCar.center_x -= 200
            if symbol == key.LEFT and self.SmallCar.center_x >= SCREEN_WIDTH//2:
                self.SmallCar.center_x -= 200
            # 右移
            if symbol == key.D and self.SmallCar.center_x <= SCREEN_WIDTH//2:
                self.SmallCar.center_x += 200
            if symbol == key.RIGHT and self.SmallCar.center_x <= SCREEN_WIDTH//2:
                self.SmallCar.center_x += 200


    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_HIGHT, SCREEN_TITLE)
    run()


# 空格
print("")

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页