new menu_manager system (far cleaner), polished win + draw detection
This commit is contained in:
parent
e3735a3786
commit
0bbcee8847
222
gui/game.py
222
gui/game.py
|
|
@ -1,4 +1,3 @@
|
||||||
# imports
|
|
||||||
import pygame
|
import pygame
|
||||||
from button import Button
|
from button import Button
|
||||||
from menu_manager import MenuManager
|
from menu_manager import MenuManager
|
||||||
|
|
@ -12,46 +11,33 @@ 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
|
||||||
|
|
||||||
# init the pygame
|
# inits
|
||||||
pygame.init()
|
pygame.init()
|
||||||
font = pygame.font.Font("Baloo2-Bold.ttf", 40)
|
font = pygame.font.Font("Baloo2-Bold.ttf", 40)
|
||||||
display = pygame.display.set_mode(WINDOW_SIZE)
|
display = pygame.display.set_mode(WINDOW_SIZE)
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
# colourss
|
# coloursss
|
||||||
primary_colour = (70, 130, 180) # button background
|
primary_colour = (70, 130, 180)
|
||||||
hover_colour = (51, 102, 145) # button hover
|
hover_colour = (51, 102, 145)
|
||||||
text_colour = (245, 245, 245) # button text
|
text_colour = (245, 245, 245)
|
||||||
tile_colour = (200, 200, 200) # tile main
|
tile_colour = (200, 200, 200)
|
||||||
tile_hover = (170, 170, 170) # tile hover
|
tile_hover = (170, 170, 170)
|
||||||
tile_text = (50, 50, 50) # tile text
|
tile_text = (50, 50, 50)
|
||||||
bg_colour = (30, 30, 40) # main background
|
bg_colour = (30, 30, 40)
|
||||||
|
|
||||||
red_tile = (255, 0, 0) # the red that a red tile is
|
red_tile = (255, 0, 0)
|
||||||
red_tile_hover = (220, 0, 0) # the red that a hovered red tile is
|
red_tile_hover = (220, 0, 0)
|
||||||
yellow_tile = (255, 255, 0) # the yellow that a yellow tile is
|
yellow_tile = (255, 255, 0)
|
||||||
yellow_tile_hover = (220, 220, 0) # the yellow that a hovered yellow tile is
|
yellow_tile_hover = (220, 220, 0)
|
||||||
|
|
||||||
# variables
|
# game state inits
|
||||||
tiles = []
|
tiles = []
|
||||||
menu_manager = MenuManager(display, bg_colour) # background colour
|
|
||||||
player = "red"
|
player = "red"
|
||||||
board_full = False
|
board_full = False
|
||||||
|
winner = None # "red", "yellow", or None for draw
|
||||||
|
|
||||||
# menu functions
|
# tile + board logic
|
||||||
def start_game_func(*_):
|
|
||||||
global board_full
|
|
||||||
board_full = False
|
|
||||||
create_tiles()
|
|
||||||
menu_manager.change_menu("game")
|
|
||||||
|
|
||||||
def settings_menu(*_):
|
|
||||||
menu_manager.change_menu("settings")
|
|
||||||
|
|
||||||
def go_back(*_):
|
|
||||||
menu_manager.change_menu("start")
|
|
||||||
|
|
||||||
# tile stuff
|
|
||||||
def create_tiles():
|
def create_tiles():
|
||||||
global tiles
|
global tiles
|
||||||
tiles = []
|
tiles = []
|
||||||
|
|
@ -64,21 +50,33 @@ def create_tiles():
|
||||||
for r in range(ROWS):
|
for r in range(ROWS):
|
||||||
x = start_x + c * (TILE_SIZE + TILE_SPACING)
|
x = start_x + c * (TILE_SIZE + TILE_SPACING)
|
||||||
y = start_y + r * (TILE_SIZE + TILE_SPACING)
|
y = start_y + r * (TILE_SIZE + TILE_SPACING)
|
||||||
|
|
||||||
id = str(c * ROWS + r)
|
|
||||||
|
|
||||||
tile = Button(
|
tile = Button(
|
||||||
x, y, TILE_SIZE, TILE_SIZE, "",
|
x, y, TILE_SIZE, TILE_SIZE, "",
|
||||||
tile_colour, tile_hover, tile_text, tile_press, None, 30, (id, c, r),
|
tile_colour, tile_hover, tile_text,
|
||||||
|
tile_press, None, 30, (c, r),
|
||||||
rounding=30
|
rounding=30
|
||||||
)
|
)
|
||||||
col.append(tile)
|
col.append(tile)
|
||||||
tiles.append(col)
|
tiles.append(col)
|
||||||
|
|
||||||
def checkWin():
|
def drop_tile(col_index):
|
||||||
|
column = tiles[col_index]
|
||||||
|
for r in reversed(range(ROWS)):
|
||||||
|
target_tile = column[r]
|
||||||
|
if target_tile.colour == tile_colour:
|
||||||
|
if player == "red":
|
||||||
|
target_tile.colour = red_tile
|
||||||
|
target_tile.hover_colour = red_tile_hover
|
||||||
|
else:
|
||||||
|
target_tile.colour = yellow_tile
|
||||||
|
target_tile.hover_colour = yellow_tile_hover
|
||||||
|
return r # row where tile landed
|
||||||
|
return None # column full
|
||||||
|
|
||||||
|
def check_win():
|
||||||
global tiles, player
|
global tiles, player
|
||||||
|
|
||||||
colours = [yellow_tile, yellow_tile_hover] if player == "red" else [red_tile, red_tile_hover] # this is backwards on purpose
|
colours = [yellow_tile, yellow_tile_hover] if player == "yellow" else [red_tile, red_tile_hover]
|
||||||
|
|
||||||
rows, cols = (6, 7)
|
rows, cols = (6, 7)
|
||||||
winCount = 4
|
winCount = 4
|
||||||
|
|
@ -99,150 +97,114 @@ def checkWin():
|
||||||
if all(tiles[col + i][row - i].colour in colours for i in range(winCount)):
|
if all(tiles[col + i][row - i].colour in colours for i in range(winCount)):
|
||||||
return [(col + i, row - i) for i in range(winCount)]
|
return [(col + i, row - i) for i in range(winCount)]
|
||||||
|
|
||||||
def drop_tile(col_index):
|
|
||||||
global player
|
|
||||||
column = tiles[col_index]
|
|
||||||
|
|
||||||
# find the lowest unoccupied tile in this column
|
|
||||||
for r in reversed(range(ROWS)):
|
|
||||||
target_tile = column[r]
|
|
||||||
|
|
||||||
# check if already taken (coloured by a player)
|
|
||||||
if target_tile.colour == tile_colour:
|
|
||||||
# claim this tile for the current player
|
|
||||||
target_tile.colour = red_tile if player == "red" else yellow_tile
|
|
||||||
target_tile.hover_colour = red_tile_hover if player == "red" else yellow_tile_hover
|
|
||||||
|
|
||||||
# print(f"Player {player} placed at col {col_index}, row {r}")
|
|
||||||
|
|
||||||
# switch turn
|
|
||||||
player = "yellow" if player == "red" else "red"
|
|
||||||
# print(f"Next turn: {player}")
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
# column is full
|
|
||||||
print(f"Column {col_index} is full!")
|
|
||||||
|
|
||||||
def is_board_full():
|
def is_board_full():
|
||||||
global tiles, board_full
|
|
||||||
for col in tiles:
|
for col in tiles:
|
||||||
for row in reversed(range(ROWS)):
|
for tile in col:
|
||||||
target = col[row]
|
if tile.colour == tile_colour:
|
||||||
|
|
||||||
if target.colour == tile_colour:
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def tile_press(tile: Button):
|
def tile_press(tile: Button):
|
||||||
global board_full
|
global board_full, winner, player
|
||||||
|
if board_full:
|
||||||
|
return
|
||||||
|
|
||||||
id, col_index, row_index = tile.extra_data # we only care about column
|
col_index, row_index = tile.extra_data
|
||||||
drop_tile(col_index)
|
row_dropped = drop_tile(col_index)
|
||||||
|
if row_dropped is None:
|
||||||
|
return # column full
|
||||||
|
|
||||||
win = checkWin()
|
win = check_win()
|
||||||
|
|
||||||
print(f"Win tiles: {win}")
|
|
||||||
|
|
||||||
|
# check for win
|
||||||
if win:
|
if win:
|
||||||
print("Somebody won fr")
|
winner = player
|
||||||
|
|
||||||
if is_board_full():
|
|
||||||
board_full = True
|
board_full = True
|
||||||
print(f"Draw! The board is full.")
|
elif is_board_full():
|
||||||
|
winner = None # draw
|
||||||
|
board_full = True
|
||||||
|
else:
|
||||||
|
player = "yellow" if player == "red" else "red"
|
||||||
|
|
||||||
|
# buttons
|
||||||
# button stuff
|
|
||||||
width, height = 280, 75
|
width, height = 280, 75
|
||||||
x = WINDOW_SIZE[0] / 2 - width / 2
|
x = WINDOW_SIZE[0] / 2 - width / 2
|
||||||
y = WINDOW_SIZE[1] / 2 - height / 2
|
y = WINDOW_SIZE[1] / 2 - height / 2
|
||||||
|
|
||||||
start_button = Button(x, y - 100, width, height, "Start Game",
|
start_button = Button(x, y - 100, width, height, "Start Game",
|
||||||
primary_colour, hover_colour, text_colour,
|
primary_colour, hover_colour, text_colour,
|
||||||
start_game_func, "Baloo2-Bold.ttf", 50, rounding=8)
|
lambda *_: start_game(), "Baloo2-Bold.ttf", 50, rounding=8)
|
||||||
|
|
||||||
settings_button = Button(x, y - 100 + height * 2, width, height, "Settings",
|
settings_button = Button(x, y - 100 + height * 2, width, height, "Settings",
|
||||||
primary_colour, hover_colour, text_colour,
|
primary_colour, hover_colour, text_colour,
|
||||||
settings_menu, "Baloo2-Bold.ttf", 50, rounding=8)
|
lambda *_: menu_manager.change_menu("settings"),
|
||||||
|
"Baloo2-Bold.ttf", 50, rounding=8)
|
||||||
|
|
||||||
go_back_button = Button(x, y, width, height, "Go back",
|
go_back_button = Button(x, y, width, height, "Go back",
|
||||||
primary_colour, hover_colour, text_colour,
|
primary_colour, hover_colour, text_colour,
|
||||||
go_back, "Baloo2-Bold.ttf", 50, rounding=8)
|
lambda *_: menu_manager.change_menu("start"),
|
||||||
|
"Baloo2-Bold.ttf", 50, rounding=8)
|
||||||
|
|
||||||
game_over_button = Button(x, y*2-50, width, height, "Go back",
|
game_over_button = Button(x, y * 2 - 50, width, height, "Go back",
|
||||||
primary_colour, hover_colour, text_colour,
|
primary_colour, hover_colour, text_colour,
|
||||||
go_back, "Baloo2-Bold.ttf", 50, rounding=8)
|
lambda *_: menu_manager.change_menu("start"),
|
||||||
|
"Baloo2-Bold.ttf", 50, rounding=8)
|
||||||
|
|
||||||
game_over_text = Button(x, 50, width, height/1.5, "text",
|
game_over_text = Button(x, 50, width, height / 1.5, "text",
|
||||||
bg_colour, (0, 0, 0), text_colour,
|
bg_colour, (0, 0, 0), text_colour,
|
||||||
None, "Baloo2-Bold.ttf", 50, rounding=8)
|
None, "Baloo2-Bold.ttf", 50, rounding=8)
|
||||||
|
|
||||||
# menu handlers
|
# menu callbacks
|
||||||
# start
|
def start_game():
|
||||||
def start_menu_events(event):
|
global board_full, player, winner
|
||||||
start_button.handle_event(event)
|
board_full = False
|
||||||
settings_button.handle_event(event)
|
winner = None
|
||||||
|
player = "red"
|
||||||
|
create_tiles()
|
||||||
|
menu_manager.change_menu("game")
|
||||||
|
|
||||||
def start_menu_draw():
|
def draw_settings(display):
|
||||||
start_button.draw(display)
|
|
||||||
settings_button.draw(display)
|
|
||||||
|
|
||||||
# settings
|
|
||||||
def settings_menu_events(event):
|
|
||||||
go_back_button.handle_event(event)
|
|
||||||
|
|
||||||
def settings_menu_draw():
|
|
||||||
text_surface = font.render("No settings yet :(", True, text_colour)
|
text_surface = font.render("No settings yet :(", True, text_colour)
|
||||||
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)
|
||||||
go_back_button.draw(display)
|
|
||||||
|
|
||||||
|
def draw_game(display):
|
||||||
# game
|
|
||||||
def game_menu_events(event):
|
|
||||||
if board_full:
|
if board_full:
|
||||||
game_over_button.handle_event(event)
|
if winner:
|
||||||
|
game_over_text.text = f"{winner.upper()} wins!"
|
||||||
else:
|
else:
|
||||||
for col in tiles:
|
game_over_text.text = "Draw!"
|
||||||
for tile in col:
|
|
||||||
tile: Button
|
|
||||||
tile.handle_event(event)
|
|
||||||
|
|
||||||
def game_menu_draw():
|
|
||||||
global player
|
|
||||||
for col in tiles:
|
|
||||||
for tile in col:
|
|
||||||
tile: Button
|
|
||||||
tile.draw(display)
|
|
||||||
|
|
||||||
if board_full:
|
|
||||||
game_over_text.text = f"{'RED' if player == 'red' else 'YELLOW'} WINS"
|
|
||||||
game_over_button.draw(display)
|
game_over_button.draw(display)
|
||||||
game_over_text.draw(display)
|
game_over_text.draw(display)
|
||||||
|
|
||||||
# register the menus
|
# menu manager init + setup
|
||||||
menu_manager.register_menu("start", start_menu_events, start_menu_draw)
|
menu_manager = MenuManager(display, bg_colour)
|
||||||
menu_manager.register_menu("settings", settings_menu_events, settings_menu_draw)
|
|
||||||
menu_manager.register_menu("game", game_menu_events, game_menu_draw)
|
menu_manager.register_menu("start",
|
||||||
|
buttons=[start_button, settings_button]
|
||||||
|
)
|
||||||
|
|
||||||
|
menu_manager.register_menu("settings",
|
||||||
|
buttons=[go_back_button],
|
||||||
|
draw=draw_settings
|
||||||
|
)
|
||||||
|
|
||||||
|
menu_manager.register_menu("game",
|
||||||
|
buttons=lambda: [tile for col in tiles for tile in col] + ([game_over_button] if board_full else []),
|
||||||
|
draw=draw_game
|
||||||
|
)
|
||||||
|
|
||||||
menu_manager.change_menu("start")
|
menu_manager.change_menu("start")
|
||||||
|
|
||||||
|
# main loop
|
||||||
# main loopy loopy
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
running = True
|
running = True
|
||||||
|
|
||||||
while running:
|
while running:
|
||||||
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
|
||||||
if event.type == pygame.KEYDOWN:
|
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
|
||||||
if event.key == pygame.K_ESCAPE:
|
|
||||||
running = False
|
running = False
|
||||||
|
|
||||||
if event.key == pygame.K_d:
|
|
||||||
board_full = True
|
|
||||||
|
|
||||||
menu_manager.handle_event(event)
|
menu_manager.handle_event(event)
|
||||||
|
|
||||||
menu_manager.draw()
|
menu_manager.draw()
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,47 @@
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
class MenuManager:
|
class MenuManager:
|
||||||
def __init__(self, display, bg_color):
|
def __init__(self, display, bg_colour, menus=None):
|
||||||
self.display = display
|
self.display = display
|
||||||
self.bg_color = bg_color
|
self.bg_colour = bg_colour
|
||||||
|
self.menus = menus or {}
|
||||||
self.current_menu = None
|
self.current_menu = None
|
||||||
self.event_handlers = {}
|
|
||||||
self.draw_handlers = {}
|
|
||||||
|
|
||||||
# switch menu, and clear the screen
|
|
||||||
def change_menu(self, name):
|
def change_menu(self, name):
|
||||||
if name not in self.draw_handlers:
|
if name not in self.menus:
|
||||||
raise ValueError(f"Menu: '{name} not registered")
|
raise ValueError(f"Menu '{name}' not registered")
|
||||||
self.current_menu = name
|
self.current_menu = name
|
||||||
self.display.fill(self.bg_color)
|
self.display.fill(self.bg_colour)
|
||||||
|
|
||||||
# register a menus event and draw functions
|
def register_menu(self, name, buttons=None, draw=None):
|
||||||
def register_menu(self, name, event_handler, draw_handler):
|
self.menus[name] = {
|
||||||
self.event_handlers[name] = event_handler
|
"buttons": buttons or [],
|
||||||
self.draw_handlers[name] = draw_handler
|
"draw": draw
|
||||||
|
}
|
||||||
|
|
||||||
# pass event to the menu handler
|
|
||||||
def handle_event(self, event):
|
def handle_event(self, event):
|
||||||
if self.current_menu in self.event_handlers:
|
if not self.current_menu:
|
||||||
self.event_handlers[self.current_menu](event)
|
return
|
||||||
|
buttons = self.menus[self.current_menu]["buttons"]
|
||||||
|
|
||||||
|
# handle dynamic buttons (function returning list)
|
||||||
|
if callable(buttons):
|
||||||
|
buttons = buttons()
|
||||||
|
|
||||||
|
for b in buttons:
|
||||||
|
b.handle_event(event)
|
||||||
|
|
||||||
# draw the current menu
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
if self.current_menu in self.draw_handlers:
|
if not self.current_menu:
|
||||||
self.draw_handlers[self.current_menu]()
|
return
|
||||||
|
buttons = self.menus[self.current_menu]["buttons"]
|
||||||
|
draw_callback = self.menus[self.current_menu]["draw"]
|
||||||
|
|
||||||
|
if callable(buttons):
|
||||||
|
buttons = buttons()
|
||||||
|
|
||||||
|
for b in buttons:
|
||||||
|
b.draw(self.display)
|
||||||
|
|
||||||
|
if draw_callback:
|
||||||
|
draw_callback(self.display)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user