下面是我自学python时,写的一个小实例——飞机大战:
精灵类:
import random
import pygame
from pygame.locals import *
SCREEN_RECT = pygame.Rect(0, 0, 600, 800)
FRAME_PER_SEC = 60
CTEAT_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 BackGroud(GameSprite):
def __init__(self, is_alt=False):
super().__init__("./Resources/img_bg_1.jpg")
if is_alt:
self.rect.y = -self.rect.height
def update(self):
super().update()
if self.rect.y >= 800:
self.rect.y = -self.rect.height
class Enemy(GameSprite):
def __init__(self):
super().__init__("./Resources/n1.png")
self.speed = random.randint(4, 6)
self.rect.bottom = 0
max_x = 442
self.rect.x = random.randint(0, max_x)
def update(self):
super().update()
if self.rect.y >= 800:
self.kill()
def __del__(self):
pass
class Hero(GameSprite):
"""英雄精灵"""
def __init__(self):
super().__init__("./Resources/mplane.png", 0)
self.rect.centerx = 300
self.rect.bottom = 800 - 20
self.bullets = pygame.sprite.Group()
def right(self):
super().update()
if self.rect.x < 480:
self.rect.x += 5
else:
self.rect.x = 485
def left(self):
super().update()
if self.rect.x > 5:
self.rect.x -= 5
else:
self.rect.x = 0
def fire(self):
for i in (0, 1, 2):
bullet = Bullet()
bullet.rect.bottom = self.rect.y - i * 50
bullet.rect.centerx = self.rect.centerx
self.bullets.add(bullet)
def __del__(self):
self.kill()
class Bullet(GameSprite):
def __init__(self):
super().__init__("./Resources/n4.png", -5)
def update(self):
super().update()
if self.rect.bottom < 0:
self.kill()
def __del__(self):
self.kill()
游戏主类:
import pygame
from plane_sprites import *
class PlaneGame(object):
def __init__(self):
self.screen = pygame.display.set_mode((600,800))
self.clock = pygame.time.Clock()
self.__creat_sprites()
pygame.time.set_timer(CTEAT_ENEMY_EVENT, 1000)
pygame.time.set_timer(HERO_FIRE_EVENT,500)
def __creat_sprites(self):
bg1=BackGroud()
bg2=BackGroud(True)
bg2.rect.y=-bg2.rect.height
self.back_groud=pygame.sprite.Group(bg1,bg2)
self.enemy_group=pygame.sprite.Group()
self.hero=Hero()
self.hero_group=pygame.sprite.Group(self.hero)
pass
def start_game(self):
while True:
self.clock.tick(FRAME_PER_SEC)
self.__event_handler()
self.__check_collide()
self.__update_sprites()
pygame.display.update()
pass
def __event_handler(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.__game_over()
elif event.type== CTEAT_ENEMY_EVENT:
enemy=Enemy()
self.enemy_group.add(enemy)
elif event.type==HERO_FIRE_EVENT:
self.hero.fire()
key_pressed=pygame.key.get_pressed()
if key_pressed[pygame.K_RIGHT]:
self.hero.right()
elif key_pressed[pygame.K_LEFT]:
self.hero.left()
def __check_collide(self):
pygame.sprite.groupcollide(self.hero.bullets,self.enemy_group,True,True)
enemys=pygame.sprite.spritecollide(self.hero,self.enemy_group,True)
if len(enemys)>0:
hero_shoot = pygame.image.load("./Resources/shoot.png")
self.screen.blit(hero_shoot,self.hero.rect)
self.__update_sprites()
#self.hero.__del__()
#self.__game_over()
pass
def __update_sprites(self):
self.back_groud.update()
self.back_groud.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)
pygame.display.update()
@staticmethod
def __game_over():
print("游戏结束")
pygame.quit()
exit()
if __name__ == '__main__':
# 创建游戏对象
game = PlaneGame()
game.start_game()