CSI 260 Final - JadenGil/Jaden-Tech-Journal GitHub Wiki

import pygame

class Game: def __init__(self): # Create game window self.SCREEN_WIDTH = 1000 self.SCREEN_HEIGHT = 600

    `# Initialize Pygame`
    `pygame.init()`
    `pygame.display.set_caption("Input frame test")`
    `self.screen = pygame.display.set_mode((self.SCREEN_WIDTH, self.SCREEN_HEIGHT))`
    `self.clock = pygame.time.Clock()`
    
    `# Set framerate`
    `self.FPS = 60`

    `# 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 run(self):`
    `self.start_game()`
    `while self.game_running:`
        `self.handle_events()`
        `self.update()`



`def start_game(self):`
    `# Prompt user to press spacebar to start game`
    `font = pygame.font.Font(None, 36)`
    `start_text = font.render("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):`
    `# 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):`
    `# Clear the screen`
    `self.screen.fill('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`
        `print("Combo Reset")`

    `# Display the button combo if the game is running`
    `if self.game_running:`
        `font = pygame.font.Font(None, 36)`
        `combo_text = font.render("Combo: " + "".join(self.button_combo), True, (255, 255, 255))`
        `combo_text_rect = combo_text.get_rect(center=(self.SCREEN_WIDTH/2, self.SCREEN_HEIGHT/2))`
        `self.screen.blit(combo_text, combo_text_rect)`

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

    `# Tick the clock`
    `self.clock.tick(self.FPS)`

`def quit_game(self):`
    `# Quit the game`
    `pygame.quit()`
    `quit()`

if __name__ == "__main__": game = Game() game.run()