调用killed()
函数后,屏幕变为白色并且什么都不做。我认为,由于mainloop()
调用,代码会再次执行所有操作,但它似乎并非如此。任何想法,我可以做些什么来解决这个问题将非常感激。 (我对图像没有任何问题。)代码:
from turtle import *
import os
import time
wn = Screen()
wn.bgcolor('black')
hp = 200
gold = 0
def m1():
os.chdir('C:/Users/Python/Projects/Images')
wn.register_shape("aaa3.gif")
wn.register_shape("chest.gif")
stoneGolem = Turtle()
stoneGolem.shape("aaa3.gif")
namewriter = Turtle()
namewriter.hideturtle()
namewriter.penup()
namewriter.color('white')
namewriter.goto(-130, -250)
fontname = ('Arial', 30, 'normal')
namewriter.write("Stone Golem", font=fontname)
writerhp = Turtle()
writerhp.hideturtle()
writerhp.penup()
writerhp.goto(-300, 250)
writerhp.color('white')
fonthp = ('Arial', 12, 'normal')
writerhp.write("Hp: " + str(hp), font=fonthp)
writergold = Turtle()
writergold.hideturtle()
writergold.penup()
writergold.goto(-200, 250)
writergold.color('white')
fontgold = ('Arial', 12, 'normal')
writergold.write("Gold: " + str(gold), font=fontgold)
def killed():
global gold
gold = gold + 50
wn.clear()
wn.bgcolor('black')
chest = Turtle()
chest.shape("chest.gif")
writer0 = Turtle()
writer0.hideturtle()
writer0.penup()
writer0.goto(-280, 180)
writer0.color('white')
font = ('Arial', 30, 'normal')
writer0.write('Stone Golem slayed! Gold + 50!', font=font)
time.sleep(3)
wn.clear()
def clickhp():
global hp
if hp > 1:
hp = hp - 1
writerhp.clear()
fonthp = ('Arial', 12, 'normal')
writerhp.write("Hp: " + str(hp), font=fonthp)
else:
killed()
hp = 200
wn.onkey(clickhp, 'Up')
wn.listen()
m1()
wn.mainloop()
答案 0 :(得分:0)
问题是您致电wn.clear()
中的killed()
。乌龟文档在这一点上令人困惑,但在屏幕单例上调用.clear()
的定义是:
从TurtleScreen中删除所有图纸和所有海龟。重置 现在空TurtleScreen到它的初始状态:白色背景,没有 背景图片,没有事件绑定和跟踪。
这就是为什么你的屏幕“只是变成白色而什么都不做”的原因。寻找一种更合适的方法来清理你的游戏。
我对您的代码提取进行了返工,试图让它更像您的预期工作:
from turtle import Turtle, Screen
# import os
import time
FONT_NAME = ('Arial', 30, 'normal')
FONT_HP = ('Arial', 12, 'normal')
FONT_GOLD = ('Arial', 12, 'normal')
def killed():
global gold
gold += 50
writergold.undo()
writergold.write('Gold: ' + str(gold), font=FONT_GOLD)
# chest = Turtle('chest.gif')
writer0.write('Stone Golem slayed! Gold + 50!', font=FONT_NAME)
time.sleep(3)
writer0.undo()
def clickhp():
global hp
if hp > 1:
hp -= 1
else:
killed()
hp = 200
writerhp.undo()
writerhp.write('Hp: ' + str(hp), font=FONT_HP)
wn = Screen()
wn.bgcolor('black')
hp = 200
gold = 0
# os.chdir('C:/Users/Python/Projects/Images')
# wn.register_shape('aaa3.gif')
# wn.register_shape('chest.gif')
# stoneGolem = Turtle('aaa3.gif')
namewriter = Turtle(visible=False)
namewriter.penup()
namewriter.color('white')
namewriter.goto(-130, -250)
namewriter.write('Stone Golem', font=FONT_NAME)
writerhp = Turtle(visible=False)
writerhp.penup()
writerhp.color('white')
writerhp.goto(-300, 250)
writerhp.write('Hp: ' + str(hp), font=FONT_HP)
writergold = Turtle(visible=False)
writergold.penup()
writergold.color('white')
writergold.goto(-200, 250)
writergold.write('Gold: ' + str(gold), font=FONT_GOLD)
writer0 = Turtle(visible=False)
writer0.penup()
writer0.color('white')
writer0.goto(-280, 180)
wn.onkey(clickhp, 'Up')
wn.listen()
wn.mainloop()