飞机大战是之前学习python时根据网上的代码改编写成的,有完整的代码和素材。
文件结构:
images文件:放置程序所需要的图片素材
sound文件:放置程序所需要的声音素材
bullet.py:有普通以及强化两种类型的子弹
import pygame
class Bullet1(pygame.sprite.Sprite):
def __init__(self, positon):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("images/bullet1.png").convert_alpha()
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = positon
self.speed = 12
self.active = True
self.mask = pygame.mask.from_surface(self.image)
def move(self):
self.rect.top -= self.speed
if self.rect.top < 0:
self.active = False
def reset(self, position):
self.rect.left, self.rect.top = position
self.active = True
class Bullet2(pygame.sprite.Sprite):
def __init__(self, positon):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("images/bullet2.png").convert_alpha()
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = positon
self.speed = 20
self.active = True
self.mask = pygame.mask.from_surface(self.image)
def move(self):
self.rect.top -= self.speed
if self.rect.top < 0:
self.active = False
def reset(self, position):
self.rect.left, self.rect.top = position
self.active = True
enemy.py:有小型,中型,大型三种类型的敌机
import pygame
from random import *
import time
import bullet
class SmallEnemy(pygame.sprite.Sprite):
def __init__(self, bg_size):
pygame.sprite.Sprite.__init__(self)
self.image1 = pygame.image.load("images/enemy1.png").convert_alpha()
self.destroy_images = []
self.destroy_images.extend([\
pygame.image.load("images/enemy1_down1.png").convert_alpha(),\
pygame.image.load("images/enemy1_down2.png").convert_alpha(),\
pygame.image.load("images/enemy1_down3.png").convert_alpha(),\
pygame.image.load("images/enemy1_down4.png").convert_alpha()