Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/AndreaTerenz/PyTablut. Tablut game client in Python. Made for challenge project for FAIKR course mo
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091import sysimport osos.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"import pygame as pgimport pygame.draw as pgdrawimport pygame.math as pgmath
from board import Board, CellType, CheckerType
class GUI: SCREEN_SIZE = pgmath.Vector2(7) * 9 * 10 GRID_SIZE = pgmath.Vector2(9) CELL_SIZE = pgmath.Vector2(SCREEN_SIZE.x / GRID_SIZE.x, SCREEN_SIZE.y / GRID_SIZE.y)
def __init__(self, title=""): pg.init() self.screen = pg.display.set_mode(GUI.SCREEN_SIZE) pg.display.set_caption(title)
def on_event(self, event): # exit program when quit event is sent if event.type == pg.QUIT or GUI.check_key(event, pg.K_ESCAPE): sys.exit(0)
@staticmethod def check_key(event, key, state=pg.KEYUP): return event.type == state and event.key == key
def draw(self, board): self.screen.fill(color="black")
for r in range(9): for c in range(9): rect = pg.Rect(c * GUI.CELL_SIZE.x, r * GUI.CELL_SIZE.y, GUI.CELL_SIZE.x, GUI.CELL_SIZE.y)
match board.grid[r, c].type: case CellType.ESCAPE: col = "#8c4408" case CellType.CASTLE: col = "#ac000e" case CellType.CAMP: col = "#101010" case _: col = "#ddeb5e"
pgdraw.rect(self.screen, color=col, rect=rect)
if board.grid[r, c].checker != CheckerType.EMPTY: match board.grid[r, c].checker: case CheckerType.BLACK: inner_col = "black" outer_col = "white" case CheckerType.KING: inner_col = "red" outer_col = "black" case _: inner_col = "white" outer_col = "black"
radius = 0.6 * GUI.CELL_SIZE.x / 2 stroke_ratio = 0.1
pgdraw.circle(self.screen, color=outer_col, center=(rect.x + GUI.CELL_SIZE.x / 2, rect.y + GUI.CELL_SIZE.y / 2), radius=radius * (1 + stroke_ratio)) pgdraw.circle(self.screen, color=inner_col, center=(rect.x + GUI.CELL_SIZE.x / 2, rect.y + GUI.CELL_SIZE.y / 2), radius=radius)
pg.display.flip()
def run(self, board, events=False): # main loop while True: # event handling, gets all event from the event queue if events: for event in pg.event.get(): self.on_event(event)
self.draw(board)
if __name__ == "__main__": gui = GUI() gui.run(Board())