Search code examples
pythonturtle-graphicspython-turtle

Python turtle module collision doesn't work at final level



I have some problem with my project. I want a game where from right side comes 5 TIE fighters(enemies) and you have to shot them in order to get points. After you reach 100 point (in code 5 for testing reasons) a boss comes and you need to kill it(with 45 shoots).
My problem is with the boss fight. The bullets collide with the enemies as expected, but when it comes to the boss fight, the bullets just ignore the boss. I tried to fix the collision, but nothing, that I tried seems to work.
I know that it would be easier with classes, but its a school project and we haven't learned classes yet, so we obviously can't use them. Sorry if the code is a bit unorganized, I am just a student, who is learning to code.
Thanks for the answers in advance.
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)

Solution

  • Try using another way to check for collision, I used this in most of my codes and it works well.

    #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()