小学生python游戏编程arcade----敌人精灵上方显示方框及子弹显示问题
- 前言
- 1、敌人精灵上方显示方框
- 1.1 修改enemy_tank类
- 1.2 引用
- 1.3 效果图
- 2、调整方法
- 2.1 类方法
- 2.2 类的引用
- 2.3 效果图
- 2.4 大小位置调整后
- 3、子弹过线自动消失
- 3.1 子弹的更新中
- 3.2 原因查到,把以下代码调为如下
- 3.3 效果图
- 3.3 重要提示
- 源码获取
前言
接上篇文章继续解绍arcade游戏编程的基本知识。敌人精灵上方显示方框及子弹显示问题
1、敌人精灵上方显示方框
1.1 修改enemy_tank类
def draw_word(self, x, y, owner,fcolor=arcade.csscolor.GREEN, fsize=18, text=None):
color1 = (128, 138, 135)
# 参数:距形中心位置,宽,高,颜色,线宽
rect = arcade.create_rectangle(x, y, 200, 60, color1)
owner.append(rect)
if text:
arcade.draw_text(text, x, y, fcolor, fsize)
else:
arcade.draw_text(self.word, x, y, fcolor, fsize)
1.2 引用
self.shapes.draw()
for aa in self.scene[LAYER_tanks]:
aa.draw_word(aa.left, aa.top + 20, self.shapes)
1.3 效果图
2、调整方法
2.1 类方法
def draw_word(self, x, y, fcolor=arcade.csscolor.GREEN, 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)
2.2 类的引用
for aa in self.scene[LAYER_tanks]:
aa.draw_word(aa.left, aa.top + 20)
2.3 效果图
2.4 大小位置调整后
3、子弹过线自动消失
3.1 子弹的更新中
# 如过子弹超过屏幕则删掉.
if (bullet.right < 0) or (bullet.bottom > SCREEN_height) or (
bullet.left > (self.tile_map.width * self.tile_map.tile_width) * TILE_Scaling) :
bullet.remove_from_sprite_lists()
3.2 原因查到,把以下代码调为如下
# 如过子弹超过地图则删掉.
if (bullet.right < 0) or (bullet.bottom > self.top_of_map) or (
bullet.left > self.end_of_map):
bullet.remove_from_sprite_lists()
3.3 效果图
3.3 重要提示
所有物品的坐标位置与相机移动无关
定义个相机左及下边距,鼠标点击时的位置需与此两值相加
# 定义一个相机边框的左边距离
left_boundary = self.view_left + (self.camera.viewport_width / 2)
if self.wanjia.center_x < left_boundary:
self.view_left -= left_boundary - self.wanjia.center_x
else:
self.view_left += self.wanjia.center_x - left_boundary
top_boundary = self.view_bottom + (self.camera.viewport_height / 2)
if self.wanjia.center_y < top_boundary:
self.view_bottom -= top_boundary - self.wanjia.center_y
else:
self.view_bottom += self.wanjia.center_y - top_boundary
源码获取
可关注博主后,私聊博主免费获取
需要技术指导,育娃新思考,企业软件合作等更多服务请联系博主
今天是以此模板持续更新此育儿专栏的第 30/50次。
可以关注我,点赞我、评论我、收藏我啦。