-
#enemy.py
-
import pygame
-
from random import *
-
-
class SmallEnemy(pygame.sprite.Sprite):
-
def __init__(self, bg_size):
-
pygame.sprite.Sprite.__init__(self)
-
-
self.image = 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() \
-
])
-
self.rect = self.image.get_rect()
-
self.width, self.height = bg_size[0], bg_size[1]
-
self.speed = 2
-
self.active = True
-
self.rect.left, self.rect.top = \
-
randint(0, self.width - self.rect.width), \
-
randint(-5 * self.height, 0) #在页面外部上方5个屏幕高度范围内随机生成一个敌机
-
self.mask = pygame.mask.from_surface(self.image) #将非透明部分标记为mask
-
-
def move(self):
-
if self.rect.top < self.height:
-
self.rect.top += self.speed
-
else:
-
self.reset()
-
-
def reset(self):
-
self.active = True
-
self.rect.left, self.rect.top = \
-
randint(0, self.width - self.rect.width), \
-
randint(-5 * self.height, 0)
-
-
class MidEnemy(pygame.sprite.Sprite):
-
energy = 8
-
-
def __init__(self, bg_size):
-
pygame.sprite.Sprite.__init__(self)
-
-
self.image = pygame.image.load("images/enemy2.png").convert_alpha()
-
self.image_hit = pygame.image.load("images/enemy2_hit.png").convert_alpha()#中弹图片
-
#添加坠机图片
-
self.destroy_images = []
-
self.destroy_images.extend([\
-
pygame.image.load("images/enemy2_down1.png").convert_alpha(), \
-
pygame.image.load("images/enemy2_down2.png").convert_alpha(), \
-
pygame.image.load("images/enemy2_down3.png").convert_alpha(), \
-
pygame.image.load("images/enemy2_down4.png").convert_alpha() \
-
])
-
self.rect = self.image.get_rect()
-
self.width, self.height = bg_size[0], bg_size[1]
-
self.speed = 1
-
self.active = True
-
self.rect.left, self.rect.top = \
-
randint(0, self.width - self.rect.width), \
-
randint(-10 * self.height, -self.height) #在页面外部上方1个到10个屏幕高度范围内随机生成一个敌机(1-10个,就保证了不会一开始出大一个大的)
-
self.mask = pygame.mask.from_surface(self.image) #将非透明部分标记为mask
-
self.energy = MidEnemy.energy
-
self.hit = False #检测是否被击中的属性
-
-
def move(self):
-
if self.rect.top < self.height:
-
self.rect.top += self.speed
-
else:
-
self.reset()
-
-
def reset(self):
-
self.active = True
-
self.energy = MidEnemy.energy
-
self.rect.left, self.rect.top = \
-
randint(0, self.width - self.rect.width), \
-
randint(-10 * self.height, -self.height)
-
-
class BigEnemy(pygame.sprite.Sprite):
-
energy = 20
-
-
def __init__(self, bg_size):
-
pygame.sprite.Sprite.__init__(self)
-
-
#大型敌机有飞行特效,两张图片切换
-
self.image1 = pygame.image.load("images/enemy3_n1.png").convert_alpha()
-
self.image2 = pygame.image.load("images/enemy3_n2.png").convert_alpha()
-
self.image_hit = pygame.image.load("images/enemy3_hit.png").convert_alpha()#中弹图片
-
#添加坠机图片
-
self.destroy_images = []
-
self.destroy_images.extend([\
-
pygame.image.load("images/enemy3_down1.png").convert_alpha(), \
-
pygame.image.load("images/enemy3_down2.png").convert_alpha(), \
-
pygame.image.load("images/enemy3_down3.png").convert_alpha(), \
-
pygame.image.load("images/enemy3_down4.png").convert_alpha(), \
-
pygame.image.load("images/enemy3_down5.png").convert_alpha(), \
-
pygame.image.load("images/enemy3_down6.png").convert_alpha() \
-
])
-
self.rect = self.image1.get_rect()
-
self.width, self.height = bg_size[0], bg_size[1]
-
self.speed = 1
-
self.active = True
-
self.rect.left, self.rect.top = \
-
randint(0, self.width - self.rect.width), \
-
randint(-15 * self.height, -5 * self.height) #在页面外部上方5-15个屏幕高度范围内随机生成一个敌机
-
self.mask = pygame.mask.from_surface(self.image1) #将非透明部分标记为mask
-
self.energy = BigEnemy.energy
-
self.hit = False #检测是否被击中的属性
-
-
def move(self):
-
if self.rect.top < self.height:
-
self.rect.top += self.speed
-
else:
-
self.reset()
-
-
def reset(self):
-
self.active = True
-
self.energy = BigEnemy.energy
-
self.rect.left, self.rect.top = \
-
randint(0, self.width - self.rect.width), \
-
randint(-15 * self.height, -5 * self.height)
-
-
-
#bullet.py
-
import pygame
-
-
class Bullet1(pygame.sprite.Sprite):#普通子弹,一次一发
-
def __init__(self, position):
-
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 = position #把飞机机头位置给子弹位置
-
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, position):
-
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 = position
-
self.speed = 14
-
self.active = False
-
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
-
-
#supply.py
-
import pygame
-
from random import *
-
-
class Bullet_Supply(pygame.sprite.Sprite):
-
def __init__(self, bg_size):
-
pygame.sprite.Sprite.__init__(self)
-
-
self.image = pygame.image.load("images/bullet_supply.png").convert_alpha()
-
self.rect = self.image.get_rect()
-
self.width, self.height = bg_size[0], bg_size[1]
-
self.rect.left, self.rect.bottom = \
-
randint(0, self.width - self.rect.width), -100 #水平方向随机,垂直方向-100
-
self.speed = 5
-
self.active = False
-
self.mask = pygame.mask.from_surface(self.image)
-
-
def move(self):
-
if self.rect.top < self.height:
-
self.rect.top += self.speed
-
else:
-
self.active = False
-
-
def reset(self):
-
self.active = True
-
self.rect.left, self.rect.bottom = \
-
randint(0, self.width - self.rect.width), -100
-
-
class Bomb_Supply(pygame.sprite.Sprite):
-
def __init__(self, bg_size):
-
pygame.sprite.Sprite.__init__(self)
-
-
self.image = pygame.image.load("images/bomb_supply.png").convert_alpha()
-
self.rect = self.image.get_rect()
-
self.width, self.height = bg_size[0], bg_size[1]
-
self.rect.left, self.rect.bottom = \
-
randint(0, self.width - self.rect.width), -100
-
self.speed = 5
-
self.active = False
-
self.mask = pygame.mask.from_surface(self.image)
-
-
def move(self):
-
if self.rect.top < self.height:
-
self.rect.top += self.speed
-
else:
-
self.active = False
-
-
def reset(self):
-
self.active = True
-
self.rect.left, self.rect.bottom = \
-
randint(0, self.width - self.rect.width), -100
95C 《零基礎入門學習Python》筆記 第095講:Pygame:飛機大戰6 下
0 留言:
發佈留言