From 9036866830d2196d9c04beb5ce8dfbdb90857cc9 Mon Sep 17 00:00:00 2001 From: AlexCami1902 Date: Mon, 19 May 2025 17:00:45 +1000 Subject: [PATCH] Add files via upload Added coin toss page for clearer explanation of what is going on. The input file needs to send to the new coinflip.py file, the coinflip.py file needs to send to the main.py file to start the match as well as interacting with the shared.py file. Signed-off-by: AlexCami1902 --- coinflip.py | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 coinflip.py diff --git a/coinflip.py b/coinflip.py new file mode 100644 index 0000000..c7f61f5 --- /dev/null +++ b/coinflip.py @@ -0,0 +1,110 @@ +import pygame +import sys +import pygame_gui +import shared +import random +import time + +# Initialize Pygame +pygame.init() +screen_width, screen_height = 600, 500 +screen = pygame.display.set_mode((screen_width, screen_height)) +pygame.display.set_caption("Team Input and Colour Picker") + +clock = pygame.time.Clock() +ui_manager = pygame_gui.UIManager((screen_width, screen_height)) + +# Colours +white = (255, 255, 255) +black = (0, 0, 0) +red = (255, 0, 0) + +# Fonts +try: + font = pygame.font.Font("fonts/PublicSans-Bold.ttf", 24) +except: + font = pygame.font.SysFont("arial", 24) + print("⚠️ Font not found, using system font.") + +# Team colours (set elsewhere) +home_team_colour = pygame.Color(0, 0, 0) +away_team_colour = pygame.Color(0, 0, 0) + +# Text rendering helper +def draw_text(text, font, colour, surface, x, y): + textobj = font.render(text, True, colour) + textrect = textobj.get_rect() + textrect.topleft = (x, y) + surface.blit(textobj, textrect) + +# Basic button class +class Button: + def __init__(self, text, x, y, w, h, colour, action=None): + self.text = text + self.rect = pygame.Rect(x, y, w, h) + self.colour = colour + self.action = action + + def draw(self, screen): + pygame.draw.rect(screen, self.colour, self.rect) + draw_text(self.text, font, white, screen, self.rect.x + 10, self.rect.y + 10) + + def is_clicked(self, pos): + return self.rect.collidepoint(pos) + + def handle_event(self, event): + if event.type == pygame.MOUSEBUTTONDOWN and self.is_clicked(event.pos): + if self.action: + self.action() + +# Coin toss logic +def cointossoutcome(): + global coinresult + coinresult = random.choice(["Heads", "Tails"]) + print(f"Coin Toss Result: {coinresult}") + if coinresult == "Heads": + shared.first_batting_team = shared.home_team + shared.second_batting_team = shared.away_team + draw_text(f"{shared.home_team} won the toss", font, red, screen, 50, 100) + pygame.display.flip() + time.sleep(1) + + else: + shared.first_batting_team = shared.away_team + shared.second_batting_team = shared.home_team + draw_text(f"{shared.away_team} won the toss", font, red, screen, 50, 100) + pygame.display.flip() + time.sleep(1) + + # Import main to continue (assuming main.py handles the switch) + import main + +# Create buttons +heads = Button("Heads", 450, 100, 120, 40, pygame.Color("dodgerblue"), cointossoutcome) +tails = Button("Tails", 450, 200, 120, 40, pygame.Color("orangered"), cointossoutcome) + +# Main loop +running = True +while running: + time_delta = clock.tick(60) / 1000.0 + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + + heads.handle_event(event) + tails.handle_event(event) + ui_manager.process_events(event) + + ui_manager.update(time_delta) + + screen.fill(black) + draw_text("Away team calls the toss", font, red, screen, 50, 50) + heads.draw(screen) + tails.draw(screen) + ui_manager.draw_ui(screen) + + pygame.display.flip() + +pygame.quit() +sys.exit()