From a319b17129456c5ed5c461a1a57c2e53cdc14a88 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 21 May 2025 00:18:59 +0000 Subject: [PATCH 1/5] I've added a Pygame script for a bouncing ball animation. This script, `bouncing_ball.py`, creates a simple graphical animation: - A ball moves and bounces off the edges of the Pygame window. - Two green triangles representing trees are displayed on either side of the screen. Features: - Initializes Pygame and sets up an 800x600 display. - Defines a `Ball` class to handle position, movement, and drawing of the ball. - Includes a `draw_tree` function to render simple triangular trees. - The main game loop updates ball position, handles bouncing off screen edges, draws trees and the ball, and controls the frame rate to 60 FPS. --- bouncing_ball.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 bouncing_ball.py diff --git a/bouncing_ball.py b/bouncing_ball.py new file mode 100644 index 0000000..0762c50 --- /dev/null +++ b/bouncing_ball.py @@ -0,0 +1,83 @@ +import pygame + +# Initialize Pygame +pygame.init() + +# Define screen dimensions +SCREEN_WIDTH = 800 +SCREEN_HEIGHT = 600 + +# Create the screen object +screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) + +# Set the window title +pygame.display.set_caption("Bouncing Ball") + +# Define colors +WHITE = (255, 255, 255) +BLACK = (0, 0, 0) +GREEN = (0, 128, 0) # A darker green for trees +RED = (255, 0, 0) # For the ball + +# Ball class +class Ball: + def __init__(self, x, y, radius, color, dx, dy): + self.x = x + self.y = y + self.radius = radius + self.color = color + self.dx = dx + self.dy = dy + + def draw(self, screen): + pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius) + + def update(self): + self.x += self.dx + self.y += self.dy + +# Function to draw a tree +def draw_tree(screen, x, y): + """Draws a simple pine tree shape. + + Args: + screen: The Pygame screen object. + x: The x-coordinate of the top point of the tree. + y: The y-coordinate of the top point of the tree. + """ + point1 = (x, y) + point2 = (x - 40, y + 100) # Bottom-left + point3 = (x + 40, y + 100) # Bottom-right + pygame.draw.polygon(screen, GREEN, [point1, point2, point3]) + +# Create a ball instance +ball = Ball(x=100, y=SCREEN_HEIGHT // 2, radius=15, color=RED, dx=5, dy=3) # Added dy for Y bounce demo + +clock = pygame.time.Clock() # New +# Game loop +running = True +while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + + ball.update() # Update ball's position + + # Bouncing logic + if ball.x + ball.radius > SCREEN_WIDTH or ball.x - ball.radius < 0: + ball.dx *= -1 + if ball.y + ball.radius > SCREEN_HEIGHT or ball.y - ball.radius < 0: + ball.dy *= -1 + + screen.fill(WHITE) # Clear screen + + # Draw trees + draw_tree(screen, x=60, y=SCREEN_HEIGHT - 150) + draw_tree(screen, x=SCREEN_WIDTH - 60, y=SCREEN_HEIGHT - 150) + + ball.draw(screen) # Draw the ball + + pygame.display.flip() # Update the full display + clock.tick(60) # New + +pygame.quit() From ca3772ab7fce2c06dc2192abfeb78b3c405b2c24 Mon Sep 17 00:00:00 2001 From: Nag Deepanjan Date: Sat, 24 May 2025 21:42:48 -0400 Subject: [PATCH 2/5] Update bouncing_ball.py Co-authored-by: amazon-q-developer[bot] <208079219+amazon-q-developer[bot]@users.noreply.github.com> --- bouncing_ball.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/bouncing_ball.py b/bouncing_ball.py index 0762c50..0d83c50 100644 --- a/bouncing_ball.py +++ b/bouncing_ball.py @@ -32,7 +32,15 @@ def __init__(self, x, y, radius, color, dx, dy): def draw(self, screen): pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius) - def update(self): +def draw(self, screen): + pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius) + + def update(self, dt): + self.x += self.dx * dt + self.y += self.dy * dt + +# Function to draw a tree +def draw_tree(screen, x, y): self.x += self.dx self.y += self.dy From 800bf1fe99e40c0421529213e2018effa8627a23 Mon Sep 17 00:00:00 2001 From: Nag Deepanjan Date: Sat, 24 May 2025 21:43:23 -0400 Subject: [PATCH 3/5] Update bouncing_ball.py Co-authored-by: amazon-q-developer[bot] <208079219+amazon-q-developer[bot]@users.noreply.github.com> --- bouncing_ball.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/bouncing_ball.py b/bouncing_ball.py index 0d83c50..e8722df 100644 --- a/bouncing_ball.py +++ b/bouncing_ball.py @@ -61,9 +61,39 @@ def draw_tree(screen, x, y): # Create a ball instance ball = Ball(x=100, y=SCREEN_HEIGHT // 2, radius=15, color=RED, dx=5, dy=3) # Added dy for Y bounce demo +clock = pygame.time.Clock() # New +# Game loop clock = pygame.time.Clock() # New # Game loop running = True +try: + while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + + ball.update() # Update ball's position + + # Bouncing logic + if ball.x + ball.radius > SCREEN_WIDTH or ball.x - ball.radius < 0: + ball.dx *= -1 + if ball.y + ball.radius > SCREEN_HEIGHT or ball.y - ball.radius < 0: + ball.dy *= -1 + + screen.fill(WHITE) # Clear screen + + # Draw trees + draw_tree(screen, x=60, y=SCREEN_HEIGHT - 150) + draw_tree(screen, x=SCREEN_WIDTH - 60, y=SCREEN_HEIGHT - 150) + + ball.draw(screen) # Draw the ball + + pygame.display.flip() # Update the full display + clock.tick(60) # New +except pygame.error as e: + print(f"A Pygame error occurred: {e}") +finally: + pygame.quit() while running: for event in pygame.event.get(): if event.type == pygame.QUIT: From ea1944d7d5729f1b398972691a448bd3bee5d2b6 Mon Sep 17 00:00:00 2001 From: Nag Deepanjan Date: Sat, 24 May 2025 21:44:26 -0400 Subject: [PATCH 4/5] Update bouncing_ball.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- bouncing_ball.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bouncing_ball.py b/bouncing_ball.py index e8722df..1ced2f6 100644 --- a/bouncing_ball.py +++ b/bouncing_ball.py @@ -15,7 +15,6 @@ # Define colors WHITE = (255, 255, 255) -BLACK = (0, 0, 0) GREEN = (0, 128, 0) # A darker green for trees RED = (255, 0, 0) # For the ball From aacff244c068481913c393d94f58947ac2f5f99a Mon Sep 17 00:00:00 2001 From: Nag Deepanjan Date: Sat, 24 May 2025 21:44:41 -0400 Subject: [PATCH 5/5] Update bouncing_ball.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- bouncing_ball.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/bouncing_ball.py b/bouncing_ball.py index 1ced2f6..6ff24de 100644 --- a/bouncing_ball.py +++ b/bouncing_ball.py @@ -20,6 +20,21 @@ # Ball class class Ball: + """ + Represents a bouncing ball in the game. + + Attributes: + x (float): The x-coordinate of the ball's center. + y (float): The y-coordinate of the ball's center. + radius (int): The radius of the ball. + color (tuple): The color of the ball in RGB format. + dx (float): The horizontal velocity of the ball. + dy (float): The vertical velocity of the ball. + + Methods: + draw(screen): Draws the ball on the given Pygame screen. + update(): Updates the ball's position based on its velocity. + """ def __init__(self, x, y, radius, color, dx, dy): self.x = x self.y = y