定义子弹的类
与定义敌人的类类似,通过调整位置是子弹出现在玩家正上方- # 新增 定义子弹的类,其中包括图片(self.img)初始位置( self.x,self.y )运动速度(self.step)
- class Bullet:
- def __init__(self):
- self.img = pygame.image.load('bullet.png')
- # 新增 将子弹放在玩家上方
- self.x = playerX+48
- self.y = playerY+5
- # 新增 定义变量表示子弹运动速度。
- self.step = 1
- # 新增 保存现有的子弹
- bullets = []
复制代码 定义子弹函数
子弹以一定速度向上运动,当出界时删除子弹
- # 新增 定义显示子弹的函数
- def show_bullet():
- # 新增 进行循环使用Bulle类进行敌人的创建
- for b in bullets:
- # 新增 使子弹出现在(b.x,b.y)的位置
- screen.blit(b.img, (b.x, b.y))
- # 新增 使子弹向上移动
- b.y -= b.step
- # 新增 判断子弹是否出界,是则移除
- if b.y < 0 :
- bullets.remove(b)
复制代码 定义空格键发射子弹- # 新增 判断按下空格键
- elif event.key ==pygame.K_SPACE :
- # 新增 创建一颗子弹
- bullets.append(Bullet())
复制代码 完整代码- # 引用 pygameimport pygame# 引用随机模块import random# 使程序初始化pygame.init()# 设置游戏窗口大小screen = pygame.display.set_mode((480, 700))# 设置标题pygame.display.set_caption('打飞机')# 设置游戏图标icon = pygame.image.load('ufo.png')pygame.display.set_icon(icon)# 引入背景图片bgImg = pygame.image.load('background.png')# 引入玩家图片playerImg = pygame.image.load('player.png')# 定义玩家初始位置playerX = 225playerY = 450# 定义变量表示玩家运动速度playerStep = 0# 变量表示敌人数量number_of_enemies = 6# 定义敌人的类,其中包括图片(self.img)初始位置( self.x,self.y )运动速度(self.step)class Enemy: def __init__(self): self.img = pygame.image.load('enemy.png') # 通过 random 函数进行随机位置坐标生成 self.x = random.randint(100, 380) self.y = random.randint(30, 70) # 定义变量表示敌人运动速度。注意:将速度改为随机数时由于为浮点型用.uniform函数 self.step = random.uniform(0.05, 0.3)# 新增 定义子弹的类,其中包括图片(self.img)初始位置( self.x,self.y )运动速度(self.step)
- class Bullet:
- def __init__(self):
- self.img = pygame.image.load('bullet.png')
- # 新增 将子弹放在玩家上方
- self.x = playerX+48
- self.y = playerY+5
- # 新增 定义变量表示子弹运动速度。
- self.step = 1
- # 新增 保存现有的子弹
- bullets = []# 创建列表进行引入number_of_enemies个敌人enemies = []for i in range(number_of_enemies): # 每进行一次循环添加一个Enemy到列表中 enemies.append(Enemy())# 定义显示敌人的函数def show_enemy(): # 进行循环使用Enemy类进行敌人的创建 for e in enemies: # 使敌人出现在(enemyX, enemyY)的位置 screen.blit(e.img, (e.x, e.y)) # 使敌人飞机左右移动 e.x += e.step # 控制敌人移动边界,当敌人碰到左右边界时反弹,当敌人运动到上下边界时停止 if e.x > 378: e.step *= -1 # 当碰到左右边界时下沉 e.y += 20 if e.x < 0: e.step *= -1 e.y += 20 # 控制边界 if e.y > 572: e.y = 572 if e.y < 0: e.y = 0# 新增 定义显示子弹的函数
- def show_bullet():
- # 新增 进行循环使用Bulle类进行敌人的创建
- for b in bullets:
- # 新增 使子弹出现在(b.x,b.y)的位置
- screen.blit(b.img, (b.x, b.y))
- # 新增 使子弹向上移动
- b.y -= b.step
- # 新增 判断子弹是否出界,是则移除
- if b.y < 0 :
- bullets.remove(b)running = True# 进行循环 游戏主循环while running: # 绘制背景 screen.blit(bgImg, (0, 0)) # 绘制玩家 screen.blit(playerImg, (playerX, playerY)) # 调用显示敌人函数 show_enemy() # 新增 显示子弹 show_bullet() # 获取游戏事件队列中的所有事件(涉及到玩家的各种交互,如鼠标点击、键盘操作、窗口事件等) for event in pygame.event.get(): # 如果事件是QUIT事件,如点击窗口的关闭按钮,则退出循环 if event.type == pygame.QUIT: # 退出循环 running = False # KEYDOWN 判断键盘是否按下 if event.type == pygame.KEYDOWN: # 判断按下左右键进行移动赋值 if event.key == pygame.K_RIGHT: playerStep = 0.5 elif event.key == pygame.K_LEFT: playerStep = -0.5 # 新增 判断按下空格键 elif event.key == pygame.K_SPACE: # 新增 创建一颗子弹 bullets.append(Bullet()) # KEYUP 判断键盘是否抬起 if event.type == pygame.KEYUP: # 抬起键盘时将移动距离改为 0 playerStep = 0 # 玩家左右移动 playerX += playerStep # 控制玩家移动边界 if playerX > 378: playerX = 378 if playerX < 0: playerX = 0 if playerY > 572: playerY = 572 if playerY < 0: playerY = 0 # 界面更新 pygame.display.update()
复制代码 运行效果如下
左右方向键进行移动,空格键发射子弹
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |