一遍又一遍地擦除和重写海龟图形中同一位置的文字和卡号?
Erase and rewrite over and over again the text and card number in the same location in turtle graphics?
集中游戏要求用户能够将卡片翻过来多次,然后才能将其与匹配的卡片正确匹配。这不是一直有效吗?它在第一次翻牌时和之后立即工作,但如果这张牌被跳过一个回合而不是之后是未定义的行为。
import turtle
from turtle import *
import tkinter.font
from tkinter import *
import time
t = turtle.Turtle()
screen = Screen()
screen.setup(width=800, height=800, startx=10, starty=10)
eraseble = []
''' this function allows the square to be erased and rewriten '''
def erasableWrite(tortoise, name, font, align, reuse=None):
eraser = turtle.Turtle() if reuse is None else reuse
eraser.hideturtle()
eraser.up()
eraser.setposition(tortoise.position())
eraser.write(name, font=font, align=align)
print('eraser = {} name = {}.'.format(eraser, name))
return eraser
startX = -280
startY = -295
textValue = [('1', 'a'), ('2', 'b'), ('3', 'c')]
t.hideturtle()
turtle.speed(0)
t.penup()
counter = 0
count = 0
for n in range(3):
t.goto(startX, startY)
t.pendown()
eraseble.append(erasableWrite(t, textValue[n][0], font=("New Times Roman", 18, "normal"), align="center"))
print('count = {}, eraseble[counter] = {} counter = {} n = {} textvalue = {}. '.format(count, eraseble[counter], counter, n, textValue[n][0]))
time.sleep(2)
count += 1
eraseble[counter].clear()
eraseble.append(erasableWrite(t, textValue[n][1], font=("New Times Roman", 18, "normal"), align="center"))
time.sleep(2)
eraseble[counter + 1].clear()
print('eraseble[counter] = {} counter = {} n = {} textvalue = {}. '.format(eraseble[counter], counter, n, textValue[n][1]))
eraseble[counter -1] = (erasableWrite(t, textValue[n][0], font=("New Times Roman", 18, "normal"), align="center"))
count +=1
startX +=40
startY += 20
t.penup()
counter +=2
` replace 1 with a `
startX = -280
startY = -295
t.goto(startX, startY)
print('count = {}, eraseble[0] = {}'.format(count, eraseble[0]))
count +=1
eraseble[0].clear()
textVal = textValue[0][0]
eraseble[0] = (erasableWrite(t, textVal, font=("New Times Roman", 18, "normal"), align="center"))
print('count = {}, eraseble[0] = {}'.format(count, eraseble[0]))
count +=1
eraseble[0].clear()
textVal = textValue[0][1]
eraseble[1] = (erasableWrite(t, textVal, font=("New Times Roman", 18, "normal"), align="center"))
time.sleep(2)
print('\n\neraseble = {} !...!'.format(eraseble))
您似乎混淆了 return 值和 erasableWrite
的 reuse
参数是如何工作的。我在下面重写了您的示例代码(稍微夸大了字体和距离):
from turtle import Screen, Turtle
import time
FONT = ("New Times Roman", 24, "bold")
def erasableWrite(tortoise, name, font, align, reuse=None):
eraser = Turtle() if reuse is None else reuse
eraser.hideturtle()
eraser.up()
eraser.setposition(tortoise.position())
eraser.write(name, font=font, align=align)
return eraser
screen = Screen()
screen.setup(width=800, height=800, startx=10, starty=10)
turtle = Turtle()
turtle.hideturtle()
turtle.penup()
erasable = []
startX = -280
startY = -295
textValue = [('1', 'a'), ('2', 'b'), ('3', 'c')]
for n in range(3):
turtle.goto(startX, startY)
erasable.append(erasableWrite(turtle, textValue[n][0], font=FONT, align="center"))
time.sleep(2)
erasable[n].clear()
erasableWrite(turtle, textValue[n][1], font=FONT, align="center", reuse=erasable[n])
time.sleep(2)
erasable[n].clear()
erasableWrite(turtle, textValue[n][0], font=FONT, align="center", reuse=erasable[n])
startX += 80
startY += 40
time.sleep(2)
startX = -280
startY = -295
turtle.goto(startX, startY)
erasable[0].clear()
textVal = textValue[0][1]
erasableWrite(turtle, textVal, font=FONT, align="center", reuse=erasable[0])
time.sleep(2)
erasable[0].clear()
textVal = textValue[0][0]
erasableWrite(turtle, textVal, font=FONT, align="center", reuse=erasable[0])
time.sleep(2)
screen.mainloop()
显示三张"cards"和它们的值,然后返回并在覆盖之前再次暴露第一张卡的值。
集中游戏要求用户能够将卡片翻过来多次,然后才能将其与匹配的卡片正确匹配。这不是一直有效吗?它在第一次翻牌时和之后立即工作,但如果这张牌被跳过一个回合而不是之后是未定义的行为。
import turtle
from turtle import *
import tkinter.font
from tkinter import *
import time
t = turtle.Turtle()
screen = Screen()
screen.setup(width=800, height=800, startx=10, starty=10)
eraseble = []
''' this function allows the square to be erased and rewriten '''
def erasableWrite(tortoise, name, font, align, reuse=None):
eraser = turtle.Turtle() if reuse is None else reuse
eraser.hideturtle()
eraser.up()
eraser.setposition(tortoise.position())
eraser.write(name, font=font, align=align)
print('eraser = {} name = {}.'.format(eraser, name))
return eraser
startX = -280
startY = -295
textValue = [('1', 'a'), ('2', 'b'), ('3', 'c')]
t.hideturtle()
turtle.speed(0)
t.penup()
counter = 0
count = 0
for n in range(3):
t.goto(startX, startY)
t.pendown()
eraseble.append(erasableWrite(t, textValue[n][0], font=("New Times Roman", 18, "normal"), align="center"))
print('count = {}, eraseble[counter] = {} counter = {} n = {} textvalue = {}. '.format(count, eraseble[counter], counter, n, textValue[n][0]))
time.sleep(2)
count += 1
eraseble[counter].clear()
eraseble.append(erasableWrite(t, textValue[n][1], font=("New Times Roman", 18, "normal"), align="center"))
time.sleep(2)
eraseble[counter + 1].clear()
print('eraseble[counter] = {} counter = {} n = {} textvalue = {}. '.format(eraseble[counter], counter, n, textValue[n][1]))
eraseble[counter -1] = (erasableWrite(t, textValue[n][0], font=("New Times Roman", 18, "normal"), align="center"))
count +=1
startX +=40
startY += 20
t.penup()
counter +=2
` replace 1 with a `
startX = -280
startY = -295
t.goto(startX, startY)
print('count = {}, eraseble[0] = {}'.format(count, eraseble[0]))
count +=1
eraseble[0].clear()
textVal = textValue[0][0]
eraseble[0] = (erasableWrite(t, textVal, font=("New Times Roman", 18, "normal"), align="center"))
print('count = {}, eraseble[0] = {}'.format(count, eraseble[0]))
count +=1
eraseble[0].clear()
textVal = textValue[0][1]
eraseble[1] = (erasableWrite(t, textVal, font=("New Times Roman", 18, "normal"), align="center"))
time.sleep(2)
print('\n\neraseble = {} !...!'.format(eraseble))
您似乎混淆了 return 值和 erasableWrite
的 reuse
参数是如何工作的。我在下面重写了您的示例代码(稍微夸大了字体和距离):
from turtle import Screen, Turtle
import time
FONT = ("New Times Roman", 24, "bold")
def erasableWrite(tortoise, name, font, align, reuse=None):
eraser = Turtle() if reuse is None else reuse
eraser.hideturtle()
eraser.up()
eraser.setposition(tortoise.position())
eraser.write(name, font=font, align=align)
return eraser
screen = Screen()
screen.setup(width=800, height=800, startx=10, starty=10)
turtle = Turtle()
turtle.hideturtle()
turtle.penup()
erasable = []
startX = -280
startY = -295
textValue = [('1', 'a'), ('2', 'b'), ('3', 'c')]
for n in range(3):
turtle.goto(startX, startY)
erasable.append(erasableWrite(turtle, textValue[n][0], font=FONT, align="center"))
time.sleep(2)
erasable[n].clear()
erasableWrite(turtle, textValue[n][1], font=FONT, align="center", reuse=erasable[n])
time.sleep(2)
erasable[n].clear()
erasableWrite(turtle, textValue[n][0], font=FONT, align="center", reuse=erasable[n])
startX += 80
startY += 40
time.sleep(2)
startX = -280
startY = -295
turtle.goto(startX, startY)
erasable[0].clear()
textVal = textValue[0][1]
erasableWrite(turtle, textVal, font=FONT, align="center", reuse=erasable[0])
time.sleep(2)
erasable[0].clear()
textVal = textValue[0][0]
erasableWrite(turtle, textVal, font=FONT, align="center", reuse=erasable[0])
time.sleep(2)
screen.mainloop()
显示三张"cards"和它们的值,然后返回并在覆盖之前再次暴露第一张卡的值。