我需要这样做,以便当您抓住单词的所有字母时,写"End"并停止游戏。(蟒蛇)



我需要在你抓住所有单词的下落字母时这样做,写";以何种;然后停止游戏。如果你捕捉到错误的字母(不在word中),它应该从word中捕捉到一个。例如(单词是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(neecached_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()

我重新安排了代码,以尽量减少主循环中发生的事情(即减少绘图)。可爱的游戏!

最新更新