当前位置:首页 >> 脚本专栏

python实现飞机大战

本文实例为大家分享了python实现飞机大战的具体代码,供大家参考,具体内容如下

实现的效果如下:

python实现飞机大战 

主程序代码如下:

import pygame
from plane_sprites import *
 
 
class PlaneGame(object):
 """飞机大战主游戏"""
 
 def __init__(self):
  print("游戏初始化")
  # 1,绘制屏幕窗口
  self.screen = pygame.display.set_mode(SCREEN_RECT.size)
  # 2,创建时钟
  self.clock = pygame.time.Clock()
  # 3,调用私有方法,精灵和精灵组的创建
  self.sprite_creat()
  # 4,设置定时器事件,创建敌机
  pygame.time.set_timer(CREATE_ENEMY_EVENT, 400)
  pygame.time.set_timer(HERO_FIRE_EVENT, 200)
 
 def sprite_creat(self):
  bg1 = BackGround()
  bg2 = BackGround(True)
 
  self.bg_group = pygame.sprite.Group(bg1, bg2)
  self.enemy_group = pygame.sprite.Group()
 
  # 创建英雄的精灵和精灵组
  self.hero = Hero()
  self.hero_group = pygame.sprite.Group(self.hero)
 
 def start_game(self):
  print("游戏开始。。。")
  while True:
   # 1,设置刷新帧率
   self.clock.tick(FRAME_PER_SEC)
   # 2,事件监听
   self.__event_handle()
   # 3,碰撞检测
   self.__check_collide()
   # 4,更新/绘制精灵和精灵组
   self.__updtae_sprites()
   # 5,更新显示
   pygame.display.update()
 
 def __event_handle(self):
  for event in pygame.event.get():
   if event.type == pygame.QUIT:
    PlaneGame.__game_over()
   elif event.type == CREATE_ENEMY_EVENT:
    print("敌机出场。。。")
    # 创建敌机精灵
    enemy = Enemy()
    # 敌机精灵添加到敌机精灵组
    self.enemy_group.add(enemy)
   elif event.type == HERO_FIRE_EVENT:
    self.hero.fire()
 
  # 移动英雄
  keys_pressed = pygame.key.get_pressed()
  if keys_pressed[pygame.K_RIGHT]:
   self.hero.speed = 5
  elif keys_pressed[pygame.K_LEFT]:
   self.hero.speed = -5
  else:
   self.hero.speed = 0
 
 def __check_collide(self):
  pygame.sprite.groupcollide(self.hero.bullets, self.enemy_group, True, True)
  enemies = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
  if len(enemies) > 0:
   self.hero.kill()
   PlaneGame.__game_over()
 
 def __updtae_sprites(self):
 
  self.bg_group.update()
  self.bg_group.draw(self.screen)
 
  self.enemy_group.update()
  self.enemy_group.draw(self.screen)
 
  self.hero_group.update()
  self.hero_group.draw(self.screen)
 
  self.hero.bullets.update()
  self.hero.bullets.draw(self.screen)
 
 @staticmethod
 def __game_over():
  print("游戏结束了")
  pygame.quit()
  exit()
 
 
if __name__ == '__main__':
 
 # 创建游戏对象
 game = PlaneGame()
 
 # 启动游戏
 game.start_game()

各个类的代码如下:

import pygame
import random
 
# 屏幕大小
SCREEN_RECT = pygame.Rect(0, 0, 480, 700)
# 刷新帧率
FRAME_PER_SEC = 60
# 创建敌机的定时器常量
CREATE_ENEMY_EVENT = pygame.USEREVENT
# 创建发射子弹事件常量
HERO_FIRE_EVENT = pygame.USEREVENT + 1
 
 
class GameSprite(pygame.sprite.Sprite):
 
 def __init__(self, image_name, speed=1):
  super().__init__()
  self.image = pygame.image.load(image_name)
  self.rect = self.image.get_rect()
  self.speed = speed
 
 def update(self):
  self.rect.y += self.speed
 
 
class BackGround(GameSprite):
 
 def __init__(self, is_alt=False):
  super().__init__("./feiji/background.png")
  if is_alt:
   self.rect.y = -self.rect.height
 
 def update(self):
  self.rect.y += self.speed
  # self.speed += 0.0001
  # 判断背景图片是否移出屏幕窗口
  if self.rect.y > SCREEN_RECT.height:
   self.rect.y = -self.rect.height
 
 
class Enemy(GameSprite):
 
 def __init__(self):
  super().__init__("./feiji/enemy0.png")
  # 随机敌机的速度
  self.speed = random.randint(2, 5)
 
  # 随机敌机的水平位置
  self.rect.x = random.randint(0, (SCREEN_RECT.width-self.rect.width))
 
 def update(self):
  super().update()
  # 判断敌机是否移出屏幕
  if self.rect.y > SCREEN_RECT.height:
   print("飞出屏幕。。。")
   # 将敌机从敌机精灵组中删除
   self.kill()
 
 def __del__(self):
  print("删除敌机")
 
 
class Hero(GameSprite):
 """英雄类"""
 
 def __init__(self):
  super(Hero, self).__init__("./feiji/hero1.png", 0)
  self.rect.centerx = SCREEN_RECT.centerx
  self.rect.bottom = SCREEN_RECT.bottom - 80
  self.bullets = pygame.sprite.Group()
 
 def update(self):
  self.rect.x += self.speed
  if self.rect.x <= 0:
   self.rect.x = 0
  elif self.rect.x >= (SCREEN_RECT.width - self.rect.width):
   self.rect.x = SCREEN_RECT.width - self.rect.width
 
 def fire(self):
  # 创建子弹精灵
  bullet = Bullet()
 
  # 设置子弹精灵的位置
  bullet.rect.bottom = self.rect.y -5
  bullet.rect.centerx = self.rect.centerx
 
  # 将子弹精灵添加到子弹精灵组中去
  self.bullets.add(bullet)
 
 
class Bullet(GameSprite):
 
 def __init__(self):
  super(Bullet, self).__init__('./feiji/bullet.png', -2)
 
 def update(self):
  super(Bullet, self).update()
  if self.rect.y <= 0:
   self.kill()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。