win checkigngit add game.py git add game.py

This commit is contained in:
Vincent Rodley 2025-08-27 19:36:37 +12:00
parent 26c88cabab
commit da4e921da1

View File

@ -18,14 +18,14 @@ 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()
# colorss # colourss
primary_color = (70, 130, 180) # button background primary_colour = (70, 130, 180) # button background
hover_color = (51, 102, 145) # button hover hover_colour = (51, 102, 145) # button hover
text_color = (245, 245, 245) # button text text_colour = (245, 245, 245) # button text
tile_color = (200, 200, 200) # tile main tile_colour = (200, 200, 200) # tile main
tile_hover = (170, 170, 170) # tile hover tile_hover = (170, 170, 170) # tile hover
tile_text = (50, 50, 50) # tile text tile_text = (50, 50, 50) # tile text
bg_color = (30, 30, 40) # main background bg_colour = (30, 30, 40) # main background
red_tile = (255, 0, 0) # the red that a red tile is red_tile = (255, 0, 0) # the red that a red tile is
red_tile_hover = (220, 0, 0) # the red that a hovered red tile is red_tile_hover = (220, 0, 0) # the red that a hovered red tile is
@ -34,7 +34,7 @@ yellow_tile_hover = (220, 220, 0) # the yellow that a hovered yellow tile is
# variables # variables
tiles = [] tiles = []
menu_manager = MenuManager(display, bg_color) # background color menu_manager = MenuManager(display, bg_colour) # background colour
player = "red" player = "red"
board_full = False board_full = False
@ -69,12 +69,36 @@ def create_tiles():
tile = Button( tile = Button(
x, y, TILE_SIZE, TILE_SIZE, "", x, y, TILE_SIZE, TILE_SIZE, "",
tile_color, tile_hover, tile_text, tile_press, None, 30, (id, c, r), tile_colour, tile_hover, tile_text, tile_press, None, 30, (id, c, r),
rounding=30 rounding=30
) )
col.append(tile) col.append(tile)
tiles.append(col) tiles.append(col)
def checkWin():
global tiles, player
colours = [yellow_tile, yellow_tile_hover] if player == "red" else [red_tile, red_tile_hover] # this is backwards on purpose
rows, cols = (6, 7)
winCount = 4
for row in range(rows):
for col in range(cols - winCount + 1):
if all(tiles[col + i][row].colour in colours for i in range(winCount)):
return [(col + i, row) for i in range(winCount)]
for col in range(cols):
for row in range(rows - winCount + 1):
if all(tiles[col][row + i].colour in colours for i in range(winCount)):
return [(col, row + i) for i in range(winCount)]
for col in range(cols - winCount + 1):
for row in range(rows - winCount + 1):
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)]
for col in range(cols - winCount + 1):
for row in range(winCount - 1, rows):
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)]
def drop_tile(col_index): def drop_tile(col_index):
global player global player
column = tiles[col_index] column = tiles[col_index]
@ -83,17 +107,17 @@ def drop_tile(col_index):
for r in reversed(range(ROWS)): for r in reversed(range(ROWS)):
target_tile = column[r] target_tile = column[r]
# check if already taken (colored by a player) # check if already taken (coloured by a player)
if target_tile.color == tile_color: if target_tile.colour == tile_colour:
# claim this tile for the current player # claim this tile for the current player
target_tile.color = red_tile if player == "red" else yellow_tile target_tile.colour = red_tile if player == "red" else yellow_tile
target_tile.hover_color = red_tile_hover if player == "red" else yellow_tile_hover 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}") # print(f"Player {player} placed at col {col_index}, row {r}")
# switch turn # switch turn
player = "yellow" if player == "red" else "red" player = "yellow" if player == "red" else "red"
print(f"Next turn: {player}") # print(f"Next turn: {player}")
break break
else: else:
# column is full # column is full
@ -105,7 +129,7 @@ def is_board_full():
for row in reversed(range(ROWS)): for row in reversed(range(ROWS)):
target = col[row] target = col[row]
if target.color == tile_color: if target.colour == tile_colour:
return False return False
return True return True
@ -116,6 +140,13 @@ def tile_press(tile: Button):
id, col_index, row_index = tile.extra_data # we only care about column id, col_index, row_index = tile.extra_data # we only care about column
drop_tile(col_index) drop_tile(col_index)
win = checkWin()
print(f"Win tiles: {win}")
if win:
print("Somebody won fr")
if is_board_full(): if is_board_full():
board_full = True board_full = True
print(f"Draw! The board is full.") print(f"Draw! The board is full.")
@ -127,21 +158,25 @@ 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_color, hover_color, text_color, primary_colour, hover_colour, text_colour,
start_game_func, "Baloo2-Bold.ttf", 50, rounding=8) start_game_func, "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_color, hover_color, text_color, primary_colour, hover_colour, text_colour,
settings_menu, "Baloo2-Bold.ttf", 50, rounding=8) settings_menu, "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_color, hover_color, text_color, primary_colour, hover_colour, text_colour,
go_back, "Baloo2-Bold.ttf", 50, rounding=8) go_back, "Baloo2-Bold.ttf", 50, rounding=8)
game_drawn_button = Button(x, y*2-50, width, height, "Go back", game_over_button = Button(x, y*2-50, width, height, "Go back",
primary_color, hover_color, text_color, primary_colour, hover_colour, text_colour,
go_back, "Baloo2-Bold.ttf", 50, rounding=8) go_back, "Baloo2-Bold.ttf", 50, rounding=8)
game_over_text = Button(x, 50, width, height/1.5, "text",
bg_colour, (0, 0, 0), text_colour,
None, "Baloo2-Bold.ttf", 50, rounding=8)
# menu handlers # menu handlers
# start # start
def start_menu_events(event): def start_menu_events(event):
@ -157,7 +192,7 @@ def settings_menu_events(event):
go_back_button.handle_event(event) go_back_button.handle_event(event)
def settings_menu_draw(): def settings_menu_draw():
text_surface = font.render("No settings yet :(", True, text_color) 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) go_back_button.draw(display)
@ -166,7 +201,7 @@ def settings_menu_draw():
# game # game
def game_menu_events(event): def game_menu_events(event):
if board_full: if board_full:
game_drawn_button.handle_event(event) game_over_button.handle_event(event)
else: else:
for col in tiles: for col in tiles:
for tile in col: for tile in col:
@ -174,13 +209,16 @@ def game_menu_events(event):
tile.handle_event(event) tile.handle_event(event)
def game_menu_draw(): def game_menu_draw():
global player
for col in tiles: for col in tiles:
for tile in col: for tile in col:
tile: Button tile: Button
tile.draw(display) tile.draw(display)
if board_full: if board_full:
game_drawn_button.draw(display) game_over_text.text = f"{'RED' if player == 'red' else 'YELLOW'} WINS"
game_over_button.draw(display)
game_over_text.draw(display)
# register the menus # register the menus
menu_manager.register_menu("start", start_menu_events, start_menu_draw) menu_manager.register_menu("start", start_menu_events, start_menu_draw)
@ -198,9 +236,13 @@ if __name__ == "__main__":
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 and event.key == pygame.K_ESCAPE: if event.type == pygame.KEYDOWN:
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()