Certainly! Below is a creative and feature-rich version of the classic Snake game using Python and Pygame. This game includes five uniquely creative features designed to elevate it beyond the traditional experience.
🐍 Creative Snake Game – “Snakeverse Adventures”
✅ Unique Features:
- Time Warps (Portals): Random portals appear and teleport the snake to a different part of the screen.
- Color Shifting Snake: The snake changes colors as it eats, creating a rainbow effect.
- Reverse Controls Zone: A special zone on the screen where the snake's controls are reversed!
- Enemy Mini-Snakes: AI-controlled mini-snakes move around, and the player must avoid them.
- Gravity Mode: Occasionally toggles gravity – the snake slowly gets pulled down and must fight gravity by moving up.
🧠 Requirements
Install Pygame via pip if not already:
pip install pygame
💻 Full Code
import pygame
import random
import sys
import math
# Initialize Pygame
pygame.init()
pygame.display.set_caption("🐍 Snakeverse Adventures")
clock = pygame.time.Clock()
# Game Constants
WIDTH, HEIGHT = 800, 600
GRID_SIZE = 20
FPS = 12
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
PORTAL_COLOR = (100, 0, 255)
REVERSE_ZONE_COLOR = (255, 235, 59)
GRAVITY_FORCE = 1
# Setup
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# Snake & Food Colors
color_wheel = [(255, 0, 0), (255, 125, 0), (255, 255, 0),
(0, 255, 0), (0, 255, 255), (0, 0, 255), (255, 0, 255)]
def draw_grid():
for x in range(0, WIDTH, GRID_SIZE):
pygame.draw.line(screen, (40, 40, 40), (x, 0), (x, HEIGHT))
for y in range(0, HEIGHT, GRID_SIZE):
pygame.draw.line(screen, (40, 40, 40), (0, y), (WIDTH, y))
def random_grid_position():
return (random.randint(0, (WIDTH - GRID_SIZE) // GRID_SIZE) * GRID_SIZE,
random.randint(0, (HEIGHT - GRID_SIZE) // GRID_SIZE) * GRID_SIZE)
class Snake:
def __init__(self):
self.positions = [((WIDTH // 2), (HEIGHT // 2))]
self.direction = random.choice(["UP", "DOWN", "LEFT", "RIGHT"])
self.color_index = 0
self.length = 1
def move(self, reverse_controls=False):
head_x, head_y = self.positions[0]
dir_map = {
"UP": (0, -GRID_SIZE),
"DOWN": (0, GRID_SIZE),
"LEFT": (-GRID_SIZE, 0),
"RIGHT": (GRID_SIZE, 0)
}
# Reverse controls if in reverse zone
if reverse_controls:
inverse_dir_map = {
"UP": "DOWN",
"DOWN": "UP",
"LEFT": "RIGHT",
"RIGHT": "LEFT"
}
move_dir = inverse_dir_map[self.direction]
else:
move_dir = self.direction
dx, dy = dir_map[move_dir]
new_pos = (head_x + dx, head_y + dy)
if self.length > 1:
self.positions = [new_pos] + self.positions[:-1]
else:
self.positions[0] = new_pos
def grow(self):
tail = self.positions[-1]
self.positions.append(tail)
self.length += 1
self.color_index = (self.color_index + 1) % len(color_wheel)
def draw(self):
for i, pos in enumerate(self.positions):
color = color_wheel[(self.color_index + i) % len(color_wheel)]
pygame.draw.rect(screen, color, pygame.Rect(pos[0], pos[1], GRID_SIZE, GRID_SIZE))
def check_self_collision(self):
return self.positions[0] in self.positions[1:]
class Food:
def __init__(self):
self.position = random_grid_position()
def draw(self):
pygame.draw.rect(screen, (0, 200, 0), pygame.Rect(self.position[0], self.position[1], GRID_SIZE, GRID_SIZE))
class Portal:
def __init__(self):
self.from_pos = random_grid_position()
self.to_pos = random_grid_position()
self.timer = 0
self.duration = 150
def draw(self):
pygame.draw.rect(screen, PORTAL_COLOR, pygame.Rect(self.from_pos[0], self.from_pos[1], GRID_SIZE, GRID_SIZE))
pygame.draw.rect(screen, PORTAL_COLOR, pygame.Rect(self.to_pos[0], self.to_pos[1], GRID_SIZE, GRID_SIZE))
def teleport(self, pos):
if pos == self.from_pos:
return self.to_pos
return pos
class ReverseZone:
def __init__(self):
self.zone = pygame.Rect(random_grid_position(), (GRID_SIZE * 3, GRID_SIZE * 3))
def in_zone(self, pos):
x, y = pos
return self.zone.colliderect(pygame.Rect(x, y, GRID_SIZE, GRID_SIZE))
def draw(self):
pygame.draw.rect(screen, REVERSE_ZONE_COLOR, self.zone)
class EnemySnake:
def __init__(self):
self.positions = [random_grid_position()]
self.direction = random.choice(["UP", "DOWN", "LEFT", "RIGHT"])
def move(self):
dir_map = {
"UP": (0, -GRID_SIZE),
"DOWN": (0, GRID_SIZE),
"LEFT": (-GRID_SIZE, 0),
"RIGHT": (GRID_SIZE, 0)
}
dx, dy = dir_map[self.direction]
new_pos = (self.positions[0][0] + dx, self.positions[0][1] + dy)
self.positions = [new_pos] + self.positions[:2]
if random.random() < 0.1:
self.direction = random.choice(["UP", "DOWN", "LEFT", "RIGHT"])
def draw(self):
for pos in self.positions:
pygame.draw.rect(screen, (255, 80, 80), pygame.Rect(pos[0], pos[1], GRID_SIZE, GRID_SIZE))
def main():
snake = Snake()
food = Food()
portal = Portal()
reverse_zone = ReverseZone()
enemy_snakes = [EnemySnake() for _ in range(2)]
running = True
gravity_mode = False
gravity_timer = 0
while running:
screen.fill(BLACK)
draw_grid()
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Snake control
if keys[pygame.K_UP] and snake.direction != "DOWN":
snake.direction = "UP"
if keys[pygame.K_DOWN] and snake.direction != "UP":
snake.direction = "DOWN"
if keys[pygame.K_LEFT] and snake.direction != "RIGHT":
snake.direction = "LEFT"
if keys[pygame.K_RIGHT] and snake.direction != "LEFT":
snake.direction = "RIGHT"
# Gravity toggle every few seconds
gravity_timer += 1
if gravity_timer >= 300:
gravity_timer = 0
gravity_mode = not gravity_mode
in_reverse = reverse_zone.in_zone(snake.positions[0])
snake.move(reverse_controls=in_reverse)
if gravity_mode:
head_x, head_y = snake.positions[0]
snake.positions[0] = (head_x, head_y + GRAVITY_FORCE)
# Eat food
if snake.positions[0] == food.position:
snake.grow()
food = Food()
# Portal updates
portal.timer += 1
if portal.timer >= portal.duration:
portal = Portal()
# Teleport snake if hits portal
snake.positions[0] = portal.teleport(snake.positions[0])
# Check self collision
if snake.check_self_collision():
print("Game Over: You ate yourself!")
break
# Enemy snake logic
for esnake in enemy_snakes:
esnake.move()
if snake.positions[0] in esnake.positions:
print("Game Over: Enemy snake got you!")
running = False
# Draw everything
snake.draw()
food.draw()
portal.draw()
reverse_zone.draw()
for esnake in enemy_snakes:
esnake.draw()
# Check wall collisions
x, y = snake.positions[0]
if x < 0 or x >= WIDTH or y < 0 or y >= HEIGHT:
print("Game Over: You hit the wall!")
break
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
🧪 Try It Out
Run the script using Python and you'll experience:
- 🌈 Rainbow snake
- 🔁 Reverse control fields
- 🌀 Teleporting portals
- 🐍 Enemies with brains!
- 🪂 Gravity forcing you down at intervals!
🔮 Suggestions for Expansion
- Add powerups (slow time, speed boost).
- Level up environments.
- Boss fights (giant AI snakes).
- Score tracking & high score saving.
Let me know if you'd like an advanced version of this game (network multiplayer, soundtrack, or GUI menu)!