猫史档案馆


【Python作品分享】【120强进36】进击的火箭【作业帖】

用户:mornwindmornwind查看:0 回复:0 评论:0 创建时间:2021-05-15T11:41:54


【作品展示】

center_image

 

【作品介绍】

以上为图片素材(为了方便我把它合成了一个GIF)

三条命。使用左右键控制火箭,不要被小行星撞到(扣一条命),坚持!

 

【作品源代码】

from arcade import *


W = 600
H = 800
T = "冲鸭 火箭!"


class starry_sky(Sprite):
    def __init__(self, sky):
        super().__init__(sky)
        self.center_x = W//2
        self.center_y = H//2
        self.change_y = -3
        self.update()

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


class Rocket(Sprite):
    def __init__(self, rocket):
        super().__init__(rocket)
        self.center_x = 300
        self.center_y = 100


class Bar():
    def __init__(self):
        self.go = 0
        self.hp = 3
        self.die = False

    def bar(self):
        draw_rectangle_filled(W//2, H-15, W, 30, color.WHITE)

    def distance(self):
        x = 10
        y = H-20
        draw_text(f"路程:{self.dis}", x, y, color.BLUE,
                  font_name=["Simhei", "PingFang"])

    def hpc(self):
        x = W//2-50
        y = H-20
        draw_text("血量:", x, y, color.BLUE, font_name=["Simhei", "PingFang"])
        self.hs = SpriteList()
        for i in range(self.hp):
            h = Sprite("血量.png")
            h.center_x = x+50+h.width*i
            h.center_y = y+5
            self.hs.append(h)
        self.hs.draw()
        if self.dis <= 5:
            self.beat = 0
        elif self.dis <= 30:
            self.beat = 10
        elif self.dis <= 60:
            self.beat = 30
        elif self.dis <= 100:
            self.beat = 50
        elif self.dis <= 200:
            self.beat = 85
        else:
            self.beat = 100
        if self.die:
            draw_text(f"您喵亡了,坚持时间{self.dis}秒,击败了全国{self.beat}%的选手!", 100, 200, color.RED,
                      font_name=["Simhei", "PingFang"])


class Asteroid(Sprite):
    def __init__(self, image):
        super().__init__(image)
        self.center_y = random.randint(H, H+H//2)
        self.change_y = -2.5


class Gameover(Sprite):
    def __init__(self, image):
        super().__init__(image)
        self.center_x = 300
        self.center_y = 400


class draw(arcade.Window):
    def __init__(self, w, h, t):
        super().__init__(w, h, t)
        self.setup()

    def setup(self):
        self.starry_sky1 = starry_sky("宇宙.png")
        self.starry_sky2 = starry_sky("宇宙.png")
        self.starry_sky2.center_y += H
        self.rocket = Rocket("火箭.png")
        self.gameover = Gameover("gameover.png")
        self.asteroids = SpriteList()
        self.create_asteroids()
        self.time = 0
        self.last_time = 0
        self.bar = Bar()
        self.game = True

    def on_draw(self):
        start_render()
        self.starry_sky1.draw()
        self.starry_sky2.draw()
        self.rocket.draw()
        for asteroid in self.asteroids:
            asteroid.draw()

        draw_line(200, 0, 200, H-30, color.BLACK, 5)
        draw_line(400, 0, 400, H-30, color.BLACK, 5)
        if not(self.game):
            self.gameover.draw()
            self.bar.die = True
        self.bar.bar()
        self.bar.distance()
        self.bar.hpc()

    def on_update(self, delta_time: float):
        if self.game:
            self.rocket.update()
            self.starry_sky1.update()
            self.starry_sky2.update()
            for i in self.asteroids:
                i.update()
                if i.top < 0:
                    i.kill()
            self.time += delta_time
            if int(self.time) != int(self.last_time) and int(self.time) % 6 == 0:
                self.last_time = self.time
                self.create_asteroids()
            self.bar.dis = int(self.time)

            hit_list = check_for_collision_with_list(
                self.rocket, self.asteroids)
            if hit_list:
                for i in hit_list:
                    i.kill()
                    self.bar.hp -= 1
            self.end()

    def on_key_release(self, symbol, modifiers):
        if symbol == key.LEFT and self.rocket.center_x >= W//2:
            self.rocket.center_x -= 200
        elif symbol == key.RIGHT and self.rocket.center_x <= W//2:
            self.rocket.center_x += 200

    def create_asteroids(self):
        num = 2
        x = random.sample([100, 300, 500], 2)
        for i in range(num):
            asteroid = Asteroid("小行星.png")
            asteroid.center_x = x[i]
            self.asteroids.append(asteroid)

    def end(self):
        if self.bar.hp <= 0:
            self.game = False


if __name__ == "__main__":
    game = draw(W, H, T)
    run()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页