Exe如下,欢迎试玩!
链接:https://pan.baidu.com/s/1Hw-DP98Jayr-jmE3ZCe9NA
提取码:2xsq
先上图,终极子弹:
我方子弹共有三种模型:基础子弹、双发子弹及S型子弹
1. 基础子弹
实现代码,定义类:
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 = 9
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self, width):
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
实例化:
bullet1 = []
bullet1_index = 0
BULLET1_NUM = 9
for i in range(BULLET1_NUM):
bullet1.append(bullet.Bullet1((me.rect.centerx - 5, me.rect.centery - 40)))
2. 双发子弹:
实现代码,定义类:
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 = 10
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self, width):
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
实例化:
bullet2 = []
bullet2_index = 0
bullet2_num = 14
for i in range(bullet2_num // 2):
bullet2.append(bullet.Bullet2((me.rect.centerx - 42, me.rect.centery)))
bullet2.append(bullet.Bullet2((me.rect.centerx + 22, me.rect.centery)))
3. S型子弹(两种类型:正S和反S)::
实现代码,定义类:
class Bullet3(pygame.sprite.Sprite):
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("images/bullet3.png").convert_alpha()
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = position
self.speed = 5
self.x_speed = 0
self.x = 0
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self, width):
self.rect.top -= self.speed
if self.rect.top < 0:
self.active = False
else:
self.x_speed += 8
if self.x_speed > 360:
self.x_speed = 0
self.rect.left = self.x + 150 * sin(self.x_speed * pi / 180)
def reset(self, position):
self.x, self.rect.top = position
self.active = True
self.x_speed = 0
实例化:
bullet3 = []
bullet3_index = 0
BULLET3_NUM = 15
for i in range(BULLET3_NUM):
bullet3.append(bullet.Bullet3(me.rect.midtop))
随着游戏难度增加,子弹采用“升级”的模式:两种补给包:双发和S型
A. “吃到”一个双发,子弹等级加1,最高为基础加双发,即三发子弹:
if bullet_level > 0:
bullets1 = bullet1
bullets1[bullet1_index].reset((me.rect.centerx - 5,
me.rect.centery - 40))
bullet1_index = (bullet1_index + 1) % BULLET1_NUM
if bullet_level == 2:
bullets1 = []
if bullet_level > 1:
bullets2 = bullet2
bullets2[bullet2_index].reset((me.rect.centerx-42,
me.rect.centery))
bullets2[bullet2_index+1].reset((me.rect.centerx+22,
me.rect.centery))
bullet2_index = (bullet2_index + 2) % bullet2_num
B. “吃到”一个S型,子弹等级加1,最高为正S加反S:
if bullet3_level > 0:
bullets3 = bullet3
bullets3[bullet3_index].reset(me.rect.midtop)
bullet3_index = (bullet3_index + 1) % BULLET3_NUM
if bullet3_level > 1:
bullets4 = bullet4
bullets4[bullet4_index].reset(me.rect.midtop)
bullet4_index = (bullet4_index + 1) % BULLET4_NUM
两种子弹可叠加,后期子弹感官强烈,增加玩家乐趣
# 各种子弹叠加
bullets = bullets1 + bullets2 + bullets3 + bullets4
for b in bullets:
if b.active:
b.move(width)
screen.blit(b.image, b.rect)
/n/n/n/n/n/n
为了平衡游戏,因后期子弹过于“无敌”,故增加敌方子弹,敌方子弹无法消灭,只能闪躲:
class Flies(pygame.sprite.Sprite):
def __init__(self, bg_size):
pygame.sprite.Sprite.__init__(self)
self.image_flies = pygame.image.load("images/enemy4.png").convert_alpha()
self.rect = self.image_flies.get_rect()
self.width, self.height = bg_size[0], bg_size[1]
self.rect.left, self.rect.top = randint(0, self.width - self.rect.width), \
randint(-5 * self.height, -self.height)
self.x = randint(-2, 2)
self.active = True
self.mask = pygame.mask.from_surface(self.image_flies)
def move_flies(self):
if self.rect.top < self.height:
if self.rect.top > 0:
self.rect.top += 2
if self.rect.left < 0 or self.rect.right > self.width:
self.x = -self.x
self.rect.left -= self.x
else:
self.rect.top += 1
else:
self.reset()
def reset(self):
self.rect.left, self.rect.top = randint(0, self.width - self.rect.width), \
randint(-5 * self.height, -self.height)
self.x = self.rect.left
self.active = True
self.x = randint(-2, 2)
如图小红点:
完整代码:
链接:https://pan.baidu.com/s/1xi-Y7J7BTNCBBFH62l__9A
提取码:ce3l
链接失效,备用链接:
https://download.csdn.net/download/jiandan44444/12776250
传送门:
pygame飞机大战关于子弹的设计(二)