猫史档案馆


【Python作品分享】重复打车-1-1-1【作品秀】

用户:王博辉666王博辉666查看:0 回复:0 评论:0 创建时间:2020-08-15T14:30:40


【作品展示】

center_image

 

【作品介绍】

“←”件车子向左

“→”件车子向右

 

【作品源代码】

from arcade import *

# 设置窗体宽和高
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 800
# 设置窗体标题
SCREEN_TITLE = "冒险小车"


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 = 100


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 = -5

class 喵():
    def __init__(self):
        self.distance=0
        self.hp=3

    def draw(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(f'血量:', pos_x, pos_y,color.BLUE, font_name=('simhei', 'PingFang'))
        hs=SpriteList()
        for i in range(self.hp):
            h=Sprite('images/血量.png')
            h.center_x=pos_x+50+h.width*i
            h.center_y=pos_y+5
            hs.append(h)
        hs.draw()
class Game(Sprite):
    def __init__(self,img):
        super().__init__(img)
        self.center_x = SCREEN_WIDTH // 2
        self.center_y = SCREEN_HEIGHT//2


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.gameover = Game('images/gameover.png')

        self.喵=喵()
        self.game=True
    def on_draw(self):
        start_render()
        # 绘制车道
        self.road1.draw()
        self.road2.draw()
        # 绘制小车
        self.喵all_car.draw()
        for cart in self.carts:
            cart.draw()
        self.喵.draw()
        self.喵.draw_distance()
        self.喵.draw_hp()
        if not self.game:
            self.gameover.draw()


    def on_update(self, delta_time: float):
        if self.game:
            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.喵.distance=int(self.total_time)
            h_list=check_for_collision_with_list(self.喵all_car,self.carts)
            if h_list:
                for h in h_list:
                    h.喵()
                    self.喵.hp-=1
            self.g()
    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_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)  # 添加到carts列表里
    def g(self):
        if self.喵.hp<=0:
            self.game=False

if __name__ == '__main__':
    game = MyCar(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
    run()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页