Final - JadenGil/Jaden-Tech-Journal GitHub Wiki

import pygame

class Game: # Class variables SCREEN_WIDTH = 1000 SCREEN_HEIGHT = 600 FPS = 60

`def __init__(self, *args, **kwargs):`
    `# Initialize Pygame`
    `pygame.init()`
    `pygame.display.set_caption(kwargs.get('caption', 'Input frame test'))`
    `self.screen = pygame.display.set_mode((kwargs.get('width', self.SCREEN_WIDTH), kwargs.get('height', self.SCREEN_HEIGHT)))`
    `self.clock = pygame.time.Clock()`

    `# Initialize game state`
    `self.game_running = False`
    `self.game_quit = False`
    
    `# Initialize button combo and time since last button press`
    `self.button_combo = []`
    `self.time_since_last_button_press = 0`

`def __enter__(self):`
    `return self`

`def __exit__(self, exc_type, exc_val, exc_tb):`
    `pygame.quit()`

`def __del__(self):`
    `pygame.quit()`

`def __str__(self):`
    `return f"Game running: {self.game_running}, Game quit: {self.game_quit}"`

`def run(self, *args, **kwargs):`
    `self.start_game()`
    `while self.game_running:`
        `self.handle_events()`
        `self.update()`

`def start_game(self, *args, **kwargs):`
    `# Prompt user to press spacebar to start game`
    `font = pygame.font.Font(None, 36)`
    `start_text = font.render(kwargs.get('start_text', 'Press spacebar to start game'), True, (255, 255, 255))`
    `start_text_rect = start_text.get_rect(center=(self.SCREEN_WIDTH/2, self.SCREEN_HEIGHT/2))`
    `self.screen.blit(start_text, start_text_rect)`
    `pygame.display.update()`

    `# Wait for spacebar to start game`
    `while not self.game_running and not self.game_quit:`
        `for event in pygame.event.get():`
            `if event.type == pygame.QUIT:`
                `# Quit the game when the window is closed`
                `self.quit_game()`
            `elif event.type == pygame.KEYDOWN:`
                `if event.key == pygame.K_SPACE:`
                    `self.game_running = True`

`def handle_events(self, *args, **kwargs):`
    `# Handle events`
    `for event in pygame.event.get():`
        `if event.type == pygame.QUIT:`
            `# Quit the game when the window is closed`
            `self.quit_game()`
        `elif event.type == pygame.KEYDOWN:`
            `if event.key == pygame.K_SPACE:`
                `if self.game_running:`
                    `# Stop the game when the spacebar is pressed again`
                    `self.game_running = False`
                    `self.button_combo.clear()`
                    `self.time_since_last_button_press = 0`
                    `print("Game stopped")`
                `else:`
                    `self.quit_game()`

            `# Checks for valid key presses`
            `if self.game_running:`
                `if event.key in [pygame.K_w, pygame.K_a, pygame.K_s, pygame.K_d] or event.unicode.isnumeric():`
                    `self.button_combo.append(event.unicode)`
                    `self.time_since_last_button_press = 0`
                    `print(self.button_combo)`

`def update(self, *args, **kwargs):`
    `# Clear the screen`
    `self.screen.fill(kwargs.get('bg_color', 'Black'))`

    `# Update time_since_last_button_press and reset combo if necessary`
    `self.time_since_last_button_press += 1`
    `if self.time_since_last_button_press > 18 or len(self.button_combo) > 9:`

                `self.button_combo.clear()`
    `self.time_since_last_button_press = 0`

`# Update the screen`
`pygame.display.update()`

`def quit_game(self, *args, **kwargs):`
   
    `def quit_game(self, *args, **kwargs):`
        `pygame.quit()`
        `self.game_running = False`
        `self.game_quit = True`