• 【无标题】


    前言

    接上篇文章继续解绍arcade游戏编程的基本知识。精灵调用图片纹理的两种类的实现,一种直接引用图片文件,一种利用arcade.load_texture的过度,直接给精灵赋值。如上次写的,游戏升级时,通过程序换精灵的颜色,用后种方法定义的类较好。但上次修改的类,还有点不能直接用PIL的image类直接给值,今天重新修改之。

    1、精灵类直接用内存图片给值

    1.1 通过查看texture类中的定义
                result = Texture(
                    cache_name,
                    image=image,
                    hit_box_algorithm=hit_box_algorithm,
                    hit_box_detail=hit_box_detail,
                )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    单独给图片,会存在两点,hit_box_algorithm,hit_box_detail却值,也就是上次文章提到的出错处。所以要加入这两个参数

    1.2 修改后的类

    特别注意点:cache_name不能是定值,否则后面不能随时调用图片

    # 不调用文件,直接给texture给值IMG类型
    class Enemy_tank_picimg(arcade.Sprite):
        def __init__(self, texturepic, scale, x, y, speed_to_player=0.2, hit_box_algorithm: Optional[str] = "Simple",):
            super().__init__(scale=scale)
            self.texture = arcade.Texture(
                f"tempimage{str(x)}",  # 此处name很关健,如填定值,图片不会换
                image=texturepic,
                hit_box_algorithm=hit_box_algorithm,
                hit_box_detail=4.5
            )
            self.center_x = x
            self.center_y = y
            self.word = 'book'
            self.hz = '书'
            self.life = 1  # 生命条数,即挨几颗子弹消失
            self.speed_to_player = speed_to_player  # 面向角色移动的速度
    
        def draw_word(self, x, y, fcolor=arcade.csscolor.WHITE_SMOKE, fsize=18, text=None):
            xs=fsize
            if text:
                arcade.draw_rectangle_filled(x+len(self.word)*xs//2-10,y+5,len(text)*xs,30,(128,138,135))
                arcade.draw_text(text, x, y, fcolor, fsize)
    
            else:
                arcade.draw_rectangle_filled(x+len(self.word)*xs//2-10,y+5,len(self.word)*xs, 30,(128,138,135))
                arcade.draw_text(self.word, x, y, fcolor, fsize)
    
        def update(self):
            super().update()
            if self.speed_to_player:
                # 敌人向角色移动变量
                self.change_x = math.cos(self.angle) * 0.2
                self.change_y = math.sin(self.angle) * 0.2
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    1.3 效果图

    在这里插入图片描述

    1.4 调用代码实现
                j=0
    
                # 转换文件来不及调用,时间问题??????文件名的问题得以解决
                tempcolorh = random.random()
                tempic = change_color_tmpic(Image.open("images/坦克2.png"), tempcolorh)
                # tempic.show()
                # tempfile = 'images/temp.png'
                # tempic.save(tempfile)
                # texture = arcade.load_texture(tempfile)
                for x in xsword:
                    tank = Enemy_tank_picimg(tempic, 0.5, posx[j], random_y)
                    tank.word = x
                    tank.hz = xsword[x]
                    self.scene.add_sprite(LAYER_tanks, tank)
                    j +=1
                    tank.update()
    
                # 去掉已显示单词
                self.word_keys.remove(self.word_keys[0])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    源码获取

    关注博主后,私聊博主免费获取
    需要技术指导,育娃新思考,企业软件合作等更多服务请联系博主

    今天是以此模板持续更新此育儿专栏的第 37/50次。
    可以关注我,点赞我、评论我、收藏我啦。

  • 相关阅读:
    git知识点
    浏览器不能访问阿里云ECS
    蓝桥杯每日一题2023.11.9
    Python控制程控电源
    前端传递bool型后端用int收不到
    多线程多进程处理服务器并发(多进程处理如何解决僵死进程)
    DSPE-PEG-NYZL1,NYZL1-PEG-DSPE,磷脂-聚乙二醇-靶向肽NYZL1,小分子靶向肽
    20221106 今天的世界发生了什么
    【JS高级】js之函数、重载、匿名函数、作用域及作用域链_03
    依靠双工福禄克测试仪进行MPO电缆认证
  • 原文地址:https://blog.csdn.net/fqfq123456/article/details/128090724