Sure! Here's a fun and creative game called "Turtle Snake Game" written in Python. It's a classic Snake game where you control a snake to eat food, grow longer, and avoid crashing into walls or yourself. The game is implemented using the turtle module, and the entire code is contained within a single file.
python
import turtle
import time
import random
# Set up the screen
wn = turtle.Screen()
wn.title("Turtle Snake Game")
wn.bgcolor("lightblue")
wn.setup(width=600, height=600)
wn.tracer(0) # Turns off automatic screen updates
# Snake head
head = turtle.Turtle()
head.shape("square")
head.color("darkgreen")
head.penup()
head.goto(0, 0)
head.direction = "stop"
# Snake food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("magenta")
food.penup()
food.goto(0, 100)
segments = []
# Score management
score = 0
high_score = 0
# Display the score
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("darkblue")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Score: 0 High Score: 0", align="center", font=("Courier", 24, "bold"))
# Functions to control the snake
def go_up():
if head.direction != "down":
head.direction = "up"
head.setheading(90)
def go_down():
if head.direction != "up":
head.direction = "down"
head.setheading(270)
def go_left():
if head.direction != "right":
head.direction = "left"
head.setheading(180)
def go_right():
if head.direction != "left":
head.direction = "right"
head.setheading(0)
def move():
if head.direction != "stop":
head.forward(20)
# Keyboard bindings
wn.listen()
wn.onkeypress(go_up, "Up")
wn.onkeypress(go_down, "Down")
wn.onkeypress(go_left, "Left")
wn.onkeypress(go_right, "Right")
# Main game loop
delay = 0.1
while True:
wn.update()
# Check collision with borders
if abs(head.xcor()) > 290 or abs(head.ycor()) > 290:
time.sleep(1)
head.goto(0, 0)
head.direction = "stop"
# Hide the segments
for segment in segments:
segment.hideturtle()
segments.clear()
# Reset the score
score = 0
delay = 0.1
pen.clear()
pen.write(f"Score: {score} High Score: {high_score}", align="center", font=("Courier", 24, "bold"))
# Check collision with food
if head.distance(food) < 20:
# Move the food to a random spot
x = random.randint(-280, 280)
y = random.randint(-280, 280)
food.goto(x, y)
# Add a new segment to the snake
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("green")
new_segment.penup()
segments.append(new_segment)
# Shorten the delay to increase difficulty
delay = max(0.05, delay - 0.005)
# Update the score
score += 10
if score > high_score:
high_score = score
pen.clear()
pen.write(f"Score: {score} High Score: {high_score}", align="center", font=("Courier", 24, "bold"))
# Move the end segments first in reverse order
for index in range(len(segments) - 1, 0, -1):
segments[index].goto(segments[index - 1].pos())
# Move the first segment to where the head was
if segments:
segments[0].goto(head.pos())
move()
# Check for collision with self
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.hideturtle()
segments.clear()
# Reset the score
score = 0
delay = 0.1
pen.clear()
pen.write(f"Score: {score} High Score: {high_score}", align="center", font=("Courier", 24, "bold"))
time.sleep(delay)
wn.mainloop()
How to Run the Game:
Ensure Python is Installed: Make sure you have Python 3 installed on your computer. You can download it from python.org.
Save the Code:
Copy the entire code above.
Paste it into a text editor or an IDE (like IDLE, PyCharm, or Visual Studio Code).
Save the file with a .py extension, e.g., turtle_snake_game.py.
Run the Game:
Open a terminal or command prompt.
Navigate to the directory where you saved turtle_snake_game.py.
Run the game by typing python turtle_snake_game.py and pressing Enter.
Game Instructions:
Objective: Guide the snake to eat the food (magenta circles). Each time the snake eats food, it grows longer, and your score increases by 10 points.
Controls:
Arrow Keys: Use the Up, Down, Left, and Right arrow keys to control the snake's movement.
Rules:
Avoid Collisions: Don't let the snake collide with the walls or its own body. If it does, the game resets.
Increase Difficulty: As you eat more food, the snake moves faster, increasing the game's difficulty.
Enjoy the Game!
Have fun playing the Turtle Snake Game! Try to beat your high score each time you play.