繁体   English   中英

我需要这样做,以便当你抓住单词的所有字母时,写下“结束”并停止游戏。(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()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM