猫史档案馆


【Python作品分享】我的小车—动态【作品秀】

用户:真诚的呆鲤鱼UhQu真诚的呆鲤鱼UhQu查看:0 回复:2 评论:0 创建时间:2021-02-08T14:12:37


【作品展示】

center_image

 

【作品介绍】

小车可以动起来哦!

这是背景

 

【作品源代码】

from arcade import *

#设置窗体宽和高
A = 800
AB = 600
#设置标题
ABC = '我的小车'

class MyDraw(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        #窗体初始化
        self.setup()

    def setup(self):
        #设置背景颜色
        set_background_color(color.WHITE)
        self.bx = 400
        self.by = 150
        #设置背景
        self.bg_list = ('images/road.png', 'images/喵.png')
        self.current_scene = 0
        self.change_bg()
        #初始气球
        self.balloons = []

    def change_bg(self):
        self.bg = Sprite(self.bg_list[self.current_scene])
        self.bg.center_x = 400
        self.bg.center_y = 300

    def on_draw(self):
        start_render()
        self.bg.draw()
        self.mydraw()
        #绘制气球
        for balloon in self.balloons:
            balloon.draw()

    def on_update(self, delta_time):
        self.bx += 2
        #改变气球的坐标
        for balloon in self.balloons:
            balloon.center_y += 5
        #判断小车是否移动到屏幕最右侧,切换为广场
        if self.bx > A and self.current_scene == 0:
            self.bx = 100
            self.current_scene = 1
            self.change_bg()
            #生成气球
            self.create_balloon()
         #判断小车是否移动到屏幕最右侧,切换为道路
        if self.bx > A and self.current_scene == 1:
            self.bx = 100
            self.current_scene = 0
            self.change_bg()
            #清除气球
            self.balloons = []

    def mydraw(self):
        #车棚
        draw_rectangle_filled(self.bx, self.by + 70, 100, 80, color.RED)
        #车窗
        draw_rectangle_filled(self.bx, self.by + 70, 40, 40, color.GRAY)
        #车身
        draw_rectangle_filled(self.bx, self.by, 200, 60, color.RED)
        #左轮
        draw_circle_filled(self.bx - 50, self.by - 30, 30, color.BLACK)
        #右轮
        draw_circle_filled(self.bx + 50, self.by - 30, 30, color.BLACK)

    def create_balloon(self):
        balloon_list = ('images/balloon01.png','images/balloon02.png', 'images/balloon03.png', 'images/balloon04.png', 'images/balloon05.png')
        num = 50
        for i in range(num):
            balloon = Sprite(random.choice(balloon_list))
            balloon.center_x = random.randint(0, A)
            balloon.center_y = -random.randint(300, 600)
            self.balloons.append(balloon)

if __name__ == '__main__':
    game = MyDraw(A, AB, ABC)
    run()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
禁止挖坟后果自负禁止挖坟后果自负

妈呀,我也做这个

点赞0


评论


BZIClawLYNEYBZIClawLYNEY

好简单呀

点赞0


评论