我在这里没有涉及到我的设置,因为那只是基本信息,而我在此代码中遇到的问题是线程。当我涉及我的线程T4和T5时,游戏只会停滞不前,我不知道为什么,因为我认为线程就像是一个附带代码,在主代码上没有效果。希望您能在这里帮助我谢谢!
from settings import *
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption('Space shoter!')
pygame.display.set_icon(icon)
clock = pygame.time.Clock()
def draw_score(surf, text, size, x, y):
def checkscore():
bossinvinc = False
def draw_healthbar(surf, x, y, pct,maxhp):
def playermovement():
class Player(pygame.sprite.Sprite):
class Mob(pygame.sprite.Sprite):
class Laser(pygame.sprite.Sprite):
class Spawnmeteroits(Thread):
def run(*args):
global spawnthebig
while running:
if meterorfallactv:
for _ in range(random.randint(2, 4)):
m = Mob(tinymeteroit, tinyspeed , tinyHP, tinypoints)
mobs.add(m)
all_sprites.add(m)
time.sleep(1)
if spawnthebig:
spawnthebig = False
m = Mob(bigmeteroit, bigspeed , bigHP, bigpoints)
mobs.add(m)
all_sprites.add(m)
time.sleep(1)
class Spawnmeteroitsmedium(Thread):
def run(*args):
while running:
if meterorfallactv:
time.sleep(3)
for _ in range(random.randint(1, 2)):
m = Mob(mediummeteroit, mediumspeed , mediumHP, mediumpoints)
mobs.add(m)
all_sprites.add(m)
class Shotlaser(Thread):
def run(*args):
while running:
pulscannonXleft = shipcenter - 23
pulscannonXright = shipcenter + 14
pulscannonY = shipbottom - 52
Lleft = Laser(pulscannonXleft,pulscannonY)
Lright = Laser(pulscannonXright,pulscannonY)
lasershotsR.add(Lright)
lasershotsL.add(Lleft)
all_sprites.add(Lleft,Lright)
time.sleep(attackspeed)
这里是这些线程之一
bossfightstart = False
lethtebosscome = False
class Bossfight(Thread):
def run(*args):
global bossfightstart, lethtebosscome, bossinvinc
while running:
if lethtebosscome:
print('yeye')
bossinvinc = True
boss = Endboss(bosshealth)
all_sprites.add(boss)
stagebossfight.add(boss)
while boss.rect.x > 70:
pass
lethtebosscome = False
bossfightstart = True
bossinvinc = False
Laserabilityoff = False
class Boss_laser(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = laserstart
self.rect = self.image.get_rect()
self.rect.centerx = random.randint(150, 600)
self.mask = pygame.sprite.from_surface(self.image)
if self.rect.centerx <= 170:
self.rect.centery = 170
else:
self.rect.centery = 190
def update(self):
self.rect.y += 10
self.rect = self.image.get_rect()
self.mask = pygame.sprite.from_surface(self.image)
if self.rect.top > HEIGHT:
self.kill()
if Laserabilityoff:
self.kill()
这里是这些线程之一
class firebosslaser(Thread):
def run(*args):
while running:
if bossfightstart:
global Laserabilityoff
time.sleep(random.randint(4, 7))
for _ in range(random.randint(3, 6)):
BL = Boss_laser()
all_sprites.add(BL)
stagebossfight.add(BL)
time.sleep(3.5) # durating of existence
Laserabilityoff = True
def playergetshit():
global Playerhealth
meterorhits = pygame.sprite.spritecollide(player,mobs,False)
if meterorhits:
truemeterorhits = pygame.sprite.spritecollide(player,mobs,True,pygame.sprite.collide_mask)
for _ in truemeterorhits:
player.HP -= 1
Playerhealth -= 1
def meteroitsgethit():
LaserhitsR = pygame.sprite.groupcollide(mobs, lasershotsR, False, False)
if LaserhitsR:
truelaserhitsR = pygame.sprite.groupcollide(mobs, lasershotsR, False, True,pygame.sprite.collide_mask)
for hit in truelaserhitsR:
hit.health -= LaserDMG
LaserhitsL = pygame.sprite.groupcollide(mobs, lasershotsL, False, False)
if LaserhitsL:
truelaserhitsL = pygame.sprite.groupcollide(mobs, lasershotsL, False, True,pygame.sprite.collide_mask)
for hit in truelaserhitsL:
hit.health -= LaserDMG
class Endboss(pygame.sprite.Sprite):
def __init__(self,bosshp):
global meterorspeed
pygame.sprite.Sprite.__init__(self)
self.image = finalboss
self.rect = self.image.get_rect()
self.HP = bosshp
self.rect.y = 0
self.rect.x = 810
self.mask = pygame.sprite.Sprite(self.image)
meterorspeed = 0
def update(self):
# movement to middle of the screen noch invincible
if self.rect.x > 70:
self.rect.x -= 3.5
self.mask = pygame.sprite.from_surface(self.image)
player = Player(Playerhealth)
healthbarX = player.rect.x - 18
healthbarY = player.rect.y + 69
all_sprites.add(player)
# game loop
running = True
meterorfallactv = True
# thread start
T1 = Spawnmeteroits(daemon = True)
T1.start()
T2 = Shotlaser(daemon = True)
T2.start()
T3 = Spawnmeteroitsmedium(daemon = True)
T3.start()
T4 = Bossfight(daemon = True)
T4.start()
T5 = firebosslaser(daemon = True)
T5.start()
while running:
clock.tick(fps)
screen.fill((40, 40, 40))
screen.blit(background,background_rect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
playermovement()
all_sprites.update()
playergetshit()
meteroitsgethit()
all_sprites.draw(screen)
draw_healthbar(screen, healthbarX, healthbarY, Playerhealth,PlayerMaxHealth)
checkscore()
pygame.display.flip()
pygame.quit()
我不得不禁用我的一些代码,因为我不允许发布这么多的代码,但是问题不在于线程
答案 0 :(得分:0)
在Bossfight中,您有以下内容:
sp
那是一个“忙循环”;解释器将基本上停在那里,但会连续检查 while boss.rect.x > 70:
pass
。将boss.rect.x > 70
换成短暂的睡眠,以免消耗过多的CPU。
firebosslaser也有类似的问题:如果pass
为False,它将永远循环。在该循环后添加bossfightstart
,您应该会没事。