import random import turtle from turtle import * import time y = False start = False cash = 100 winCount = 0 winValue = 0 square = 20 pen = Turtle() pen.hideturtle() pen.speed(0) screen = Screen() color = ["red","blue"] label = ["Red", "Blue"] racers = [] names = ["John", "Jon", "Jonne"] # Asks to start a tutorial if the user wants to practice before playing. def instruction(): global y textinput("Game Console","Welcome! Enter 'return' to continue: ") x = textinput("Game Console","Enter 'Yes' to initalise the tutorial, if not, press 'return': ") print("Initalising ...") time.sleep(1) # AI: 'x.lower()' if x.lower() == "yes": y = True return y # Writes the tutorial label def tutorial2(): pen.hideturtle() pen.penup() pen.goto(-200,150) pen.color("white") pen.write("Tutorial",font=("Arial", 50, "normal")) # Draws turtles and calls track drawing def setup(): global racers racers = [] drawTrack() for i in range(3): t = Turtle() t.nickname = names[i] t.shape("turtle") t.penup() t.goto(-350, i*50 + 125) t.pendown() racers.append(t) # Draws the track. def drawTrack(): pen.hideturtle() pen.speed(0) # Background bgcolor("darkgreen") # White Panel 1 pen.penup() pen.goto(120, -200) pen.color("white") pen.begin_fill() pen.pendown() pen.goto(340, -200) pen.goto(340, 350) pen.goto(120, 350) pen.goto(120, -200) pen.end_fill() # White Panel 2 pen.penup() pen.goto(-350, -315) pen.begin_fill() pen.pendown() pen.goto(340, -315) pen.goto(340, -20) pen.goto(-350, -20) pen.goto(-350, -300) pen.end_fill() # Track Fill pen.penup() pen.goto(-350, 0) pen.color("gray") pen.begin_fill() pen.pendown() pen.goto(100, 0) pen.goto(100, 350) pen.goto(-350, 350) pen.goto(-350, 0) pen.end_fill() # Checker Tile color_toggle = True for y in range(0, 350, square): pen.penup() pen.goto(80, y + 20) pen.pendown() if color_toggle: pen.color("black", "black") else: pen.color("black", "white") pen.begin_fill() for _ in range(4): pen.forward(square) pen.right(90) pen.end_fill() color_toggle = not color_toggle # Displays racers nad ask the user for a prediction def getPrediction(): displayRacers() p = "" # AI: x not in [n.lower() for n in names] while p.lower() not in [n.lower() for n in names]: p = textinput("Racers Prediction", "Select your prediction of which racer will win.") return p # Creates a drawing of the turtles. def displayRacers(): pen.hideturtle() pen.speed(0) pen.color("black") for i in range(3): pen.penup() pen.goto(150, i * 50+100) pen.write(f"Turtle {i+1}: {racers[i].nickname}",font=("Arial", 20, "normal")) # The racer system def race(prediction): global winner winner = None while winner is None: for i in range(3): racers[i].fd(random.randint(0,1)) # ----- AI ----- if start == True: if racers[i].nickname.lower() == prediction.lower(): racers[i].fd(winValue) winner = checkWin() # Highlights the user selected turtle def highlight(prediction): # ----- AI ----- for t in racers: if t.nickname.lower() == prediction.lower(): t.color("blue") t.shapesize(2,2) break # ----- AI ----- # Makes sure the race is not neverending def checkWin(): for i in range(3): if racers[i].xcor() >= 100: # return the winningturtle return racers[i] return None # Writes win or lose def displayResult(prediction): global cash turtle_number = racers.index(winner) + 1 pen.up() pen.goto(-300, -225) pen.color("red") pen.write(f"Turtle {turtle_number} ({winner.nickname}) Won! x = {winner.xcor()}",font=("Arial",33,"bold")) pen.goto(-300, -275) # AI: x.capitalize() pen.write(f"Prediction: {prediction.capitalize()}", font=("Arial",33,"bold")) pen.up() pen.goto(-175, -200) # if the prediction was right: if winner.nickname.lower() == prediction.lower(): pen.color("green") pen.write("WIN", font=("Arial",150,"bold")) cash += 1000 else: pen.color("red") pen.write("LOSE", font=("Arial",150,"bold")) cash -= 50 time.sleep(1) clearscreen() # A minigame to give you an advantage during thr game. # AI: screen selection and other stuff to make it shorter def gamble(): global start, cash, winValue start = True pen.clear() screen.tracer(0) pen.up() pen.goto(-250, 275) pen.write("Welcome to the secret gambling minigame!", font=("Arial", 25, "bold")) while True: try: moneyChoice = int(textinput( "Credit Card", f"Available Cash: ${cash}\nEnter Cash:" )) if 0 < moneyChoice <= cash: break except: pass cash -= moneyChoice choice = {"color": None} def choose(c): choice["color"] = c circles = [] for i, c in enumerate(color): t = Turtle() t.shape("circle") t.shapesize(10) t.color(c) t.penup() t.goto(-200 + 390 * i, 0) t.onclick(lambda x, y, col=c: choose(col)) circles.append(t) while choice["color"] is None: screen.update() for t in circles: t.hideturtle() colorAnswer = random.choice(color) pen.clear() pen.color("green" if choice["color"] == colorAnswer else "red") pen.goto(-100, -50) pen.write("WIN" if choice["color"] == colorAnswer else "LOSE", font=("Arial", 100, "bold")) if choice["color"] == colorAnswer: winValue += 0.1 * moneyChoice cash += moneyChoice * 2 screen.tracer(1) print(f"Points: {winValue}") print(f"Credit Card: ${cash}") if cash > 0: tryAgain = textinput("Game Console","Try Again?") if tryAgain.lower() == "yes": pen.clear() gamble() elif tryAgain.lower() == "no": pen.clear() else: tryAgain = textinput("Game Console","Try Again?") time.sleep(1) clearscreen() # The game if the user didn't do the tutorial def submain(): textinput("Game Console","The amount of money entered, accounts to your total points.") gamble() setup() prediction = getPrediction() highlight(prediction) race(prediction) displayResult(prediction) # AI: clearscreen() clearscreen() # The function is just the tutorial part of the game def tutorial(): setup() tutorial2() prediction = getPrediction() highlight(prediction) race(prediction) displayResult(prediction) clearscreen() def main(): y = instruction() # Game if tutorial was activated if y == True: tutorial() x = textinput("Game Console","Try Again? Enter 'Yes' or 'No': ") while x.lower() == "yes" and cash > 0: bgcolor("white") submain() while x.lower() == "yes" or "no": x = textinput("Game Console","Try Again? Enter 'Yes' or 'No': ") # Game if tutorial wasn't activated if y == False: submain() x = textinput("Game Console","Try Again? Enter 'Yes' or 'No': ") while x.lower() == "yes" and cash > 0: submain() while x.lower() == "yes" or "no": x = textinput("Game Console","Try Again? Enter 'Yes' or 'No': ") main()