0

I am attempting to finish a program for a class, and I am doing fairly well. It is a simple turtle-graphics game in python, where you attempt to avoid poison dots and get navigate to a square. However, my program starts immediately, before the user clicks on the screen. How can I fix this? Thanks!

My code:

# This game involves avoiding red poison blobs while attempting to navigate to
# a square. If you hit the blob, you begin to speed up, making it more difficult
# not to hit more. Additionally, you lose a point. If you reach the square you
# get a point.

import turtle
import math
import random

# screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.tracer(3)

# Draw border
pen1 = turtle.Turtle()
pen1.color("white")
pen1.penup()
pen1.setposition(-275,-275)
pen1.pendown()
pen1.pensize(5)
for side in range(4):
    pen1.forward(550)
    pen1.left(90)
pen1.hideturtle()

# player
player = turtle.Turtle()
player.color("dark green")
player.shape("turtle")
player.penup()

# poisonBlob
maxpoisonBlob = 15
poisonBlob = []

for a in range(maxpoisonBlob):
    poisonBlob.append(turtle.Turtle())
    poisonBlob[a].color("dark red")
    poisonBlob[a].shape("circle")
    poisonBlob[a].shapesize(4, 4, 4)
    poisonBlob[a].penup()
    poisonBlob[a].speed(0)
    poisonBlob[a].setposition(random.randint(-255, 255), random.randint(-255, 255))

maxfood = 1
food = []

for a in range(maxfood):
        food.append(turtle.Turtle())
        food[a].color("light blue")
        food[a].shape("square")
        food[a].penup()
        food[a].speed(0)
        food[a].setposition(random.randint(-240, 240), random.randint(-240, 240))

# speed variable
speed = 6.5

def turnleft():
    player.left(30)

def turnright():
    player.right(30)

def increasespeed():
    global speed
    speed += 1

def touchPoison(t1, t2):
    d = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2) + math.pow(t1.ycor()-t2.ycor(),2))
    if d < 50:
        return True
    else:
        return False

def touchfood(z1, z2):
        d = math.sqrt(math.pow(z1.xcor()-z2.xcor(),2) + math.pow(z1.ycor()-z2.ycor(),2))
        if d < 20:
                return True
        else:
                return False


turtle.listen()
turtle.onkey(turnleft, "Left")
turtle.onkey(turnright, "Right")


def main():
    print("Help your turtle navigate red poison blobs while attempting to navigate to the\n"
          "food! If you hit the poison, you begin to speed up, making it more difficult\n"
          "not to hit more. Additionally, you lose a point. If you reach the square you\n"
          "get a point. To navigate, click the screen, and then use the right and left\n"
          "arrow keys. Quickly, your turtle is running away!")

    score = 0

    while True:
        player.forward(speed)

        # turtle boundary
        if player.xcor() > 260 or player.xcor() < -260:
            player.right(180)

        # turtle boundary
        if player.ycor() > 260 or player.ycor() < -260:
            player.right(180)

        # move poison
        for a in range(maxpoisonBlob):
            poisonBlob[a].forward(3)

           # Poison boundaries
            if poisonBlob[a].xcor() > 220 or poisonBlob[a].xcor() < -220:
                poisonBlob[a].right(180)

            # Poision boundaries
            if poisonBlob[a].ycor() > 220 or poisonBlob[a].ycor() < -220:
                poisonBlob[a].right(180)     

            # Poison touching
            if touchPoison(player, poisonBlob[a]):
                increasespeed()
                poisonBlob[a].setposition(random.randint(-230, 230), random.randint(-230, 230))
                poisonBlob[a].right(random.randint(0,360))
                score -= 1
                #Draw score
                pen1.undo()
                pen1.penup()
                pen1.hideturtle()
                pen1.setposition(-260, 280)
                scorestring = "Score: %s" %score
                pen1.write(scorestring, font=("Calibri", 12))

        for a in range(maxfood):

            #Positive Point Checking
            if touchfood(player, food[a]):
                food[a].setposition(random.randint(-230, 230), random.randint(-230, 230))
                score += 1
                #Draw Score
                pen1.undo()
                pen1.penup()
                pen1.hideturtle()
                pen1.setposition(-260, 280)
                scorestring = "Score: %s" %score
                pen1.write(scorestring, font=("Calibri", 12))

if __name__ == "__main__":
    main()
smithy
  • 1
  • 5

2 Answers2

1

Generally we try not to directly answer homework questions, but I will give you some guidance.

I'm not familiar with the library you're using, but the basic idea is that you need to wait until the user clicks the screen before continuing. My understanding is that you want to do so after the print() function in main, so that the user has to read the message and then click to continue.

A quick search turned up this link: Turtle in python- Trying to get the turtle to move to the mouse click position and print its coordinates

This goes into detail of the onscreenclick() event. Judging by your onkey() methods you are already familiar with binding to events, so you should be able to use this knowledge to bind to the onscreenclick() event and then you just need to break your code up a bit so that it doesn't execute your game until the onscreenclick() event has happened.

If that isn't clear enough, help me understand where your confusion is and I'll assist further. Thanks!

Community
  • 1
  • 1
lukevp
  • 705
  • 4
  • 16
1

You could begin by defining a function where you call your "if" statement and then use turtle.onscreenclick() by inserting your newly defined function in it like so:

def Game(x, y):
    if __name__ == "__main__":
        main()

turtle.onscreenclick(Game)

Make sure to insert all this at the end!

Now, adding the (x, y) in the parenthesis after the function name in the function definition assigns the spot the user clicks on in the graphics window its corresponding (x, y) coordinates, which in turn activates the function due to the action of clicking on the screen. I did this successfully, so I can assure you it works! I hope this helps! :)

R. Kap
  • 599
  • 1
  • 10
  • 33