Hi, I’m having a problem where this silly program I’m trying to put together should keep your drawn line visible when you’re done drawing and it works fine if you get it right on the first try. But if you make a mistake and draw outside the white lines defining that triangle that you must draw within and the line gets promptly erased as it should, when you try and succeed on a second attempt, the resulting successful line is deleted as well and it should be kept but I don’t know why. This is the code:
import pygame import math # 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.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.start_point = (100, 300) self.end_point = (300, 300) self.message = "" self.points = 0 # Points counter self.level = 1 # Current level def reset_game(self): self.tracing = False self.current_line.clear() self.starting_inside = False self.strayed_off_path = False self.message = "" self.level += 1 # Move to the next level self.set_shape_and_points() def set_shape_and_points(self): # Define shapes and starting/ending points for different levels 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): # Draw a bush shape in the background pygame.draw.polygon(screen, BUSH_COLOR, self.bush_coords) def draw_shape(self): # Create a surface for the triangle with transparency 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 with thickness of 30 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, (500, 50)) 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.message = "Stay within the lines. Try again." self.stop_tracing() # Stop tracing immediately on straying 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): # Successful completion self.traced_path.extend(self.current_line) self.points += 10 # Add points for successful completion self.message = "Congratulations! You completed the figure!" else: # Do not clear traced_path on failure, only clear current_line self.message = "You need to finish at the blue circle." if not self.strayed_off_path else self.message self.tracing = False self.current_line.clear() # Clear current line on stop 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 run(self): self.set_shape_and_points() # Initialize first level running = True while running: screen.fill((173, 216, 230)) # Light blue background self.draw_bush_background() self.draw_shape() self.draw_circle(self.start_point, RED) self.draw_circle(self.end_point, BLUE) # Draw all previously traced lines 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) 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.tracing: # Prevent drawing if level is completed 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: self.stop_tracing() if self.message.startswith("Congratulations"): self.reset_game() # Load next level # Draw the current line while tracing 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) 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__": game = ShapeTracingGame() game.run()
submitted by /u/Plastic-Bee4052
[link] [comments]
r/learnpython Hi, I’m having a problem where this silly program I’m trying to put together should keep your drawn line visible when you’re done drawing and it works fine if you get it right on the first try. But if you make a mistake and draw outside the white lines defining that triangle that you must draw within and the line gets promptly erased as it should, when you try and succeed on a second attempt, the resulting successful line is deleted as well and it should be kept but I don’t know why. This is the code: import pygame import math # 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.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.start_point = (100, 300) self.end_point = (300, 300) self.message = “” self.points = 0 # Points counter self.level = 1 # Current level def reset_game(self): self.tracing = False self.current_line.clear() self.starting_inside = False self.strayed_off_path = False self.message = “” self.level += 1 # Move to the next level self.set_shape_and_points() def set_shape_and_points(self): # Define shapes and starting/ending points for different levels 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): # Draw a bush shape in the background pygame.draw.polygon(screen, BUSH_COLOR, self.bush_coords) def draw_shape(self): # Create a surface for the triangle with transparency 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 with thickness of 30 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, (500, 50)) 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.message = “Stay within the lines. Try again.” self.stop_tracing() # Stop tracing immediately on straying 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): # Successful completion self.traced_path.extend(self.current_line) self.points += 10 # Add points for successful completion self.message = “Congratulations! You completed the figure!” else: # Do not clear traced_path on failure, only clear current_line self.message = “You need to finish at the blue circle.” if not self.strayed_off_path else self.message self.tracing = False self.current_line.clear() # Clear current line on stop 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 run(self): self.set_shape_and_points() # Initialize first level running = True while running: screen.fill((173, 216, 230)) # Light blue background self.draw_bush_background() self.draw_shape() self.draw_circle(self.start_point, RED) self.draw_circle(self.end_point, BLUE) # Draw all previously traced lines 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) 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.tracing: # Prevent drawing if level is completed 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: self.stop_tracing() if self.message.startswith(“Congratulations”): self.reset_game() # Load next level # Draw the current line while tracing 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) 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__”: game = ShapeTracingGame() game.run() submitted by /u/Plastic-Bee4052 [link] [comments]
Hi, I’m having a problem where this silly program I’m trying to put together should keep your drawn line visible when you’re done drawing and it works fine if you get it right on the first try. But if you make a mistake and draw outside the white lines defining that triangle that you must draw within and the line gets promptly erased as it should, when you try and succeed on a second attempt, the resulting successful line is deleted as well and it should be kept but I don’t know why. This is the code:
import pygame import math # 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.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.start_point = (100, 300) self.end_point = (300, 300) self.message = "" self.points = 0 # Points counter self.level = 1 # Current level def reset_game(self): self.tracing = False self.current_line.clear() self.starting_inside = False self.strayed_off_path = False self.message = "" self.level += 1 # Move to the next level self.set_shape_and_points() def set_shape_and_points(self): # Define shapes and starting/ending points for different levels 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): # Draw a bush shape in the background pygame.draw.polygon(screen, BUSH_COLOR, self.bush_coords) def draw_shape(self): # Create a surface for the triangle with transparency 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 with thickness of 30 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, (500, 50)) 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.message = "Stay within the lines. Try again." self.stop_tracing() # Stop tracing immediately on straying 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): # Successful completion self.traced_path.extend(self.current_line) self.points += 10 # Add points for successful completion self.message = "Congratulations! You completed the figure!" else: # Do not clear traced_path on failure, only clear current_line self.message = "You need to finish at the blue circle." if not self.strayed_off_path else self.message self.tracing = False self.current_line.clear() # Clear current line on stop 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 run(self): self.set_shape_and_points() # Initialize first level running = True while running: screen.fill((173, 216, 230)) # Light blue background self.draw_bush_background() self.draw_shape() self.draw_circle(self.start_point, RED) self.draw_circle(self.end_point, BLUE) # Draw all previously traced lines 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) 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.tracing: # Prevent drawing if level is completed 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: self.stop_tracing() if self.message.startswith("Congratulations"): self.reset_game() # Load next level # Draw the current line while tracing 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) 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__": game = ShapeTracingGame() game.run()
submitted by /u/Plastic-Bee4052
[link] [comments]