【问题标题】:Python simple score (point) system on a simple game简单游戏上的 Python 简单分数(点)系统
【发布时间】:2020-09-25 23:03:15
【问题描述】:

我使用海龟模块制作了一个简单的游戏。游戏的目标是通过左右输入在一定数量的移动中返回到原始位置。因此,我尝试制作一个计分(积分)系统,该系统计算用户完成的移动次数,并在用户在指定移动次数内返回原始位置时打印获胜消息。如果用户(玩家)没有这样做,它会打印一条失败消息。但是,它不会计算每个移动(点),并且当它打印分数时,无论玩家做了多少移动,它总是打印“1”。如果问题看起来过于简单,我们深表歉意,但非常感谢您的帮助。

代码如下:

import turtle

print("Try to return to original position in exactly 12 moves")
my_turtle = turtle.Turtle()
fx = my_turtle.pos()
score = 0
tries = 12

def turtles():
    while True:
      score = 0
      directions = input("Enter which way the turtle goes: ")
      if directions == "Right" or directions == "R":
            my_turtle.right(90)
            my_turtle.forward(100)
            score += 1
            print(score)
            if fx == my_turtle.pos() and tries == score:
              print("Well done")
            elif fx == my_turtle.pos and tries > score or score > tries:
              print("Return to original position in exactly 12 moves")

      directions1 = input("Enter which way the turtle goes: ")
      if directions1 == "Left" or directions1 == "L":
            my_turtle.left(90)
            my_turtle.forward(100)
            score += 1
            print(score)
            if fx == my_turtle.pos() and tries == score:
              print("Well done")
            elif fx == my_turtle.pos and tries > score or score > tries:
              print("Return to original position in exactly 12 moves")

turtles()


turtle.done()

【问题讨论】:

    标签: python scoring python-turtle


    【解决方案1】:

    问题在于循环顶部的score = 0 调用。这会在每次循环迭代时重置分数,因此 score 不可能是 01 以外的任何东西。

    【讨论】:

    • 哦!我明白了,谢谢。我应该把它放在while循环之前吗?
    • 您应该只在游戏开始时将其设置为 0。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 2022-06-23
    • 2022-12-09
    • 2014-09-10
    相关资源
    最近更新 更多