记分制

2024-09-29 00:22:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我怎样才能建立一个分数系统,当我达到平方1时,它会增加1。一个记分系统,当我打到平方2时,它会重新回到0。当我打到平方2时,也回到起始位置。所以,当我打到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()

Tags: importdefmathcolorplayerspeedshapeturtle
1条回答
网友
1楼 · 发布于 2024-09-29 00:22:36

这基本上就是你想要的:

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

分数显示在屏幕的左下角

相关问题 更多 >