猫史档案馆


【Python作品分享】拦截敌机【作品秀】

用户:回家会尽快回家会尽快查看:0 回复:0 评论:0 创建时间:2021-09-19T10:40:21


【作品展示】

center_image

 

【作品介绍】

有一个作品

 

【作品源代码】

# 导入库中大部分功能
from arcade import *
# 导入随机库
import random
# 继承Window类


class MyWindow(Window):
    # 定义设置的方法(自己定义的,记得调用)
    def setup(self):
        # 加载精灵
        self.bg = Sprite('bg.png')
        # 位置
        self.bg.position = 400, 300
        # 储存敌机的列表-arcade列表的创建方式(类)
        self.plane_list = SpriteList()
        # 定时生成敌机
        schedule(self.create_plane, 2)

    # 定义生成敌机的函数(定时调用此函数,一定要有一个参数time)
    def create_plane(self, time):
        # 循环生成多个敌机
        num = random.randint(1, 3)
        for i in range(num):
            # 加载精灵
            self.plane = Sprite('plane.png', scale=0.7)
            # 位置
            # 位置随机在上方生成敌机
            self.plane.center_x = random.randint(0, 800)
            self.plane.center_y = random.randint(500, 600)
            # 速度随机,change_y<0,-5<-2(从小到大)写随机范围
            self.plane.change_y = random.randint(-5, -2)
            # 添加到列表
            self.plane_list.append(self.plane)

    # 重写鼠标释放的方法
    def on_mouse_release(self, x, y, button, modifiers):
        # 保存击中(存在于plane_list)中的精灵
        hit_list = get_sprites_at_point((x, y), self.plane_list)
        # 删除击中且存在于plane-list的敌机
        # 遍历hit_list
        for hit in hit_list:
            # 删除存在于精灵列表中的对应的这个精灵
            self.plane_list.remove(hit)

    # 重写绘制的方法
    def on_draw(self):
        # 渲染
        start_render()
        # 绘制精灵
        self.bg.draw()
        # 绘制整个列表
        self.plane_list.draw()

    # 重写更新的方法
    def on_update(self, time):
        # 更新敌机
        self.plane_list.update()


# 实例
w = MyWindow()
# 调用
w.setup()
# 运行
run()

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页