如何在Python Turtle中覆盖和重写文本?

时间:2019-05-15 20:01:18

标签: python windows turtle-graphics

问题出在#num注释更改部分。应该在数字周围画一个绿色框,然后在该位置写一个新的。首先,我测试了#AP,它运行良好。因此,我添加了#HP,但是由于某种原因它不起作用,#AP也停止了。我没有收到错误消息,尽管查看了但没有看到问题。

import turtle
import winsound
import os
import time
#screen
wn = turtle.Screen()
wn.bgcolor("green")
wn.title("game")
hp=100
ap=10

def song1():
    winsound.PlaySound("sound1", winsound.SND_FILENAME)


#menu
time.sleep(2)
men_pen = turtle.Turtle()
men_pen.speed(0)
men_pen.color("black")
men_pen.penup()
men_pen.setposition(-1000,-200)
men_pen.pensize(4)
men_pen.pendown()
men_pen.fd(2500)

#menu text
men_pen.penup()
men_pen.setposition(-625,-265)
men_pen.pendown()
men_pen.write("HP=",True,font=('terminal',18, 'bold'))
men_pen.penup()
men_pen.setposition(-560,-265)
men_pen.pendown()
men_pen.write(hp ,font=('terminal',18,'bold'))
men_pen.penup()
men_pen.setposition(-625,-312)
men_pen.pendown()
men_pen.write("AP=",True,font=('terminal',18,'bold'))
men_pen.penup()
men_pen.setposition(-560,-312)
men_pen.write(ap ,font=('terminal',18,'bold'))
men_pen.penup()
men_pen.setposition(-499,-240)
men_pen.pendown()
men_pen.write("William" ,True,font=('terminal',20,'bold'))

#num changers
#AP
ap1= turtle.Turtle()
ap1.speed(0)
ap1.penup()
ap1.color("green")
ap1.penup()
ap1.setposition(-559,-311)
ap1.pendown()
def changeap():
    ap1.down()
    ap1.begin_fill()
    for i in range(4):
        ap1.penup()
        ap1.fd(36)
        ap1.left(90)
    ap1.color("green")
    ap1.end_fill()
    ap1.pendown()
    men_pen.penup()
    men_pen.setposition(-560,-312)

    men_pen.write(ap ,font=('terminal',18,'bold'))
    men_pen.penup()

#HP
hp2=turtle.Turtle()
hp2.speed(0)
hp2.penup()
hp2.color("green")
hp2.setposition(-561,-314)
hp2.pendown()

def changehp():
    hp2.down()
    hp2.begin_fill()
    for i in range(4):
        hp2.fd(50)
        hp2.left(90)
    hp2.color("green")
    hp2.end_fill()
    hp2.pendown()
    men_pen.penup()
    men_pen.setposition(-560,-312)
    men_pen.pendown()
    men_pen.write(ap , font=('Terminal',18,'bold'))




hp = 20
ap = 1000
time.sleep(5)
changeap()
time.sleep(5)
changehp()

1 个答案:

答案 0 :(得分:0)

有一种更简单的方法可以解决此问题。可以使用乌龟的.clear()(或.undo())方法来代替文本,而不用背景彩色矩形覆盖文本,然后再重新重写:

from turtle import Screen, Turtle
import time

FONT = ('terminal', 18, 'bold')

def changeap():
    ap_pen.clear()
    ap_pen.write(ap, font=FONT)

def changehp():
    hp_pen.clear()
    hp_pen.write(hp, font=FONT)

# screen
screen = Screen()
screen.setup(1300, 650)
screen.bgcolor("green")
screen.title("game")

hp = 100
ap = 10

# menu
men_pen = Turtle(visible=False)
men_pen.speed('fastest')
men_pen.pensize(4)

men_pen.penup()
men_pen.setposition(-650, -200)
men_pen.pendown()
men_pen.fd(1300)

# num changers
# AP
ap_pen = Turtle(visible=False)
ap_pen.speed('fastest')
ap_pen.penup()
ap_pen.setposition(-560, -312)

# HP
hp_pen = Turtle(visible=False)
hp_pen.speed('fastest')
hp_pen.penup()
hp_pen.setposition(-560, -265)

# menu text
men_pen.penup()
men_pen.setposition(-625, -265)
men_pen.write("HP=", True, font=FONT)

hp_pen.write(hp, font=FONT)

men_pen.setposition(-625, -312)
men_pen.write("AP=", True, font=FONT)

ap_pen.write(ap, font=FONT)

men_pen.setposition(-499, -240)
men_pen.write("William", True, font=FONT)

hp = 20
ap = 1000

time.sleep(5)
changeap()
time.sleep(5)
changehp()

screen.mainloop()