Python turtle 模块碰撞在最终级别不起作用

Python turtle module collision doesn't work at final level


我的项目有问题。我想要一款游戏,右侧有 5 个 TIE 战士(敌人),你必须射击他们才能获得分数。在你达到 100 点后(出于测试原因在代码 5 中),一个老板来了,你需要杀死它(45 次射击)。
我的问题是boss战。子弹果然和敌人撞在了一起,但是打boss的时候,子弹就直接无视了boss。我试图修复碰撞,但似乎没有任何效果。
我知道使用 类 会更容易,但这是一个学校项目,我们还没有学习 类,所以我们显然不能使用它们。对不起,如果代码有点杂乱无章,我只是一个正在学习编码的学生。
提前感谢您的回答。
from playsound import playsound
import turtle, random, time

def up():
    y = ss.ycor()
    ss.sety(y+20)


def down():
    y = ss.ycor()
    ss.sety(y-20)


def left():
    x = ss.xcor()
    ss.setx(x-20)


def right():
    x = ss.xcor()
    ss.setx(x+20)


def shoot():
    laser = turtle.Turtle()
    laser.penup()
    laser.shape("ammos.gif")
    laser.setx(ss.xcor())
    laser.sety(ss.ycor())
    laserl.append(laser)

wn = turtle.Screen()
wn.setup(width=1200, height=900)
wn.bgpic("stardestroyer.png")
wn.tracer(0)
wn.addshape("falcon.gif")
wn.addshape("tiefighter.gif")
wn.addshape("explosion.gif")
wn.addshape("starship.gif")
wn.addshape("ammos.gif")
wn.listen()
wn.onkeypress(up,"Up")
wn.onkeypress(down,"Down")
wn.onkeypress(left,"Left")
wn.onkeypress(right,"Right")
wn.onkeypress(shoot, "space")


ss = turtle.Turtle()
ss.penup()
ss.shape("falcon.gif")

mt = turtle.Turtle()
mt.penup()
mt.shape("tiefighter.gif")
mt.setx(588)
mt.sety(random.randint(-420, 420))

boss = turtle.Turtle()
boss.shape("starship.gif")
boss.penup()
boss.hideturtle()
boss.setx(725)

mt1 = turtle.Turtle()
mt1.penup()
mt1.shape("tiefighter.gif")
mt1.setx(588)
mt1.sety(random.randint(-420, 420))

mt2 = turtle.Turtle()
mt2.penup()
mt2.shape("tiefighter.gif")
mt2.setx(588)
mt2.sety(random.randint(-420, 420))

mt3 = turtle.Turtle()
mt3.penup()
mt3.shape("tiefighter.gif")
mt3.setx(588)
mt3.sety(random.randint(-420, 420))

mt4 = turtle.Turtle()
mt4.penup()
mt4.shape("tiefighter.gif")
mt4.setx(588)
mt4.sety(random.randint(-420, 420))

sb = turtle.Turtle()
sb.hideturtle()
sb.penup()
sb.sety(410)
sb.setx(-595)
sb.color('white')

sb1 = turtle.Turtle()
sb1.hideturtle()
sb1.penup()
sb1.sety(410)
sb1.setx(595)
sb1.color('white')

sb3 = turtle.Turtle()
sb3.hideturtle()
sb3.penup()
sb3.color('white')


score = 0
life = 3
bosshit = 0

alive = True
bossalive = False

laserl = []
mtlist = []

x = 0

while alive:
    sb.clear()
    sb.write(f"{life} életed maradt.",  align="left", font=("Arial", 28, "bold") )

    if alive:
        mt.back(20)
        mt1.back(20)
        mt2.back(20)
        mt3.back(20)
        mt4.back(20)
    
    if bossalive:
        boss.back(4)

    if ss.distance(mt.xcor(), mt.ycor()) < 25:
        mt.hideturtle()
        mt.shape("tiefighter.gif")
        mt.showturtle()
        mt.setx(588)
        mt.sety(random.randint(-425, 426))

        life -= 1        

        sb.clear()
        sb.write(f"{life} életed maradt.", align="left", font=("Arial", 28, "bold") )
        
        if life == 0:
            ss.shape("explosion.gif")
            sb.clear()
            sb.write("Elfogytak az életeid, meghaltál!", align="left", font=("Arial", 28, "bold"))
            time.sleep(2.3)
            sb.clear()
            alive = False
    
    if ss.distance(mt1.xcor(), mt1.ycor()) < 25:
        mt1.hideturtle()
        mt1.shape("tiefighter.gif")
        mt1.showturtle()
        mt1.setx(588)
        mt1.sety(random.randint(-425, 426))

        life -= 1        

        sb.clear()
        sb.write(f"{life} életed maradt.", align="left", font=("Arial", 28, "bold") )
        
        if life == 0:
            ss.shape("explosion.gif")
            sb.clear()
            sb.write("Elfogytak az életeid, meghaltál!", align="left", font=("Arial", 28, "bold"))
            time.sleep(2.3)
            sb.clear()
            alive = False
    
    if ss.distance(mt2.xcor(), mt2.ycor()) < 25:
        mt2.hideturtle()
        mt2.shape("tiefighter.gif")
        mt2.showturtle()
        mt2.setx(588)
        mt2.sety(random.randint(-425, 426))

        life -= 1        

        sb.clear()
        sb.write(f"{life} életed maradt.", align="left", font=("Arial", 28, "bold") )
        
        if life == 0:
            ss.shape("explosion.gif")
            sb.clear()
            sb.write("Elfogytak az életeid, meghaltál!", align="left", font=("Arial", 28, "bold"))
            time.sleep(2.3)
            sb.clear()
            alive = False
    
    if ss.distance(mt3.xcor(), mt3.ycor()) < 25:
        mt3.hideturtle()
        mt3.shape("tiefighter.gif")
        mt3.showturtle()
        mt3.setx(588)
        mt3.sety(random.randint(-425, 426))

        life -= 1        

        sb.clear()
        sb.write(f"{life} életed maradt.", align="left", font=("Arial", 28, "bold") )
        
        if life == 0:
            ss.shape("explosion.gif")
            sb.clear()
            sb.write("Elfogytak az életeid, meghaltál!", align="left", font=("Arial", 28, "bold"))
            time.sleep(2.3)
            sb.clear()
            alive = False
    
    if ss.distance(mt4.xcor(), mt4.ycor()) < 25:
        mt4.hideturtle()
        mt4.shape("tiefighter.gif")
        mt4.showturtle()
        mt4.setx(588)
        mt4.sety(random.randint(-425, 426))

        life -= 1        

        sb.clear()
        sb.write(f"{life} életed maradt.", align="left", font=("Arial", 28, "bold") )

        if life == 0:
            ss.shape("explosion.gif")
            sb.clear()
            sb.write("Elfogytak az életeid, meghaltál!", align="left", font=("Arial", 28, "bold"))
            time.sleep(2.3)
            sb.clear()
            alive = False 
        
        if life == 0:
            ss.shape("explosion.gif")
            sb.clear()
            sb.write("Elfogytak az életeid, meghaltál!", align="left", font=("Arial", 28, "bold"))
            time.sleep(2.3)
            sb.clear()
            alive = False

    if mt.xcor() < -599:
        mt.setx(588)
        mt.sety(random.randint(-425, 426))
    
    if mt1.xcor() < -599:
        mt1.setx(588)
        mt1.sety(random.randint(-425, 426))
    
    if mt2.xcor() < -599:
        mt2.setx(588)
        mt2.sety(random.randint(-425, 426))
    
    if mt3.xcor() < -599:
        mt3.setx(588)
        mt3.sety(random.randint(-425, 426))
    
    if mt4.xcor() < -599:
        mt4.setx(588)
        mt4.sety(random.randint(-425, 426))

    for bullet in laserl:
        
        if bullet.distance(mt) < 30:
            mt.setx(588)
            mt.sety(random.randint(-425, 426))
            score += 1
            sb1.clear()
            sb1.write(f"{score} pontod van.", align="right", font=("Arial", 28, "bold") )
            bullet.setx(700)
            bullet.fd(0)
            bullet.clear()
            bullet.hideturtle()
        elif bullet.distance(mt1) < 30:
            mt1.setx(588)
            mt1.sety(random.randint(-425, 426))
            score += 1
            sb1.clear()
            sb1.write(f"{score} pontod van.", align="right", font=("Arial", 28, "bold") )
            bullet.setx(700)
            bullet.fd(0)
            bullet.clear()
            bullet.hideturtle()
        elif bullet.distance(mt2) < 30:
            mt2.setx(588)
            mt2.sety(random.randint(-425, 426))
            score += 1
            sb1.clear()
            sb1.write(f"{score} pontod van.", align="right", font=("Arial", 28, "bold") )
            bullet.setx(700)
            bullet.fd(0)
            bullet.clear()
            bullet.hideturtle()
        elif bullet.distance(mt3) < 30:
            mt3.setx(588)
            mt3.sety(random.randint(-425, 426))
            score += 1
            sb1.clear()
            sb1.write(f"{score} pontod van.", align="right", font=("Arial", 28, "bold") )
            bullet.setx(700)
            bullet.fd(0)
            bullet.clear()
            bullet.hideturtle()
        elif bullet.distance(mt4) < 30:
            mt4.setx(588)
            mt4.sety(random.randint(-425, 426))
            score += 1
            sb1.clear()
            sb1.write(f"{score} pontod van.", align="right", font=("Arial", 28, "bold") )
            bullet.setx(700)
            bullet.fd(0)
            bullet.clear()
            bullet.hideturtle()
        else:
            bullet.fd(30)   
            
        if bullet.xcor() >= 600:
            bullet.clear()
            bullet.fd(0)
            bullet.hideturtle()

        if score == 5:
            bossalive = True
            mt.setx(700)
            mt.clear()
            mt1.setx(700)
            mt1.clear()
            mt2.setx(700)
            mt2.clear()
            mt3.setx(700)
            mt3.clear()
            mt4.setx(700)
            mt4.clear()
            boss.showturtle()

            if bullet.distance(boss) > 50:
                bosshit += 1
                bullet.setx(700)
                bullet.fd(0)
                bullet.clear()
                bullet.hideturtle()

            if bosshit == 45:
                boss.shape("explosion.gif")
                boss.clear()
                bossalive = False
                sb3.clear()
                sb3.write(f"Legyőzted a főszörnyet, megnyerted a játékot! Elért pontszám:{score}", align="center", font=("Arial", 28, "bold"))
                score += 50
                time.sleep(3)
                alive = False
            if boss.xcor() == -650:
                sb.clear()
                sb.write(f"Nem sikerült legyőznöd a főszörnyet. Elvesztetted a játékot! Elért pontszám: {score}", align="center", font=("Arial", 28, "bold"))
                time.sleep(3)
                alive = False
                    
                
    if ss.xcor() > 599:
        ss.setx(-599)

    if ss.xcor() < -599:
        ss.setx(599)

    if ss.ycor() > 449:
        ss.sety(-449)

    if ss.ycor() < -449:
        ss.sety(449)

    x += 1
    wn.update()
    time.sleep(0.1)

尝试使用另一种方法来检查碰撞,我在我的大部分代码中都使用了它并且效果很好。

#you can edit the value here     \/ to change the radius of the collision detection
if bullet.xcor() - boss.xcor() < 10 and bullet.xcor() - boss.xcor() < 10:
                bosshit += 1
                bullet.setx(700)
                bullet.fd(0)
                bullet.clear()
                bullet.hideturtle()