-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreeplay.py
118 lines (95 loc) · 6.16 KB
/
freeplay.py
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
import pygame
from constants import *
import sys, os
class Freeplay():
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Platformer")
self.font = pygame.font.Font(None, 36)
self.selected_map = None
def create_button(self, text, x, y, color):
text_surface = self.font.render(text, True, Color.BLACK.value)
text_rect = text_surface.get_rect(center=(x, y))
return text_surface, text_rect, color
def create_dropdown(self):
file_names = []
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
# Running as a bundled executable
base_path = sys._MEIPASS
else:
# Running as a regular script
base_path = os.path.abspath(".")
# Construct the full path to the "images/map_thumbnail" folder
thumbnail_folder = os.path.join(base_path, "images", "map_thumbnail")
# Check if the folder exists
if os.path.exists(thumbnail_folder):
file_names = [f for f in os.listdir(thumbnail_folder) if os.path.isfile(os.path.join(thumbnail_folder, f))]
dropdown_options = []
for name in file_names:
dropdown_options.append({"id" : name.replace("map", "").replace(".png", ""),"image": pygame.image.load("images/map_thumbnail/"+name), "title": name.replace("map", "Map ").replace(".png", "")})
footer = 200
margin = 10
dropdown_rect = pygame.Rect(margin, 100, SCREEN_WIDTH - 2 * margin, SCREEN_HEIGHT - margin - footer) # Adjust the size of the dropdown
dropdown_color = Color.LIGHT_GREY.value
return dropdown_options, dropdown_rect, dropdown_color
def resize_image(self, original_image, target_size):
original_rect = original_image.get_rect()
aspect_ratio = original_rect.width / original_rect.height
new_width = int(target_size * aspect_ratio)
new_height = int(target_size)
return pygame.transform.scale(original_image, (new_width, new_height))
def run(self):
return_button_surface, return_button_rect, return_button_color = self.create_button("Return", 50, 50, Color.RED.value)
play_button_surface, play_button_rect, play_button_color = self.create_button("Play", SCREEN_WIDTH // 2, SCREEN_HEIGHT - 50, Color.GREEN.value)
dropdown_options, dropdown_rect, dropdown_color = self.create_dropdown()
num_images_per_row = 4
image_width = 120 # Adjust the size of the images
image_height = 90 # Adjust the size of the images
#TODO put some variables in constants.py
margin_inside_selector = 50
selector_size = (SCREEN_WIDTH - 2 * 10, SCREEN_HEIGHT - 210)
col_spacing = (selector_size[0] - (margin_inside_selector*2) - (num_images_per_row * image_width)) // (num_images_per_row - 1)
row_spacing = (selector_size[1] - (margin_inside_selector*2) - ((len(dropdown_options) // num_images_per_row) * image_height)) // (num_images_per_row - 1)
title_height = 20
for option in dropdown_options:
option["image"] = self.resize_image(option["image"], min(image_width, image_height))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if return_button_rect.collidepoint(event.pos):
pygame.quit()
return ["homepage", None, None]
elif play_button_rect.collidepoint(event.pos):
if self.selected_map is not None :
pygame.quit()
return ["game", FPS, [int(self.selected_map)]]
# Check if any dropdown option is clicked
for option in dropdown_options:
image_rect = option["image"].get_rect(topleft=(margin_inside_selector + dropdown_rect.x + (dropdown_options.index(option) % num_images_per_row) * (image_width + col_spacing),
margin_inside_selector + dropdown_rect.y + (dropdown_options.index(option) // num_images_per_row) * (image_height + row_spacing + title_height)))
title_rect = pygame.Rect(image_rect.left, image_rect.bottom, image_width, title_height)
if image_rect.collidepoint(event.pos) or title_rect.collidepoint(event.pos):
play_button_surface, play_button_rect, play_button_color = self.create_button("Play " + option['title'], SCREEN_WIDTH // 2, SCREEN_HEIGHT - 50, Color.GREEN.value)
self.selected_map = option['id']
self.screen.fill(Color.WHITE.value)
# Draw return button
pygame.draw.rect(self.screen, return_button_color, return_button_rect)
self.screen.blit(return_button_surface, return_button_rect)
# Draw play button
pygame.draw.rect(self.screen, play_button_color, play_button_rect)
self.screen.blit(play_button_surface, play_button_rect)
# Draw dropdown options
pygame.draw.rect(self.screen, dropdown_color, dropdown_rect)
for option in dropdown_options:
image_rect = option["image"].get_rect(topleft=(margin_inside_selector + dropdown_rect.x + (dropdown_options.index(option) % num_images_per_row) * (image_width + col_spacing),
margin_inside_selector + dropdown_rect.y + (dropdown_options.index(option) // num_images_per_row) * (image_height + row_spacing + title_height)))
self.screen.blit(option["image"], image_rect)
title_font = pygame.font.Font(None, title_height)
title_surface = title_font.render(option["title"], True, Color.BLACK.value)
title_rect = title_surface.get_rect(topleft=(image_rect.left, image_rect.bottom))
self.screen.blit(title_surface, title_rect)
pygame.display.flip()