Untitled
import pygame import random import time import math # Initialize pygame pygame.init() # Set up display screen_width, screen_height = 800, 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Typing Speed Game") # Set up fonts font = pygame.font.Font(None, 36) large_font = pygame.font.Font(None, 50) # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) # Game variables zombie_speed = 2 char_speed = 0 # The character moves for each correct character typed level = 1 words = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "honeydew"] word = random.choice(words) input_text = '' wpm = 0 accuracy = 0 total_characters = 0 correct_characters = 0 game_over = False current_char_index = 0 # Tracks the position of the character being typed in the word # Rectangle vertices for path vertices = [(50, 50), (750, 50), (750, 550), (50, 550)] player_index = 0 zombie_index = 2 # Starting at a different vertex texts_starting_x = 100 texts_starting_y = 100 # Function to calculate words per minute def calculate_wpm(start_time, end_time, correct_words): time_taken = (end_time - start_time) / 60 return correct_words / time_taken # Function to calculate accuracy def calculate_accuracy(total, correct): if total == 0: return 0 return (correct / total) * 100 # Function to move along a path between two points def move_towards(current_pos, target_pos, speed): current_x, current_y = current_pos target_x, target_y = target_pos distance = math.sqrt((target_x - current_x) ** 2 + (target_y - current_y) ** 2) if distance == 0: return current_pos # Already at target ratio = speed / distance new_x = current_x + (target_x - current_x) * ratio new_y = current_y + (target_y - current_y) * ratio return new_x, new_y # Main game loop def game_loop(): global zombie_speed, char_speed, level, word, input_text, wpm, accuracy, total_characters, correct_characters, game_over, player_index, zombie_index, current_char_index # Game states word_display_time = 0 start_time = time.time() clock = pygame.time.Clock() player_pos = vertices[player_index] zombie_pos = vertices[zombie_index] while True: screen.fill(WHITE) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() target_player_pos = 0 if event.type == pygame.KEYDOWN and not game_over: print(event.unicode) if event.unicode == word[current_char_index]: # Correct character typed char_speed = 5 # Move a bit for each correct character correct_characters += 1 current_char_index += 1 total_characters += 1 print("correct character typed") # Move player towards the next vertex target_player_pos = vertices[(player_index + 1) % 4] player_pos = move_towards(player_pos, target_player_pos, char_speed) input_text += event.unicode else: total_characters += 1 # Incorrect character char_speed = 0 # If the word is completed, reset the word if current_char_index == len(word): word = random.choice(words) current_char_index = 0 # Reset character index input_text = '' else: char_speed = 0 # Move zombie towards the next vertex target_zombie_pos = vertices[(zombie_index + 1) % 4] zombie_pos = move_towards(zombie_pos, target_zombie_pos, zombie_speed) # Check if player reached the target vertex if player_pos == target_player_pos: player_index = (player_index + 1) % 4 # Check if zombie reached the target vertex if zombie_pos == target_zombie_pos: zombie_index = (zombie_index + 1) % 4 # Check if the zombie catches the player distance = math.sqrt((zombie_pos[0] - player_pos[0]) ** 2 + (zombie_pos[1] - player_pos[1]) ** 2) if distance < 50: game_over = True # Display current word, input text, and stats word_surface = font.render(f"Word: {word}", True, BLACK) input_surface = font.render(f"Type: {input_text}", True, BLACK) level_surface = font.render(f"Level: {level}", True, BLACK) wpm_surface = font.render(f"WPM: {wpm:.2f}", True, BLACK) accuracy_surface = font.render(f"Accuracy: {accuracy:.2f}%", True, BLACK) screen.blit(word_surface, (texts_starting_x, texts_starting_y)) screen.blit(input_surface, (texts_starting_x, texts_starting_y + 50)) screen.blit(level_surface, (texts_starting_x, texts_starting_y + 100)) screen.blit(wpm_surface, (texts_starting_x, texts_starting_y + 150)) screen.blit(accuracy_surface, (texts_starting_x, texts_starting_y + 200)) # Draw player and zombie pygame.draw.circle(screen, BLACK, (int(player_pos[0]), int(player_pos[1])), 25) pygame.draw.circle(screen, RED, (int(zombie_pos[0]), int(zombie_pos[1])), 25) if game_over: end_time = time.time() wpm = calculate_wpm(start_time, end_time, correct_characters // 5) accuracy = calculate_accuracy(total_characters, correct_characters) game_over_surface = large_font.render("Game Over", True, RED) screen.blit(game_over_surface, (screen_width // 2 - 100, screen_height // 2)) pygame.display.update() pygame.time.wait(2000) break pygame.display.update() clock.tick(60) # Run the game game_loop() pygame.quit()
Leave a Comment