我怎样才能建立一个分数系统,当我达到平方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()
这基本上就是你想要的:
分数显示在屏幕的左下角
相关问题 更多 >
编程相关推荐