加入收藏 | 设为首页 | 会员中心 | 我要投稿 温州站长网 (https://www.0577zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长资讯 > 评论 > 正文

500行代码,教你用Python写个微信飞机大战

发布时间:2019-10-22 11:24:46 所属栏目:评论 来源:上海小胖
导读:副标题#e# 【大咖·来了 第7期】10月24日晚8点观看《智能导购对话机器人实践》 这几天在重温微信小游戏的飞机大战,玩着玩着就在思考人生了,这飞机大战怎么就可以做的那么好,操作简单,简单上手。 帮助蹲厕族、YP族、饭圈女孩在无聊之余可以有一样东西让

实现子弹类,完成子弹的主要操作

  1. # 飞机子弹类 
  2. class Bullet(): 
  3.     def __init__(self, image_path=os.path.join(source_dir,'bullet.png'), background_size=(480, 700), plan=None, speed=1000): 
  4.         ''' 
  5.         :param image_path: 子弹的图片地址 
  6.         :param background_size: 游戏窗口大小 
  7.         :param plan: 飞机对象 
  8.         :param speed: 子弹飞行速度 
  9.         ''' 
  10.         self.image = pygame.image.load(image_path).convert_alpha() 
  11.         self.background_size = background_size 
  12.         self.speed = background_size[1] / speed 
  13.         # 子弹是否击中敌机 
  14.         self.destroyed = False 
  15.         self.position = self._get_position(plan) 
  16.  
  17.     def _get_position(self, plan): 
  18.         ''' 
  19.         根据plan得到子弹发出位置 
  20.         :param plan: 飞机对象 
  21.         ''' 
  22.         bullet_size = self.image.get_size() 
  23.         plan_width = plan.image_size[0] 
  24.         x = (plan_width-bullet_size[0]) / 2 
  25.         return [plan.position[0] + x, plan.position[1]] 
  26.  
  27.     def update(self, time_passed): 
  28.         ''' 
  29.         改变子弹位置 
  30.         :param time_passed: 距离上次绘制图像到现在的时间 
  31.         ''' 
  32.         # 如果子弹超出屏幕或者击中敌机,就设置self.position[1]为-100,在plan.draw的时候就移除它 
  33.         if self.position[1] + self.image.get_size()[1] <= 0 or self.destroyed: 
  34.             self.position[1] = -100 
  35.             return 
  36.  
  37.         # 改变的距离 = 时间 * 速率 
  38.         self.position[1] -= time_passed * self.speed 

500行代码,教你用Python写个微信飞机大战

这样,我们就把所有的操作都实现完了,接下来只需要使用 Game().run(),就可以运行我们的游戏了。

(编辑:温州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读