飞机一只

 

# -*- coding: utf-8 -*-
# File Name: planeSprites
# Description :
# Author : zongyanzhang
# date: 2018/12/28

import pygame, random

# 所有的类都将继承此类
class BaseSprite(pygame.sprite.Sprite):
def __init__(self, img, speed=1):
super(BaseSprite, self).__init__()
self.image = pygame.image.load(img)
self.rect = self.image.get_rect()
self.speed = speed

def update(self):
self.rect.y += self.speed


# 背景类
class BackGround(BaseSprite):
def __init__(self, img, speed=1, pos=True):
super(BackGround, self).__init__(img, speed)
if pos == False:
self.rect.y = -self.rect.height

def update(self):
super(BackGround, self).update()
if self.rect.y == self.rect.height:
self.rect.y = -self.rect.height

class Bullet(BaseSprite):
def update(self):
self.rect.y -= self.speed
if self.rect.bottom <= 0:
self.kill()

# 战机类
class HeroPlane(BaseSprite):
def __init__(self, img, window, speed=1):
super(HeroPlane, self).__init__(img, speed)
self.bulletgroup = pygame.sprite.Group()
self.rect.centerx = window.get_rect().centerx
self.rect.y = window.get_rect().height-self.rect.height-10

def fire(self):
for i in range(3):
b = Bullet('./images/bullet1.png', 6)
b.rect.midbottom = self.rect.midtop
self.bulletgroup.add(b)

def update(self):
pass

# 敌机类
class EnemyPlane(BaseSprite):
def __init__(self, img, window, energy=1, speed=3):
super(EnemyPlane, self).__init__(img, speed)
self.window = window
self.energy = energy
self.rect.x = random.randint(0, window.get_rect().width-self.rect.width)
if energy < 5:
# 小型
self.rect.top = random.randint(-self.rect.height-10, -self.rect.height)
elif 10 > energy >= 5:
self.rect.top = random.randint(-self.rect.height*5 - 10, -self.rect.height*5)
elif energy >= 10:
self.rect.top = random.randint(-self.rect.height*10-10, -self.rect.height*10)

def update(self):
super(EnemyPlane, self).update()
if self.rect.y > self.window.get_rect().height:
self.kill()


######################################################################################
# -*- coding: utf-8 -*-
# File Name: main
# Description :
# Author : zongyanzhang
# date: 2018/12/28

import planeSprites as ps
from pygame.locals import *
import sys
import pygame

class Game(object):
def __init__(self, width=480, height=700):
# 游戏的初始化
self.window = pygame.display.set_mode((width, height))
pygame.display.set_caption('飞机大战1.0')
self.clock = pygame.time.Clock()

self.__create_sprites()
# 定义产生子弹的事件
pygame.time.set_timer(USEREVENT, 300)
# 定义产生小敌机的事件
pygame.time.set_timer(USEREVENT+1, 500)
# 定义产生中敌机的事件
pygame.time.set_timer(USEREVENT+2, 1000)

def __create_sprites(self):
# 创建精灵
bg1 = ps.BackGround('./images/background.png')
bg2 = ps.BackGround('./images/background.png', pos=False)
self.bg_group = pygame.sprite.Group(bg1, bg2)

self.hero = ps.HeroPlane('./images/me1.png', self.window)

# 创建小型敌机组
self.smallEnemy = pygame.sprite.Group()
for i in range(2):
self.smallEnemy.add(ps.EnemyPlane('./images/enemy1.png', \
self.window, speed=5))
# 创建中型敌机组
self.midEnemy = pygame.sprite.Group()
self.midEnemy.add(ps.EnemyPlane('./images/enemy2.png', \
self.window, energy=5, speed=3))


def startGame(self):
'''开始游戏'''
while True:
self.__get_event()
self.__collide_sprites()
self.__show_sprites()
self.clock.tick(45)
pygame.display.update()

def __get_event(self):
'''获取事件'''
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit(0)
elif event.type == USEREVENT:
self.hero.fire()
elif event.type == USEREVENT+1:
self.smallEnemy.add(ps.EnemyPlane('./images/enemy1.png', \
self.window, speed=5))
elif event.type == USEREVENT+2:
self.midEnemy.add(ps.EnemyPlane('./images/enemy2.png', \
self.window, energy=5, speed=3))

def __show_sprites(self):
'''显示精灵'''
self.bg_group.update()
self.bg_group.draw(self.window)

self.window.blit(self.hero.image, self.hero.rect)
self.hero.bulletgroup.update()
self.hero.bulletgroup.draw(self.window)

self.smallEnemy.update()
self.smallEnemy.draw(self.window)

self.midEnemy.update()
self.midEnemy.draw(self.window)

def __collide_sprites(self):
'''碰撞检测'''
pass


if __name__ == '__main__':
game = Game()
game.startGame()




posted @ 2018-12-28 16:49  Teacher张  阅读(269)  评论(0编辑  收藏  举报