From 9ae90e1b61debd8fc4b8bc8e29b7e3f39859e586 Mon Sep 17 00:00:00 2001 From: Vincent Rodley Date: Wed, 20 Aug 2025 14:23:34 +1200 Subject: [PATCH] holy my code was bad. optimized tile creation, made the colours nice, rounded buttons --- README.md | 2 +- gui/button.py | 5 +++-- gui/game.py | 51 +++++++++++++++++++++++++++++++++------------------ 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index d5b57bc..b5943f5 100644 --- a/README.md +++ b/README.md @@ -32,4 +32,4 @@ Both game modes include: ### Player vs CPU -- Fix priority issues: sometimes the CPU prefers a vertical 3-in-a-row instead of completing a horizontal win. \ No newline at end of file +- Fix priority issues: sometimes the CPU prefers a vertical 3-in-a-row instead of completing a horizontal win. \ No newline at end of file diff --git a/gui/button.py b/gui/button.py index cfa9884..8de86c0 100644 --- a/gui/button.py +++ b/gui/button.py @@ -1,7 +1,7 @@ import pygame class Button: - def __init__(self, x, y, width, height, text, color, hover_color, text_color, action, font, font_size, extra_data = None): + def __init__(self, x, y, width, height, text, color, hover_color, text_color, action, font, font_size, extra_data = None, rounding = 0): self.rect = pygame.Rect(x, y, width, height) self.text = text self.color = color @@ -11,9 +11,10 @@ class Button: self.action = action self.font = pygame.font.Font(font, font_size) self.extra_data = extra_data + self.rounding = rounding def draw(self, screen): - pygame.draw.rect(screen, self.current_color, self.rect) + pygame.draw.rect(screen, self.current_color, self.rect, border_radius=self.rounding) text_surface = self.font.render(self.text, True, self.text_color) text_rect = text_surface.get_rect(center=self.rect.center) screen.blit(text_surface, text_rect) diff --git a/gui/game.py b/gui/game.py index d5ca7c6..fcef391 100644 --- a/gui/game.py +++ b/gui/game.py @@ -4,28 +4,30 @@ from pygame import Vector2 as v2, Color as Colour from button import Button -# some constst +# some pygame constst WINDOW_SIZE = (768, 768) -WINDOW_SCALE = 1 TARGET_FPS = 60 # pygame inits pygame.init() -display = pygame.display.set_mode(v2(WINDOW_SIZE)*WINDOW_SCALE) +display = pygame.display.set_mode(v2(WINDOW_SIZE)) clock = pygame.time.Clock() # more variable inits -menu = "start" +menu = "" tiles = [] +COLS = 7 +ROWS = 6 # menu functions def change_menu(targetMenu): global menu menu = targetMenu - display.fill('black') + display.fill(bg_color) def start_game_func(*_): change_menu("game") + create_tiles() def settings_menu(*_): change_menu("settings") @@ -33,6 +35,18 @@ def settings_menu(*_): def go_back(*_): change_menu("start") +def create_tiles(): + global tiles + tiles = [] + for c in range(COLS): + for r in range(ROWS): + tile = Button( + 50*c+50, 50*r+50, 30, 30, str(len(tiles)), + tile_color, tile_hover, tile_text, tile_press, None, 30, (len(tiles),c,r), + rounding=5 + ) + tiles.append(tile) + # gets called when you click on a tile def tile_press(tile): tile_id,x,y = tile.extra_data @@ -50,9 +64,20 @@ if __name__ == "__main__": x = WINDOW_SIZE[0] / 2 - width / 2 # center of the screen horizontally y = (WINDOW_SIZE[1] / 2 - height / 2) # center of the screen vertically - start_button = Button(x, y - 100, width, height, "Start Game", (0, 150, 0), (255, 0, 0), (255, 255, 255), start_game_func, "Baloo2-Bold.ttf", 50) - settings_button = Button(x, y-100+height*2, width, height, "Settings", (0, 150, 0), (255, 0, 0), (255, 255, 255), settings_menu, "Baloo2-Bold.ttf", 50) - go_back_button = Button(x, y, width, height, "Go back", (0, 150, 0), (255, 0, 0), (255, 255, 255), go_back, "Baloo2-Bold.ttf", 50) + # === Color Variables === + primary_color = (70, 130, 180) # button background + hover_color = (100, 149, 237) # button hover + text_color = (245, 245, 245) # button text + tile_color = (200, 200, 200) # tile main + tile_hover = (170, 170, 170) # tile hover + tile_text = (50, 50, 50) # tile text + bg_color = (30, 30, 40) # main background + + start_button = Button(x, y - 100, width, height, "Start Game", primary_color, hover_color, text_color, start_game_func, "Baloo2-Bold.ttf", 50, rounding=8) + settings_button = Button(x, y-100+height*2, width, height, "Settings", primary_color, hover_color, text_color, settings_menu, "Baloo2-Bold.ttf", 50, rounding=8) + go_back_button = Button(x, y, width, height, "Go back", primary_color, hover_color, text_color, go_back, "Baloo2-Bold.ttf", 50, rounding=8) + + change_menu("start") # Game loop while running: @@ -89,16 +114,6 @@ if __name__ == "__main__": go_back_button.draw(display) # basic connect-4 ahh grid elif menu == "game": - COLS = 7 - ROWS = 6 - - tiles = [] - - for c in range(COLS): - for r in range(ROWS): - tile = Button(50*c+50, 50*r+50, 30, 30, str(len(tiles)), (255, 255, 255), (150, 150, 150), (255, 0, 0), tile_press, None, 30, (len(tiles),c,r)) - tiles.append(tile) - for tile in tiles: tile.draw(display) else: