implemented xustom keybinds
This commit is contained in:
+7
-5
@@ -33,6 +33,8 @@ class Game:
|
|||||||
|
|
||||||
self.playing = None
|
self.playing = None
|
||||||
|
|
||||||
|
self.control_handler = settings.control_handler
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
"""Main game loop."""
|
"""Main game loop."""
|
||||||
self.playing = True
|
self.playing = True
|
||||||
@@ -53,14 +55,14 @@ class Game:
|
|||||||
if event.type == pg.QUIT:
|
if event.type == pg.QUIT:
|
||||||
sys.exit()
|
sys.exit()
|
||||||
elif event.type == pg.KEYDOWN:
|
elif event.type == pg.KEYDOWN:
|
||||||
if event.key == pg.K_UP:
|
if event.key == self.control_handler.controls['Up']:
|
||||||
rotated_shape = [list(row) for row in zip(*self.current_tetromino.shape[::-1])]
|
rotated_shape = [list(row) for row in zip(*self.current_tetromino.shape[::-1])]
|
||||||
if not self.game_grid.check_collision(rotated_shape, self.current_tetromino.x, self.current_tetromino.y):
|
if not self.game_grid.check_collision(rotated_shape, self.current_tetromino.x, self.current_tetromino.y):
|
||||||
self.current_tetromino.shape = rotated_shape # Apply rotation
|
self.current_tetromino.shape = rotated_shape # Apply rotation
|
||||||
elif event.key == pg.K_SPACE:
|
elif event.key == pg.K_SPACE:
|
||||||
self.current_tetromino.instant_drop(self.game_grid) # Perform instant drop
|
self.current_tetromino.instant_drop(self.game_grid) # Perform instant drop
|
||||||
self.lock_tetromino()
|
self.lock_tetromino()
|
||||||
elif event.key == pg.K_c:
|
elif event.key == self.control_handler.controls['Hold']:
|
||||||
self.handle_hold()
|
self.handle_hold()
|
||||||
elif event.key == pg.K_ESCAPE: # Pause the game
|
elif event.key == pg.K_ESCAPE: # Pause the game
|
||||||
self.playing = False
|
self.playing = False
|
||||||
@@ -72,17 +74,17 @@ class Game:
|
|||||||
|
|
||||||
keys = pg.key.get_pressed()
|
keys = pg.key.get_pressed()
|
||||||
|
|
||||||
if keys[pg.K_RIGHT]:
|
if keys[self.control_handler.controls['Right']]:
|
||||||
if current_time - self.move_timer > self.move_interval: # Ensure delay between movements
|
if current_time - self.move_timer > self.move_interval: # Ensure delay between movements
|
||||||
if not self.game_grid.check_collision(self.current_tetromino.shape, self.current_tetromino.x + 1, self.current_tetromino.y):
|
if not self.game_grid.check_collision(self.current_tetromino.shape, self.current_tetromino.x + 1, self.current_tetromino.y):
|
||||||
self.current_tetromino.move(1, 0) # Move right
|
self.current_tetromino.move(1, 0) # Move right
|
||||||
self.move_timer = current_time # Reset timer
|
self.move_timer = current_time # Reset timer
|
||||||
elif keys[pg.K_LEFT]:
|
elif keys[self.control_handler.controls['Left']]:
|
||||||
if current_time - self.move_timer > self.move_interval: # Ensure delay between movements
|
if current_time - self.move_timer > self.move_interval: # Ensure delay between movements
|
||||||
if not self.game_grid.check_collision(self.current_tetromino.shape, self.current_tetromino.x - 1, self.current_tetromino.y):
|
if not self.game_grid.check_collision(self.current_tetromino.shape, self.current_tetromino.x - 1, self.current_tetromino.y):
|
||||||
self.current_tetromino.move(-1, 0) # Move left
|
self.current_tetromino.move(-1, 0) # Move left
|
||||||
self.move_timer = current_time # Reset timer
|
self.move_timer = current_time # Reset timer
|
||||||
elif keys[pg.K_DOWN]:
|
elif keys[self.control_handler.controls['Down']]:
|
||||||
fall_interval = 40
|
fall_interval = 40
|
||||||
|
|
||||||
# Handle automatic falling based on timer
|
# Handle automatic falling based on timer
|
||||||
|
|||||||
+28
-8
@@ -75,11 +75,6 @@ class StartMenu:
|
|||||||
self.quit.update(self.screen)
|
self.quit.update(self.screen)
|
||||||
pg.display.update()
|
pg.display.update()
|
||||||
|
|
||||||
def draw_options(self):
|
|
||||||
self.screen.blit(get_background(), (0, 0))
|
|
||||||
|
|
||||||
pg.display.update()
|
|
||||||
|
|
||||||
|
|
||||||
class GameMenu:
|
class GameMenu:
|
||||||
def __init__(self, screen, clock):
|
def __init__(self, screen, clock):
|
||||||
@@ -126,8 +121,11 @@ class OptionMenu:
|
|||||||
def __init__(self, screen, clock):
|
def __init__(self, screen, clock):
|
||||||
self.menu_running = None
|
self.menu_running = None
|
||||||
self.screen = screen
|
self.screen = screen
|
||||||
|
self.custom_screen = pygame.Surface((setting.width /2, setting.height /2))
|
||||||
self.clock = clock
|
self.clock = clock
|
||||||
self.screen_size = self.screen.get_size()
|
|
||||||
|
self.control_handler = setting.control_handler
|
||||||
|
self.actions = setting.actions
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.menu_running = True
|
self.menu_running = True
|
||||||
@@ -145,8 +143,30 @@ class OptionMenu:
|
|||||||
if event.type == pg.KEYDOWN:
|
if event.type == pg.KEYDOWN:
|
||||||
if event.key == pg.K_ESCAPE:
|
if event.key == pg.K_ESCAPE:
|
||||||
self.menu_running = False
|
self.menu_running = False
|
||||||
|
if event.key == self.control_handler.controls['Left']:
|
||||||
|
self.actions['Left'] = True
|
||||||
|
if event.key == self.control_handler.controls['Right']:
|
||||||
|
self.actions['Right'] = True
|
||||||
|
if event.key == self.control_handler.controls['Up']:
|
||||||
|
self.actions['Up'] = True
|
||||||
|
if event.key == self.control_handler.controls['Down']:
|
||||||
|
self.actions['Down'] = True
|
||||||
|
|
||||||
|
if event.type == pygame.KEYUP:
|
||||||
|
if event.key == self.control_handler.controls['Left']:
|
||||||
|
self.actions['Left'] = False
|
||||||
|
if event.key == self.control_handler.controls['Right']:
|
||||||
|
self.actions['Right'] = False
|
||||||
|
if event.key == self.control_handler.controls['Up']:
|
||||||
|
self.actions['Up'] = False
|
||||||
|
if event.key == self.control_handler.controls['Down']:
|
||||||
|
self.actions['Down'] = False
|
||||||
|
self.control_handler.update(self.actions)
|
||||||
|
setting.actions = self.actions
|
||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
self.screen.blit(get_background(), (0, 0))
|
self.custom_screen.blit(get_background(), (0, 0))
|
||||||
|
self.control_handler.render(self.custom_screen)
|
||||||
|
self.screen.blit(pygame.transform.scale(self.custom_screen, (setting.width + 400, setting.height + 400)), (0, 0))
|
||||||
pg.display.update()
|
pg.display.update()
|
||||||
|
reset_keys(self.actions)
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
from game.util.control import Controls_Handler
|
||||||
|
from game.util.util import load_save
|
||||||
|
|
||||||
width, height = 0, 0
|
width, height = 0, 0
|
||||||
|
|
||||||
#grid surface
|
#grid surface
|
||||||
@@ -6,6 +9,11 @@ g_height, g_width = 600, 300
|
|||||||
#score
|
#score
|
||||||
game_score = 0
|
game_score = 0
|
||||||
|
|
||||||
|
actions = {"Left": False, "Right": False, "Up": False, "Down": False, "Hold": False}
|
||||||
|
|
||||||
|
save = load_save()
|
||||||
|
control_handler = Controls_Handler(save)
|
||||||
|
|
||||||
rows = 20
|
rows = 20
|
||||||
cols = 10
|
cols = 10
|
||||||
cell_size = 30
|
cell_size = 30
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import pygame, sys
|
import pygame, sys
|
||||||
from util import write_save
|
from game.util.util import write_save, get_font
|
||||||
|
|
||||||
class Controls_Handler():
|
class Controls_Handler():
|
||||||
def __init__(self, save):
|
def __init__(self, save):
|
||||||
@@ -13,9 +13,9 @@ class Controls_Handler():
|
|||||||
else: self.navigate_menu(actions)
|
else: self.navigate_menu(actions)
|
||||||
|
|
||||||
def render(self, surface):
|
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.draw_text(surface, "Control Profile " + str(self.curr_block+1) , 20, pygame.Color((255, 255, 255)), 480 / 2, 270/8)
|
||||||
self.display_controls(surface, self.save_file["controls"][str(self.curr_block)])
|
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)
|
if self.curr_block == self.save_file["current_profile"]: self.draw_text(surface, "*" , 20, pygame.Color((255, 255, 255)), 20, 20)
|
||||||
|
|
||||||
def navigate_menu(self, actions):
|
def navigate_menu(self, actions):
|
||||||
# Move the cursor up and down
|
# Move the cursor up and down
|
||||||
@@ -25,8 +25,7 @@ class Controls_Handler():
|
|||||||
if actions["Left"]: self.curr_block = (self.curr_block -1) % len(self.save_file["controls"])
|
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"])
|
if actions["Right"]: self.curr_block = (self.curr_block +1) % len(self.save_file["controls"])
|
||||||
# Handle Selection
|
# Handle Selection
|
||||||
if actions["Action1"] or actions["Start"]:
|
if pygame.key.get_pressed()[pygame.K_RETURN]:
|
||||||
# Set the current profile to be the main one
|
|
||||||
if self.cursor_dict[self.curr_index] == "Set":
|
if self.cursor_dict[self.curr_index] == "Set":
|
||||||
self.controls = self.save_file["controls"][str(self.curr_block)]
|
self.controls = self.save_file["controls"][str(self.curr_block)]
|
||||||
self.save_file["current_profile"] = self.curr_block
|
self.save_file["current_profile"] = self.curr_block
|
||||||
@@ -55,18 +54,18 @@ class Controls_Handler():
|
|||||||
done = True
|
done = True
|
||||||
|
|
||||||
def display_controls(self,surface, controls):
|
def display_controls(self,surface, controls):
|
||||||
color = (255,13,5) if self.selected else (255,250,239)
|
color = (173, 2, 2) if self.selected else (207, 190, 41)
|
||||||
pygame.draw.rect(surface, color, (80 , 270/4 - 10 + (self.curr_index*30), 320, 20) )
|
pygame.draw.rect(surface, color, (80 , 270/4 - 10 + (self.curr_index*30), 320, 20) )
|
||||||
i = 0
|
i = 0
|
||||||
for control in controls:
|
for control in controls:
|
||||||
self.draw_text(surface, control + ' - ' + pygame.key.name(controls[control]),20,
|
self.draw_text(surface, control + ' - ' + pygame.key.name(controls[control]),20,
|
||||||
pygame.Color((0,0,0)), 480 / 2, 270/4 + i)
|
pygame.Color((255,255,255)), 480 / 2, 270/4 + i)
|
||||||
i += 30
|
i += 30
|
||||||
self.draw_text(surface, "Set Current Profile",20, pygame.Color((0,0,0)), 480 / 2, 270/4 + i)
|
self.draw_text(surface, "Set Current Profile",20, pygame.Color((255, 255, 255)), 480 / 2, 270/4 + i)
|
||||||
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
self.selected = False
|
self.selected = False
|
||||||
self.font = pygame.font.Font("RetroFont.ttf", 20)
|
self.font = get_font(20)
|
||||||
self.cursor_dict = {}
|
self.cursor_dict = {}
|
||||||
self.curr_index = 0
|
self.curr_index = 0
|
||||||
i = 0
|
i = 0
|
||||||
|
|||||||
+9
-14
@@ -1,24 +1,18 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import json, os
|
import json, os
|
||||||
|
|
||||||
# calculate fall speed
|
|
||||||
|
|
||||||
def get_fall_interval(score):
|
def get_fall_interval(score):
|
||||||
"""
|
"""
|
||||||
Calculate the fall interval based on the current score.
|
Calculate the fall interval based on the current score.
|
||||||
"""
|
"""
|
||||||
base_interval = 1000 # Base interval in milliseconds
|
base_interval = 1000
|
||||||
decrease_per_1000_points = 50 # Decrease in ms per 1000 points
|
decrease_per_1000_points = 50
|
||||||
min_interval = 100 # Minimum interval in milliseconds
|
min_interval = 100
|
||||||
|
|
||||||
# Calculate the fall interval
|
|
||||||
interval = base_interval - (score // 400) * decrease_per_1000_points
|
interval = base_interval - (score // 400) * decrease_per_1000_points
|
||||||
|
|
||||||
# Ensure the interval doesn't go below the minimum
|
|
||||||
return max(interval, min_interval)
|
return max(interval, min_interval)
|
||||||
|
|
||||||
# Draw text simplified
|
|
||||||
|
|
||||||
def draw_text(surface, text, font, x, y, color):
|
def draw_text(surface, text, font, x, y, color):
|
||||||
"""Utility function to render text onto a surface."""
|
"""Utility function to render text onto a surface."""
|
||||||
text_surface = font.render(text, True, color)
|
text_surface = font.render(text, True, color)
|
||||||
@@ -26,6 +20,7 @@ def draw_text(surface, text, font, x, y, color):
|
|||||||
surface.blit(text_surface, text_rect)
|
surface.blit(text_surface, text_rect)
|
||||||
|
|
||||||
def get_font(size):
|
def get_font(size):
|
||||||
|
pygame.font.init()
|
||||||
return pygame.font.Font("assets/font.ttf", size)
|
return pygame.font.Font("assets/font.ttf", size)
|
||||||
|
|
||||||
def get_background():
|
def get_background():
|
||||||
@@ -35,7 +30,7 @@ def get_background():
|
|||||||
|
|
||||||
def load_score(filename):
|
def load_score(filename):
|
||||||
"""Load JSON data only if the file is not empty."""
|
"""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
|
if os.path.exists(filename) and os.path.getsize(filename) > 0:
|
||||||
with open(filename, "r") as file:
|
with open(filename, "r") as file:
|
||||||
return json.load(file)
|
return json.load(file)
|
||||||
return {}
|
return {}
|
||||||
@@ -70,10 +65,10 @@ def load_save():
|
|||||||
def create_save():
|
def create_save():
|
||||||
new_save = {
|
new_save = {
|
||||||
"controls":{
|
"controls":{
|
||||||
"0" :{"Left": pygame.K_a, "Right": pygame.K_d, "Up": pygame.K_w, "Down": pygame.K_s,
|
"0" :{"Left": pygame.K_LEFT, "Right": pygame.K_RIGHT, "Up": pygame.K_UP, "Down": pygame.K_DOWN,
|
||||||
"Start": pygame.K_RETURN, "Action1": pygame.K_SPACE},
|
"Hold": pygame.K_c},
|
||||||
"1" :{"Left": pygame.K_a, "Right": pygame.K_d, "Up": pygame.K_w, "Down": pygame.K_s,
|
"1" :{"Left": pygame.K_LEFT, "Right": pygame.K_RIGHT, "Up": pygame.K_UP, "Down": pygame.K_DOWN,
|
||||||
"Start": pygame.K_RETURN, "Action1": pygame.K_SPACE}
|
"Hold": pygame.K_c}
|
||||||
},
|
},
|
||||||
"current_profile": 0
|
"current_profile": 0
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-3
@@ -1,3 +1 @@
|
|||||||
{
|
7300
|
||||||
"score": [1000, 700, 500, 6000, 5000]
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user