python实现飞机大战
非常简单
没有完成需求
不想统计得分
不想搞大怪真么没意思
就是无聊打打怪
界面展示:
代码以下:
1、[工具类] plane_sprites.py
import pygame
import random
# 面板大小
RECT_SIZE = pygame.Rect(0, 0, 480, 700)
# 帧率
FRAME_FOR_SEC = 60
# 背景地址
BACKGROUND_FILE = "./image/background.png"
# 敌机地址
ENEMY_FILE = "./image/enemy1.png"
# 子弹地址
BULLET_FILE = "./image/bullet1.png"
# 飞机地址
HEROPLANE_FILE = "./image/me2.png"
# 飞机大小
PLANE_SIZE = pygame.Rect(185, 550, 102, 126)
# 飞机平移速度
PLANE_SPEED = 2
# 子弹大小
BULLET_SIZE = pygame.Rect(0, 0, 5, 11)
# 子弹速度
BULLET_SPEED = -2
# 子弹的定时器常量
BULLET_TIMER_EVENT = pygame.USEREVENT
# 子弹出现间隔 ms
BULLET_OCCUR = 1000
# 子弹距离间隔
BULLET_DIM = 30
# 敌机的定时器常量
ENEMY_TIMER_EVENT = pygame.USEREVENT + 1
# 敌机出现间隔 ms
ENEMY_OCCUR = 1500
# 敌机大小
ENEMY_SIZE = pygame.Rect(0, 0, 57, 43)
# 敌机最低速度
LOW_SPEED = 1
# 敌机最高速度
UP_SPEED = 4
class GameSprite(pygame.sprite.Sprite):
def __init__(self, image_name, site=(0,0),speed=1):
super().__init__()
x,y = site
self.speed = speed
self.image = pygame.image.load(image_name)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def update(self):
self.rect.y += self.speed
class BackGround(GameSprite):
def __init__(self, is_alt):
if is_alt:
super().__init__(BACKGROUND_FILE,(0, -RECT_SIZE.height))
else:
super().__init__(BACKGROUND_FILE)
def update(self):
super().update()
if self.rect.y >= RECT_SIZE.height:
self.rect.y -= 2*RECT_SIZE.height
class EnemyPlane(GameSprite):
def __init__(self):
rand_x = random.randint(0, RECT_SIZE.width - ENEMY_SIZE.width)
rand_speed = random.uniform(LOW_SPEED, UP_SPEED)
super().__init__(ENEMY_FILE, (rand_x, -ENEMY_SIZE.height), rand_speed)
def update(self):
super().update()
if self.rect.y >= RECT_SIZE.height:
self.kill()
def __del__(self):
pass;
# print("敌机逃脱,位置在(%d,%d)"%(self.rect.x,self.rect.y))
class HeroPlane(GameSprite):
def __init__(self):
x, y = PLANE_SIZE.x, PLANE_SIZE.y
super().__init__(HEROPLANE_FILE, (x, y), 0)
self.rect.centerx = RECT_SIZE.centerx
self.rect.bottom = RECT_SIZE.bottom - 120
self.bullet_group = pygame.sprite.Group()
def update(self,direction=0):
self.rect.x += self.speed
if (self.rect.x < 0):
self.rect.x = 0
if (self.rect.x > RECT_SIZE.width - PLANE_SIZE.width):
self.rect.x = RECT_SIZE.width - PLANE_SIZE.width
def fire(self):
print("开火!!!!")
for i in (0,1,2):
bullet = Bullet()
bullet.rect.bottom = self.rect.y - BULLET_DIM * i
bullet.rect.centerx = self.rect.centerx
self.bullet_group.add(bullet)
class Bullet(GameSprite):
def __init__(self):
super().__init__(BULLET_FILE, (0,0), BULLET_SPEED)
def update(self):
super().update()
if self.rect.bottom < 0:
self.kill()
def __del__(self):
pass;
# print("销毁子弹")
2、[主程序] planegame_main.py
from plane_sprites import *
class PlaneGame(object):
def __create_sprites(self, *sprites):
bg1 = BackGround(0)
bg2 = BackGround(1)
self.hero = HeroPlane()
self.stat_group = pygame.sprite.Group(bg1, bg2)
self.hero_group = pygame.sprite.Group(self.hero)
self.enemy_group = pygame.sprite.Group()
# 敌机定时器事件
pygame.time.set_timer(ENEMY_TIMER_EVENT, ENEMY_OCCUR)
pygame.time.set_timer(BULLET_TIMER_EVENT, BULLET_OCCUR)
def __event_handler(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
PlaneGame.__game_over()
elif event.type == ENEMY_TIMER_EVENT:
enemy = EnemyPlane()
self.enemy_group.add(enemy)
''' 按1s发射三枚
elif event.type == BULLET_TIMER_EVENT:
self.hero.fire()
'''
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
self.hero.speed = PLANE_SPEED
elif event.key == pygame.K_LEFT:
self.hero.speed = -PLANE_SPEED
elif event.key == pygame.K_SPACE:
self.hero.fire()
'''空格控制子弹
elif event.key == pygame.K_SPACE:
self.hero.fire()
'''
elif event.type == pygame.KEYUP:
self.hero.speed = 0
def __check_collide(self):
pygame.sprite.groupcollide(self.enemy_group, self.hero.bullet_group, True, True)
destroy_enemy = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
if len(destroy_enemy)>0 :
self.hero.kill()
PlaneGame.__game_over()
def __update_sprites(self):
# 精灵组update
self.stat_group.update()
self.enemy_group.update()
self.hero_group.update()
self.hero.bullet_group.update()
# 精灵组draw
self.stat_group.draw(self.screen)
self.enemy_group.draw(self.screen)
self.hero_group.draw(self.screen)
self.hero.bullet_group.draw(self.screen)
@staticmethod
def __game_over():
print("游戏结束")
pygame.quit()
exit()
def __init__(self,screen_size, *sprites):
print("游戏初始化")
pygame.init()
# 创建游戏窗口
self.screen = pygame.display.set_mode(screen_size, 0, 32)
# 创建游戏时钟
self.clock = pygame.time.Clock()
# 调用 精灵与精灵组的创造
self.__create_sprites(sprites)
def start_game(self):
print("游戏开始!")
while True:
# 设置更新频率
self.clock.tick(FRAME_FOR_SEC)
# 事件监听
self.__event_handler()
# 冲突检测
self.__check_collide()
# 更新/绘制精灵组
self.__update_sprites()
# 更新显示
pygame.display.update()
if __name__ == '__main__':
# 创建游戏对象
my_game = PlaneGame(RECT_SIZE.size)
my_game.start_game()