draw detetection. some optimizing
This commit is contained in:
parent
be51abf777
commit
26c88cabab
61
gui/game.py
61
gui/game.py
|
|
@ -18,11 +18,6 @@ font = pygame.font.Font("Baloo2-Bold.ttf", 40)
|
|||
display = pygame.display.set_mode(WINDOW_SIZE)
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
# variables
|
||||
tiles = []
|
||||
menu_manager = MenuManager(display, (30, 30, 40)) # background color
|
||||
player = "red"
|
||||
|
||||
# colorss
|
||||
primary_color = (70, 130, 180) # button background
|
||||
hover_color = (51, 102, 145) # button hover
|
||||
|
|
@ -37,8 +32,16 @@ red_tile_hover = (220, 0, 0) # the red that a hovered red tile is
|
|||
yellow_tile = (255, 255, 0) # the yellow that a yellow tile is
|
||||
yellow_tile_hover = (220, 220, 0) # the yellow that a hovered yellow tile is
|
||||
|
||||
# variables
|
||||
tiles = []
|
||||
menu_manager = MenuManager(display, bg_color) # background color
|
||||
player = "red"
|
||||
board_full = False
|
||||
|
||||
# menu functions
|
||||
def start_game_func(*_):
|
||||
global board_full
|
||||
board_full = False
|
||||
create_tiles()
|
||||
menu_manager.change_menu("game")
|
||||
|
||||
|
|
@ -62,18 +65,18 @@ def create_tiles():
|
|||
x = start_x + c * (TILE_SIZE + TILE_SPACING)
|
||||
y = start_y + r * (TILE_SIZE + TILE_SPACING)
|
||||
|
||||
id = str(c * ROWS + r)
|
||||
|
||||
tile = Button(
|
||||
x, y, TILE_SIZE, TILE_SIZE, str(c * ROWS + r),
|
||||
tile_color, tile_hover, tile_text, tile_press, None, 30, (str(c * ROWS + r), c, r),
|
||||
rounding=5
|
||||
x, y, TILE_SIZE, TILE_SIZE, "",
|
||||
tile_color, tile_hover, tile_text, tile_press, None, 30, (id, c, r),
|
||||
rounding=30
|
||||
)
|
||||
col.append(tile)
|
||||
tiles.append(col)
|
||||
|
||||
def tile_press(tile: Button):
|
||||
def drop_tile(col_index):
|
||||
global player
|
||||
|
||||
_, col_index, _ = tile.extra_data # we only care about column
|
||||
column = tiles[col_index]
|
||||
|
||||
# find the lowest unoccupied tile in this column
|
||||
|
|
@ -81,7 +84,7 @@ def tile_press(tile: Button):
|
|||
target_tile = column[r]
|
||||
|
||||
# check if already taken (colored by a player)
|
||||
if target_tile.color not in (red_tile, yellow_tile):
|
||||
if target_tile.color == tile_color:
|
||||
# claim this tile for the current player
|
||||
target_tile.color = red_tile if player == "red" else yellow_tile
|
||||
target_tile.hover_color = red_tile_hover if player == "red" else yellow_tile_hover
|
||||
|
|
@ -96,6 +99,28 @@ def tile_press(tile: Button):
|
|||
# column is full
|
||||
print(f"Column {col_index} is full!")
|
||||
|
||||
def is_board_full():
|
||||
global tiles, board_full
|
||||
for col in tiles:
|
||||
for row in reversed(range(ROWS)):
|
||||
target = col[row]
|
||||
|
||||
if target.color == tile_color:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def tile_press(tile: Button):
|
||||
global board_full
|
||||
|
||||
id, col_index, row_index = tile.extra_data # we only care about column
|
||||
drop_tile(col_index)
|
||||
|
||||
if is_board_full():
|
||||
board_full = True
|
||||
print(f"Draw! The board is full.")
|
||||
|
||||
|
||||
# button stuff
|
||||
width, height = 280, 75
|
||||
x = WINDOW_SIZE[0] / 2 - width / 2
|
||||
|
|
@ -113,6 +138,9 @@ go_back_button = Button(x, y, width, height, "Go back",
|
|||
primary_color, hover_color, text_color,
|
||||
go_back, "Baloo2-Bold.ttf", 50, rounding=8)
|
||||
|
||||
game_drawn_button = Button(x, y*2-50, width, height, "Go back",
|
||||
primary_color, hover_color, text_color,
|
||||
go_back, "Baloo2-Bold.ttf", 50, rounding=8)
|
||||
|
||||
# menu handlers
|
||||
# start
|
||||
|
|
@ -134,17 +162,26 @@ def settings_menu_draw():
|
|||
display.blit(text_surface, text_rect)
|
||||
go_back_button.draw(display)
|
||||
|
||||
|
||||
# game
|
||||
def game_menu_events(event):
|
||||
if board_full:
|
||||
game_drawn_button.handle_event(event)
|
||||
else:
|
||||
for col in tiles:
|
||||
for tile in col:
|
||||
tile: Button
|
||||
tile.handle_event(event)
|
||||
|
||||
def game_menu_draw():
|
||||
for col in tiles:
|
||||
for tile in col:
|
||||
tile: Button
|
||||
tile.draw(display)
|
||||
|
||||
if board_full:
|
||||
game_drawn_button.draw(display)
|
||||
|
||||
# register the menus
|
||||
menu_manager.register_menu("start", start_menu_events, start_menu_draw)
|
||||
menu_manager.register_menu("settings", settings_menu_events, settings_menu_draw)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user