#import random import random #import turtle from turtle import * #Mr. BW, I tried to add music, it does not work..... import os #import time import time #--------------------------- #Make a finsih line function Like a Mario Kart VIBE!! def makeFinishLine(): t = Turtle() t.hideturtle() t.speed(0) block_size = 20 x = 300 for y in range(-280, 280, block_size): t.penup() t.goto(x, y) if (y // block_size) % 2 == 0: t.color("black") else: t.color("white") t.begin_fill() for _ in range(4): t.forward(block_size) t.right(90) t.end_fill() t.penup() t.goto(310, 300) t.color("black") t.write("FINISH", align="center", font=("Arial", 18, "bold")) #--------------------------- #Make a start line function def makeStartLine(): startLine = Turtle() startLine.speed(0) startLine.color("#2c3e50") startLine.pensize(4) startLine.penup() startLine.goto(-300, 280) startLine.pendown() startLine.goto(-300,-300) startLine.penup() startLine.goto(-300,300) startLine.write("START", align="center", font=("Arial", 16, "bold")) startLine.hideturtle() #--------------------------- #Make a track for distinguishing the Turtles def makeTrack(): x = Turtle() x.hideturtle() x.speed(0) # Road background x.color("#bdc3c7") x.penup() x.goto(-300, -40) x.begin_fill() for _ in range(2): x.forward(600) x.left(90) x.forward(280) x.left(90) x.end_fill() # Lane lines x.color("white") x.pensize(2) for i in range(6): y = i * 40 x.penup() x.goto(-300, y) x.pendown() x.goto(300, y) #--------------------------- #Add all these in ONE function def drawTrack(): makeStartLine() makeFinishLine() makeTrack() #--------------------------- #Banana for entertainment and chaos! def createBananas(): global bananas bananas = [] for i in range(4): b = Turtle() b.speed(0) b.shape("circle") b.shapesize(0.7, 0.7) b.color("gold") b.penup() x = random.randint(-150, 200) y = random.choice([0,40,80,120,160,200]) b.goto(x,y) bananas.append(b) #--------------------------- #Boosters for entertainment and speed! def createBoosters(): global boosters boosters = [] for i in range(4): shoes = Turtle() shoes.speed(0) shoes.shape("classic") shoes.shapesize(0.8, 0.8) shoes.color("navy") shoes.penup() x = random.randint(-150, 200) y = random.choice([0,40,80,120,160,200]) shoes.goto(x,y) boosters.append(shoes) #--------------------------- #Set up a screen for the game def setUpScreen(): screen = Screen() screen.bgcolor("#7ec850") screen.title("Turtle Race Track") screen.setup(width=1000, height= 800) #--------------------------- def setup(): #Call the setUpScreen function setUpScreen() #call the drawTrack function drawTrack() #create a list of 5 Turtles and move them into start position global racers, nameLabels names = ["Jeff", "Fred", "Alice", "Xin", "Max", "Brad"] #ADD color to distinguish the turtles colors = ["red", "green", "blue", "purple", "orange", "black"] racers = [] nameLabels = [] for i in range(6): racers.append(Turtle()) #ADD the color! racers[i].color(colors[i]) for i in range(6): racers[i].nickname = names[i] racers[i].shape("turtle") racers[i].penup() racers[i].speed(0) racers[i].goto(-300, i*40) label = Turtle() label.speed(0) label.hideturtle() label.penup() label.color(colors[i]) label.goto(racers[i].xcor(), racers[i].ycor() +20) label.write(racers[i].nickname, align="center", font=("Arial", 10, "bold")) nameLabels.append(label) #--------------------------- #Name on the top of the turtle def updateNameLabels(): for i in range(6): label = nameLabels[i] racer = racers[i] label.clear() label.goto(racer.xcor(), racer.ycor() + 20) label.write(racer.nickname, align="center", font=("Arial", 10, "bold")) #--------------------------- # get prediction and return it def getPrediction(): displayRacers() p = textinput("Racers Prediction", "Which racer will win?") racerListPen.clear() return p #--------------------------- #display the racers so that the player can identify which turtle could win def displayRacers(): global racerListPen racerListPen = Turtle() racerListPen.penup() for i in range(6): racerListPen.goto(330, 150 - i * 40) racerListPen.write(f"Turtle{i+1} : {racers[i].nickname}") racerListPen.hideturtle() #--------------------------- #countdown for the excitement! def countdown(): pen = Turtle() pen.hideturtle() pen.color("black") pen.penup() pen.goto(0, 0) sizes = [40,60,80, 100] countDown = [3,2,1,"GO!"] os.system("afplay mario-kart-8-countdown.mp3") #It does not work..... for i in range(4): pen.clear() pen.write(countDown[i], align="center", font=("Arial", sizes[i], "bold")) os.system("afplay ./mario-kart-8-countdown.mp3") time.sleep(1) #this is why I need to put "import time" at the top pen.clear() #--------------------------- def race(): #move all Turtles while they haven't reached the finish line win = checkwin() while win == False: for i in range(6): racers[i].forward(random.randint(10, 30)) for banana in bananas: if racers[i].distance(banana) < 20: racers[i].backward(40) banana.hideturtle() bananas.remove(banana) for booster in boosters: if racers[i].distance(booster) < 20: racers[i].forward(40) booster.hideturtle() boosters.remove(booster) updateNameLabels() #did anyone win yet? win = checkwin() #--------------------------- def checkwin(): for i in range(6): if racers[i].xcor() >= 300: return True return False #--------------------------- def displayWinner(prediction): global winIndex, winnerdraw #But why I need to Gobal winIndex??? #Call out the displayturtle winner() #display the name of the winning Turtle maxDistance = 300 for i in range(6): if racers[i].xcor() > maxDistance: maxDistance = racers[i].xcor() winIndex = racers[i].nickname y = -180 winnerdraw.goto(0, y) winnerWrite(f"🏁 Winner: {winIndex}", size=28, bold = True) winnerdraw.goto(0, y - 40) winnerWrite(f"Distance: {int(maxDistance)}") winnerdraw.goto(0, y - 80) winnerWrite(f"Your prediction: {prediction}") winnerdraw.goto(0, y - 130) if winIndex.lower() == prediction.lower(): winnerdraw.color("#2E8B57") winnerWrite("You Win! 🎉", bold = True) else: winnerdraw.color("#555555") winnerWrite("YOU LOSE....") #--------------------------- def winner(): global winnerdraw winnerdraw = Turtle() winnerdraw.speed(0) winnerdraw.color("#333333") winnerdraw.penup() winnerdraw.goto(-100, -200) winnerdraw.hideturtle() #--------------------------- def winnerWrite(text, size=22, bold=False): style = "bold" if bold else "normal" winnerdraw.write(text, align="center", font=("Arial", size, style)) #--------------------------- def main(): setup() prediction = getPrediction() #Create bananas and Boosters AFTER THE PREDICTION TO FOOL THE PLAYERS!!! createBananas() createBoosters() countdown() race() displayWinner(prediction) #--------------------------- main() #---------------------------