import random from turtle import * #GLOABL VARIABLE coins = 100 fans = [] racers = [] anchors = [] # <-- anchors added raceRunning = False restartText = None currentPrediction = None currentBet = 0 # Pens resultPen = Turtle() resultPen.hideturtle() resultPen.penup() racersPen = Turtle() racersPen.hideturtle() racersPen.penup() #TRAck def drawTrack(): screen = Screen() screen.bgcolor("lightgreen") pen = Turtle() pen.hideturtle() pen.speed(0) pen.penup() # GRANDSTANDS pen.color("gray") pen.begin_fill() pen.goto(-360, 180) pen.pendown() pen.goto(360, 180) pen.goto(360, 280) pen.goto(-360, 280) pen.goto(-360, 180) pen.end_fill() pen.penup() # LANE LINES pen.color("black") for i in range(6): pen.goto(-350, 40 * i - 20) pen.pendown() pen.forward(700) pen.penup() # FENCE pen.width(2) pen.goto(-350, -300) pen.pendown() pen.forward(700) pen.penup() for x in range(-350, 351, 50): pen.goto(x, -300) pen.pendown() pen.setheading(90) pen.forward(40) pen.penup() # FINISH LINE pen.width(4) pen.goto(300, -20) pen.pendown() pen.goto(300, 180) pen.penup() drawFans() drawRestartButton() #TURTLE RACERS def setup(): global racers, anchors names = ["Jeff", "fred", "alice", "xin", "max"] colors = ["red", "blue", "purple", "orange", "darkgreen"] racers = [] anchors = [] for i in range(5): # ANCHOR a = Turtle() a.hideturtle() a.penup() a.shape("square") a.shapesize(1.2, 1.2) a.color(colors[i]) a.goto(-320, i * 40) a.showturtle() anchors.append(a) # RACER t = Turtle() t.nickname = names[i] t.color(colors[i]) t.shape("turtle") t.penup() t.goto(-300, i * 40) racers.append(t) #RACE def race(): global raceRunning raceRunning = True updateRestartButton() stepRace() def stepRace(): global raceRunning if checkWin(): raceRunning = False updateRestartButton() displayResult(currentPrediction, currentBet) return for r in racers: r.forward(random.randint(10, 30)) Screen().ontimer(stepRace, 100) def checkWin(): for r in racers: if r.xcor() >= 300: return True return False #BETTING def displayRacers(): racersPen.clear() for i in range(5): racersPen.goto(-300, -40 * i - 50) racersPen.color(racers[i].color()[0]) # change the color racersPen.write(f"Turtle {i+1} : {racers[i].nickname}", font=("Arial", 14, "bold")) def getPrediction(): global coins displayRacers() prediction = textinput("Prediction", "Which racer will win?") bet = int(textinput("Bet", f"How many coins? (Max {coins})")) if bet > coins: bet = coins return prediction, bet def displayResult(prediction, bet): global coins resultPen.clear() resultPen.goto(50, -80) winIndex = 0 maxDist = -300 for i in range(5): if racers[i].xcor() > maxDist: maxDist = racers[i].xcor() winIndex = i winner = racers[winIndex].nickname resultPen.write(f"Coins: {coins}", font=("Arial", 25, "normal")) resultPen.sety(resultPen.ycor() - 40) resultPen.write(f"Winner: {winner}", font=("Arial", 25, "normal")) resultPen.sety(resultPen.ycor() - 40) resultPen.write(f"Your prediction: {prediction}", font=("Arial", 25, "normal")) resultPen.sety(resultPen.ycor() - 40) if winner.lower() == prediction.lower(): coins += bet * 2 resultPen.write(f"You WIN. Gained {bet} coins", font=("Arial", 25, "normal")) else: coins -= bet resultPen.write(f"You lose. Lost {bet} coins", font=("Arial", 25, "normal")) resultPen.sety(resultPen.ycor() - 20) resultPen.write(f"Coins remaining: {coins}", font=("Arial", 25, "normal")) #FAN def drawFans(): global fans fans = [] start_x = -340 y = 210 for row in range(2): for i in range(20): fan = Turtle() fan.hideturtle() fan.speed(0) fan.penup() fan.color("black", "black") x = start_x + i * 35 fan.goto(x, y + row * 25) fan.dot(16) fan.goto(x + 11, y - 14 + row * 25) fan.setheading(90) fan.pendown() fan.begin_fill() fan.circle(12, 180) fan.end_fill() fan.penup() fans.append(fan) #RESTART def restartGame(x=None, y=None): global currentPrediction, currentBet if raceRunning: return for i in range(5): racers[i].penup() racers[i].goto(-300, i * 40) currentPrediction, currentBet = getPrediction() race() def drawRestartButton(): global restartText button = Turtle() button.penup() button.goto(0, -260) button.shape("square") button.shapesize(2, 8) button.fillcolor("lightblue") button.showturtle() button.onclick(restartGame) restartText = Turtle() restartText.hideturtle() restartText.penup() restartText.goto(-45, -270) restartText.write("RESTART", font=("Arial", 16, "bold")) def updateRestartButton(): restartText.clear() if raceRunning: restartText.write("LOCKED", font=("Arial", 16, "bold")) else: restartText.write("RESTART", font=("Arial", 16, "bold")) #START SCREEN startPens = [] def startScreen(): screen = Screen() screen.bgcolor("black") title = Turtle() title.hideturtle() title.color("white") title.penup() title.goto(0, 80) title.write("TURTLE RACE", align="center", font=("Arial", 36, "bold")) subtitle = Turtle() subtitle.hideturtle() subtitle.color("gray") subtitle.penup() subtitle.goto(0, 20) subtitle.write("Click anywhere to start", align="center", font=("Arial", 18, "normal")) startPens.extend([title, subtitle]) screen.onclick(startGame) def clearStartScreen(): for pen in startPens: pen.clear() pen.hideturtle() startPens.clear() Screen().onclick(None) def startGame(x=None, y=None): clearStartScreen() Screen().bgcolor("lightgreen") drawTrack() setup() global currentPrediction, currentBet currentPrediction, currentBet = getPrediction() race() #MAIN startScreen() done() drawTrack() setup() currentPrediction, currentBet = getPrediction() race() done()