import pygame
import random
import sys
import math
import time
pygame.init()
info = pygame.display.Info()
screen_width = info.current_w
screen_height = info.current_h
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("")
COLORS = {
'white': (255, 255, 255),
'black': (0, 0, 0),
'green': (0, 255, 0),
'dark_green': (0, 150, 0),
'red': (255, 0, 0),
'blue': (0, 100, 255),
'dark_blue': (0, 50, 150),
'gray': (200, 200, 200),
'dark_gray': (100, 100, 100),
'light_gray': (230, 230, 230),
'purple': (128, 0, 128),
'cyan': (0, 255, 255)
}
cell_size = 25
game_area_x = 50
game_area_y = 100
grid_width = (screen_width - 2 * game_area_x) // cell_size
grid_height = int((screen_height * 0.45) / cell_size)
font_large = pygame.font.Font(None, 72)
font_medium = pygame.font.Font(None, 48)
font_small = pygame.font.Font(None, 36)
font_tiny = pygame.font.Font(None, 24)
class SnakeGame:
def __init__(self):
self.reset_game()
self.game_state = "START"
self.high_score = 0
self.setup_controls()
self.start_time = time.time()
self.show_binary = True
self.binary_timer = 0
def reset_game(self):
center_x = grid_width // 2
center_y = grid_height // 2
self.snake = [(center_x, center_y), (center_x - 1, center_y), (center_x - 2, center_y)]
self.direction = "RIGHT"
self.next_direction = "RIGHT"
self.score = 0
self.game_speed = 2
self.food = self.generate_food()
def setup_controls(self):
button_size = min(100, screen_width // 5)
map_bottom = game_area_y + grid_height * cell_size
control_y = map_bottom + (screen_height - map_bottom) // 2
center_x = screen_width // 2
gap = button_size + 50
control_y = min(control_y, screen_height - gap - 20)
self.buttons = {
'UP': {
'rect': pygame.Rect(center_x - button_size//2, control_y - gap, button_size, button_size),
'action': 'UP',
'symbol': '^'
},
'DOWN': {
'rect': pygame.Rect(center_x - button_size//2, control_y + gap - button_size, button_size, button_size),
'action': 'DOWN',
'symbol': 'v'
},
'LEFT': {
'rect': pygame.Rect(center_x - gap, control_y - button_size//2, button_size, button_size),
'action': 'LEFT',
'symbol': '<'
},
'RIGHT': {
'rect': pygame.Rect(center_x + gap - button_size, control_y - button_size//2, button_size, button_size),
'action': 'RIGHT',
'symbol': '>'
}
}
def generate_food(self):
while True:
food = (random.randint(0, grid_width - 1), random.randint(0, grid_height - 1))
if food not in self.snake:
return food
def move_snake(self):
self.direction = self.next_direction
head_x, head_y = self.snake[0]
if self.direction == "UP":
new_head = (head_x, head_y - 1)
elif self.direction == "DOWN":
new_head = (head_x, head_y + 1)
elif self.direction == "LEFT":
new_head = (head_x - 1, head_y)
elif self.direction == "RIGHT":
new_head = (head_x + 1, head_y)
self.snake.insert(0, new_head)
if new_head == self.food:
self.score += 1
self.food = self.generate_food()
else:
self.snake.pop()
def check_collision(self):
head = self.snake[0]
head_x, head_y = head
if head_x < 0 or head_x >= grid_width or head_y < 0 or head_y >= grid_height:
return True
if head in self.snake[1:]:
return True
return False
def change_direction(self, new_direction):
opposite = {
"UP": "DOWN",
"DOWN": "UP",
"LEFT": "RIGHT",
"RIGHT": "LEFT"
}
if new_direction != opposite.get(self.direction):
self.next_direction = new_direction
def handle_input(self, pos):
if self.game_state == "START" or self.game_state == "GAME_OVER":
if self.game_state == "GAME_OVER" and self.score > self.high_score:
self.high_score = self.score
self.reset_game()
self.game_state = "PLAYING"
self.start_time = time.time()
elif self.game_state == "PLAYING":
for button_name, button_data in self.buttons.items():
if button_data['rect'].collidepoint(pos):
self.change_direction(button_data['action'])
def draw_binary_screen(self):
screen.fill(COLORS['black'])
current_time = time.time()
elapsed = current_time - self.start_time
binary_chars = "01"
for y in range(0, screen_height, 30):
for x in range(0, screen_width, 30):
char = random.choice(binary_chars)
wave = math.sin(x * 0.01 + elapsed * 3) * math.cos(y * 0.01 + elapsed * 2)
if char == '1':
g = int(100 + 155 * abs(wave))
color = (0, g, 0)
else:
g = int(50 + 50 * abs(wave))
color = (0, g, 0)
alpha = int(100 + 100 * abs(wave))
text = font_tiny.render(char, True, color)
text.set_alpha(alpha)
screen.blit(text, (x + random.randint(-2, 2), y + random.randint(-2, 2)))
bar_width = min(400, screen_width - 100)
bar_height = 30
bar_x = (screen_width - bar_width) // 2
bar_y = screen_height // 2 + 80
progress = max(0, 1 - elapsed / 30)
pygame.draw.rect(screen, COLORS['dark_gray'], (bar_x, bar_y, bar_width, bar_height), border_radius=8)
pygame.draw.rect(screen, COLORS['green'], (bar_x, bar_y, int(bar_width * progress), bar_height), border_radius=8)
pygame.draw.rect(screen, COLORS['white'], (bar_x, bar_y, bar_width, bar_height), 2, border_radius=8)
remaining = max(0, 30 - int(elapsed))
if remaining > 0:
countdown_text = font_large.render(str(remaining), True, COLORS['green'])
countdown_rect = countdown_text.get_rect(center=(screen_width // 2, screen_height // 2))
screen.blit(countdown_text, countdown_rect)
pygame.display.flip()
if elapsed > 30:
self.show_binary = False
self.game_state = "START"
def draw_text(self, text, font, color, center_pos):
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect(center=center_pos)
screen.blit(text_surface, text_rect)
def draw_button(self, button_data, is_pressed=False):
rect = button_data['rect']
color = COLORS['dark_blue'] if is_pressed else COLORS['blue']
pygame.draw.rect(screen, color, rect, border_radius=15)
pygame.draw.rect(screen, COLORS['white'], rect, 3, border_radius=15)
symbol = button_data['symbol']
symbol_surface = font_medium.render(symbol, True, COLORS['white'])
symbol_rect = symbol_surface.get_rect(center=rect.center)
screen.blit(symbol_surface, symbol_rect)
def draw_game_area(self):
area_rect = pygame.Rect(game_area_x - 15, game_area_y - 15,
grid_width * cell_size + 30, grid_height * cell_size + 30)
pygame.draw.rect(screen, COLORS['dark_gray'], area_rect, border_radius=12)
for i in range(grid_width + 1):
x = game_area_x + i * cell_size
pygame.draw.line(screen, COLORS['light_gray'],
(x, game_area_y),
(x, game_area_y + grid_height * cell_size))
for i in range(grid_height + 1):
y = game_area_y + i * cell_size
pygame.draw.line(screen, COLORS['light_gray'],
(game_area_x, y),
(game_area_x + grid_width * cell_size, y))
def draw_snake(self):
for i, segment in enumerate(self.snake):
x = game_area_x + segment[0] * cell_size
y = game_area_y + segment[1] * cell_size
if i == 0:
color = COLORS['dark_green']
else:
color = COLORS['green']
pygame.draw.rect(screen, color,
(x + 3, y + 3, cell_size - 6, cell_size - 6),
border_radius=6)
if i == 0:
eye_offset = cell_size // 4
eye_size = 4
if self.direction == "RIGHT":
eye1_pos = (x + cell_size - eye_offset, y + eye_offset)
eye2_pos = (x + cell_size - eye_offset, y + cell_size - eye_offset)
elif self.direction == "LEFT":
eye1_pos = (x + eye_offset, y + eye_offset)
eye2_pos = (x + eye_offset, y + cell_size - eye_offset)
elif self.direction == "UP":
eye1_pos = (x + eye_offset, y + eye_offset)
eye2_pos = (x + cell_size - eye_offset, y + eye_offset)
else:
eye1_pos = (x + eye_offset, y + cell_size - eye_offset)
eye2_pos = (x + cell_size - eye_offset, y + cell_size - eye_offset)
pygame.draw.circle(screen, COLORS['white'], eye1_pos, eye_size)
pygame.draw.circle(screen, COLORS['white'], eye2_pos, eye_size)
def draw_food(self):
x = game_area_x + self.food[0] * cell_size + cell_size // 2
y = game_area_y + self.food[1] * cell_size + cell_size // 2
pygame.draw.circle(screen, COLORS['red'], (x, y), cell_size // 2 - 4)
def draw_start_screen(self):
screen.fill(COLORS['black'])
def draw_game_over_screen(self):
overlay = pygame.Surface((screen_width, screen_height))
overlay.set_alpha(180)
overlay.fill(COLORS['black'])
screen.blit(overlay, (0, 0))
def draw(self):
if self.show_binary:
self.draw_binary_screen()
return
if self.game_state == "START":
self.draw_start_screen()
elif self.game_state == "PLAYING":
screen.fill(COLORS['black'])
self.draw_game_area()
self.draw_food()
self.draw_snake()
for button_name, button_data in self.buttons.items():
self.draw_button(button_data)
elif self.game_state == "GAME_OVER":
self.draw_game_area()
self.draw_food()
self.draw_snake()
self.draw_game_over_screen()
pygame.display.flip()
def run(self):
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
self.handle_input(event.pos)
if self.game_state == "PLAYING":
self.move_snake()
if self.check_collision():
if self.score > self.high_score:
self.high_score = self.score
self.game_state = "GAME_OVER"
self.draw()
clock.tick(self.game_speed)
pygame.quit()
sys.exit()
def main():
game = SnakeGame()
game.run()
if __name__ == "__main__":
main()