跟着视频打的,没有做完整
plane_main文件
import pygame
from plane_sprites import *
class PlaneGame(object):
def __init__(self):
#游戏窗口
self.screen=pygame.display.set_mode(SCREEN.size)
self.clock=pygame.time.Clock()
self.__creat_sprites()
pygame.time.set_timer(CREAT_ENEMY_EVENT,1000)
pygame.time.set_timer(HERO_FIRE,500)
def __creat_sprites(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):
while True:
self.clock.tick(FPS)
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(self)
elif event.type==CREAT_ENEMY_EVENT:
enemy=Enemy()
self.enemy_group.add(enemy)
elif event.type==HERO_FIRE:
self.hero.fire()
# elif event.type==pygame.KEYDOWN and event.key==pygame.K_RIGHT:
# print("向右移动")
# elif event.type==pygame.KEYDOWN and event.key==pygame.K_LEFT:
# print("向左移动")
key_event=pygame.key.get_pressed()
if key_event[pygame.K_RIGHT]:
self.hero.speed=2
elif key_event[pygame.K_LEFT]:
self.hero.speed=-2
else:
self.hero.speed=0
def __check_collide(self):
pygame.sprite.groupcollide(self.enemy_group,self.hero.bullet_group,True,True)
en=pygame.sprite.spritecollide(self.hero,self.enemy_group,True)
if len(en)>0:
self.hero.kill()
PlaneGame.__game_over()
def __update_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.bullet_group.update()
self.hero.bullet_group.draw(self.screen)
def __game_over(self):
print("退出游戏")
pygame.quit()
exit()
if __name__ == '__main__':
print("游戏开始。。。")
game=PlaneGame()
game.start_game()
plane_sprites文件
import random
import pygame
SCREEN=pygame.Rect(0,0,480,700)
FPS=60
CREAT_ENEMY_EVENT=pygame.USEREVENT
HERO_FIRE=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.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.bottom=0
max_a=SCREEN.width-self.rect.width
self.rect.x=random.randint(0,max_a)
def update(self):
super().update()
if self.rect.y>=SCREEN.height:
#print("删除")
#kill方法可以将精灵从所有精灵组中移除
self.kill()
def __del__(self):
#print("删除%s" %self.rect)
pass
class Hero(GameSprite):
def __init__(self):
super().__init__("./images/me1.png",0)
self.rect.centerx=SCREEN.centerx
self.rect.bottom=SCREEN.bottom-120
self.bullet_group=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.right:
self.rect.right=SCREEN.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.bullet_group.add(bullet)
class Bullet(GameSprite):
def __init__(self):
super().__init__("./images/bullet1.png",speed=-2)
def update(self):
super().update()
if self.rect.bottom<0:
self.kill()
def __del__(self):
print("删除")