简体   繁体   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)

I need to do that when you catch all the falling letters of the word, write "End" and stop the game.当你抓住单词的所有下降字母时,我需要这样做,写下“结束”并停止游戏。 And if you catch the wrong letter (which is not in word) it should take one of the already caught ones from word.如果你抓住了错误的字母(不在单词中),它应该从单词中取出一个已经捕获的字母。 Ex.(word is vasara) if you have caught 'V' and than Y it should take away V. (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()

I've reworked your code below adding the two features you describe.我在下面重新编写了您的代码,添加了您描述的两个功能。 I've changed caught_letters (nee cached_letters ) from a list to a set to simplify the logic.我已将caught_letters (nee cached_letters ) 从一个list更改为一set以简化逻辑。 I've tossed your while True: , which has no place in an event-driven world like turtle, replacing it with an ontimer() event:我扔掉了你的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()

I rearranged the code to minimize what happens in the main loop (ie less drawing.) Cute game!我重新安排了代码以最小化主循环中发生的事情(即减少绘图)。可爱的游戏!

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

相关问题 您如何在python中编写正则表达式,以查找仅包含字母,数字和下划线的所有单词? - How do you write a regex in python that finds all word which contain only letters, numbers and underscore? 如果秘密词中的所有字母都在guessed_letters列表中,如何在hangman中结束游戏 - How to end game in hangman if ALL letters in the secret word are in guessed_letters list Python 字典宾果游戏。 需要帮助检查垂直胜利 - Python dictionary Bingo game. Need help checking for vertical win 使python写字母 - Make python write letters Python hang子手游戏。 Python 3 - Python hangman game. Python 3 我需要在python游戏中制作一个健康系统 - I need to make a health system in a python game Python-一个单词中包含多个字母的Hangman游戏 - Python - Hangman Game with multiple letters in one word 所以我在 python 上做游戏,我想知道,你如何让 python 检查你的输入是否与另一个变量相同? - so i was making a game on python and i want to know, how do you make python check if your input is the same as another variable? 是否可以在 Python 中为 USB 记忆棒编写代码,所以当您插入它时,它会从 PC 下载所有文件? - Is it possible to write a code in Python for a USB stick so when you plug it it downloaded all the files from the pc? Python 在字母和 * 之间添加空格到词尾 - Python adding spaces between letters and * to end of word
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM