我需要做到这一点,以便当你抓住单词的所有字母时,写 "End" 并停止游戏。(Python)
I need to make it so that when you catch all the letters of the word, write "End" and stop the game.(Python)
当你抓住单词的所有下降字母时,我需要这样做,
写“结束”并停止游戏。如果你抓错了字母(不在单词中),它应该从单词中取出一个已经抓到的字母。例如(单词是 vasara)如果你抓住了 'V' 而不是 Y 它应该带走 V. (Python)
from turtle import *
from random import *
ABC = u'ABCDEFGHIJKLMOPRSTVZ'
word = u"vasara".upper()
chached_letters = []
la = 1
P = Turtle()
P.hideturtle()
def info():
hideturtle()
speed(0); penup(); color("grey")
goto(-200, -180); pendown(); forward(400); penup()
goto(-180, -200)
for letter in word:
if letter in chached_letters:
color('blue')
else:
color('grey')
write( letter, font=("Arial", 18))
forward(20)
info()
screen = getscreen()
drops = []
for k in range(10):
new = Turtle()
drops.append(new)
new.speed(0)
new.penup()
x = -200 + k*35
y = randint(100, 200)
new.goto(x, y)
new.color('blue')
new.setheading(-90)
new.hideturtle()
new.step = randint(2, 6)
new.letter = choice( ABC )
new.write( new.letter, font = ("Arial", 18) )
def spust(x, y):
for drop in drops:
Lx = drop.xcor()
Ly = drop.ycor()
if Lx < x < Lx+20 and Ly < y < Ly+20:
chached_letters.append( drop.letter )
drop.sety(200)
drop.letter = choice( ABC )
info()
screen.onclick(spust)
screen.tracer(0)
while True:
for drop in drops:
drop.forward( drop.step )
drop.clear()
drop.write( drop.letter, font = ("Arial", 18) )
if drop.ycor() < -180:
drop.sety(200)
drop.letter = choice (ABC)
screen.update()
我在下面修改了您的代码,添加了您描述的两个功能。我已将 caught_letters
(nee cached_letters
)从 list
更改为 set
以简化逻辑。我已经把你的 while True:
扔掉了,它在像乌龟这样的事件驱动的世界中没有立足之地,用 ontimer()
事件代替它:
from turtle import Screen, Turtle
from random import choice, randint
from string import ascii_uppercase as ABC
FONT = ("Arial", 18, 'normal')
BIGFONT = ("Arial", 36, 'bold')
word = "vasara".upper()
word_letters = set(word)
def info():
turtle.goto(-180, -200)
for letter in word:
turtle.color('blue' if letter in caught_letters else 'grey')
turtle.write(letter, font=FONT)
turtle.forward(20)
def spust(x, y):
screen.onclick(None) # disable handler inside handler
for drop in drops:
if drop.distance(x, y) < 20:
if drop.letter in word:
caught_letters.add(drop.letter)
elif caught_letters:
# if you catch a wrong letter (which is not in word)
# it should take one of the already caught ones from word
caught_letters.pop()
drop.letter = None
break
info()
screen.onclick(spust) # reenable handler
def running():
for drop in drops:
drop.forward(drop.step)
if drop.letter is None or drop.ycor() < -180:
drop.sety(200)
drop.letter = choice(ABC)
drop.clear()
drop.write(drop.letter, font=FONT)
if word_letters == caught_letters:
# when you catch all the falling letters of
# the word, write "End" and stop the game.
screen.onclick(None)
marker.home()
marker.write("End", align='center', font=BIGFONT)
else:
screen.ontimer(running, 75) # milliseconds
screen.update()
screen = Screen()
screen.tracer(0)
marker = Turtle()
marker.hideturtle()
marker.speed('fastest')
marker.color("grey")
marker.penup()
marker.goto(-200, -180)
marker.pendown()
marker.forward(400)
marker.penup()
turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')
turtle.penup()
caught_letters = set()
info()
drops = []
for k in range(10):
new = Turtle()
new.hideturtle()
new.speed('fastest')
new.color('blue')
new.setheading(-90)
new.penup()
x = -200 + k*35
y = randint(100, 200)
new.goto(x, y)
new.step = randint(3, 6)
new.letter = choice(ABC)
new.write(new.letter, font=FONT)
drops.append(new)
screen.onclick(spust)
running()
screen.mainloop()
我重新安排了代码以尽量减少主循环中发生的事情(即减少绘图。)可爱的游戏!
当你抓住单词的所有下降字母时,我需要这样做, 写“结束”并停止游戏。如果你抓错了字母(不在单词中),它应该从单词中取出一个已经抓到的字母。例如(单词是 vasara)如果你抓住了 'V' 而不是 Y 它应该带走 V. (Python)
from turtle import *
from random import *
ABC = u'ABCDEFGHIJKLMOPRSTVZ'
word = u"vasara".upper()
chached_letters = []
la = 1
P = Turtle()
P.hideturtle()
def info():
hideturtle()
speed(0); penup(); color("grey")
goto(-200, -180); pendown(); forward(400); penup()
goto(-180, -200)
for letter in word:
if letter in chached_letters:
color('blue')
else:
color('grey')
write( letter, font=("Arial", 18))
forward(20)
info()
screen = getscreen()
drops = []
for k in range(10):
new = Turtle()
drops.append(new)
new.speed(0)
new.penup()
x = -200 + k*35
y = randint(100, 200)
new.goto(x, y)
new.color('blue')
new.setheading(-90)
new.hideturtle()
new.step = randint(2, 6)
new.letter = choice( ABC )
new.write( new.letter, font = ("Arial", 18) )
def spust(x, y):
for drop in drops:
Lx = drop.xcor()
Ly = drop.ycor()
if Lx < x < Lx+20 and Ly < y < Ly+20:
chached_letters.append( drop.letter )
drop.sety(200)
drop.letter = choice( ABC )
info()
screen.onclick(spust)
screen.tracer(0)
while True:
for drop in drops:
drop.forward( drop.step )
drop.clear()
drop.write( drop.letter, font = ("Arial", 18) )
if drop.ycor() < -180:
drop.sety(200)
drop.letter = choice (ABC)
screen.update()
我在下面修改了您的代码,添加了您描述的两个功能。我已将 caught_letters
(nee cached_letters
)从 list
更改为 set
以简化逻辑。我已经把你的 while True:
扔掉了,它在像乌龟这样的事件驱动的世界中没有立足之地,用 ontimer()
事件代替它:
from turtle import Screen, Turtle
from random import choice, randint
from string import ascii_uppercase as ABC
FONT = ("Arial", 18, 'normal')
BIGFONT = ("Arial", 36, 'bold')
word = "vasara".upper()
word_letters = set(word)
def info():
turtle.goto(-180, -200)
for letter in word:
turtle.color('blue' if letter in caught_letters else 'grey')
turtle.write(letter, font=FONT)
turtle.forward(20)
def spust(x, y):
screen.onclick(None) # disable handler inside handler
for drop in drops:
if drop.distance(x, y) < 20:
if drop.letter in word:
caught_letters.add(drop.letter)
elif caught_letters:
# if you catch a wrong letter (which is not in word)
# it should take one of the already caught ones from word
caught_letters.pop()
drop.letter = None
break
info()
screen.onclick(spust) # reenable handler
def running():
for drop in drops:
drop.forward(drop.step)
if drop.letter is None or drop.ycor() < -180:
drop.sety(200)
drop.letter = choice(ABC)
drop.clear()
drop.write(drop.letter, font=FONT)
if word_letters == caught_letters:
# when you catch all the falling letters of
# the word, write "End" and stop the game.
screen.onclick(None)
marker.home()
marker.write("End", align='center', font=BIGFONT)
else:
screen.ontimer(running, 75) # milliseconds
screen.update()
screen = Screen()
screen.tracer(0)
marker = Turtle()
marker.hideturtle()
marker.speed('fastest')
marker.color("grey")
marker.penup()
marker.goto(-200, -180)
marker.pendown()
marker.forward(400)
marker.penup()
turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')
turtle.penup()
caught_letters = set()
info()
drops = []
for k in range(10):
new = Turtle()
new.hideturtle()
new.speed('fastest')
new.color('blue')
new.setheading(-90)
new.penup()
x = -200 + k*35
y = randint(100, 200)
new.goto(x, y)
new.step = randint(3, 6)
new.letter = choice(ABC)
new.write(new.letter, font=FONT)
drops.append(new)
screen.onclick(spust)
running()
screen.mainloop()
我重新安排了代码以尽量减少主循环中发生的事情(即减少绘图。)可爱的游戏!