import pygame
from plane_sprites import*
from Background import BackGround
from Enemy import Enemy
from Hero import Hero
from time import sleep
from Bullet import Bullet
class Plane_main(object):
def __init__(self):
print('游戏初始化')
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)
self.count = 0
self.score = 0
def __create_sprites(self):
# 背景组
self.back_group = pygame.sprite.Group()
bg1 = BackGround()
bg2 = BackGround(True)
pygame.mixer.init()
pygame.mixer.music.load("./Capo Productions - Journey 00_00_00-00_00_59.ogg")
pygame.mixer.music.play(-1)
self.back_group.add(bg1,bg2)
# 敌机组
self.enemy_group = pygame.sprite.Group()
#敌级销毁组
self.enemy1_down_group=pygame.sprite.Group()
# 英雄组
self.hero_group = pygame.sprite.Group()
self.hero = Hero()
self.hero_group.add(self.hero)
def start_game(self):
pygame.init()
print("开始游戏...")
# pygame.mixer.init()
# pygame.mixer.music.load("./Capo Productions - Journey 00_00_00-00_00_59.ogg")
# pygame.mixer.music.play(-1)
# gameIcon = pygame.image.load("./name.png")
# pygame.display.set_icon(gameIcon)
while True:
self.count+=1
# 1. 设置刷新帧率
self.clock.tick(10)
# 2. 事件监听
self.__event_handler()
# 3. 碰撞检测
self.__check_collide()
# 4. 更新精灵组
self.__update_sprites()
# 5. 更新屏幕显示
pygame.display.update()
def __event_handler(self):
"""事件监听"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
Plane_main.__game_over()
elif event.type == CREATE_ENEMY_EVENT:
self.enemy_group.add(Enemy())
elif event.type == HERO_FIRE_EVENT:
self.hero.fire()
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
print('向右移动')
self.hero.speed = 5
self.hero.speed1 = 0
elif keys[pygame.K_LEFT]:
print('向左移动')
self.hero.speed = -5
self.hero.speed1 = 0
elif keys[pygame.K_UP]:
print('向上移动')
self.hero.speed1 = -5
self.hero.speed = 0
elif keys[pygame.K_DOWN]:
print('向下移动')
self.hero.speed1= 5
self.hero.speed = 0
else:
self.hero.speed = 0
self.hero.speed1 = 0
if event.type == HERO_FIRE_EVENT:
self.hero.fire()
print('发射子弹!!!!!')
def __check_collide(self):
"""碰撞检测"""
# 1. 子弹摧毁敌机
enemy_down = pygame.sprite.groupcollide(self.enemy_group, self.hero.bullets,True, True)
enemy1_down_group.add(enemy_down)
# 2. 敌机撞毁英雄
enemies = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
# 判断列表时候有内容`
if len(enemies) > 0:
sleep(3)
# # 让英雄牺牲
self.hero.kill()
# 结束游戏
Plane_main.__game_over()
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)
self.drawText(str(self.score),SCREEN_RECT.width - 50,50)
for enemy1_down in enemy1_down_group:
self.screen.blit(enemy1_down_surface[enemy1_down.down_index],enemy1_down.rect)
if self.count % 5 ==0:
enemy1_down.down_index += 1
if enemy1_down.down_index ==3:
# pygame.mixer.music.load("./baozha.ogg")
# pygame.mixer.music.play()
self.score+=5
enemy1_down_group.remove(enemy1_down)
print(self.score)
#更新精灵组
# for group in [self.back_group, self.enemy_group, self.hero_group,self.hero.bullets]:
# group.update()
# group.draw(self.screen)
@staticmethod
def __game_over():
"""游戏结束"""
print("游戏结束")
pygame.quit()
exit()
def drawText(self,text,posx,posy,textHeight=48,fontColor=(0,0,0),backgroundColor=(111,225,1)):
fontObj = pygame.font.Font('freesansbold.ttf',textHeight)
textSurfaceObj = fontObj.render(text,13,fontColor,backgroundColor)
textRectObj = textSurfaceObj.get_rect()
textRectObj.center = (posx,posy)
self.screen.blit(textSurfaceObj,textRectObj)
#判断当前执行的文件是否是主文件
#如果不是 则if下的代码不会执行
if __name__ == '__main__':
game = Plane_main()
game.start_game()
from GameSprite import GameSprite
from plane_sprites import *
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):
# 1. 调用父类的方法实现
super().update()
# 2. 判断是否移出屏幕,如果移出屏幕,将图像设置到屏幕的上方
if self.rect.y >= SCREEN_RECT.height:
self.rect.y = -self.rect.height
设置子弹图片 Bullet.py
from GameSprite import GameSprite
class Bullet(GameSprite):
"""docstring for ClassName"""
def __init__(self):
super().__init__('./images/bullet2.png',-30)
def update(self):
super().update()
if self.rect.bottom < 0:
self.kill()
from GameSprite import GameSprite
from plane_sprites import *
import random
class Enemy(GameSprite):
def __init__(self):
super().__init__('./images/enemy1.png')
self.speed = random.randint(1,6)
max_x = SCREEN_RECT.width - self.rect.width
self.rect.x = random.randint(0,max_x)
self.rect.bottom = 0
self.down_index = 0 #敌机销毁图片索引
def update(self):
super().update()
# print(SCREEN_RECT.height,SCREEN_RECT.y)
if self.rect.y >= SCREEN_RECT.height:
# print('飞机飞出屏幕')
self.kill()
def __del__(self):
pass
from GameSprite import GameSprite
from plane_sprites import *
from Bullet import Bullet
class Hero(GameSprite):
def __init__(self):
super().__init__('./images/hero.gif')
self.speed1=0
self.rect.centerx = SCREEN_RECT.centerx
self.rect.bottom = SCREEN_RECT.bottom - 100
self.bullets = pygame.sprite.Group()
def update(self):
self.rect.x += self.speed
self.rect.y += self.speed1
#判断左边界
if self.rect.left <= 0:
self.rect.left = 0
if self.rect.right > SCREEN_RECT.width:
self.rect.right = SCREEN_RECT.width
if self.rect.top<=0:
self.rect.top = 0
if self.rect.bottom>SCREEN_RECT.height:
self.rect.bottom = SCREEN_RECT.height
def fire(self):
# 1. 创建子弹精灵
bullet = Bullet()
# 2. 设置精灵的位置
bullet.rect.bottom = self.rect.y - 40
bullet.rect.centerx = self.rect.centerx
# 3. 将精灵添加到精灵组
self.bullets.add(bullet)
bullet1 = Bullet()
bullet1.rect.bottom = self.rect.y - 20
bullet1.rect.centerx = self.rect.centerx -30
self.bullets.add(bullet1)
bullet2 = Bullet()
bullet2.rect.bottom = self.rect.y - 20
bullet2.rect.centerx = self.rect.centerx +30
self.bullets.add(bullet2)
import pygame
import random
SCREEN_RECT = pygame.Rect(0,0,480,800)
FRAME_PER_SEC = 60
CREATE_ENEMY_EVENT = pygame.USEREVENT
HERO_FIRE_EVENT = pygame.USEREVENT + 1
#爆炸销毁图片
bg1 = pygame.image.load('./images/enemy1_down1.png')
bg2 = pygame.image.load('./images/enemy1_down2.png')
bg3 = pygame.image.load('./images/enemy1_down3.png')
bg4 = pygame.image.load('./images/enemy0_down4.png')
#爆炸精灵组
enemy1_down_group = pygame.sprite.Group()
#把爆炸图片放到列表当中
enemy1_down_surface = []
enemy1_down_surface.append(bg1)
enemy1_down_surface.append(bg2)
enemy1_down_surface.append(bg3)
enemy1_down_surface.append(bg4)
class GameSprite(pygame.sprite.Sprite):
def __init__(self,image_name,speed = 1):
#当发生继承时,不论父类是否有init方法,只要子类
#定义了init方法,那么一定要在子类的init方法中
#通过super()调用父类的init方法
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