接上篇文章继续解绍arcade游戏编程的基本知识。精灵调用图片纹理的两种类的实现,一种直接引用图片文件,一种利用arcade.load_texture的过度,直接给精灵赋值。如上次写的,游戏升级时,通过程序换精灵的颜色,用后种方法定义的类较好。但上次修改的类,还有点不能直接用PIL的image类直接给值,今天重新修改之。
result = Texture(
cache_name,
image=image,
hit_box_algorithm=hit_box_algorithm,
hit_box_detail=hit_box_detail,
)
单独给图片,会存在两点,hit_box_algorithm,hit_box_detail却值,也就是上次文章提到的出错处。所以要加入这两个参数
特别注意点: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

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])
可关注博主后,私聊博主免费获取
需要技术指导,育娃新思考,企业软件合作等更多服务请联系博主
今天是以此模板持续更新此育儿专栏的第 37/50次。
可以关注我,点赞我、评论我、收藏我啦。