holy my code was bad. optimized tile creation, made the colours nice, rounded buttons
This commit is contained in:
parent
e8aeee7360
commit
9ae90e1b61
|
|
@ -32,4 +32,4 @@ Both game modes include:
|
||||||
|
|
||||||
### Player vs CPU
|
### Player vs CPU
|
||||||
|
|
||||||
- Fix priority issues: sometimes the CPU prefers a vertical 3-in-a-row instead of completing a horizontal win.
|
- Fix priority issues: sometimes the CPU prefers a vertical 3-in-a-row instead of completing a horizontal win.
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
class Button:
|
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.rect = pygame.Rect(x, y, width, height)
|
||||||
self.text = text
|
self.text = text
|
||||||
self.color = color
|
self.color = color
|
||||||
|
|
@ -11,9 +11,10 @@ class Button:
|
||||||
self.action = action
|
self.action = action
|
||||||
self.font = pygame.font.Font(font, font_size)
|
self.font = pygame.font.Font(font, font_size)
|
||||||
self.extra_data = extra_data
|
self.extra_data = extra_data
|
||||||
|
self.rounding = rounding
|
||||||
|
|
||||||
def draw(self, screen):
|
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_surface = self.font.render(self.text, True, self.text_color)
|
||||||
text_rect = text_surface.get_rect(center=self.rect.center)
|
text_rect = text_surface.get_rect(center=self.rect.center)
|
||||||
screen.blit(text_surface, text_rect)
|
screen.blit(text_surface, text_rect)
|
||||||
|
|
|
||||||
51
gui/game.py
51
gui/game.py
|
|
@ -4,28 +4,30 @@ from pygame import Vector2 as v2, Color as Colour
|
||||||
|
|
||||||
from button import Button
|
from button import Button
|
||||||
|
|
||||||
# some constst
|
# some pygame constst
|
||||||
WINDOW_SIZE = (768, 768)
|
WINDOW_SIZE = (768, 768)
|
||||||
WINDOW_SCALE = 1
|
|
||||||
TARGET_FPS = 60
|
TARGET_FPS = 60
|
||||||
|
|
||||||
# pygame inits
|
# pygame inits
|
||||||
pygame.init()
|
pygame.init()
|
||||||
display = pygame.display.set_mode(v2(WINDOW_SIZE)*WINDOW_SCALE)
|
display = pygame.display.set_mode(v2(WINDOW_SIZE))
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
# more variable inits
|
# more variable inits
|
||||||
menu = "start"
|
menu = ""
|
||||||
tiles = []
|
tiles = []
|
||||||
|
COLS = 7
|
||||||
|
ROWS = 6
|
||||||
|
|
||||||
# menu functions
|
# menu functions
|
||||||
def change_menu(targetMenu):
|
def change_menu(targetMenu):
|
||||||
global menu
|
global menu
|
||||||
menu = targetMenu
|
menu = targetMenu
|
||||||
display.fill('black')
|
display.fill(bg_color)
|
||||||
|
|
||||||
def start_game_func(*_):
|
def start_game_func(*_):
|
||||||
change_menu("game")
|
change_menu("game")
|
||||||
|
create_tiles()
|
||||||
|
|
||||||
def settings_menu(*_):
|
def settings_menu(*_):
|
||||||
change_menu("settings")
|
change_menu("settings")
|
||||||
|
|
@ -33,6 +35,18 @@ def settings_menu(*_):
|
||||||
def go_back(*_):
|
def go_back(*_):
|
||||||
change_menu("start")
|
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
|
# gets called when you click on a tile
|
||||||
def tile_press(tile):
|
def tile_press(tile):
|
||||||
tile_id,x,y = tile.extra_data
|
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
|
x = WINDOW_SIZE[0] / 2 - width / 2 # center of the screen horizontally
|
||||||
y = (WINDOW_SIZE[1] / 2 - height / 2) # center of the screen vertically
|
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)
|
# === Color Variables ===
|
||||||
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)
|
primary_color = (70, 130, 180) # button background
|
||||||
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)
|
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
|
# Game loop
|
||||||
while running:
|
while running:
|
||||||
|
|
@ -89,16 +114,6 @@ if __name__ == "__main__":
|
||||||
go_back_button.draw(display)
|
go_back_button.draw(display)
|
||||||
# basic connect-4 ahh grid
|
# basic connect-4 ahh grid
|
||||||
elif menu == "game":
|
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:
|
for tile in tiles:
|
||||||
tile.draw(display)
|
tile.draw(display)
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user