• 用pygame实现打飞机游戏-8-抽出我机和敌机的基类


      1 #coding=utf-8
      2 import pygame
      3 import random
      4 #导入pygame模块
      5 from pygame.locals import *
      6 #导入检测键盘的子模块
      7 import time
      8 class PublicAircraft():
      9     def __init__(self,screen,planeName):
     10         self.planeName = planeName 
     11         self.screen = screen
     12         self.imageFile = pygame.image.load(self.imagePath).convert()
     13         self.bulletList = []
     14     def display(self):
     15         self.screen.blit(self.imageFile,(self.x,self.y))
     16         for bullet in self.bulletList:
     17             bullet.display()
     18             bullet.move()
     19         needDelBullet = []
     20         for i in self.bulletList:
     21             if i.judge():
     22                 needDelBullet.append(i)
     23         for i in needDelBullet:
     24             self.bulletList.remove(i)
     25     def shoot(self):
     26         newBullet = PublicBullet(self.x,self.y,self.screen,self.planeName)
     27         self.bulletList.append(newBullet)
     28 class HeroAircraft(PublicAircraft):
     29     def __init__(self,screen,planeName):
     30         self.x = 250
     31         self.y = 600
     32         self.imagePath = "./feiji/hero.gif"
     33         super().__init__(screen,planeName)
     34     def moveLeft(self):
     35         self.x -= 20
     36     def moveRight(self):
     37         self.x += 20
     38     def moveUp(self):
     39         self.y -= 20
     40     def moveDown(self):
     41         self.y += 20
     42 class EnemyAircraft(PublicAircraft):
     43     def __init__(self,screen,planeName):
     44         self.x = 0
     45         self.y = 0
     46         self.imagePath = "./feiji/enemy-1.gif"
     47         super().__init__(screen,planeName)
     48         self.direction = "right"
     49     def move(self):
     50         if self.direction == "right":
     51             self.x += 2
     52         elif self.direction == "left":
     53             self.x -= 2
     54         if self.x > 480-50:
     55             self.direction = "left"
     56         elif self.x < 0:
     57             self.direction = "right"
     58     def shoot(self):
     59         num = random.randint(1,5)
     60         if num == 5:
     61             super().shoot()
     62             print("敌人射子弹")
     63 class PublicBullet(object):
     64     def __init__(self,x,y,screen,planeName):
     65         self.screen = screen
     66         self.planeName = planeName
     67         #定义默认值,接收从飞机类中传过来的参数
     68         if planeName == "aircraft":
     69             self.x = x+40
     70             self.y = y-20
     71             imagePath = "./feiji/bullet-3.gif"
     72         elif planeName == "enemy":
     73             self.x = x+30
     74             self.y = y+30
     75             imagePath = "./feiji/bullet-1.gif"
     76         self.bulletImageFile = pygame.image.load(imagePath).convert()
     77     def move(self):
     78         if self.planeName == "aircraft":
     79             self.y -= 2
     80         if self.planeName == "enemy":
     81             self.y += 2
     82     def display(self):
     83         self.screen.blit(self.bulletImageFile,(self.x,self.y))
     84     def judge(self):
     85         if self.y<0 or self.y>890:
     86             return True
     87         else:
     88             return False
     89 
     90 if __name__ == '__main__':
     91 #__name__变量,判断该模块是作为脚本被执行,还是被调用,当直接执行的时候,就是main,在被别人调用的时候,就是name
     92     screen = pygame.display.set_mode((480,980),0,32)
     93 #设置屏幕,0,32是默认值
     94     bgImageFile = './feiji/background.png'
     95 #导入图片
     96     background = pygame.image.load(bgImageFile).convert()
     97 #背景用background保存
     98     aircraft = HeroAircraft(screen,"aircraft")
     99     enemy = EnemyAircraft(screen,"enemy")
    100     #aircraftImageFile =  './feiji/hero.gif'
    101     #导入飞机图片
    102     #aircraft = pygame.image.load(aircraftImageFile).convert()
    103     #飞机图用aircraft保存
    104 
    105 while True:
    106     screen.blit(background,(0,0))
    107     #设置背景在屏幕的坐标,0.0是左上角的坐标。
    108     #screen.blit(aircraft,(x,y))
    109     #设置飞机在屏幕的坐标
    110     aircraft.display()
    111     enemy.move()
    112     enemy.shoot()
    113     enemy.display()
    114 
    115     for event in pygame.event.get():
    116         #在发生的事件当中循环,意思可以等同于获取所有的键盘操作
    117         if event.type == QUIT:
    118             #判断是否是按下了关闭键
    119             print("exit")
    120             exit()
    121             #退出程序
    122         elif event.type ==KEYDOWN:
    123             #判断是否按下了按键
    124             if event.key == K_a or event.key == K_LEFT:
    125             #判断是否按下了a键或者左键
    126                 print('')
    127                 aircraft.moveLeft()
    128             elif event.key == K_d or event.key == K_RIGHT:
    129             #判断是否按下了d键或者右键
    130                 print('')
    131                 aircraft.moveRight()
    132             elif event.key == K_w or event.key == K_UP:
    133             #判断是否按下了w键或者上键
    134                 print('')
    135                 aircraft.moveUp()
    136             elif event.key == K_s or event.key == K_DOWN:
    137             #判断是否按下了s键或者下键
    138                 print('')
    139                 aircraft.moveDown()
    140             elif event.key == K_SPACE:
    141             #判断是否按下了空格键
    142                 print('射子弹')
    143                 aircraft.shoot()
    144     time.sleep(0.01)
    145     pygame.display.update()
    146     #更新屏幕
  • 相关阅读:
    对象关系【继承】【依赖】【关联】【聚合】【组合】
    对象关系【继承】【依赖】【关联】【聚合】【组合】
    Spring使用p名称空间配置属性
    Spring使用p名称空间配置属性
    spring实例化bean的方式
    MS04011远程缓冲区溢出代码
    注入下载文件的代码到IE进程然后执行下载的文件
    缓冲区溢出漏洞发掘之整数范围限制浅析
    基于ICMP的木马的编写
    凯撒密文的破解编程实现
  • 原文地址:https://www.cnblogs.com/huxiaoyi/p/8988819.html
Copyright © 2020-2023  润新知