郦珠雨 发表于 2025-8-12 14:05:59

pygame小游戏飞机大战_6敌人开火

引入敌人子弹:
算出敌人子弹出射点并在敌人出射点绘制子弹
# 引入敌人子弹的类
class EnemyBullet(object):
    def __init__(self, screen, x, y):
      # 定义坐标
      self.x = x + 29 - 2
      self.y = y + 43
      # 引入子弹图片
      self.image = pygame.image.load("./images/bullet2.png")
      # 显示窗口
      self.screen = screen
      # 定义速度
      self.speed = 4

    # 定义显示子弹的方法
    def display(self):
      # 显示子弹到窗口
      self.screen.blit(self.image, (self.x, self.y))

    # 定义移动子弹的方法
    def auto_move(self):
      # 修改子弹y坐标使子弹飞起来
      self.y += self.speed在敌人类中引入子弹存入列表中并进行循环实现发射:
 
# 引入敌方飞机的类class Enemy(object):    # 创建玩家的方法,引入screen模块    def __init__(self, screen):      # 引入图片创建敌人      self.enemy = pygame.image.load("./images/enemy1.png")# 51*43      # 设置速度变量      self.speed = 5      self.x = 0      self.y = 0      # 使screen变为飞机类的一个属性      self.screen = screen      # 定义装子弹的列表      self.bullets = []    def display(self):      # 绘制敌人      self.screen.blit(self.enemy, (self.x, self.y))      # 遍历所有子弹      for bullet in self.bullets:            # 引入让子弹飞起来的方法            bullet.auto_move()            # 显示子弹            bullet.display()    # 定义移动飞机的方法    def auto_move(self):      # 将direction改为全局变量      global direction      # 判断方向进行移动      if direction == "right":            self.x += self.speed      if direction == "left":            self.x -= self.speed      # 定义碰壁时调整移动方向      if self.x >= 480 - 51:            direction = "left"      if self.x = 480 - 51:            direction = "left"      if self.x = 480 - 51:            direction = "left"      if self.x

顶豌 发表于 2025-11-22 12:01:58

分享、互助 让互联网精神温暖你我

玛凶 发表于 2025-11-30 07:16:31

感谢分享,学习下。

崔竹 发表于 2025-12-27 23:57:40

感谢分享,下载保存了,貌似很强大

零幸 发表于 2026-1-18 12:55:24

这个好,看起来很实用

靳谷雪 发表于 2026-1-18 21:07:18

东西不错很实用谢谢分享

龙梨丝 发表于 2026-1-19 01:20:56

感谢分享

院儿饯 发表于 2026-1-19 01:44:42

不错,里面软件多更新就更好了

仁夹篇 发表于 2026-1-21 04:34:16

不错,里面软件多更新就更好了

锄淫鲷 发表于 2026-1-21 23:30:13

分享、互助 让互联网精神温暖你我

梁丘艷蕙 发表于 2026-1-22 11:19:28

yyds。多谢分享

茹静曼 发表于 2026-1-23 15:02:52

谢谢楼主提供!

强怀梅 发表于 2026-1-27 08:24:20

东西不错很实用谢谢分享

沃盼盼 发表于 2026-1-28 08:08:45

懂技术并乐意极积无私分享的人越来越少。珍惜

决台 发表于 2026-2-3 09:27:01

谢谢分享,辛苦了

丁若云 发表于 2026-2-5 05:46:49

感谢,下载保存了

志灿隐 发表于 2026-2-7 09:29:13

收藏一下   不知道什么时候能用到

晁红叶 发表于 2026-2-8 16:56:53

感谢分享

骆贵 发表于 2026-2-8 19:47:49

谢谢分享,辛苦了

府扔影 发表于 2026-2-9 15:50:38

感谢分享
页: [1] 2
查看完整版本: pygame小游戏飞机大战_6敌人开火