forked from Neuraxio/New-Empty-Python-Project-Base
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbouncing_ball.py
More file actions
135 lines (106 loc) · 3.72 KB
/
bouncing_ball.py
File metadata and controls
135 lines (106 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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)
GREEN = (0, 128, 0) # A darker green for trees
RED = (255, 0, 0) # For the ball
# 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
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 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
# 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
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:
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()