import pygame
import random
import math
import time
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Quantum Snake")
clock = pygame.time.Clock()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
PURPLE = (128, 0, 128)
CYAN = (0, 255, 255)
ORANGE = (255, 165, 0)
class Snake:
def __init__(self):
self.positions = [(WIDTH//2, HEIGHT//2)]
self.direction = (20, 0)
self.grow_amount = 0
self.ghost_mode = False
self.ghost_timer = 0
self.time_fragments = []
self.gravity_affected = False
self.velocity_y = 0
def move(self):
head = self.positions[0]
dx, dy = self.direction
if self.gravity_affected:
self.velocity_y += 0.5
new_head = (head[0] + dx, head[1] + self.velocity_y)
if new_head[1] >= HEIGHT - 20:
new_head = (new_head[0], HEIGHT - 20)
self.velocity_y = 0
else:
new_head = (head[0] + dx, head[1] + dy)
if self.ghost_mode:
new_head = (new_head[0] % WIDTH, new_head[1] % HEIGHT)
else:
if new_head[0] < 0 or new_head[0] >= WIDTH or new_head[1] < 0 or new_head[1] >= HEIGHT:
return False
self.positions.insert(0, new_head)
if self.grow_amount > 0:
self.grow_amount -= 1
else:
self.positions.pop()
return True
def draw(self, screen):
for i, pos in enumerate(self.positions):
if self.ghost_mode:
alpha = 128
color = (*CYAN, alpha)
s = pygame.Surface((20, 20))
s.set_alpha(alpha)
s.fill(CYAN)
screen.blit(s, pos)
else:
color = GREEN if i == 0 else BLUE
pygame.draw.rect(screen, color, (*pos, 20, 20))
for fragment in self.time_fragments:
pygame.draw.rect(screen, PURPLE, (*fragment, 20, 20), 2)
class Food:
def __init__(self):
self.position = self.random_position()
self.type = random.choice(['normal', 'ghost', 'time', 'gravity', 'portal'])
self.portal_exit = None
if self.type == 'portal':
self.portal_exit = self.random_position()
def random_position(self):
x = random.randint(0, (WIDTH-20)//20) * 20
y = random.randint(0, (HEIGHT-20)//20) * 20
return (x, y)
def draw(self, screen):
colors = {
'normal': RED,
'ghost': CYAN,
'time': PURPLE,
'gravity': ORANGE,
'portal': YELLOW
}
pygame.draw.circle(screen, colors[self.type],
(self.position[0] + 10, self.position[1] + 10), 10)
if self.type == 'portal' and self.portal_exit:
pygame.draw.circle(screen, YELLOW,
(self.portal_exit[0] + 10, self.portal_exit[1] + 10), 10, 3)
class BlackHole:
def __init__(self):
self.position = (random.randint(100, WIDTH-100), random.randint(100, HEIGHT-100))
self.radius = 50
self.strength = 3
def draw(self, screen):
for r in range(self.radius, 0, -5):
alpha = 255 - (r * 255 // self.radius)
color = (alpha, 0, alpha)
pygame.draw.circle(screen, color, self.position, r, 1)
def apply_gravity(self, snake):
head = snake.positions[0]
dx = self.position[0] - head[0]
dy = self.position[1] - head[1]
distance = math.sqrt(dx**2 + dy**2)
if distance < self.radius * 2 and distance > 0:
force = self.strength * (1 - distance / (self.radius * 2))
pull_x = (dx / distance) * force
pull_y = (dy / distance) * force
new_dir_x = snake.direction[0] + pull_x
new_dir_y = snake.direction[1] + pull_y
magnitude = math.sqrt(new_dir_x**2 + new_dir_y**2)
if magnitude > 0:
snake.direction = (new_dir_x / magnitude * 20, new_dir_y / magnitude * 20)
def main():
snake = Snake()
food = Food()
black_holes = []
score = 0
running = True
game_over = False
time_rewind_positions = []
recording_time = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN and not game_over:
if event.key == pygame.K_UP and not snake.gravity_affected:
snake.direction = (0, -20)
elif event.key == pygame.K_DOWN and not snake.gravity_affected:
snake.direction = (0, 20)
elif event.key == pygame.K_LEFT:
snake.direction = (-20, 0)
elif event.key == pygame.K_RIGHT:
snake.direction = (20, 0)
elif event.key == pygame.K_SPACE and snake.gravity_affected:
snake.velocity_y = -15
elif event.key == pygame.K_r and len(time_rewind_positions) > 0:
snake.positions = time_rewind_positions.pop()
if not game_over:
if not snake.move():
game_over = True
if snake.positions[0] in snake.positions[1:] and not snake.ghost_mode:
game_over = True
if recording_time and len(snake.positions) > 0:
time_rewind_positions.append(snake.positions[:])
if len(time_rewind_positions) > 100:
time_rewind_positions.pop(0)
if snake.ghost_mode:
snake.ghost_timer -= 1
if snake.ghost_timer <= 0:
snake.ghost_mode = False
for black_hole in black_holes:
black_hole.apply_gravity(snake)
if math.dist(snake.positions[0], black_hole.position) < 30:
game_over = True
if snake.positions[0] == food.position:
score += 1
if food.type == 'normal':
snake.grow_amount += 3
elif food.type == 'ghost':
snake.ghost_mode = True
snake.ghost_timer = 150
elif food.type == 'time':
snake.time_fragments = snake.positions[-5:]
snake.grow_amount += 5
elif food.type == 'gravity':
snake.gravity_affected = not snake.gravity_affected
snake.velocity_y = 0
elif food.type == 'portal' and food.portal_exit:
snake.positions[0] = food.portal_exit
food = Food()
if score % 5 == 0:
black_holes.append(BlackHole())
if len(black_holes) > 3:
black_holes.pop(0)
screen.fill(BLACK)
for black_hole in black_holes:
black_hole.draw(screen)
snake.draw(screen)
food.draw(screen)
font = pygame.font.Font(None, 36)
score_text = font.render(f"Score: {score}", True, WHITE)
screen.blit(score_text, (10, 10))
if snake.ghost_mode:
ghost_text = font.render(f"Ghost: {snake.ghost_timer//30}", True, CYAN)
screen.blit(ghost_text, (10, 50))
if snake.gravity_affected:
gravity_text = font.render("GRAVITY ON", True, ORANGE)
screen.blit(gravity_text, (WIDTH - 200, 10))
if game_over:
game_over_text = font.render("GAME OVER", True, RED)
screen.blit(game_over_text, (WIDTH//2 - 100, HEIGHT//2))
pygame.display.flip()
clock.tick(10)
pygame.quit()
if __name__ == "__main__":
main()