Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How could I fix this error?
#1
Hello everyone, I'm new at python, and as I wanted to learn some basic python, I decided to create a project using python turtle. I spent some hours at this until I found and error which I don't know how to fix nor what piece of code causes it.

Here's my code:

#To-Do
#1. Stop levels from completing randomly without the player doing anything

import turtle, winsound, random, time, os

level = 1
dc = 1000, 1000

#Game window
wn = turtle.Screen()
wn.setup(800, 600)
wn.bgpic('sea.gif')
wn.title("A fish game")
wn.tracer(0)
wn.cv._rootwindow.resizable(False, False)

#Shapes
turtle.register_shape('fish-up.gif')
turtle.register_shape('fish-down.gif')
turtle.register_shape('fish-left.gif')
turtle.register_shape('fish-right.gif')
turtle.register_shape('bullet.gif')
turtle.register_shape('crab.gif')
turtle.register_shape('seahorse.gif')
turtle.register_shape('pufferfish.gif')

#Fish
fish = turtle.Turtle()
fish.shape('fish-right.gif')
fish.penup()
fish.goto(0, 0)
fish.speed(0)
fish.lifes = 3

#Bullet
bullet = turtle.Turtle()
bullet.shape("bullet.gif")
bullet.shapesize(stretch_len=5, stretch_wid=5)
bullet.speed(0)
bullet.penup()
bullet.goto(-400, -40000)
bullet.hideturtle()
bullet.dx = 0
bullet.dy = 0

#Pen
pen = turtle.Turtle()
pen.penup()
pen.goto(-300, 200)
pen.color('black')
pen.hideturtle()

#Enemy 1 (crab)
crabx = random.randint(-350, 350)
craby = random.randint(-250, 250)
crab = turtle.Turtle()
crab.shape("crab.gif")
crab.penup()
crab.goto(crabx, craby)
crab.speed(0)
crab.dx = 0.045
crab.dy = -0.045

#Enemy 2 (seahorse)
seahorsex = random.randint(-350, 350)
seahorsey = random.randint(-250, 250)
seahorse = turtle.Turtle()
seahorse.shape('seahorse.gif')
seahorse.hideturtle()
seahorse.penup()
seahorse.goto(400, 400)
seahorse.speed(0)
seahorse.dx = 0.07
seahorse.dy = 0.07

#Enemy 3 (pufferfish)
pufferfishx = random.randint(-350, 350)
pufferfishy = random.randint(-250, 250)
pufferfish = turtle.Turtle()
pufferfish.shape("pufferfish.gif")
pufferfish.hideturtle()
pufferfish.penup()
pufferfish.goto(pufferfishx, pufferfishy)
pufferfish.speed(0)
pufferfish.dx = -0.02
pufferfish.dy = -0.02

#Movement
def move_up():
    fish.shape('fish-up.gif')
    y = fish.ycor()
    y += 10
    fish.sety(y)
    bullet.dx = 0
    bullet.dy = 0.08

def move_down():
    fish.shape('fish-down.gif')
    y = fish.ycor()
    y -= 10
    fish.sety(y)
    bullet.dx = 0
    bullet.dy = -0.08

def move_left():
    fish.shape('fish-left.gif')
    x = fish.xcor()
    x -= 10
    fish.setx(x)
    bullet.dx = -0.08
    bullet.dy = 0

def move_right():
    fish.shape('fish-right.gif')
    x = fish.xcor()
    x += 10
    fish.setx(x)
    bullet.dx = 0.08
    bullet.dy = 0

def shoot_bullet(x, y):
    bullet.showturtle()
    bullet.goto(fish.xcor(), fish.ycor())
    winsound.PlaySound("shoot.wav", winsound.SND_ASYNC)

def stop_bullet(x, y):
    bullet.hideturtle()
    bullet.goto(0, 0)

#Keyboard binding
wn.listen()
wn.onkeypress(move_up, "w")
wn.onkeypress(move_down, "s")
wn.onkeypress(move_left, "a")
wn.onkeypress(move_right, "d")
wn.onkeypress(wn.bye, "r")
wn.onscreenclick(shoot_bullet, 1)
wn.onscreenclick(stop_bullet, 3)

#Levels
if level == 2:
    seahorse.showturtle()

#Main game loop
while True:
    wn.update()

    #The Bullet movement
    bullet.setx(bullet.xcor() + bullet.dx)
    bullet.sety(bullet.ycor() + bullet.dy)

    #Enemy 1 movement
    crab.setx(crab.xcor() + crab.dx)
    crab.sety(crab.ycor() + crab.dy)

    if crab.xcor() > 385:
        crab.setx(385)
        crab.dx *= -1

    if crab.xcor() < -385:
        crab.setx(-385)
        crab.dx *= -1

    if crab.ycor() > 285:
        crab.sety(285)
        crab.dy *= -1

    if crab.ycor() < -285:
        crab.sety(-285)
        crab.dy *= -1

    #Enemy 2 movement
    seahorse.setx(seahorse.xcor() + seahorse.dx)
    seahorse.sety(seahorse.ycor() + seahorse.dy)

    if seahorse.xcor() > 385:
        seahorse.setx(385)
        seahorse.dx *= -1

    if seahorse.xcor() < -385:
        seahorse.setx(-385)
        seahorse.dx *= -1

    if seahorse.ycor() > 285:
        seahorse.sety(285)
        seahorse.dy *= -1

    if seahorse.ycor() < -285:
        seahorse.sety(-285)
        seahorse.dy *= -1

    #Enemy 3 movement
    pufferfish.setx(pufferfish.xcor() + pufferfish.dx)
    pufferfish.sety(pufferfish.ycor() + pufferfish.dy)

    if pufferfish.xcor() > 385:
        pufferfish.setx(385)
        pufferfish.dx *= -1

    if pufferfish.xcor() < -385:
        pufferfish.setx(-385)
        pufferfish.dx *= -1

    if pufferfish.ycor() > 285:
       pufferfish.sety(285)
       pufferfish.dy *= -1

    if pufferfish.ycor() < -285:
       pufferfish.sety(-285)
       pufferfish.dy *= -1

    #Window Borders
    if fish.ycor() > 280:
        fish.sety(280)

    if fish.ycor() < -275:
        fish.sety(-275)

    if fish.xcor() > 375:
        fish.setx(375)

    if fish.xcor() < -380:
        fish.setx(-380)

    #Collisions
    if fish.distance(crab) < 20:
        fish.lifes -= 1
        wn.title('You have ' + lifes + ' lifes left')
        fish.goto(0, 0)
        crabx = random.randint(-350, 350)
        craby = random.randint(-250, 250)
        crab.goto(crabx, craby)
        bullet.hideturtle()

    if fish.distance(seahorse) < 20:
        fish.lifes -= 1
        fish.goto(0, 0)
        seahorsex = random.randint(-350, 350)
        seahorsey = random.randint(-250, 250)
        seahorse.goto(seahorsex, seahorsey)
        bullet.hideturtle()

    if fish.distance(pufferfish) < 60:
        fish.lifes -= 1
        fish.goto(0, 0)
        pufferfishx = random.randint(-350, 350)
        pufferfishy = random.randint(-250, 250)
        pufferfish.goto(pufferfishx, pufferfishy)
        bullet.hideturtle()

    if bullet.distance(crab) < 20:
        fish.lives = 3
        fish.goto(0, 0)
        crab.goto(1000, 1000)
        crab.hideturtle()
        bullet.hideturtle()
        pen.write('You passed level 1!', align='left', font=('Arial', 12, 'normal'))
        i = input("Press any key to continue\n")
        level = 2
        fish.goto(0, 0)
        seahorse.showturtle()
        seahorse.goto(seahorsex, seahorsey)
        pen.clear()
    elif crab.xcor() == dc:
        seahorse.goto(1000, 1000)
        pufferfish.goto(1000, 1000)

    if bullet.distance(seahorse) < 20:
        fish.lives = 3
        fish.goto(0, 0)
        seahorse.goto(1000, 1000)
        seahorse.hideturtle()
        bullet.hideturtle()
        pen.write('You passed level 2!', align='left', font=('Arial', 12, 'normal'))
        i = input("Press any key to continue\n")
        level = 3
        fish.goto(0, 0)
        pufferfish.showturtle()
        pufferfish.goto(pufferfishx, pufferfishy)
        pen.clear()
    elif seahorse.xcor() == dc:
        pufferfish.goto(506546546540, 500)
        crab.goto(500, -503289438274838287432)

    if bullet.distance(pufferfish) < 30:
        fish.lives = 3
        fish.goto(0, 0)
        pufferfish.goto(1000, 1000)
        pufferfish.hideturtle()
        bullet.hideturtle()
        pen.write('You passed level 3!\nCongratulations!', align='left', font=('Arial', 12, 'normal'))
        i = input("Press any key to continue\n")
        level = 4
        fish.goto(0, 0)
        pufferfish.hideturtle()
        pufferfish.goto(4632043626732, 5)
        pen.clear()

    if fish.lifes == 0:
        fish.hideturtle()
        bullet.hideturtle()
        crab.hideturtle()
        pen.write('Game over! Press\n R to exit', align='left', font=('Arial', 12, 'normal'))
The error is that for some reason it says that I complete levels randomly without doing anything. For example, when I start the game and try to complete the first level, it says "You completed level 2", when I didn't even finish the first one. I tried changing literally everything, but I don't know what else to do. I hope I've been clear enough, any help is appreciated!
Reply


Messages In This Thread
How could I fix this error? - by urmom33 - Dec-01-2022, 06:36 PM
RE: How could I fix this error? - by deanhystad - Dec-01-2022, 08:54 PM
RE: How could I fix this error? - by woooee - Dec-01-2022, 10:15 PM
RE: How could I fix this error? - by deanhystad - Dec-01-2022, 11:12 PM
RE: How could I fix this error? - by deanhystad - Dec-04-2022, 04:36 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020