因此,我有一个11年级的项目,需要在此代码中添加一个部分,以便在类似于此的菜单中列出越过终点线的前3只海龟,但是我需要它说第一名,第二名,第三名,然后将相应的海龟移到那里。麻烦的是,我制作的新曲目(项目告诉我),但是在第一个乌龟越过后,乌龟就不会越过终点线,而且我不确定如何添加新菜单作为代码最初给予没有意义。
我当前拥有的代码如下;
#==========================================================
# Imports
#==========================================================
from PIL import Image, ImageTk
from turtle import *
import turtle
from random import randint
from turtle import Screen, Turtle
from math import sin, cos, atan2, pi
from random import randrange
racers = [] #defines what racers is
#==========================================================
# GAME
#==========================================================
# Creating the window
screen = turtle.Screen()
screen.setup(1225, 1000)
pil_img = Image.open("eightLane.gif") # Use PIL to open .jpg image.
tk_img = ImageTk.PhotoImage(pil_img) # Convert it into something tkinter can use.
canvas = turtle.getcanvas() # Get the tkinter Canvas of this TurtleScreen.
# Create a Canvas image object holding the tkinter image.
img_obj_id = canvas.create_image(0, 0, image=tk_img, anchor='center')
title("RACING TURTLES")
#==========================
# Creating the turtles
#==========================
#in future adjust position
LINEUP = [ # (color, (starting postion))
('red', (0, 90)), #creates each turtle and where it goes
('yellow', (-55, 120)),
('blue', (-120, 150)),
('green', (-195, 165)),
('dark goldenrod', (-270, 180)),
('blue violet', (-365, 170)),
('magenta', (-465, 140)),
('light slate gray', (-550, 100)),
]
for index, (color, position) in enumerate(LINEUP):
racer = Turtle('turtle', visible=False) #pen settings for placing the turtles initially
racer.setheading(180 + index * 10)
racer.speed('fastest')
racer.color(color)
racer.penup()
racer.setposition(position)
racer.showturtle() #shows the turtle now that it is in position
racers.append(racer)
DELTA = 0.4 # angle at which they move
def radii(index): # calculate concentric ellipse radii
return 265 + index * 44, 90 + index * 36
def race(): #how often it moves a turtle
"""
every 1/1000th of a second, pick a random
racer and move it forward a bit
"""
index = randrange(len(racers))
racer = racers[index]
# get angle from x, y; increase angle; compute new x, y
theta = atan2(racer.ycor(), racer.xcor()) + DELTA
a, b = radii(index)
x = a * cos(theta) #fancy maths
y = b * sin(theta)
racer.setheading(racer.towards(x, y)) #tells the racer where to face
racer.setposition(x, y) # moves the racer to the position
# check if a racer has crossed the finish line
if pi/2 < theta < pi/2 + DELTA/2: #if the racer has crossed the line run next line else skip
pass #someone one
else:
screen.ontimer(race, 100) #keeps the loop going
race()
screen.mainloop()
任何帮助或建议,将不胜感激。