Game crashes when I try to run it why? /u/Plastic-Bee4052 Python Education

Hi, this it the code for my game and it crashes when I double click the file name. Why? How can I solve it?

import pygame import math import tkinter as tk from tkinter import messagebox # Initialize Pygame pygame.init() # Constants WIDTH, HEIGHT = 600, 400 FPS = 60 LIGHT_GRAY = (211, 211, 211) GREEN = (0, 255, 0) RED = (255, 0, 0) BLUE = (0, 0, 255) BUSH_COLOR = (34, 139, 34) TRANSPARENCY = 128 # Alpha value for transparency (0-255) # Set up display screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Shape Tracing Game") # Font for messages font = pygame.font.SysFont('Arial', 24) class ShapeTracingGame: def __init__(self): self.clock = pygame.time.Clock() self.reset_game() self.game_started = False # Track if the game has started self.tk_root = tk.Tk() # Create a Tkinter root window self.tk_root.withdraw() # Hide the root window def reset_game(self): self.tracing = False self.traced_path = [] # Store completed paths self.current_line = [] # Store the current line being drawn self.starting_inside = False self.strayed_off_path = False self.points = 0 # Points counter self.level = 1 # Current level self.set_shape_and_points() # Initialize first level def set_shape_and_points(self): if self.level == 1: self.start_point = (100, 300) self.end_point = (300, 300) self.shape_coords = [(200, 50), (100, 300), (300, 300)] # Triangle self.bush_coords = [(180, 60), (80, 280), (320, 280), (420, 60), (300, 300), (100, 300)] elif self.level == 2: self.start_point = (50, 350) self.end_point = (550, 350) self.shape_coords = [(300, 50), (100, 350), (500, 350)] # Larger triangle self.bush_coords = [(180, 60), (80, 380), (520, 380), (420, 60), (300, 350), (100, 350)] # Add more levels as needed def draw_bush_background(self): pygame.draw.polygon(screen, BUSH_COLOR, self.bush_coords) def draw_shape(self): triangle_surface = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA) pygame.draw.polygon(triangle_surface, (*LIGHT_GRAY, TRANSPARENCY), self.shape_coords) pygame.draw.polygon(triangle_surface, LIGHT_GRAY, self.shape_coords, 30) # Outline screen.blit(triangle_surface, (0, 0)) def draw_circle(self, center, color): pygame.draw.circle(screen, color, center, 15) def display_message(self, message): text = font.render(message, True, (0, 0, 0)) screen.blit(text, (WIDTH // 2 - text.get_width() // 2, HEIGHT // 2 - text.get_height() // 2)) def display_points(self): points_text = font.render(f"Points: {self.points}", True, (0, 0, 0)) screen.blit(points_text, (500, 100)) def is_within_circle(self, point, center, radius): return math.hypot(point[0] - center[0], point[1] - center[1]) <= radius def start_tracing(self, pos): if self.is_within_circle(pos, self.start_point, 15): self.starting_inside = True self.tracing = True self.current_line.append(pos) else: self.message = "You must start within the red circle!" def trace_shape(self, pos): if self.tracing: if len(self.current_line) > 0: pygame.draw.line(screen, GREEN, self.current_line[-1], pos, 5) self.current_line.append(pos) if not self.is_within_shape(pos): self.strayed_off_path = True self.game_over() def game_over(self): messagebox.showinfo("Game Over!", f"Final Score: {self.points}") self.reset_game() # Reset game for next attempt def stop_tracing(self): if self.tracing: if self.starting_inside and not self.strayed_off_path and self.is_within_circle(self.current_line[-1], self.end_point, 15): self.traced_path.extend(self.current_line) self.points += 10 # Add points for successful completion self.message = "Congratulations! You completed the figure!" if self.level < 2: # Move to the next level if available self.level += 1 self.set_shape_and_points() else: self.game_over() self.tracing = False def is_within_shape(self, point): for i in range(len(self.shape_coords)): x1, y1 = self.shape_coords[i] x2, y2 = self.shape_coords[(i + 1) % len(self.shape_coords)] if self.distance_to_segment(point, (x1, y1), (x2, y2)) <= 15: return True return False def distance_to_segment(self, p, v, w): l2 = (w[0] - v[0]) ** 2 + (w[1] - v[1]) ** 2 if l2 == 0: return math.hypot(p[0] - v[0], p[1] - v[1]) t = max(0, min(1, ((p[0] - v[0]) * (w[0] - v[0]) + (p[1] - v[1]) * (w[1] - v[1])) / l2)) projection = (v[0] + t * (w[0] - v[0]), v[1] + t * (w[1] - v[1])) return math.hypot(p[0] - projection[0], p[1] - projection[1]) def draw_start_button(self): button_rect = pygame.Rect(WIDTH // 2 - 100, HEIGHT // 2 + 25, 200, 50) pygame.draw.rect(screen, (100, 200, 100), button_rect) text = font.render("Start Game", True, (0, 0, 0)) screen.blit(text, (WIDTH // 2 - text.get_width() // 2, HEIGHT // 2 + 40)) def run(self): running = True while running: screen.fill((173, 216, 230)) # Light blue background if not self.game_started: self.draw_start_button() else: self.draw_bush_background() self.draw_shape() self.draw_circle(self.start_point, RED) self.draw_circle(self.end_point, BLUE) if self.traced_path: for i in range(1, len(self.traced_path)): pygame.draw.line(screen, GREEN, self.traced_path[i - 1], self.traced_path[i], 5) if self.current_line: for i in range(1, len(self.current_line)): pygame.draw.line(screen, GREEN, self.current_line[i - 1], self.current_line[i], 5) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: # Left mouse button if not self.game_started: self.game_started = True # Start the game self.reset_game() # Reset game else: if self.strayed_off_path: self.reset_game() # Reset game on game over else: self.start_tracing(event.pos) elif event.type == pygame.MOUSEMOTION: if self.tracing: self.trace_shape(event.pos) elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1 and not self.strayed_off_path: self.stop_tracing() if self.message: self.display_message(self.message) self.display_points() # Display points pygame.display.flip() self.clock.tick(FPS) pygame.quit() if __name__ == "__main__": try: game = ShapeTracingGame() game.run() except Exception as e: print(f"An error occurred: {e}") 

submitted by /u/Plastic-Bee4052
[link] [comments]

​r/learnpython Hi, this it the code for my game and it crashes when I double click the file name. Why? How can I solve it? import pygame import math import tkinter as tk from tkinter import messagebox # Initialize Pygame pygame.init() # Constants WIDTH, HEIGHT = 600, 400 FPS = 60 LIGHT_GRAY = (211, 211, 211) GREEN = (0, 255, 0) RED = (255, 0, 0) BLUE = (0, 0, 255) BUSH_COLOR = (34, 139, 34) TRANSPARENCY = 128 # Alpha value for transparency (0-255) # Set up display screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption(“Shape Tracing Game”) # Font for messages font = pygame.font.SysFont(‘Arial’, 24) class ShapeTracingGame: def __init__(self): self.clock = pygame.time.Clock() self.reset_game() self.game_started = False # Track if the game has started self.tk_root = tk.Tk() # Create a Tkinter root window self.tk_root.withdraw() # Hide the root window def reset_game(self): self.tracing = False self.traced_path = [] # Store completed paths self.current_line = [] # Store the current line being drawn self.starting_inside = False self.strayed_off_path = False self.points = 0 # Points counter self.level = 1 # Current level self.set_shape_and_points() # Initialize first level def set_shape_and_points(self): if self.level == 1: self.start_point = (100, 300) self.end_point = (300, 300) self.shape_coords = [(200, 50), (100, 300), (300, 300)] # Triangle self.bush_coords = [(180, 60), (80, 280), (320, 280), (420, 60), (300, 300), (100, 300)] elif self.level == 2: self.start_point = (50, 350) self.end_point = (550, 350) self.shape_coords = [(300, 50), (100, 350), (500, 350)] # Larger triangle self.bush_coords = [(180, 60), (80, 380), (520, 380), (420, 60), (300, 350), (100, 350)] # Add more levels as needed def draw_bush_background(self): pygame.draw.polygon(screen, BUSH_COLOR, self.bush_coords) def draw_shape(self): triangle_surface = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA) pygame.draw.polygon(triangle_surface, (*LIGHT_GRAY, TRANSPARENCY), self.shape_coords) pygame.draw.polygon(triangle_surface, LIGHT_GRAY, self.shape_coords, 30) # Outline screen.blit(triangle_surface, (0, 0)) def draw_circle(self, center, color): pygame.draw.circle(screen, color, center, 15) def display_message(self, message): text = font.render(message, True, (0, 0, 0)) screen.blit(text, (WIDTH // 2 – text.get_width() // 2, HEIGHT // 2 – text.get_height() // 2)) def display_points(self): points_text = font.render(f”Points: {self.points}”, True, (0, 0, 0)) screen.blit(points_text, (500, 100)) def is_within_circle(self, point, center, radius): return math.hypot(point[0] – center[0], point[1] – center[1]) <= radius def start_tracing(self, pos): if self.is_within_circle(pos, self.start_point, 15): self.starting_inside = True self.tracing = True self.current_line.append(pos) else: self.message = “You must start within the red circle!” def trace_shape(self, pos): if self.tracing: if len(self.current_line) > 0: pygame.draw.line(screen, GREEN, self.current_line[-1], pos, 5) self.current_line.append(pos) if not self.is_within_shape(pos): self.strayed_off_path = True self.game_over() def game_over(self): messagebox.showinfo(“Game Over!”, f”Final Score: {self.points}”) self.reset_game() # Reset game for next attempt def stop_tracing(self): if self.tracing: if self.starting_inside and not self.strayed_off_path and self.is_within_circle(self.current_line[-1], self.end_point, 15): self.traced_path.extend(self.current_line) self.points += 10 # Add points for successful completion self.message = “Congratulations! You completed the figure!” if self.level < 2: # Move to the next level if available self.level += 1 self.set_shape_and_points() else: self.game_over() self.tracing = False def is_within_shape(self, point): for i in range(len(self.shape_coords)): x1, y1 = self.shape_coords[i] x2, y2 = self.shape_coords[(i + 1) % len(self.shape_coords)] if self.distance_to_segment(point, (x1, y1), (x2, y2)) <= 15: return True return False def distance_to_segment(self, p, v, w): l2 = (w[0] – v[0]) ** 2 + (w[1] – v[1]) ** 2 if l2 == 0: return math.hypot(p[0] – v[0], p[1] – v[1]) t = max(0, min(1, ((p[0] – v[0]) * (w[0] – v[0]) + (p[1] – v[1]) * (w[1] – v[1])) / l2)) projection = (v[0] + t * (w[0] – v[0]), v[1] + t * (w[1] – v[1])) return math.hypot(p[0] – projection[0], p[1] – projection[1]) def draw_start_button(self): button_rect = pygame.Rect(WIDTH // 2 – 100, HEIGHT // 2 + 25, 200, 50) pygame.draw.rect(screen, (100, 200, 100), button_rect) text = font.render(“Start Game”, True, (0, 0, 0)) screen.blit(text, (WIDTH // 2 – text.get_width() // 2, HEIGHT // 2 + 40)) def run(self): running = True while running: screen.fill((173, 216, 230)) # Light blue background if not self.game_started: self.draw_start_button() else: self.draw_bush_background() self.draw_shape() self.draw_circle(self.start_point, RED) self.draw_circle(self.end_point, BLUE) if self.traced_path: for i in range(1, len(self.traced_path)): pygame.draw.line(screen, GREEN, self.traced_path[i – 1], self.traced_path[i], 5) if self.current_line: for i in range(1, len(self.current_line)): pygame.draw.line(screen, GREEN, self.current_line[i – 1], self.current_line[i], 5) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: # Left mouse button if not self.game_started: self.game_started = True # Start the game self.reset_game() # Reset game else: if self.strayed_off_path: self.reset_game() # Reset game on game over else: self.start_tracing(event.pos) elif event.type == pygame.MOUSEMOTION: if self.tracing: self.trace_shape(event.pos) elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1 and not self.strayed_off_path: self.stop_tracing() if self.message: self.display_message(self.message) self.display_points() # Display points pygame.display.flip() self.clock.tick(FPS) pygame.quit() if __name__ == “__main__”: try: game = ShapeTracingGame() game.run() except Exception as e: print(f”An error occurred: {e}”) submitted by /u/Plastic-Bee4052 [link] [comments] 

Hi, this it the code for my game and it crashes when I double click the file name. Why? How can I solve it?

import pygame import math import tkinter as tk from tkinter import messagebox # Initialize Pygame pygame.init() # Constants WIDTH, HEIGHT = 600, 400 FPS = 60 LIGHT_GRAY = (211, 211, 211) GREEN = (0, 255, 0) RED = (255, 0, 0) BLUE = (0, 0, 255) BUSH_COLOR = (34, 139, 34) TRANSPARENCY = 128 # Alpha value for transparency (0-255) # Set up display screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Shape Tracing Game") # Font for messages font = pygame.font.SysFont('Arial', 24) class ShapeTracingGame: def __init__(self): self.clock = pygame.time.Clock() self.reset_game() self.game_started = False # Track if the game has started self.tk_root = tk.Tk() # Create a Tkinter root window self.tk_root.withdraw() # Hide the root window def reset_game(self): self.tracing = False self.traced_path = [] # Store completed paths self.current_line = [] # Store the current line being drawn self.starting_inside = False self.strayed_off_path = False self.points = 0 # Points counter self.level = 1 # Current level self.set_shape_and_points() # Initialize first level def set_shape_and_points(self): if self.level == 1: self.start_point = (100, 300) self.end_point = (300, 300) self.shape_coords = [(200, 50), (100, 300), (300, 300)] # Triangle self.bush_coords = [(180, 60), (80, 280), (320, 280), (420, 60), (300, 300), (100, 300)] elif self.level == 2: self.start_point = (50, 350) self.end_point = (550, 350) self.shape_coords = [(300, 50), (100, 350), (500, 350)] # Larger triangle self.bush_coords = [(180, 60), (80, 380), (520, 380), (420, 60), (300, 350), (100, 350)] # Add more levels as needed def draw_bush_background(self): pygame.draw.polygon(screen, BUSH_COLOR, self.bush_coords) def draw_shape(self): triangle_surface = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA) pygame.draw.polygon(triangle_surface, (*LIGHT_GRAY, TRANSPARENCY), self.shape_coords) pygame.draw.polygon(triangle_surface, LIGHT_GRAY, self.shape_coords, 30) # Outline screen.blit(triangle_surface, (0, 0)) def draw_circle(self, center, color): pygame.draw.circle(screen, color, center, 15) def display_message(self, message): text = font.render(message, True, (0, 0, 0)) screen.blit(text, (WIDTH // 2 - text.get_width() // 2, HEIGHT // 2 - text.get_height() // 2)) def display_points(self): points_text = font.render(f"Points: {self.points}", True, (0, 0, 0)) screen.blit(points_text, (500, 100)) def is_within_circle(self, point, center, radius): return math.hypot(point[0] - center[0], point[1] - center[1]) <= radius def start_tracing(self, pos): if self.is_within_circle(pos, self.start_point, 15): self.starting_inside = True self.tracing = True self.current_line.append(pos) else: self.message = "You must start within the red circle!" def trace_shape(self, pos): if self.tracing: if len(self.current_line) > 0: pygame.draw.line(screen, GREEN, self.current_line[-1], pos, 5) self.current_line.append(pos) if not self.is_within_shape(pos): self.strayed_off_path = True self.game_over() def game_over(self): messagebox.showinfo("Game Over!", f"Final Score: {self.points}") self.reset_game() # Reset game for next attempt def stop_tracing(self): if self.tracing: if self.starting_inside and not self.strayed_off_path and self.is_within_circle(self.current_line[-1], self.end_point, 15): self.traced_path.extend(self.current_line) self.points += 10 # Add points for successful completion self.message = "Congratulations! You completed the figure!" if self.level < 2: # Move to the next level if available self.level += 1 self.set_shape_and_points() else: self.game_over() self.tracing = False def is_within_shape(self, point): for i in range(len(self.shape_coords)): x1, y1 = self.shape_coords[i] x2, y2 = self.shape_coords[(i + 1) % len(self.shape_coords)] if self.distance_to_segment(point, (x1, y1), (x2, y2)) <= 15: return True return False def distance_to_segment(self, p, v, w): l2 = (w[0] - v[0]) ** 2 + (w[1] - v[1]) ** 2 if l2 == 0: return math.hypot(p[0] - v[0], p[1] - v[1]) t = max(0, min(1, ((p[0] - v[0]) * (w[0] - v[0]) + (p[1] - v[1]) * (w[1] - v[1])) / l2)) projection = (v[0] + t * (w[0] - v[0]), v[1] + t * (w[1] - v[1])) return math.hypot(p[0] - projection[0], p[1] - projection[1]) def draw_start_button(self): button_rect = pygame.Rect(WIDTH // 2 - 100, HEIGHT // 2 + 25, 200, 50) pygame.draw.rect(screen, (100, 200, 100), button_rect) text = font.render("Start Game", True, (0, 0, 0)) screen.blit(text, (WIDTH // 2 - text.get_width() // 2, HEIGHT // 2 + 40)) def run(self): running = True while running: screen.fill((173, 216, 230)) # Light blue background if not self.game_started: self.draw_start_button() else: self.draw_bush_background() self.draw_shape() self.draw_circle(self.start_point, RED) self.draw_circle(self.end_point, BLUE) if self.traced_path: for i in range(1, len(self.traced_path)): pygame.draw.line(screen, GREEN, self.traced_path[i - 1], self.traced_path[i], 5) if self.current_line: for i in range(1, len(self.current_line)): pygame.draw.line(screen, GREEN, self.current_line[i - 1], self.current_line[i], 5) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: # Left mouse button if not self.game_started: self.game_started = True # Start the game self.reset_game() # Reset game else: if self.strayed_off_path: self.reset_game() # Reset game on game over else: self.start_tracing(event.pos) elif event.type == pygame.MOUSEMOTION: if self.tracing: self.trace_shape(event.pos) elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1 and not self.strayed_off_path: self.stop_tracing() if self.message: self.display_message(self.message) self.display_points() # Display points pygame.display.flip() self.clock.tick(FPS) pygame.quit() if __name__ == "__main__": try: game = ShapeTracingGame() game.run() except Exception as e: print(f"An error occurred: {e}") 

submitted by /u/Plastic-Bee4052
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *