最近学习了点python基础,初步完成了飞机大战小游戏,但是有些功能还需进一步完善。
效果图:
plane_main.py
import pygame
from plane_sprites import *
pygame.init()
class PlaneGame(object):
"""飞机大战主游戏"""
def __init__(self):
self.screen = pygame.display.set_mode(SCREEN_RECT.size)
self.clock = pygame.time.Clock()
self.__create_sprites()
# 设置定时器事件,创建敌机
pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000)
pygame.time.set_timer(HERO_FIRE_EVENT, 500)
def __create_sprites(self):
bg1 = Background()
bg2 = Background(True)
self.back_group = pygame.sprite.Group(bg1, bg2)
# 创建敌机精灵组
self.enemy_group = pygame.sprite.Group()
# 创建英雄的精灵和精灵组
self.hero = Hero()
self.hero_group = pygame.sprite.Group(self.hero)
def __update_sprites(self):
self.back_group.update()
self.back_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)
def start_game(self):
while True:
# 设置刷新帧率
self.clock.tick(FRAME_PER_SEC)
# 事件监听
self.__event_handler()
# 碰撞检测
self.__check_collide()
# 更新绘制精灵组
self.__update_sprites()
# 更新显示
pygame.display.update()
def __event_handler(self):
for event in pygame.event.get():
# 判断用户是否点击了关闭按钮
if event.type == pygame.QUIT:
PlaneGame.__game_over()
elif event.type == CREATE_ENEMY_EVENT:
# 创建敌机精灵并添加到精灵组
enemy = Enemy()
self.enemy_group.add(enemy)
elif event.type == HERO_FIRE_EVENT:
self.hero.fire()
# elif event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
# print("向右移动")
# 使用键盘提供的方法获取键盘按键
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_RIGHT]:
self.hero.speed = 2
elif keys_pressed[pygame.K_LEFT]:
self.hero.speed = -2
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()
@staticmethod
def __game_over():
print("游戏结束!")
pygame.quit()
exit()
if __name__ == '__main__':
game = PlaneGame()
game.start_game()
plane_sprites.py
import random
import pygame
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__("./images/background.png")
# 判断是否是交替图像,如果是,需要设置初始位置
if is_alt:
self.rect.y = -self.rect.height
def update(self):
super().update()
if self.rect.y >= SCREEN_RECT.height:
self.rect.y = -self.rect.height
class Enemy(GameSprite):
"""敌机精灵"""
def __init__(self):
# 调用父类方法,创建敌机精灵
super().__init__("./images/enemy1.png")
# 指定随机速度
self.speed = random.randint(1, 3)
# 指定随机位置
self.rect.bottom = 0
max_x = SCREEN_RECT.width - self.rect.width
self.rect.x = random.randint(0, max_x)
def update(self):
super().update()
if self.rect.y >= SCREEN_RECT.height:
# kill方法将精灵从精灵组中销毁
self.kill()
def __del__(self):
pass
class Hero(GameSprite):
"""英雄精灵"""
def __init__(self):
# 调用父类方法,设置image&speed
super().__init__("./images/me1.png", 0)
# 设置英雄的初始位置
self.rect.centerx = SCREEN_RECT.centerx
self.rect.bottom = SCREEN_RECT.bottom - 120
# 创建子弹精灵组
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.right > SCREEN_RECT.right:
self.rect.right = SCREEN_RECT.right
# 发送子弹
def fire(self):
for i in (0, 1, 2):
# 创建子弹精灵
bullet = Bullet()
# 设置位置
bullet.rect.bottom = self.rect.y - i * 20
bullet.rect.centerx = self.rect.centerx
# 将精灵添加到精灵组
self.bullets.add(bullet)
class Bullet(GameSprite):
"""子弹精灵"""
def __init__(self):
super().__init__("./images/bullet1.png", -2)
def update(self):
super().update()
if self.rect.bottom < 0:
self.kill()
def __del__(self):
pass
项目图片素材: