|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
以下是bullet模块
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
以下是main模块关于子弹方面的代码
#生成子弹
bullet1 = []
bullet1_index = 0
BULLET1_NUM = 4
for i in range(BULLET1_NUM):
bullet1.append(bullet.Bullet1(me.rect.midtop))
#绘制发射子弹(十帧一发)
if not(delay % 10):
bullet1[bullet1_index].reset(me.rect.midtop)
bullet1_index = (bullet1_index + 1) % BULLET1_NUM
#检测子弹是否击中敌机
for b in bullet1:
if b.active:
b.move()
screen.blit(b.image, b.rect)
enemy_hit = pygame.sprite.spritecollide(b, enemies, False, pygame.sprite.collide_mask)
b.active = False
for e in enemy_hit:
e.active = False
enemy_hit 一个列表,包含碰撞的精灵,if 语句 直接接一句b.active = false, 这是直接把子弹写死了, 加一句 if enemy_hit:(如果存在碰撞)
|
|