-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
153 lines (123 loc) · 4.88 KB
/
main.py
File metadata and controls
153 lines (123 loc) · 4.88 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import pygame
import sys
import time
import score
from games.game1 import game1
from games.game2 import game2
from games.game3 import game3
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60
BACKGROUND_COLOR = (50, 50, 50)
MENU_TITLE_COLOR = (255, 215, 0)
MENU_ITEM_COLOR = (200, 200, 200)
MENU_ITEM_HOVER_COLOR = (255, 255, 255)
def main():
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Snakepit")
clock = pygame.time.Clock()
title_font = pygame.font.SysFont(None, 72)
menu_font = pygame.font.SysFont(None, 48)
current_score = score.init_score()
games = [
{"name": "Red Clicker", "module": game1},
{"name": "Blue Clicker", "module": game2},
{"name": "Green Clicker", "module": game3},
]
menu_state = {
"active": True,
"selected_index": 0,
}
active_game = {"module": None, "state": None}
last_time = time.time()
running = True
while running:
current_time = time.time()
dt = current_time - last_time
last_time = current_time
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
running = False
if menu_state["active"]:
handle_menu_events(event, menu_state, games, active_game, screen)
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
# Return to menu
if active_game["module"]:
active_game["module"].game_shutdown(active_game["state"])
active_game["module"] = None
active_game["state"] = None
menu_state["active"] = True
if menu_state["active"]:
render_menu(screen, title_font, menu_font, menu_state, games, current_score)
else:
if active_game["module"] and active_game["state"]:
active_game["state"], score_change = active_game["module"].game_update(
screen, active_game["state"], events, dt, current_score
)
if score_change != 0:
current_score = score.update_score(current_score, score_change)
pygame.display.flip()
clock.tick(FPS)
if active_game["module"] and active_game["state"]:
active_game["module"].game_shutdown(active_game["state"])
pygame.quit()
sys.exit()
def handle_menu_events(event, menu_state, games, active_game, screen):
"""Handle events in the menu state."""
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
menu_state["selected_index"] = (menu_state["selected_index"] - 1) % len(
games
)
elif event.key == pygame.K_DOWN:
menu_state["selected_index"] = (menu_state["selected_index"] + 1) % len(
games
)
elif event.key == pygame.K_RETURN:
selected_game = games[menu_state["selected_index"]]
active_game["module"] = selected_game["module"]
active_game["state"] = active_game["module"].game_init(screen)
menu_state["active"] = False
def render_menu(screen, title_font, menu_font, menu_state, games, current_score):
"""Render the main menu."""
screen.fill(BACKGROUND_COLOR)
title_text = title_font.render("Snakepit", True, MENU_TITLE_COLOR)
title_rect = title_text.get_rect(center=(SCREEN_WIDTH // 2, 100))
screen.blit(title_text, title_rect)
score_text = menu_font.render(
score.format_score(current_score), True, MENU_TITLE_COLOR
)
score_rect = score_text.get_rect(center=(SCREEN_WIDTH // 2, 170))
screen.blit(score_text, score_rect)
for i, game in enumerate(games):
color = (
MENU_ITEM_HOVER_COLOR
if i == menu_state["selected_index"]
else MENU_ITEM_COLOR
)
game_text = menu_font.render(game["name"], True, color)
text_rect = game_text.get_rect(center=(SCREEN_WIDTH // 2, 250 + i * 70))
screen.blit(game_text, text_rect)
if i == menu_state["selected_index"]:
pygame.draw.polygon(
screen,
color,
[
(text_rect.left - 20, text_rect.centery),
(text_rect.left - 40, text_rect.centery - 10),
(text_rect.left - 40, text_rect.centery + 10),
],
)
instructions = [
"Use UP/DOWN arrows to select a game",
"Press ENTER to start the selected game",
"Press ESC during a game to return to menu",
]
instruction_font = pygame.font.SysFont(None, 24)
for i, instruction in enumerate(instructions):
instruction_text = instruction_font.render(instruction, True, (150, 150, 150))
screen.blit(instruction_text, (20, SCREEN_HEIGHT - 80 + i * 25))
if __name__ == "__main__":
main()