o1-mini-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Write a python turtle snake game that can be played in a windows window with mouse.
- User
Write a python turtle snake game that can be played in a windows window with mouse.
- Assistant
Write a python turtle snake game that can be played in a windows window with mouse.
Sure! Below is a Python script that creates a simple Snake game using the turtle module. In this version, the snake follows the mouse cursor, allowing you to control its direction by moving the mouse within the game window.
turtle and random modules.import turtle
import time
import random
# Set up the screen
wn = turtle.Screen()
wn.title("Snake Game by ChatGPT")
wn.bgcolor("lightgreen")
wn.setup(width=600, height=600)
wn.tracer(0) # Turns off the screen updates
# Snake head
head = turtle.Turtle()
head.shape("square")
head.color("black")
head.penup()
head.goto(0, 0)
head.direction = "stop"
# Food
food = turtle.Turtle()
food.shape("circle")
food.color("red")
food.penup()
food.goto(0, 100)
segments = []
# Score
score = 0
delay = 0.1
# Pen for scoring
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("blue")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write(f"Score: {score}", align="center", font=("Courier", 24, "normal"))
# Function to move the snake towards the mouse
def follow_mouse(x, y):
head.setheading(head.towards(x, y))
# Keyboard bindings
wn.listen()
wn.onscreenclick(follow_mouse)
def move():
if head.direction == "stop":
return
# Move the head forward by 20 units
head.forward(20)
# Move the end segments first in reverse order
for index in range(len(segments)-1, 0, -1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x, y)
# Move segment 0 to where the head is
if len(segments) > 0:
segments[0].goto(head.xcor(), head.ycor())
# Schedule the next move
wn.update()
# Main game 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"
# Hide the segments
for segment in segments:
segment.goto(1000, 1000) # Move off-screen
# Clear the segments list
segments.clear()
# Reset the score
score = 0
pen.clear()
pen.write(f"Score: {score}", align="center", font=("Courier", 24, "normal"))
# Check for collision with food
if head.distance(food) < 20:
# Move the food to a random spot
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x, y)
# Add a segment
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("grey")
new_segment.penup()
segments.append(new_segment)
# Increase the score
score += 10
pen.clear()
pen.write(f"Score: {score}", align="center", font=("Courier", 24, "normal"))
# Move the snake
move()
# Check for collisions with the body
for segment in segments:
if segment.distance(head) < 20:
time.sleep(1)
head.goto(0, 0)
head.direction = "stop"
# Hide the segments
for segment in segments:
segment.goto(1000, 1000)
# Clear the segments list
segments.clear()
# Reset the score
score = 0
pen.clear()
pen.write(f"Score: {score}", align="center", font=("Courier", 24, "normal"))
time.sleep(delay)
Save the Code:
snake_game.py.Run the Script:
snake_game.py is saved.python snake_game.py
Play the Game:
Speed Adjustment:
delay variable to make the snake move faster or slower. A smaller value makes the snake move faster.Window Size:
wn.setup(width=600, height=600) to adjust the size of the game window.Colors and Shapes:
head.color(), food.color(), etc.Enjoy playing your Snake game!
Not yet included in a published release.