猫史档案馆


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

用户:淘气的炸弹人K3Pe淘气的炸弹人K3Pe查看:0 回复:2 评论:0 创建时间:2020-04-27T13:39:54


【作品展示】

center_image

 

【作品介绍】

贼秀

 

【作品源代码】

from arcade import*
# 设置窗体宽和高
SCREEN_WIDTH = 600
SCREEN_HELGHT= 800
# 设置窗体标题
c = "冒险小车"
class Road(Sprite):
    def __init__(self, image):
        super().__init__(image)
        # 车道
        self.center_x = SCREEN_WIDTH//2
        self.center_y = SCREEN_HELGHT//2
        # 设置车道的改变值
        self.change_y = -4
    def update(self):
        super().update()
        if self.center_y <= SCREEN_HELGHT//2-SCREEN_HELGHT:
            self.center_y = SCREEN_HELGHT//2+SCREEN_HELGHT
class SmallCar(Sprite):
    def __init__(self, image):
        super().__init__(image)
        # 小车
        self.center_x = SCREEN_WIDTH//2
        self.center_y = 200
    def update(self):
        self.image.update()
class Cart(Sprite):
    def __init__(self,image):
        super().__init__(image)
        self.center_y = random.randint(
            SCREEN_HELGHT, SCREEN_HELGHT+SCREEN_HELGHT//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_HELGHT//2


class StatusBar():
    def __init__(self):
        self.distance=0
        self.hp=2
    def draw_bar(self):
        draw_rectangle_filled(
            SCREEN_WIDTH//2, SCREEN_HELGHT-15, SCREEN_WIDTH,30,color.WHITE)
    def draw_distance(self):
        pos_x=10
        pos_y = SCREEN_HELGHT-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_HELGHT-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+5
            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.road = Road("images/车道.png")
        self.road2 = Road("images/车道.png")
        self.road2.center_y = SCREEN_HELGHT//2+SCREEN_HELGHT
        #小车
        self.喵all_car = SmallCar("images/小车1.png")
        #大车
        self.carts=SpriteList()
        self.喵all_car.draw()
        self.create_cart()
        #程序运行总时间
        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.road.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()
    def on_update(self, delta_time: float):
        if self.game_status:
            # 移动车道
            self.road.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) % 6 == 0:
                self.create_cart()
                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
    def create_cart(self):
        cart_list = ("images/大车1.png",
                      "images/大车2.png",
                      "images/大车3.png",
                        )
        x = random.sample(
            [SCREEN_WIDTH//2-200, SCREEN_WIDTH//2, SCREEN_WIDTH//2+200], 2)
        num = 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_HELGHT, c)
    run()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
一块软糖一块软糖

厉害👍

点赞0


评论


黑小帅黑小帅

???你在逗我???一堆黑色》???

点赞0


评论