Turtle Game

Golden Turtle Game Python Code 2023

Here you find Golden Turtle Game Python Code or Python Turtle Game complete code then you need to just copy and paste it into the code editor, Hello my dear python game lovers Today in this article I want to share How to code a Golden Turtle game in Python with custom AI, So our challenge is to make 2 turtles, first one metal collector or turtle collector and 2nd our golden turtle including 100 accurate. Hi Buddy my name is Amol and I am the founder of amolblog.com. This is the Python turtle Turtle Game project Code If you have any other requests or project ideas then please share them with YouTube Channel.

Please note: If you want only the final code please jump to the Final code see below table of contents.

Step 1 import set up turtle and Screen

# Step 1 import set up turtle and Screen
import turtle
import time
import random
wn = turtle.Screen()
wn.title("Golden Turtle")
wn.bgcolor("lightgreen")
wn.setup(width=600, height=600)

Step 2 Create a metal collector

# Step 2 Create a metal collector
head = turtle.Turtle()
head.speed(0)
head.shape("circle")
head.color("black")
head.penup()
head.goto(0, 0)
head.direction = "stop"

Step 3 Create a golden turtle

# Step 4 Create a golden turtle
food = turtle.Turtle()
food.speed(0)
food.shape("turtle")
food.color("red")
food.penup()
food.goto(0, 100)

Step 4 Control collect speed direction

# Step 5 Control collect speed direction
def move():
    if head.direction == "up":
        y = head.ycor()
        head.sety(y + 2)
    if head.direction == "down":
        y = head.ycor()
        head.sety(y - 2)
    if head.direction == "left":
        x = head.xcor()
        head.setx(x - 2)
    if head.direction == "right":
        x = head.xcor()
        head.setx(x + 2)

def go_up():
    head.direction = "up"

def go_down():
    head.direction = "down"

def go_left():
    head.direction = "left"

def go_right():
    head.direction = "right"

Step 5 Control collect with key

# Step 6 Control collect with key
wn.listen()
wn.onkeypress(go_up, "Up")
wn.onkeypress(go_down, "Down")
wn.onkeypress(go_left, "Left")
wn.onkeypress(go_right, "Right")

Step 6 Start the game with a while loop

# Step 7 Start the game with a while loop
while True:
    wn.update()

    # Check for collision with the border
    if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
        time.sleep(1)
        head.goto(0, 0)
        head.direction = "stop"
    
    # Check for collision with the food
    if head.distance(food) < 20:
        x = random.randint(-290, 290)
        y = random.randint(-290, 290)
        food.goto(x, y)
    
    move()

wn.mainloop()

Final code

# Step 1 import set up turtle and Screen
import turtle
import time
import random
wn = turtle.Screen()
wn.title("Golden Turtle")
wn.bgcolor("lightgreen")
wn.setup(width=600, height=600)

# Step 2 Create a metal collector
head = turtle.Turtle()
head.speed(0)
head.shape("circle")
head.color("black")
head.penup()
head.goto(0, 0)
head.direction = "stop"

# Step 4 Create a golden turtle
food = turtle.Turtle()
food.speed(0)
food.shape("turtle")
food.color("red")
food.penup()
food.goto(0, 100)

# Step 5 Control collect speed direction
def move():
    if head.direction == "up":
        y = head.ycor()
        head.sety(y + 2)
    if head.direction == "down":
        y = head.ycor()
        head.sety(y - 2)
    if head.direction == "left":
        x = head.xcor()
        head.setx(x - 2)
    if head.direction == "right":
        x = head.xcor()
        head.setx(x + 2)

def go_up():
    head.direction = "up"

def go_down():
    head.direction = "down"

def go_left():
    head.direction = "left"

def go_right():
    head.direction = "right"

# Step 6 Control collect with key
wn.listen()
wn.onkeypress(go_up, "Up")
wn.onkeypress(go_down, "Down")
wn.onkeypress(go_left, "Left")
wn.onkeypress(go_right, "Right")

# Step 7 Start the game with a while loop
while True:
    wn.update()

    # Check for collision with the border
    if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
        time.sleep(1)
        head.goto(0, 0)
        head.direction = "stop"
    
    # Check for collision with the food
    if head.distance(food) < 20:
        x = random.randint(-290, 290)
        y = random.randint(-290, 290)
        food.goto(x, y)
    
    move()

wn.mainloop()

How to control a turtle with a key?

wn = turtle.Screen()
wn.listen()
wn.onkeypress(go_up, “Up”)
wn.onkeypress(go_down, “Down”)
wn.onkeypress(go_left, “Left”)
wn.onkeypress(go_right, “Right”)

How to use a while loop for the turtle game?

# Step 7 Start the game with a while loop
while True:
wn.update()

# Check for collision with the border
if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
time.sleep(1)
head.goto(0, 0)
head.direction = “stop”

# Check for collision with the food
if head.distance(food) < 20:
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x, y)

move()

wn.mainloop()

Another Python Turtle Games Project For You:

Scroll to Top
Ola State Wise Subsidy 2023 Basics of Python Interview Questions Valentine’s Day Python Special Code Evil Golden Turtle Python Game Pong Game In Python