Python 海龟图形中的生命计数器
Lives counter in Python turtle graphics
我需要一些帮助,了解如何在 Python 海龟图形中制作生命计数器。
我的代码:
def draw_lives():
global lives
lives = turtle.Turtle()
lives.penup
lives.hideturtle
lives.goto(-200, 400)
while True:
lives.write("Lives: " + str(lives), font=("Arial", 50, "normal"))
if lives > 0:
lives.write("You have lost all lives. Try again.", font=("Arial", 50, "normal"))
break
我想过让我的生活对抗一只海龟,而不仅仅是某个地方的一个随机计数器(这实际上听起来更好)。
此外,我是否收到 if lives > 0:
的错误,即 Turtle
和 int
实例之间不支持 >
。
有人可以帮忙吗?
您的代码构造不当——让我们看一下细节。主要问题是您对 both counter 和 turtle 使用相同的变量名称 lives
显示计数器。以不同的方式命名它们。如果这样做,则不需要:
global lives
下一题是基础Python:
lives.penup
lives.hideturtle
这些是方法调用,因此它们应该是:
lives.penup()
lives.hideturtle()
最后,您的 while True:
在这里没有任何业务,或者在 turtle 事件驱动程序中。您在 if
语句中遗漏了一两行代码。
让我们修改您的代码,使其更新屏幕上 lives
计数器的值:
from turtle import Screen, Turtle
FONT = ("Arial", 24, "normal")
def draw_lives():
lives_pen.clear()
if lives > 0:
lives_pen.write("Lives: " + str(lives), font=FONT)
else:
lives_pen.write("You have lost all lives. Try again.", font=FONT)
lives = 5
lives_pen = Turtle() # this turtle is only used to update lives counter display
lives_pen.hideturtle()
lives_pen.penup()
lives_pen.goto(-200, 300)
lives_pen.write("Lives: " + str(lives), font=FONT)
if __name__ == '__main__':
from time import sleep
# test code
screen = Screen()
for _ in range(lives + 1):
draw_lives()
sleep(1)
lives -= 1
screen.exitonclick()
__main__
部分只是测试代码,以确认 draw_lives()
以我们想要的方式工作——所以扔掉它。
像 lives_pen
这样的 Utility turtles 应该只创建 一次,而不是每次你需要更新计数器的时候,因为它们是全局实体并且在函数退出。
It's a bad practice to use global
in your code. 相反,您可以为您的海龟创建自定义属性。
超级简单,几乎没有什么不便:
from turtle import Turtle
pen = Turtle()
pen.lives = 5 # Here, the lives attribute is created
您甚至可以对字体执行相同的操作,尽管这可能是不必要的:
pen.font = ("Arial", 30, "normal")
如果失去生命是更新生命计数的唯一情况,请不要在循环中不断重写它们
(当然,除非有什么东西妨碍了文本,并且您希望文本显示在顶部),
只有在失去生命时才重写它。
我们可以重绘 和 在这样的函数中更新生命:
def update_lives():
pen.clear()
if pen.lives:
pen.write(f"Lives: {pen.lives}", font=pen.font)
else:
pen.write("You have lost all lives. Try again.", font=pen.font)
pen.lives -= 1 # Remove this line and this becomes your typical text updater
为了在实际中看到这一点,我实现了一个演示,其中每当用户按下 SPACE 栏时就会失去生命:
from turtle import Turtle, Screen
wn = Screen()
def update_lives():
pen.clear()
if pen.lives:
pen.write(f"Lives: {pen.lives}", font=pen.font)
else:
pen.write("You have lost all lives. Try again.", font=pen.font)
pen.lives -= 1
pen = Turtle(visible=False)
pen.penup()
pen.goto(-300, 200)
pen.lives = 5
pen.font = ("Arial", 30, "normal")
update_lives()
wn.listen()
wn.onkey(update_lives, 'space')
使用上面的代码,当用户到达 0
时,再次按下 SPACE 将使函数继续显示负值。
要解决这个问题,对于您的主游戏循环,请使用 while pen.lives
告诉 python 仅继续循环,直到剩余生命数大于 0
:
while pen.lives:
# Your game code here
wn.update()
输出:
我需要一些帮助,了解如何在 Python 海龟图形中制作生命计数器。
我的代码:
def draw_lives():
global lives
lives = turtle.Turtle()
lives.penup
lives.hideturtle
lives.goto(-200, 400)
while True:
lives.write("Lives: " + str(lives), font=("Arial", 50, "normal"))
if lives > 0:
lives.write("You have lost all lives. Try again.", font=("Arial", 50, "normal"))
break
我想过让我的生活对抗一只海龟,而不仅仅是某个地方的一个随机计数器(这实际上听起来更好)。
此外,我是否收到 if lives > 0:
的错误,即 Turtle
和 int
实例之间不支持 >
。
有人可以帮忙吗?
您的代码构造不当——让我们看一下细节。主要问题是您对 both counter 和 turtle 使用相同的变量名称 lives
显示计数器。以不同的方式命名它们。如果这样做,则不需要:
global lives
下一题是基础Python:
lives.penup
lives.hideturtle
这些是方法调用,因此它们应该是:
lives.penup()
lives.hideturtle()
最后,您的 while True:
在这里没有任何业务,或者在 turtle 事件驱动程序中。您在 if
语句中遗漏了一两行代码。
让我们修改您的代码,使其更新屏幕上 lives
计数器的值:
from turtle import Screen, Turtle
FONT = ("Arial", 24, "normal")
def draw_lives():
lives_pen.clear()
if lives > 0:
lives_pen.write("Lives: " + str(lives), font=FONT)
else:
lives_pen.write("You have lost all lives. Try again.", font=FONT)
lives = 5
lives_pen = Turtle() # this turtle is only used to update lives counter display
lives_pen.hideturtle()
lives_pen.penup()
lives_pen.goto(-200, 300)
lives_pen.write("Lives: " + str(lives), font=FONT)
if __name__ == '__main__':
from time import sleep
# test code
screen = Screen()
for _ in range(lives + 1):
draw_lives()
sleep(1)
lives -= 1
screen.exitonclick()
__main__
部分只是测试代码,以确认 draw_lives()
以我们想要的方式工作——所以扔掉它。
像 lives_pen
这样的 Utility turtles 应该只创建 一次,而不是每次你需要更新计数器的时候,因为它们是全局实体并且在函数退出。
It's a bad practice to use global
in your code. 相反,您可以为您的海龟创建自定义属性。
超级简单,几乎没有什么不便:
from turtle import Turtle
pen = Turtle()
pen.lives = 5 # Here, the lives attribute is created
您甚至可以对字体执行相同的操作,尽管这可能是不必要的:
pen.font = ("Arial", 30, "normal")
如果失去生命是更新生命计数的唯一情况,请不要在循环中不断重写它们 (当然,除非有什么东西妨碍了文本,并且您希望文本显示在顶部), 只有在失去生命时才重写它。
我们可以重绘 和 在这样的函数中更新生命:
def update_lives():
pen.clear()
if pen.lives:
pen.write(f"Lives: {pen.lives}", font=pen.font)
else:
pen.write("You have lost all lives. Try again.", font=pen.font)
pen.lives -= 1 # Remove this line and this becomes your typical text updater
为了在实际中看到这一点,我实现了一个演示,其中每当用户按下 SPACE 栏时就会失去生命:
from turtle import Turtle, Screen
wn = Screen()
def update_lives():
pen.clear()
if pen.lives:
pen.write(f"Lives: {pen.lives}", font=pen.font)
else:
pen.write("You have lost all lives. Try again.", font=pen.font)
pen.lives -= 1
pen = Turtle(visible=False)
pen.penup()
pen.goto(-300, 200)
pen.lives = 5
pen.font = ("Arial", 30, "normal")
update_lives()
wn.listen()
wn.onkey(update_lives, 'space')
使用上面的代码,当用户到达 0
时,再次按下 SPACE 将使函数继续显示负值。
要解决这个问题,对于您的主游戏循环,请使用 while pen.lives
告诉 python 仅继续循环,直到剩余生命数大于 0
:
while pen.lives:
# Your game code here
wn.update()
输出: