-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.py
More file actions
72 lines (62 loc) · 2.93 KB
/
menu.py
File metadata and controls
72 lines (62 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import pygame
import math
from snake import Snake
class Menu:
def __init__(self, width=500, height=500):
self.width = width
self.height = height
self.screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Slither - Main Menu")
self.clock = pygame.time.Clock()
self.font_title = pygame.font.SysFont(None, 80, bold=True)
self.font_button = pygame.font.SysFont(None, 32, bold=True)
self.font_small = pygame.font.SysFont(None, 18)
self.play_button_rect = pygame.Rect(width // 2 - 80, height // 2 - 20, 160, 60)
self.running = True
self.play_pressed = False
def handle_input(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if self.play_button_rect.collidepoint(event.pos):
self.play_pressed = True
self.running = False
def draw_curvy_snake(self, y_offset):
"""Draw a decorative snake using the in-game `Snake.draw` style."""
# choose a menu cell size for the decorative snake
menu_cell = 18
grid_y = max(1, y_offset // menu_cell)
start_x = 3
length = 8
s = Snake((start_x, grid_y))
s.body = [(start_x + i, grid_y) for i in range(length)]
# draw using the same style as in-game snake (color red)
s.draw(self.screen, menu_cell, color=(220, 30, 30))
def draw(self):
# Black background
self.screen.fill((10, 10, 10))
# Draw title "SLITHER"
title = self.font_title.render("SLITHER", True, (220, 30, 30)) # Red
title_rect = title.get_rect(center=(self.width // 2, 80))
self.screen.blit(title, title_rect)
# Draw play button (red background)
pygame.draw.rect(self.screen, (220, 30, 30), self.play_button_rect, border_radius=12)
pygame.draw.rect(self.screen, (255, 100, 100), self.play_button_rect, width=4, border_radius=12)
play_text = self.font_button.render("PLAY", True, (255, 255, 255))
play_rect = play_text.get_rect(center=self.play_button_rect.center)
self.screen.blit(play_text, play_rect)
# Draw instructions
instr1 = self.font_small.render("Use arrow keys to move the snake", True, (200, 200, 200))
instr2 = self.font_small.render("Eat the yellow food to grow", True, (200, 200, 200))
self.screen.blit(instr1, (self.width // 2 - instr1.get_width() // 2, self.height // 2 + 80))
self.screen.blit(instr2, (self.width // 2 - instr2.get_width() // 2, self.height // 2 + 110))
# Draw curvy snake at the bottom
self.draw_curvy_snake(self.height - 100)
pygame.display.flip()
def run(self):
while self.running:
self.handle_input()
self.draw()
self.clock.tick(60)
return self.play_pressed