start of Menu implementation
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
class Button():
|
||||
def __init__(self, image, pos, text_input, font, base_color, hovering_color):
|
||||
self.image = image
|
||||
self.x_pos = pos[0]
|
||||
self.y_pos = pos[1]
|
||||
self.font = font
|
||||
self.base_color, self.hovering_color = base_color, hovering_color
|
||||
self.text_input = text_input
|
||||
self.text = self.font.render(self.text_input, True, self.base_color)
|
||||
if self.image is None:
|
||||
self.image = self.text
|
||||
self.rect = self.image.get_rect(center=(self.x_pos, self.y_pos))
|
||||
self.text_rect = self.text.get_rect(center=(self.x_pos, self.y_pos))
|
||||
|
||||
def update(self, screen):
|
||||
if self.image is not None:
|
||||
screen.blit(self.image, self.rect)
|
||||
screen.blit(self.text, self.text_rect)
|
||||
|
||||
def checkForInput(self, position):
|
||||
if position[0] in range(self.rect.left, self.rect.right) and position[1] in range(self.rect.top, self.rect.bottom):
|
||||
return True
|
||||
return False
|
||||
|
||||
def changeColor(self, position):
|
||||
if position[0] in range(self.rect.left, self.rect.right) and position[1] in range(self.rect.top, self.rect.bottom):
|
||||
self.text = self.font.render(self.text_input, True, self.hovering_color)
|
||||
else:
|
||||
self.text = self.font.render(self.text_input, True, self.base_color)
|
||||
@@ -0,0 +1,83 @@
|
||||
import pygame, sys
|
||||
from util import write_save
|
||||
|
||||
class Controls_Handler():
|
||||
def __init__(self, save):
|
||||
self.save_file = save
|
||||
self.curr_block = save["current_profile"]
|
||||
self.controls = self.save_file["controls"][str(self.curr_block)]
|
||||
self.setup()
|
||||
|
||||
def update(self, actions):
|
||||
if self.selected: self.set_new_control()
|
||||
else: self.navigate_menu(actions)
|
||||
|
||||
def render(self, surface):
|
||||
self.draw_text(surface, "Control Profile " + str(self.curr_block+1) , 20, pygame.Color((0,0,0)), 480 / 2, 270/8)
|
||||
self.display_controls(surface, self.save_file["controls"][str(self.curr_block)])
|
||||
if self.curr_block == self.save_file["current_profile"]: self.draw_text(surface, "*" , 20, pygame.Color((0,0,0)), 20, 20)
|
||||
|
||||
def navigate_menu(self, actions):
|
||||
# Move the cursor up and down
|
||||
if actions["Down"]: self.curr_index = (self.curr_index + 1) % (len(self.save_file["controls"][str(self.curr_block)]) + 1)
|
||||
if actions["Up"]: self.curr_index = (self.curr_index - 1) % (len(self.save_file["controls"][str(self.curr_block)]) + 1)
|
||||
# Switch between profiles
|
||||
if actions["Left"]: self.curr_block = (self.curr_block -1) % len(self.save_file["controls"])
|
||||
if actions["Right"]: self.curr_block = (self.curr_block +1) % len(self.save_file["controls"])
|
||||
# Handle Selection
|
||||
if actions["Action1"] or actions["Start"]:
|
||||
# Set the current profile to be the main one
|
||||
if self.cursor_dict[self.curr_index] == "Set":
|
||||
self.controls = self.save_file["controls"][str(self.curr_block)]
|
||||
self.save_file["current_profile"] = self.curr_block
|
||||
write_save(self.save_file)
|
||||
else:
|
||||
self.selected = True
|
||||
|
||||
def set_new_control(self):
|
||||
selected_control = self.cursor_dict[self.curr_index]
|
||||
done = False
|
||||
while not done:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
done = True
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_ESCAPE:
|
||||
done = True
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
elif event.key not in self.save_file["controls"][str(self.curr_block)].values():
|
||||
self.save_file["controls"][str(self.curr_block)][selected_control] = event.key
|
||||
write_save(self.save_file)
|
||||
self.selected = False
|
||||
done = True
|
||||
|
||||
def display_controls(self,surface, controls):
|
||||
color = (255,13,5) if self.selected else (255,250,239)
|
||||
pygame.draw.rect(surface, color, (80 , 270/4 - 10 + (self.curr_index*30), 320, 20) )
|
||||
i = 0
|
||||
for control in controls:
|
||||
self.draw_text(surface, control + ' - ' + pygame.key.name(controls[control]),20,
|
||||
pygame.Color((0,0,0)), 480 / 2, 270/4 + i)
|
||||
i += 30
|
||||
self.draw_text(surface, "Set Current Profile",20, pygame.Color((0,0,0)), 480 / 2, 270/4 + i)
|
||||
|
||||
def setup(self):
|
||||
self.selected = False
|
||||
self.font = pygame.font.Font("RetroFont.ttf", 20)
|
||||
self.cursor_dict = {}
|
||||
self.curr_index = 0
|
||||
i = 0
|
||||
for control in self.controls:
|
||||
self.cursor_dict[i] = control
|
||||
i += 1
|
||||
self.cursor_dict[i] = "Set"
|
||||
|
||||
def draw_text(self,surface, text, size, color, x, y):
|
||||
text_surface = self.font.render(text, True, color, size)
|
||||
text_surface.set_colorkey((0,0,0))
|
||||
text_rect = text_surface.get_rect()
|
||||
text_rect.center = (x, y)
|
||||
surface.blit(text_surface, text_rect)
|
||||
@@ -0,0 +1,86 @@
|
||||
import pygame
|
||||
import json, os
|
||||
|
||||
# calculate fall speed
|
||||
|
||||
def get_fall_interval(score):
|
||||
"""
|
||||
Calculate the fall interval based on the current score.
|
||||
"""
|
||||
base_interval = 1000 # Base interval in milliseconds
|
||||
decrease_per_1000_points = 50 # Decrease in ms per 1000 points
|
||||
min_interval = 100 # Minimum interval in milliseconds
|
||||
|
||||
# Calculate the fall interval
|
||||
interval = base_interval - (score // 400) * decrease_per_1000_points
|
||||
|
||||
# Ensure the interval doesn't go below the minimum
|
||||
return max(interval, min_interval)
|
||||
|
||||
# Draw text simplified
|
||||
|
||||
def draw_text(surface, text, font, x, y, color):
|
||||
"""Utility function to render text onto a surface."""
|
||||
text_surface = font.render(text, True, color)
|
||||
text_rect = text_surface.get_rect(center=(x, y))
|
||||
surface.blit(text_surface, text_rect)
|
||||
|
||||
def get_font(size):
|
||||
return pygame.font.Font("assets/font.ttf", size)
|
||||
|
||||
def get_background():
|
||||
return pygame.image.load('assets/menu_background.png')
|
||||
|
||||
#For the Leaderboard
|
||||
|
||||
def load_score(filename):
|
||||
"""Load JSON data only if the file is not empty."""
|
||||
if os.path.exists(filename) and os.path.getsize(filename) > 0: # Check if file exists & not empty
|
||||
with open(filename, "r") as file:
|
||||
return json.load(file)
|
||||
return {}
|
||||
|
||||
def add_score(filename, score):
|
||||
if os.path.exists(filename):
|
||||
with open(os.path.join(os.getcwd(), filename), "w") as file:
|
||||
json.dump(score, file)
|
||||
|
||||
# For keybinds (control.py)
|
||||
|
||||
def load_existing_save(savefile):
|
||||
with open(os.path.join(savefile), 'r+') as file:
|
||||
controls = json.load(file)
|
||||
return controls
|
||||
|
||||
def write_save(data):
|
||||
with open(os.path.join(os.getcwd(),'save.json'), 'w') as file:
|
||||
json.dump(data, file)
|
||||
|
||||
def load_save():
|
||||
try:
|
||||
# Save is loaded
|
||||
save = load_existing_save('save.json')
|
||||
except:
|
||||
# No save file, so create one
|
||||
save = create_save()
|
||||
write_save(save)
|
||||
return save
|
||||
|
||||
|
||||
def create_save():
|
||||
new_save = {
|
||||
"controls":{
|
||||
"0" :{"Left": pygame.K_a, "Right": pygame.K_d, "Up": pygame.K_w, "Down": pygame.K_s,
|
||||
"Start": pygame.K_RETURN, "Action1": pygame.K_SPACE},
|
||||
"1" :{"Left": pygame.K_a, "Right": pygame.K_d, "Up": pygame.K_w, "Down": pygame.K_s,
|
||||
"Start": pygame.K_RETURN, "Action1": pygame.K_SPACE}
|
||||
},
|
||||
"current_profile": 0
|
||||
}
|
||||
|
||||
return new_save
|
||||
|
||||
def reset_keys(actions):
|
||||
for action in actions:
|
||||
actions[action] = False
|
||||
return actions
|
||||
Reference in New Issue
Block a user