制定评分系统

Make a score system

我如何制作一个分数系统,当我点击 square1 时它会增加 1。一个计分系统,当我点击 square2 时,它会重新回到 0。当我点击 square2 时,也循环回到开始位置。所以当我点击方块 2 时,循环并重新将分数重新设置为 0。

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

这基本上就是你想要的:

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

分数显示在屏幕的左下角。