gpt4 book ai didi

简单游戏上的 Python 简单分数(点)系统

转载 作者:行者123 更新时间:2023-12-04 09:43:20 26 4
gpt4 key购买 nike

我使用海龟模块制作了一个简单的游戏。游戏的目标是通过左右输入在一定数量的移动中返回到原始位置。因此,我尝试制作一个计分(积分)系统,该系统计算用户完成的移动次数,并在用户在指定移动次数内返回到原始位置时打印获胜消息。如果用户(玩家)未能这样做,它会打印一条失败消息。但是,它不会计算每个移动(点),并且当它打印分数时,无论玩家做了多少移动,它总是打印“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()

最佳答案

问题是 score = 0在循环顶部调用。这会在每次循环迭代时重置分数,所以没有办法 score可以是 0 以外的任何东西或 1 .

关于简单游戏上的 Python 简单分数(点)系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62237370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com