虽然英雄天资聪慧,已经有了发射子弹的多种方式,但这些技能被封印住了,需要捡装备,生技能来解锁。
一个好好的射击游戏,被玩成了RPG模式。
掉落来了。
装备一般有两种方式,一个是随机出现的箱子,一个是打死怪后随机掉落。
建立一个supply.py,新建一个Supply类,暂时提供子弹的升级卷轴。
该类也是一个飞行的精灵,在屏幕中飞行,持续一定的时间,不捡掉就消失。
from setting import *
from animation import *
class Supply(pygame.sprite.Sprite):
def __init__(self):
self._layer = 8
self.groups = allgroup,supplygroup
pygame.sprite.Sprite.__init__(self,self.groups)
self.animation = Animation()
self.images = self.animation.load_image('images/supply/bulletsupply.png',249,81,1,3)
#必不可少的两条代码
self.image = self.images[0]
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
#初设速度、角度
self.x_speed = 2.00
self.y_speed = 2.00
self.speed = 2
self.duration = 20000
self.motion_type = 0
self.start_time = pygame.time.get_ticks()
# self.clock.get_timer()
#会自动调用
def update(self):
current_time = pygame.time.get_ticks()
if current_time - self.start_time > self.duration:
self.kill()
self.image = self.animation.get_current_image()
if random.randint(0, 100) == 1:
self.x_speed = random.randint(-2, 2)
self.y_speed = random.randint(-2, 2)
self.image = self.animation.get_current_image()
#镜面反射移动,供给出现的位置可能为负数或者超过屏幕边界,因此要留有判断的余量,
#不能直接 self.rect.x < 0
if self.rect.x > SCENEWIDTH or self.rect.x < -50:
self.x_speed = -self.x_speed
print(self.rect.x,self.rect.y)
if self.rect.y > SCENEHEIGHT or self.rect.y < -50:
self.y_speed = -self.y_speed
print(self.rect.x,self.rect.y)
self.rect.x += self.x_speed
self.rect.y += self.y_speed
#供给默认从中心计算
def set_pos(self, x, y):
self.rect.center = (x,y)
# self.rect.y = y
在main函数里,增加掉落的随机判定
def collideEvent(self):
hero_shot_enemy = pygame.sprite.groupcollide(herobulletgroup,enemygroup,True,True)
if hero_shot_enemy:
#hero_shot_enemy为子弹 key为 子弹,value 为 飞机
for v in hero_shot_enemy:
#获得 key 的 values,
#即获得当前子弹射中的敌机列表
enemys = hero_shot_enemy[v]
#遍历敌机
for enemy in enemys:
#用随机函数控制掉率
if random.randint(0,50) == 0:
supply_pos = enemy.rect.center
supply = Supply()
supply.set_pos(supply_pos[0],supply_pos[1])
hero_collide_supply = pygame.sprite.spritecollide(self.hero,supplygroup,True)
if hero_collide_supply:
self.hero.fire_type += 1
不能让self.hero.fire_type 无限大,可以自己在main.py里控制,也可以在hero.py里控制。
可以让他产生爆炸等效果(后续再写),或者干脆沿用最高等级的发射方式。
#超限了,还是360吧
else:
for angle in range(0, 360, 10):
bullet = HeroBullet()
bullet.set_bullet_type(bullettype)
pos_x = hero_fire_pos.x
pos_y = hero_fire_pos.y
bullet.set_speed(self.bullet_speed, angle)
bullet.set_pos(pos_x, pos_y)
来看看效果,为了测试,提高了掉落