Search code examples
pythonpython-3.xpython-turtle

Make a score system


How can I make a score system, that increases by one, when I hit square1. A score system, that restart back to 0, when I hit square2. And also loop back to the beginning position, when I hit square2. So loop back and restart the score to 0, when I hit square 2.

import turtle
import math
from time import sleep

wn = 0
player = turtle.Turtle()
square1 = turtle.Turtle()
square2 = turtle.Turtle()

speed = 1

def init():
    wn = turtle.Screen()
    wn.bgcolor("blue")
    wn.tracer(3)
    turtle.listen()
    turtle.onkey(turnLeft, "Left")
    turtle.onkey(turnRight, "Right")

def initCharacters():
    player.color("red")
    player.shape("circle")
    player.penup()
    player.speed(0)
    player.setposition(25, 20)
    
    square1.color("white")
    square1.shape("square")
    square1.penup()
    square1.speed(0)
    square1.setposition(100, 100)

    square2.color("yellow")
    square2.shape("square")
    square2.penup()
    square2.speed(0)
    square2.setposition(150, 50)
    
def distanceIsLessThan(p, q, distance):
    return math.pow(p.xcor() - q.xcor(), 2) + math.pow(p.ycor() - q.ycor(), 2) < math.pow(distance, 2)
    
def turnLeft():
    player.left(90)

def turnRight():
    player.right(90)

def main():
    init()
    initCharacters()
    
    while True:
        player.forward(speed)

        if distanceIsLessThan(player, square1, 20) or \
           distanceIsLessThan(player, square2, 20):
            # print("Touch")
            initCharacters()

        sleep(0.02)
        
 
if __name__ == "__main__":
    main()

Solution

  • This is basically what you want:

    import turtle
    import math
    from time import sleep
    
    #Adding the integer for the score
    score=0
    
    wn = 0
    player = turtle.Turtle()
    square1 = turtle.Turtle()
    square2 = turtle.Turtle()
    speed = 1
    
    #Here Im making a new turtle for showing the score
    turtle_score=turtle.Turtle()
    turtle_score.penup()
    turtle_score.hideturtle()
    turtle_score.goto(-300.00,-245.00)
    turtle_score.write("")
    
    def init():
        wn = turtle.Screen()
        wn.bgcolor("blue")
        wn.tracer(3)
        turtle.listen()
        turtle.onkey(turnLeft, "Left")
        turtle.onkey(turnRight, "Right")
    
    def initCharacters():
        player.color("red")
        player.shape("circle")
        player.penup()
        player.speed(0)
        player.setposition(25, 20)
        
        square1.color("white")
        square1.shape("square")
        square1.penup()
        square1.speed(0)
        square1.setposition(100, 100)
    
        square2.color("yellow")
        square2.shape("square")
        square2.penup()
        square2.speed(0)
        square2.setposition(150, 50)
        
    def distanceIsLessThan(p, q, distance):
        return math.pow(p.xcor() - q.xcor(), 2) + math.pow(p.ycor() - q.ycor(), 2) < math.pow(distance, 2)
        
    def turnLeft():
        player.left(90)
    
    def turnRight():
        player.right(90)
    
    def main():
        init()
        initCharacters()
    
        global score
        
        while True:
            player.forward(speed)
            
            #Checking which square the player collided with and updating the score.
            if distanceIsLessThan(player, square1, 20):
                score=score+1
                player.fd(40)
                turtle_score.undo()
                turtle_score.write(str(score))
            if distanceIsLessThan(player, square2, 20):
                score=0
                turtle_score.undo()
                turtle_score.write(str(score))
                # print("Touch")
                initCharacters()
    
            sleep(0.02)
            
     
    if __name__ == "__main__":
        main()
    

    The score shows up at the bottom left corner of the screen.