微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

想要海龟在我按下显示字母的键后消失

如何解决想要海龟在我按下显示字母的键后消失

这是我的代码。我希望馅饼在我按下显示在上面的键后消失,但它们不动。

import turtle as trtl
import random as rand
import leaderboard as lb

#-----game configuration-----
# To view in trinket change the values of font_size,spot_size,and 
# screen_size by half
font_setup = ("Arial",20,"normal")
timer = 30
counter_interval = 1000 
timer_up = False
score = 0

#-----initialize the turtles-----

score_writer = trtl.Turtle()
score_writer.hideturtle()
score_writer.penup()
score_writer.goto(360,160) # x,y set to fit on smaller screen
score_writer.pendown()
#score_writer.showturtle()

counter =  trtl.Turtle()
counter.hideturtle()
counter.penup()
counter.goto(-160,y set to fit on smaller screen
counter.pendown()
#counter.showturtle()

#-----game functions-----

# countdown function
def countdown():
  global timer,timer_up
  counter.clear()
  if timer <= 0:
    counter.write("Time's Up",font=font_setup)
    timer_up = True

  else:
    counter.write("Timer: " + str(timer),font=font_setup)
    timer -= 1
    counter.getscreen().ontimer(countdown,counter_interval) 

# update and display the score
def update_score():
  global score
  score = score + 1
  score_writer.clear()
  score_writer.write(score,font=font_setup)

# what happens when the spot is clicked
def patty_tapped(x,y):
  global timer_up
  if (not timer_up):
    update_score()
  

#-----setup-----
patty_image = "patty.gif" # Store the file name of your shape

wn = trtl.Screen()
wn.setup(width=1.0,height=1.0)
wn.addshape(patty_image) # Make the screen aware of the new file
wn.bgpic("underwater.png")

letters = ['A','S','D','F','G','H','J','K','L'] #list of letters

pattyList = []
pattyLetters = []

for i in range(5): # range 5 = the amount of pattys on the grill
  pattyList.append(trtl.Turtle())
  pattyLetters.append(rand.choice(letters)) #TO MAKE RANDOM


#-----functions-----
# given a turtle,set that turtle to be shaped by the image file
def draw_patty(index):
  pattyList[index].pu()
  pattyList[index].shape(patty_image)
  wn.tracer(False)
  pattyList[index].setx(rand.randint(-275,275)) #reset patty pos.
  pattyList[index].sety(rand.randint(-90,0))  #reset patty pos.
  pattyList[index].sety(pattyList[index].ycor()-35)
  pattyList[index].color("white")
  pattyList[index].write(pattyLetters[index],align="center",font=("Arial",40,"bold"))
  pattyList[index].sety(pattyList[index].ycor()+35)
  pattyList[index].showturtle()
  wn.tracer(True)
  wn.update()

  
def drop_patty(index):
    pattyList[index].pu()
    pattyList[index].clear()
    pattyList[index].sety(-150)
    pattyList[index].hideturtle()
    pattyLetters[index] = rand.choice(letters)
    draw_patty(index)

    

def typedA():
  for i in range (5):
    if pattyLetters[i] == 'A':
      drop_patty(i)
      update_score()

def typedS():
  for i in range (5):
    if pattyLetters[i] == 'S':
      drop_patty(i)
      update_score()

def typedD():
  for i in range (5):
    if pattyLetters[i] == 'D':
      drop_patty(i)
      update_score()

def typedF():
  for i in range (5):
    if pattyLetters[i] == 'F':
      drop_patty(i)
      update_score()


def typedG():
  for i in range (5):
    if pattyLetters[i] == 'G':
      drop_patty(i)
      update_score()

def typedH():
  for i in range (5):
    if pattyLetters[i] == 'H':
      drop_patty(i)
      update_score()

def typedJ():
  for i in range (5):
    if pattyLetters[i] == 'J':
      drop_patty(i)
      update_score()

def typedK():
  for i in range (5):
    if pattyLetters[i] == 'K':
      drop_patty(i)
      update_score()

def typedL():
  for i in range (5):
    if pattyLetters[i] == 'L':
       drop_patty(i)
       update_score()
#-----function calls-----

for i in range(5):
  draw_patty(i)
wn.onkeypress(typedA,'a')
wn.onkeypress(typedS,'s')
wn.onkeypress(typedD,'d')
wn.onkeypress(typedF,'f')
wn.onkeypress(typedG,'g')
wn.onkeypress(typedH,'h')
wn.onkeypress(typedJ,'j')
wn.onkeypress(typedK,'k')
wn.onkeypress(typedL,'l')


  
# starting the game
def start_game():
  counter.getscreen().ontimer(countdown,counter_interval)


#----------events----------
start_game()
wn = trtl.Screen()
wn.mainloop()
wn.listen()

解决方法

这是一个问题:

wn.mainloop()
wn.listen()

调用 mainloop() 后,您将控制权移交给 tkinter 的事件循环,并且在此调用之后不会执行任何操作,直到乌龟关闭。在 listen() 之前执行 mainloop(),否则您的按键将被忽略。

这是对您的代码进行的修改,并进行了上述修复以及一些海龟和 Python 调整:

from turtle import Screen,Turtle
from random import choice,randint

#-----game configuration-----
# To view in trinket change the values of font_size,# spot_size,and screen_size by half
FONT_SETUP = ('Arial',20,'normal')

COUNTER_INTERVAL = 1000

LETTERS = ['A','S','D','F','G','H','J','K','L']  # list of letters

PATTIES = 5
PATTY_IMAGE = "patty.gif"  # Store the file name of your shape

# global variables
timer = 30
timer_up = False
score = 0

#-----game functions-----

# countdown function
def countdown():
    global timer,timer_up

    counter.clear()

    if timer <= 0:
        counter.write("Time's Up",font=FONT_SETUP)
        timer_up = True
    else:
        counter.write("Timer: " + str(timer),font=FONT_SETUP)
        timer -= 1
        screen.ontimer(countdown,COUNTER_INTERVAL)

# update and display the score
def update_score():
    global score

    score += 1
    score_writer.clear()
    score_writer.write(score,font=FONT_SETUP)

# starting the game
def start_game():
    screen.ontimer(countdown,COUNTER_INTERVAL)

#-----functions-----
# given a turtle,set that turtle to be shaped by the image file
def draw_patty(index):
    screen.tracer(False)

    pattyList[index].setx(randint(-275,275))  # reset patty position
    y = randint(-90,0)
    pattyList[index].sety(y - 35)
    pattyList[index].write(pattyLetters[index],align='center',font=('Arial',40,'bold'))
    pattyList[index].sety(y)
    pattyList[index].showturtle()

    screen.tracer(True)
    screen.update()

def drop_patty(index):
    pattyList[index].hideturtle()
    pattyList[index].clear()

    pattyLetters[index] = choice(LETTERS)

    draw_patty(index)

def typedLetter(letter):
    while letter in pattyLetters:
        drop_patty(pattyLetters.index(letter))
        update_score()

def typedA():
    typedLetter('A')

def typedS():
    typedLetter('S')

def typedD():
    typedLetter('D')

def typedF():
    typedLetter('F')

def typedG():
    typedLetter('G')

def typedH():
    typedLetter('H')

def typedJ():
    typedLetter('J')

def typedK():
    typedLetter('K')

def typedL():
    typedLetter('L')

screen = Screen()

#-----initialize the turtles-----

score_writer = Turtle()
score_writer.hideturtle()
score_writer.penup()
score_writer.goto(360,160)  # x,y set to fit on smaller screen

counter = Turtle()
counter.hideturtle()
counter.penup()
counter.goto(-160,y set to fit on smaller screen

#-----setup-----

screen.setup(width=1.0,height=1.0)
screen.addshape(PATTY_IMAGE)  # Make the screen aware of the new file
screen.bgpic("underwater.png")

pattyList = []
pattyLetters = []

for i in range(PATTIES):
    patty = Turtle()
    patty.penup()
    patty.shape(PATTY_IMAGE)
    patty.color('white')
    pattyList.append(patty)

    pattyLetters.append(choice(LETTERS))

    draw_patty(i)

#-----function calls-----

screen.onkeypress(typedA,'a')
screen.onkeypress(typedS,'s')
screen.onkeypress(typedD,'d')
screen.onkeypress(typedF,'f')
screen.onkeypress(typedG,'g')
screen.onkeypress(typedH,'h')
screen.onkeypress(typedJ,'j')
screen.onkeypress(typedK,'k')
screen.onkeypress(typedL,'l')
screen.listen()

#----------events----------

start_game()

screen.mainloop()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。