import os import pgzrun import random # game window position os.environ['SDL_VIDEO_WINDOW_POS'] = "150,150" # game window dimensions WIDTH = 960 HEIGHT = 540 # word list to type WORDS = ["python", "coding", "velocity", "keyboard", "rhythm", "algorithm", "developer", "software", "challenge", "galaxy", "typing", "precision"] # game state variables state = { "score": 0, "typed_chars": 0, "current_word": "", "user_input": "", "time_left": 15.0, "game_active": False, "show_results": False } # setup/reset function def reset_game(): state["score"] = 0 state["typed_chars"] = 0 state["time_left"] = 15.0 state["user_input"] = "" state["current_word"] = random.choice(WORDS) state["game_active"] = True state["show_results"] = False # start the first word state["current_word"] = random.choice(WORDS) # update the time and states (e.g. if the time runs out, show results) def update(dt): if state["game_active"]: state["time_left"] -= dt if state["time_left"] <= 0: state["time_left"] = 0 state["game_active"] = False state["show_results"] = True # website design def draw(): screen.fill((30, 30, 45)) # background color if not state["game_active"] and not state["show_results"]: # show the title screen screen.draw.text("TYPING CHALLENGE", center=(WIDTH//2, 200), fontsize=60, color="cyan") screen.draw.text("Press SPACE to Start", center=(WIDTH//2, 300), fontsize=40, color="white") elif state["game_active"]: # time and score display screen.draw.text(f"Time: {round(state['time_left'], 1)}s", (50, 50), fontsize=40, color="orange") screen.draw.text(f"Words: {state['score']}", (WIDTH - 200, 50), fontsize=40, color="green") # current word to type screen.draw.text(state["current_word"], center=(WIDTH//2, HEIGHT//2 - 40), fontsize=80, color="white") # user word input screen.draw.text(state["user_input"], center=(WIDTH//2, HEIGHT//2 + 60), fontsize=60, color="yellow") # underline design for user input screen.draw.line((WIDTH//2 - 150, HEIGHT//2 + 100), (WIDTH//2 + 150, HEIGHT//2 + 100), "white") elif state["show_results"]: # calculate words per minute: (chars / 5) / (seconds / 60) # since the game is 15 seconds (0.25 minutes): wpm = (state["typed_chars"] / 5) / (15 / 60) # display results screen.draw.text("TIME'S UP!", center=(WIDTH//2, 150), fontsize=80, color="red") screen.draw.text(f"WPM: {int(wpm)}", center=(WIDTH//2, 250), fontsize=100, color="yellow") screen.draw.text(f"Words Finished: {state['score']}", center=(WIDTH//2, 330), fontsize=40, color="white") screen.draw.text("Press SPACE to Play Again", center=(WIDTH//2, 450), fontsize=30, color="gray") # handle user keyboard input def on_key_down(key, unicode): # handle Start/Restart if key == keys.SPACE and not state["game_active"]: reset_game() return if state["game_active"]: if key == keys.BACKSPACE: state["user_input"] = state["user_input"][:-1] elif unicode: # Add character to input state["user_input"] += unicode # Check if word matches if state["user_input"].strip() == state["current_word"]: state["score"] += 1 state["typed_chars"] += len(state["current_word"]) + 1 state["user_input"] = "" state["current_word"] = random.choice(WORDS) pgzrun.go()