猫史档案馆


【Python作品分享】【36强进10】向航天人致敬——给航天人的一张明信片【作品秀】

用户:活泼的重金怪gdte活泼的重金怪gdte查看:0 回复:0 评论:0 创建时间:2021-05-23T23:03:47


【作品展示】

center_image

 

【作品介绍】

这是我制作的一个模拟火箭发射全过程的作品,依次点击按钮,操纵火箭运送卫星到地球同步轨道,并向航天人致敬。

 

【作品源代码】

from arcade import *
import arcade
import random

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

SCREEN_TITLE = "向航天人致敬——给航天人的一张明信片"


class MyDraw(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        self.setup()

    def setup(self):
        # 背景颜色
        set_background_color(color.BLACK)
        self.x = 400
        self.y = 300
        self.total_time = 0
        self.last_time = 0
        self.flame_flag = True
        self.posx, self.posy = 0, 0
        self.splita = True
        self.splitb = True
        self.splitc = True
        self.splitd = True
        self.splite = True
        self.start_flag = True
        self.pos_x = SCREEN_WIDTH//2
        self.pos_y = SCREEN_HEIGHT//2+150
        self.pos1 = 1
        self.pos2 = 1
        self.senta = False
        self.recivea = False

    def on_draw(self):
        start_render()
        self.button6()
        if 340 < self.posx < 458 and 271 < self.posy < 331 and self.start_flag:
            self.start_flag = False
        if not self.start_flag:
            draw_rectangle_filled(
                SCREEN_WIDTH//2, SCREEN_HEIGHT//2, 120, 60, color.BLACK)
            self.create_dot()
            self.rocket()
            schedule(self.change_flame, 0.1)
            self.button1()
            if 658 < self.posx < 779 and 500 < self.posy < 560 or not self.splita:
                self.split1()
            if 660 < self.posx < 779 and 422 < self.posy < 481 or not self.splitb:
                self.split2()
            if 660 < self.posx < 779 and 342 < self.posy < 399 or not self.splitc:
                self.split3()
                if self.pos1 < 52 and self.senta:
                    schedule(self.sent1, 1)
                    self.drawdot1()
            if 660 < self.posx < 779 and 260 < self.posy < 320 or not self.splitd:
                self.recive()
                if self.pos2 < 52 and self.recivea:
                    schedule(self.sent2, 1)
                    self.drawdot2()
            if 660 < self.posx < 779 and 33 < self.posy < 92 or not self.splite:
                self.data()

    def on_update(self, delta_time):
        if not self.start_flag:
            if int(self.last_time) != int(self.total_time) and int(self.total_time) % 2 == 0:
                self.create_dot()
                self.last_time = self.total_time

    def rocket(self):
        draw_rectangle_filled(self.x, self.y, 50, 230, color.RED)
        draw_rectangle_filled(self.x-50, self.y-50, 45, 130, color.RED)
        draw_rectangle_filled(self.x+51, self.y-50, 45, 130, color.RED)
        draw_circle_filled(self.x+50, self.y+13, 22, color.RED)
        draw_circle_filled(self.x-50, self.y+13, 22, color.RED)
        draw_rectangle_filled(self.x, self.y+145, 50, 60, color.BLUE)
        draw_circle_filled(self.x, self.y+175, 25, color.BLUE)
        draw_circle_filled(self.x, self.y+90, 15, color.SKY_BLUE)
        draw_circle_filled(self.x, self.y+50, 15, color.SKY_BLUE)
        draw_text("中\n国\n航\n天", self.x-13, self.y-83, color.GOLD,
                  font_name=("simhei", "PingFang"), font_size=22)
        if self.flame_flag:
            draw_triangle_filled(self.x-28, self.y-115, self.x-72,
                                 self.y-115, self.x-50, self.y-150, color.GOLD)
            draw_triangle_filled(self.x+25, self.y-115, self.x+72,
                                 self.y-115, self.x+50, self.y-150, color.GOLD)
            draw_triangle_filled(self.x-28, self.y-115, self.x+25,
                                 self.y-115, self.x, self.y-170, color.GOLD)
        else:
            draw_triangle_filled(self.x-28, self.y-115, self.x-72,
                                 self.y-115, self.x-50, self.y-170, color.GOLD)
            draw_triangle_filled(self.x+25, self.y-115, self.x+72,
                                 self.y-115, self.x+50, self.y-170, color.GOLD)
            draw_triangle_filled(self.x-28, self.y-115, self.x+25,
                                 self.y-115, self.x, self.y-190, color.GOLD)

    def button1(self):
        draw_rectangle_filled(720, 530, 120, 60, color.RED)
        draw_text("一二级分离", 665, 520, color.BLUE,
                  font_name=("simhei", "PingFang"), font_size=18)

    def button2(self):
        draw_rectangle_filled(720, 530-80, 120, 60, color.RED)
        draw_text("星箭分离", 665+15, 520-80, color.BLUE,
                  font_name=("simhei", "PingFang"), font_size=18)

    def button3(self):
        draw_rectangle_filled(720, 530-80-80, 120, 60, color.RED)
        draw_text("抛弃整流罩", 665+2, 520-80-80, color.BLUE,
                  font_name=("simhei", "PingFang"), font_size=18)

    def button4(self):
        draw_rectangle_filled(720, 530-80*3, 120, 60, color.RED)
        draw_text("接收指令", 665+15, 520-80*3, color.BLUE,
                  font_name=("simhei", "PingFang"), font_size=18)

    def button5(self):
        draw_rectangle_filled(720, 530-80*6+10, 120, 60, color.YELLOW)
        draw_text("向航天人\n  致敬", 665+5+5, 510-80*6+10, color.RED,
                  font_name=("simhei", "PingFang"), font_size=18)

    def button6(self):
        draw_rectangle_filled(
            SCREEN_WIDTH//2, SCREEN_HEIGHT//2, 120, 60, color.YELLOW)
        draw_text("点火发射", SCREEN_WIDTH//2-45, SCREEN_HEIGHT//2-10, color.RED,
                  font_name=("simhei", "PingFang"), font_size=18)

    def change_flame(self, time):
        self.flame_flag = not self.flame_flag

    def create_dot(self):
        for i in range(25):
            sizes = [3, 4, 5, 6, 7]
            x = random.randint(0, 800)
            y = random.randint(0, 600)
            size = random.choice(sizes)
            draw_circle_filled(x, y, size, color.GOLD)

    def on_mouse_release(self, x, y, button, modifiers):
        self.posx, self.posy = x, y

    def change(self):
        if self.y-115+140+90 > SCREEN_HEIGHT//2:
            self.y -= 13

    def manmadestar(self):
        draw_rectangle_filled(self.pos_x, self.pos_y, 160, 70, color.GRAY)
        draw_circle_filled(self.pos_x-80, self.pos_y, 35, color.GRAY)
        draw_circle_filled(self.pos_x+80, self.pos_y, 35, color.GRAY)
        draw_rectangle_filled(self.pos_x, self.pos_y+65, 5, 60, color.YELLOW)
        draw_rectangle_filled(self.pos_x, self.pos_y-65, 5, 60, color.YELLOW)
        draw_rectangle_filled(self.pos_x, self.pos_y +
                              55+50, 60, 100, color.COBALT)
        draw_rectangle_filled(self.pos_x, self.pos_y -
                              55-50, 60, 100, color.COBALT)
        draw_rectangle_outline(self.pos_x, self.pos_y +
                               55+50, 60, 100, color.SKY_BLUE)
        draw_rectangle_outline(self.pos_x, self.pos_y -
                               55-50, 60, 100, color.SKY_BLUE)
        for i in range(4):
            num = i+1
            draw_line(self.pos_x-30, self.pos_y+55+20*num,
                      self.pos_x+30, self.pos_y+55+20*num, color.SKY_BLUE)
        for i in range(4):
            num = i+1
            draw_line(self.pos_x-30, self.pos_y-55-20*num,
                      self.pos_x+30, self.pos_y-55-20*num, color.SKY_BLUE)
        for i in range(2):
            num = i+1
            draw_line(self.pos_x-30+20*num, self.pos_y-155,
                      self.pos_x-30+20*num, self.pos_y-55, color.SKY_BLUE)
        for i in range(2):
            num = i+1
            draw_line(self.pos_x-30+20*num, self.pos_y+155,
                      self.pos_x-30+20*num, self.pos_y+55, color.SKY_BLUE)
        draw_text("中国航天", self.pos_x-110+55, self.pos_y-11, color.RED,
                  font_name=("simhei", "PingFang"), font_size=22)

    def earth(self):
        draw_circle_filled(400, -804, 900, color.BLUE)
        draw_rectangle_filled(self.pos_x, self.pos_y -
                              290-130, 15, 15, color.GRAY)

    def split1(self):
        draw_rectangle_filled(self.x, self.y-45, 50, 140, color.BLACK)
        draw_rectangle_filled(self.x-50, self.y-50, 45, 130, color.BLACK)
        draw_rectangle_filled(self.x+51, self.y-50, 45, 130, color.BLACK)
        draw_circle_filled(self.x+50, self.y+13, 22, color.BLACK)
        draw_circle_filled(self.x-50, self.y+13, 22, color.BLACK)
        if self.flame_flag:
            draw_triangle_filled(self.x-28, self.y-115, self.x-72,
                                 self.y-115, self.x-50, self.y-150, color.BLACK)
            draw_triangle_filled(self.x+25, self.y-115, self.x+72,
                                 self.y-115, self.x+50, self.y-150, color.BLACK)
            draw_triangle_filled(self.x-28, self.y-115, self.x+25,
                                 self.y-115, self.x, self.y-170, color.BLACK)
        else:
            draw_triangle_filled(self.x-28, self.y-115, self.x-72,
                                 self.y-115, self.x-50, self.y-170, color.BLACK)
            draw_triangle_filled(self.x+25, self.y-115, self.x+72,
                                 self.y-115, self.x+50, self.y-170, color.BLACK)
            draw_triangle_filled(self.x-28, self.y-115, self.x+25,
                                 self.y-115, self.x, self.y-190, color.BLACK)
        if self.flame_flag:
            draw_triangle_filled(self.x-28, self.y-115+140, self.x+25,
                                 self.y-115+140, self.x, self.y-130, color.GOLD)
        else:
            draw_triangle_filled(self.x-28, self.y-115+140, self.x+25,
                                 self.y-115+140, self.x, self.y-150, color.GOLD)
        self.splita = False
        self.button2()

    def split2(self):
        draw_rectangle_filled(self.x, self.y-45+115, 50, 90, color.BLACK)
        if self.flame_flag:
            draw_triangle_filled(self.x-28, self.y-115+140, self.x+25,
                                 self.y-115+140, self.x, self.y-130, color.BLACK)
        else:
            draw_triangle_filled(self.x-28, self.y-115+140, self.x+25,
                                 self.y-115+140, self.x, self.y-150, color.BLACK)
        if self.flame_flag:
            draw_triangle_filled(self.x-29, self.y-115+140+90, self.x+25,
                                 self.y-115+140+90, self.x, self.y+60, color.SKY_BLUE)
            draw_triangle_filled(self.x-29, self.y-115+140+90, self.x+25,
                                 self.y-115+140+90, self.x, self.y+90, color.GOLD)
        else:
            draw_triangle_filled(self.x-27, self.y-115+140+90, self.x+25,
                                 self.y-115+140+90, self.x, self.y+40, color.SKY_BLUE)
            draw_triangle_filled(self.x-27, self.y-115+140+90, self.x+25,
                                 self.y-115+140+90, self.x, self.y+70, color.GOLD)
        self.change()
        self.splitb = False
        self.earth()
        self.button3()

    def split3(self):
        if self.flame_flag:
            draw_triangle_filled(self.x-30, SCREEN_HEIGHT//2, self.x+27,
                                 SCREEN_HEIGHT//2, self.x, self.y+60, color.BLACK)
            draw_triangle_filled(self.x-30, SCREEN_HEIGHT//2, self.x+27,
                                 SCREEN_HEIGHT//2, self.x, self.y+90, color.BLACK)
        else:
            draw_triangle_filled(self.x-30, SCREEN_HEIGHT//2, self.x+27,
                                 SCREEN_HEIGHT//2, self.x, self.y+40, color.BLACK)
            draw_triangle_filled(self.x-30, SCREEN_HEIGHT//2, self.x+27,
                                 SCREEN_HEIGHT//2, self.x, self.y+70, color.BLACK)
        draw_rectangle_filled(
            SCREEN_WIDTH//2, SCREEN_HEIGHT//2+45, 50, 90, color.BLACK)
        self.splitc = False
        self.manmadestar()
        self.line()
        self.button4()
        self.senta = True

    def sent1(self, time):
        self.pos1 += 1

    def sent2(self, time):
        self.pos2 += 1

    def recive(self):
        self.splitd = False
        self.recivea = True
        draw_rectangle_filled(self.pos_x, self.pos_y -
                              165-15*8-3, 10, 15*17-3, color.GREEN)
        self.button5()

    def data(self):
        self.splite = False
        draw_text('''国之大器,国运千钧!\n\n干惊天动地的事,做隐姓埋名的人!\n\n向航天人致敬!''',
                  20-2, 50, color.RED,
                  font_name=("simhei", "PingFang"), font_size=40)

    def drawdot1(self):
        num = self.pos1
        draw_circle_filled(self.pos_x, self.pos_y-165-5*num, 5, color.RED)

    def drawdot2(self):
        num = self.pos2
        draw_circle_filled(self.pos_x, self.pos_y-165-5*52+5*num, 5, color.RED)

    def line(self):
        draw_rectangle_filled(self.pos_x, self.pos_y -
                              165-15*8-3, 10, 15*17-3, color.YELLOW)

if __name__ == "__main__":
    game = MyDraw(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
    run()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页