项目代码 | plane
# -*- coding:utf-8 -*- import pygame, time from Plane import Plane from HeroPlane import HeroPlane from Screen import Screen from pygame.locals import * def key_control(plane_temp): # 获取事件,比如按键等 for event in pygame.event.get(): # 判断是否是点击了退出按钮 if event.type == QUIT: print("exit") exit() # 判断是否是按下了键 elif event.type == KEYDOWN: # 检测按键是否是a或者left if event.key == K_a or event.key == K_LEFT: print('left') plane_temp.move_left() # 检测按键是否是d或者right elif event.key == K_d or event.key == K_RIGHT: print('right') plane_temp.move_right() # 检测按键是否是空格键 elif event.key == K_SPACE: print('space') plane_temp.fire() def main(): screen = pygame.display.set_mode((480, 852), 0, 32) # 创建窗口对象 screen_temp = Screen(screen) # 创建一个飞机对象 plane = Plane(screen) # 创建敌机对象 enemyPlane = HeroPlane(screen) while True: screen_temp.display() # 显示窗口 plane.display(plane.bullet_list) # 显示飞机 enemyPlane.display(enemyPlane.bullet_list) # 敌机显示 enemyPlane.move() # 敌机移动 enemyPlane.fire() # 敌机开火 key_control(plane) # 键盘事件监听 pygame.display.update() # 更新窗口 time.sleep(0.01) # 延时0.01秒,防止程序内存溢出 if __name__ == '__main__': main()
Base.py
基类
# -*- coding:utf-8 -*- import pygame class Base(object): # 背景图片 image = None def __init__(self, screen_temp, x, y, image_path): self.x = x self.y = y self.screen = screen_temp self.image_load(image_path) # 飞机赋值图片对象 def image_load(self, image_path): self.image = pygame.image.load(image_path)
BasePlane.py
飞机基类
# -*- coding:utf-8 -*- from Base import Base class BasePlane(Base): def __init__(self, screen_temp, x, y, image_path): Base.__init__(self, screen_temp, x, y, image_path) # 显示飞机 def display(self, bullet_list): self.screen.blit(self.image, (self.x, self.y)) # 显示飞机 for bullet in bullet_list: # 循环取出子弹对象 # 判断子弹是否越界 if bullet.judge(): bullet_list.remove(bullet) # 如果子弹越界就删除子弹 bullet.display() # 显示子弹 bullet.move() # 子弹移动步长
Plane.py
飞机对象
# -*- coding:utf-8 -*- from Bullet import Bullet from BasePlane import BasePlane class Plane(BasePlane): # 储存子弹对象 bullet_list = [] def __init__(self, screen_temp): BasePlane.__init__(self, screen_temp, 210, 700, "./resource/hero1.png") # 飞机向左移动偏移量 def move_left(self): self.x -= 10 # 飞机向右移动偏移量 def move_right(self): self.x += 10 # 将飞机创建的子弹对象存储进 bullet_list 列表中 def fire(self): self.bullet_list.append(Bullet(self.screen, self.x, self.y)) print(self.bullet_list)
HeroPlane.py
敌机对象
# -*- coding:utf-8 -*- import random from BasePlane import BasePlane from EnemyBullet import EnemyBullet class HeroPlane(BasePlane): # 定义一个类属性用来保存 direction = 'right' # 储存子弹对象 bullet_list = [] def __init__(self, screen_temp): BasePlane.__init__(self, screen_temp, 0, 0, "./resource/enemy-1.gif") # 敌机移动轨迹 def move(self): # 敌机创建的子弹默认向右移动 if self.direction == 'right': self.x += 5 # 每次向右移动增加 5px 的步长 elif self.direction == 'left': # 向左移动 self.x -= 5 # 每次向左移动减少 5px 的步长 # 当敌机向右移动到了边界就向左移动 if self.x > 480 - 50: # 480 是界面总宽度; 50 是飞机宽度. 所以敌机移动的距离应该是界面宽度-敌机宽度 ( 移动距离 = 界面宽度 - 敌机宽度 ) self.direction = 'left' elif self.x <= 0: # 当敌机移动到了最左边就会继续向右移动 self.direction = 'right' # 开火 def fire(self): random_temp = random.randint(1, 100) # 随机生成 1 - 100的随机数 if (random_temp == 20) or (random_temp == 78): # 随机数概率 self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y)) # 创建敌机子弹对象
BaseBullet.py
子弹基类
# -*- coding:utf-8 -*- from Base import Base class BaseBullet(Base): def __init__(self, screen_temp, x, y, image_path): Base.__init__(self, screen_temp, x, y, image_path) # 子弹背景 def display(self): self.screen.blit(self.image, (self.x, self.y))
Bullet.py
子弹对象
# -*- coding:utf-8 -*- from BaseBullet import BaseBullet class Bullet(BaseBullet): def __init__(self, screen_temp, x, y): BaseBullet.__init__(self, screen_temp, x + 40, y - 20, "./resource/bullet.png") # 子弹步长 def move(self): self.y -= 10 # 判断子弹y轴是否已经越界 def judge(self): if self.y < 0: return True else: return False
EnemyBullet.py
敌机子弹对象
# -*- coding:utf-8 -*- from BaseBullet import BaseBullet class EnemyBullet(BaseBullet): def __init__(self, screen_temp, x, y): BaseBullet.__init__(self, screen_temp, x + 30, y + 30, "./resource/bullet-1.gif") # 子弹移动步长 def move(self): self.y += 20 # 判断子弹y轴是否已经越界 def judge(self): if self.y > 890: # 890 界面总高度 return True else: return False
Screen.py
窗口对象
# -*- coding:utf-8 -*- from Base import Base class Screen(Base): def __init__(self, screen_temp): Base.__init__(self, screen_temp, 0, 0, "./resource/background.png") def display(self): self.screen.blit(self.image, (self.x, self.y))