如何解决制作评分系统
我如何制作一个分数系统,当我点击 square1 时,它会增加一。一个分数系统,当我点击 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()
分数显示在屏幕的左下角。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。