用pygame实现打飞机游戏-5-发射子弹

时间:2022-09-01 15:40:09
  1 #coding=utf-8
  2 import pygame
  3 #导入pygame模块
  4 from pygame.locals import *
  5 #导入检测键盘的子模块
  6 class AircraftCoordinate(object):
  7     def __init__(self,screen):
  8         #设置飞机默认的位置
  9         self.x = 250
 10         self.y = 600
 11         self.screen =screen
 12         #设置要显示的内容的窗口
 13         self.imagePath = "./feiji/hero.gif"
 14         self.aircraftImageFile = pygame.image.load(self.imagePath).convert()
 15         self.aircraftBullet = []
 16         #在默认值中定义一个aircraftBullet列表
 17     def display(self):
 18         self.screen.blit(self.aircraftImageFile,(self.x,self.y))
 19 
 20         for bullet in self.aircraftBullet:
 21             bullet.display()
 22             bullet.move()
 23         #无限显示子弹,即按空格就显示子弹
 24     def moveLeft(self):
 25         self.x -= 20
 26     def moveRight(self):
 27         self.x += 20
 28     def moveUp(self):
 29         self.y -= 20
 30     def moveDown(self):
 31         self.y += 20
 32     def shoot(self):
 33         newBullet = BulletCoordinate(self.x,self.y,self.screen)
 34         #定义一个newBullet变量,传x,y,screen到BulletCoordinate类中
 35         self.aircraftBullet.append(newBullet)
 36         #在aircraftBullet中创建新子弹
 37 class BulletCoordinate(object):
 38      def __init__(self,x,y,screen):
 39          #定义默认值,接收从飞机类中传过来的参数
 40          self.x = x+40
 41          self.y = y-20
 42          self.screen = screen
 43          self.imagePath = "./feiji/bullet-3.gif"
 44          self.bulletImageFile = pygame.image.load(self.imagePath).convert()
 45      def move(self):
 46          self.y -= 0.5
 47      def display(self):
 48          self.screen.blit(self.bulletImageFile,(self.x,self.y))
 49      
 50 
 51 
 52 if __name__ == '__main__':
 53 #__name__变量,判断该模块是作为脚本被执行,还是被调用,当直接执行的时候,就是main,在被别人调用的时候,就是name
 54     screen = pygame.display.set_mode((480,980),0,32)
 55 #设置屏幕,0,32是默认值
 56     bgImageFile = './feiji/background.png'
 57 #导入图片
 58     background = pygame.image.load(bgImageFile).convert()
 59 #背景用background保存
 60     aircraft = AircraftCoordinate(screen)
 61     #aircraftImageFile =  './feiji/hero.gif'
 62     #导入飞机图片
 63     #aircraft = pygame.image.load(aircraftImageFile).convert()
 64     #飞机图用aircraft保存
 65 
 66 while True:
 67     screen.blit(background,(0,0))
 68     #设置背景在屏幕的坐标,0.0是左上角的坐标。
 69     #screen.blit(aircraft,(x,y))
 70     #设置飞机在屏幕的坐标
 71 
 72     aircraft.display()
 73     for event in pygame.event.get():
 74         #在发生的事件当中循环,意思可以等同于获取所有的键盘操作
 75         if event.type == QUIT:
 76             #判断是否是按下了关闭键
 77             print("exit")
 78             exit()
 79             #退出程序
 80         elif event.type ==KEYDOWN:
 81             #判断是否按下了按键
 82             if event.key == K_a or event.key == K_LEFT:
 83             #判断是否按下了a键或者左键
 84                 print('')
 85                 aircraft.moveLeft()
 86             elif event.key == K_d or event.key == K_RIGHT:
 87             #判断是否按下了d键或者右键
 88                 print('')
 89                 aircraft.moveRight()
 90             elif event.key == K_w or event.key == K_UP:
 91             #判断是否按下了w键或者上键
 92                 print('')
 93                 aircraft.moveUp()
 94             elif event.key == K_s or event.key == K_DOWN:
 95             #判断是否按下了s键或者下键
 96                 print('')
 97                 aircraft.moveDown()
 98             elif event.key == K_SPACE:
 99             #判断是否按下了空格键
100                 print('射子弹')
101                 aircraft.shoot()
102 
103     pygame.display.update()
104     #更新屏幕