Compare commits
2 Commits
15205b7a3e
...
574d79947b
| Author | SHA1 | Date | |
|---|---|---|---|
| 574d79947b | |||
| 90c8364e40 |
|
|
@ -36,4 +36,6 @@ Both game modes include:
|
||||||
|
|
||||||
### GUI
|
### GUI
|
||||||
|
|
||||||
- Add the CPU
|
- Add the CPU
|
||||||
|
- animations
|
||||||
|
- make the cursor follow the mouse
|
||||||
|
|
@ -256,9 +256,7 @@ def cpu_move_provider(player, board):
|
||||||
try:
|
try:
|
||||||
with open("settings.json", "r") as f:
|
with open("settings.json", "r") as f:
|
||||||
settings = json.load(f)
|
settings = json.load(f)
|
||||||
print(f"Settings: {settings}")
|
|
||||||
search_depth = settings.get("cpu_search_depth", 5)
|
search_depth = settings.get("cpu_search_depth", 5)
|
||||||
print(f"search depth: {search_depth}")
|
|
||||||
except (FileNotFoundError, json.JSONDecodeError):
|
except (FileNotFoundError, json.JSONDecodeError):
|
||||||
search_depth = 5
|
search_depth = 5
|
||||||
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
{
|
{
|
||||||
"display_mode": "emojis",
|
"display_mode": "coloured_background",
|
||||||
"cpu_search_depth": 5
|
"cpu_search_depth": 5
|
||||||
}
|
}
|
||||||
|
|
@ -2,7 +2,7 @@ import pygame
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
class Button:
|
class Button:
|
||||||
def __init__(self, x, y, width, height, text, colour, hover_colour, text_colour, action, font, font_size, extra_data = None, rounding = 0):
|
def __init__(self, x, y, width, height, text, colour, hover_colour, text_colour, action, font, font_size, extra_data=None, rounding=0, outline_colour=None, outline_width=0):
|
||||||
self.rect = pygame.Rect(x, y, width, height)
|
self.rect = pygame.Rect(x, y, width, height)
|
||||||
self.text = text
|
self.text = text
|
||||||
self.colour = colour
|
self.colour = colour
|
||||||
|
|
@ -12,6 +12,8 @@ class Button:
|
||||||
self.action = action
|
self.action = action
|
||||||
self.extra_data = extra_data
|
self.extra_data = extra_data
|
||||||
self.rounding = rounding
|
self.rounding = rounding
|
||||||
|
self.outline_colour = outline_colour
|
||||||
|
self.outline_width = outline_width
|
||||||
|
|
||||||
if isinstance(font, pygame.font.Font):
|
if isinstance(font, pygame.font.Font):
|
||||||
self.font = font
|
self.font = font
|
||||||
|
|
@ -24,10 +26,15 @@ class Button:
|
||||||
|
|
||||||
def draw(self, screen):
|
def draw(self, screen):
|
||||||
pygame.draw.rect(screen, self.current_colour, self.rect, border_radius=self.rounding)
|
pygame.draw.rect(screen, self.current_colour, self.rect, border_radius=self.rounding)
|
||||||
text_surface = self.font.render(self.text, True, self.text_colour)
|
|
||||||
text_rect = text_surface.get_rect(center=self.rect.center)
|
if self.outline_colour and self.outline_width > 0:
|
||||||
screen.blit(text_surface, text_rect)
|
pygame.draw.rect(screen, self.outline_colour, self.rect, width=self.outline_width, border_radius=self.rounding)
|
||||||
|
|
||||||
|
if self.text:
|
||||||
|
text_surface = self.font.render(self.text, True, self.text_colour)
|
||||||
|
text_rect = text_surface.get_rect(center=self.rect.center)
|
||||||
|
screen.blit(text_surface, text_rect)
|
||||||
|
|
||||||
def update_colour(self):
|
def update_colour(self):
|
||||||
if self.rect.collidepoint(pygame.mouse.get_pos()):
|
if self.rect.collidepoint(pygame.mouse.get_pos()):
|
||||||
self.current_colour = self.hover_colour
|
self.current_colour = self.hover_colour
|
||||||
|
|
@ -38,5 +45,4 @@ class Button:
|
||||||
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and self.rect.collidepoint(event.pos):
|
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and self.rect.collidepoint(event.pos):
|
||||||
if self.action:
|
if self.action:
|
||||||
self.action(self)
|
self.action(self)
|
||||||
|
|
||||||
self.update_colour()
|
self.update_colour()
|
||||||
|
|
|
||||||
|
|
@ -13,13 +13,21 @@ TILE_SIZE, TILE_SPACING = 50, 20
|
||||||
GRID_WIDTH = COLS * TILE_SIZE + (COLS - 1) * TILE_SPACING
|
GRID_WIDTH = COLS * TILE_SIZE + (COLS - 1) * TILE_SPACING
|
||||||
GRID_HEIGHT = ROWS * TILE_SIZE + (ROWS - 1) * TILE_SPACING
|
GRID_HEIGHT = ROWS * TILE_SIZE + (ROWS - 1) * TILE_SPACING
|
||||||
|
|
||||||
|
# grid origin cache for cursor drawing
|
||||||
|
GRID_ORIGIN_X = None
|
||||||
|
GRID_ORIGIN_Y = None
|
||||||
|
|
||||||
|
# cursor attributes
|
||||||
|
CURSOR_HEIGHT = 18
|
||||||
|
CURSOR_MARGIN = 14
|
||||||
|
|
||||||
# inits
|
# inits
|
||||||
pygame.init()
|
pygame.init()
|
||||||
font = pygame.font.Font(ROOT_PATH / "Baloo2-Bold.ttf", 50)
|
font = pygame.font.Font(ROOT_PATH / "Baloo2-Bold.ttf", 50)
|
||||||
display = pygame.display.set_mode(WINDOW_SIZE)
|
display = pygame.display.set_mode(WINDOW_SIZE)
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
# coloursss
|
# colours
|
||||||
primary_colour = (70, 130, 180)
|
primary_colour = (70, 130, 180)
|
||||||
hover_colour = (51, 102, 145)
|
hover_colour = (51, 102, 145)
|
||||||
text_colour = (245, 245, 245)
|
text_colour = (245, 245, 245)
|
||||||
|
|
@ -37,16 +45,27 @@ yellow_tile_hover = (220, 220, 0)
|
||||||
tiles = []
|
tiles = []
|
||||||
player = "red"
|
player = "red"
|
||||||
board_full = False
|
board_full = False
|
||||||
winner = None # "red", "yellow", or None for draw
|
winner = None
|
||||||
|
|
||||||
|
# cursor state
|
||||||
|
cursor_col = 0
|
||||||
|
|
||||||
|
# repeat cursor move
|
||||||
|
key_held = None
|
||||||
|
key_held_time = 0
|
||||||
|
initial_delay = 200 # ms before repeat starts
|
||||||
|
repeat_interval = 100 # ms between repeats
|
||||||
|
|
||||||
# tile + board logic
|
# tile + board logic
|
||||||
def create_tiles():
|
def create_tiles():
|
||||||
global tiles
|
global tiles, GRID_ORIGIN_X, GRID_ORIGIN_Y
|
||||||
tiles = []
|
tiles = []
|
||||||
|
|
||||||
start_x = (WINDOW_SIZE[0] - GRID_WIDTH) // 2
|
start_x = (WINDOW_SIZE[0] - GRID_WIDTH) // 2
|
||||||
start_y = (WINDOW_SIZE[1] - GRID_HEIGHT) // 2
|
start_y = (WINDOW_SIZE[1] - GRID_HEIGHT) // 2
|
||||||
|
|
||||||
|
GRID_ORIGIN_X, GRID_ORIGIN_Y = start_x, start_y
|
||||||
|
|
||||||
for c in range(COLS):
|
for c in range(COLS):
|
||||||
col = []
|
col = []
|
||||||
for r in range(ROWS):
|
for r in range(ROWS):
|
||||||
|
|
@ -56,7 +75,7 @@ def create_tiles():
|
||||||
x, y, TILE_SIZE, TILE_SIZE, "",
|
x, y, TILE_SIZE, TILE_SIZE, "",
|
||||||
tile_colour, tile_hover, tile_text,
|
tile_colour, tile_hover, tile_text,
|
||||||
tile_press, None, 30, (c, r),
|
tile_press, None, 30, (c, r),
|
||||||
rounding=30
|
rounding=30, outline_colour=(0,0,0), outline_width=5
|
||||||
)
|
)
|
||||||
col.append(tile)
|
col.append(tile)
|
||||||
tiles.append(col)
|
tiles.append(col)
|
||||||
|
|
@ -72,8 +91,8 @@ def drop_tile(col_index):
|
||||||
else:
|
else:
|
||||||
target_tile.colour = yellow_tile
|
target_tile.colour = yellow_tile
|
||||||
target_tile.hover_colour = yellow_tile_hover
|
target_tile.hover_colour = yellow_tile_hover
|
||||||
return r # row where tile landed
|
return r
|
||||||
return None # column full
|
return None
|
||||||
|
|
||||||
def check_win():
|
def check_win():
|
||||||
global tiles, player
|
global tiles, player
|
||||||
|
|
@ -106,28 +125,30 @@ def is_board_full():
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def tile_press(tile: Button):
|
# drop a tile in a column
|
||||||
|
def play_move(col_index: int):
|
||||||
global board_full, winner, player
|
global board_full, winner, player
|
||||||
if board_full:
|
if board_full or not tiles:
|
||||||
return
|
return
|
||||||
|
|
||||||
col_index, row_index = tile.extra_data
|
|
||||||
row_dropped = drop_tile(col_index)
|
row_dropped = drop_tile(col_index)
|
||||||
if row_dropped is None:
|
if row_dropped is None:
|
||||||
return # column full
|
return
|
||||||
|
|
||||||
win = check_win()
|
win = check_win()
|
||||||
|
|
||||||
# check for win
|
|
||||||
if win:
|
if win:
|
||||||
winner = player
|
winner = player
|
||||||
board_full = True
|
board_full = True
|
||||||
elif is_board_full():
|
elif is_board_full():
|
||||||
winner = None # draw
|
winner = None
|
||||||
board_full = True
|
board_full = True
|
||||||
else:
|
else:
|
||||||
player = "yellow" if player == "red" else "red"
|
player = "yellow" if player == "red" else "red"
|
||||||
|
|
||||||
|
def tile_press(tile: Button):
|
||||||
|
col_index, _row_index = tile.extra_data
|
||||||
|
play_move(col_index)
|
||||||
|
|
||||||
# buttons
|
# buttons
|
||||||
width, height = 280, 75
|
width, height = 280, 75
|
||||||
x = WINDOW_SIZE[0] / 2 - width / 2
|
x = WINDOW_SIZE[0] / 2 - width / 2
|
||||||
|
|
@ -158,10 +179,11 @@ game_over_text = Button(x, 50, width, height / 1.5, "text",
|
||||||
|
|
||||||
# menu callbacks
|
# menu callbacks
|
||||||
def start_game():
|
def start_game():
|
||||||
global board_full, player, winner
|
global board_full, player, winner, cursor_col
|
||||||
board_full = False
|
board_full = False
|
||||||
winner = None
|
winner = None
|
||||||
player = "red"
|
player = "red"
|
||||||
|
cursor_col = 0
|
||||||
create_tiles()
|
create_tiles()
|
||||||
menu_manager.change_menu("game")
|
menu_manager.change_menu("game")
|
||||||
|
|
||||||
|
|
@ -170,11 +192,60 @@ def draw_settings(display):
|
||||||
text_rect = text_surface.get_rect(center=(WINDOW_SIZE[0] / 2, WINDOW_SIZE[1] / 2 - 100))
|
text_rect = text_surface.get_rect(center=(WINDOW_SIZE[0] / 2, WINDOW_SIZE[1] / 2 - 100))
|
||||||
display.blit(text_surface, text_rect)
|
display.blit(text_surface, text_rect)
|
||||||
|
|
||||||
|
# checks if a column is full
|
||||||
|
def column_is_full(c: int) -> bool:
|
||||||
|
return all(t.colour != tile_colour for t in tiles[c])
|
||||||
|
|
||||||
|
# helper for ghost, find the lowest empty tile
|
||||||
|
def lowest_empty_row(c: int):
|
||||||
|
for r in reversed(range(ROWS)):
|
||||||
|
if tiles[c][r].colour == tile_colour:
|
||||||
|
return r
|
||||||
|
return None
|
||||||
|
|
||||||
|
# ghost preview tile
|
||||||
|
def draw_ghost_piece(display):
|
||||||
|
if not tiles or GRID_ORIGIN_X is None:
|
||||||
|
return
|
||||||
|
row = lowest_empty_row(cursor_col)
|
||||||
|
if row is None: # column full
|
||||||
|
return
|
||||||
|
|
||||||
|
# semi-transparent ghost color
|
||||||
|
base_color = red_tile if player == "red" else yellow_tile
|
||||||
|
ghost_color = (*base_color, 120) # RGBA
|
||||||
|
|
||||||
|
# compute position
|
||||||
|
x = GRID_ORIGIN_X + cursor_col * (TILE_SIZE + TILE_SPACING)
|
||||||
|
y = GRID_ORIGIN_Y + row * (TILE_SIZE + TILE_SPACING)
|
||||||
|
|
||||||
|
# create surface with alpha
|
||||||
|
surf = pygame.Surface((TILE_SIZE, TILE_SIZE), pygame.SRCALPHA)
|
||||||
|
pygame.draw.circle(surf, ghost_color, (TILE_SIZE//2, TILE_SIZE//2), TILE_SIZE//2 - 4)
|
||||||
|
# --- NEW: white outline ---
|
||||||
|
pygame.draw.circle(surf, (255, 255, 255), (TILE_SIZE//2, TILE_SIZE//2), TILE_SIZE//2 - 4, 3)
|
||||||
|
|
||||||
|
display.blit(surf, (x, y))
|
||||||
|
|
||||||
|
# draws the cursor
|
||||||
|
def draw_cursor(display):
|
||||||
|
if not tiles or GRID_ORIGIN_X is None:
|
||||||
|
return
|
||||||
|
base_color = red_tile if player == "red" else yellow_tile
|
||||||
|
color = base_color if not column_is_full(cursor_col) else (120, 120, 120)
|
||||||
|
col_center_x = GRID_ORIGIN_X + cursor_col * (TILE_SIZE + TILE_SPACING) + TILE_SIZE // 2
|
||||||
|
y_top = GRID_ORIGIN_Y + GRID_HEIGHT + CURSOR_MARGIN
|
||||||
|
points = [
|
||||||
|
(col_center_x, y_top),
|
||||||
|
(col_center_x - TILE_SIZE//3, y_top + CURSOR_HEIGHT),
|
||||||
|
(col_center_x + TILE_SIZE//3, y_top + CURSOR_HEIGHT),
|
||||||
|
]
|
||||||
|
pygame.draw.polygon(display, color, points)
|
||||||
|
|
||||||
def draw_game(display):
|
def draw_game(display):
|
||||||
if board_full:
|
if board_full:
|
||||||
if winner:
|
if winner:
|
||||||
game_over_text.text = f"{winner.title()} wins!"
|
game_over_text.text = f"{winner.title()} wins!"
|
||||||
# Set the text colour based on the winner
|
|
||||||
if winner == "red":
|
if winner == "red":
|
||||||
game_over_text.text_colour = red_tile
|
game_over_text.text_colour = red_tile
|
||||||
elif winner == "yellow":
|
elif winner == "yellow":
|
||||||
|
|
@ -184,16 +255,14 @@ def draw_game(display):
|
||||||
game_over_button.draw(display)
|
game_over_button.draw(display)
|
||||||
else:
|
else:
|
||||||
game_over_text.text = "Red's turn!" if player == "red" else "Yellow's turn!"
|
game_over_text.text = "Red's turn!" if player == "red" else "Yellow's turn!"
|
||||||
if player == "red":
|
game_over_text.text_colour = red_tile if player == "red" else yellow_tile
|
||||||
game_over_text.text_colour = red_tile
|
|
||||||
elif player == "yellow":
|
|
||||||
game_over_text.text_colour = yellow_tile
|
|
||||||
|
|
||||||
|
|
||||||
game_over_text.draw(display)
|
game_over_text.draw(display)
|
||||||
|
|
||||||
|
|
||||||
# menu manager init + setup
|
if not board_full:
|
||||||
|
draw_cursor(display)
|
||||||
|
draw_ghost_piece(display)
|
||||||
|
|
||||||
|
# menu manager init
|
||||||
menu_manager = MenuManager(display, bg_colour)
|
menu_manager = MenuManager(display, bg_colour)
|
||||||
|
|
||||||
menu_manager.register_menu("start",
|
menu_manager.register_menu("start",
|
||||||
|
|
@ -212,10 +281,20 @@ menu_manager.register_menu("game",
|
||||||
|
|
||||||
menu_manager.change_menu("start")
|
menu_manager.change_menu("start")
|
||||||
|
|
||||||
|
# cursor movement + wrapping
|
||||||
|
def move_cursor(direction: str):
|
||||||
|
global cursor_col
|
||||||
|
if direction == "left":
|
||||||
|
cursor_col = (cursor_col - 1) % COLS
|
||||||
|
elif direction == "right":
|
||||||
|
cursor_col = (cursor_col + 1) % COLS
|
||||||
|
|
||||||
# main loop
|
# main loop
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
running = True
|
running = True
|
||||||
while running:
|
while running:
|
||||||
|
dt = clock.tick(TARGET_FPS) # time since last frame
|
||||||
|
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
running = False
|
running = False
|
||||||
|
|
@ -224,9 +303,32 @@ if __name__ == "__main__":
|
||||||
running = False
|
running = False
|
||||||
elif event.key == pygame.K_d:
|
elif event.key == pygame.K_d:
|
||||||
board_full = True
|
board_full = True
|
||||||
|
elif event.key == pygame.K_LEFT:
|
||||||
|
move_cursor("left")
|
||||||
|
key_held = "left"
|
||||||
|
key_held_time = 0
|
||||||
|
elif event.key == pygame.K_RIGHT:
|
||||||
|
move_cursor("right")
|
||||||
|
key_held = "right"
|
||||||
|
key_held_time = 0
|
||||||
|
elif event.key in (pygame.K_RETURN, pygame.K_KP_ENTER):
|
||||||
|
if tiles and not board_full and not column_is_full(cursor_col):
|
||||||
|
play_move(cursor_col)
|
||||||
|
elif event.type == pygame.KEYUP:
|
||||||
|
if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
|
||||||
|
key_held = None
|
||||||
|
key_held_time = 0
|
||||||
|
|
||||||
menu_manager.handle_event(event)
|
menu_manager.handle_event(event)
|
||||||
|
|
||||||
|
# repeat held key
|
||||||
|
if key_held:
|
||||||
|
key_held_time += dt
|
||||||
|
if key_held_time > initial_delay:
|
||||||
|
if (key_held_time - initial_delay) // repeat_interval > \
|
||||||
|
(key_held_time - initial_delay - dt) // repeat_interval:
|
||||||
|
move_cursor(key_held)
|
||||||
|
|
||||||
display.fill(bg_colour)
|
display.fill(bg_colour)
|
||||||
menu_manager.draw()
|
menu_manager.draw()
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
clock.tick(TARGET_FPS)
|
|
||||||
Loading…
Reference in New Issue
Block a user