forked from virtualeiro/pygame-imfvj2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaula10_2_collisionWithBounciness.py
75 lines (55 loc) · 1.54 KB
/
aula10_2_collisionWithBounciness.py
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
import pygame
import math
# Initialize Pygame
pygame.init()
# Set up the display
width, height = 800, 400
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Bouncing Ball Simulation")
# Colors
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
# Ball parameters
ball_radius = 20
ball_x = ball_radius
ball_y = height // 2
ball_velocity = pygame.Vector2(50, 0) # Initial velocity vector
# Mass of the ball
ball_mass = 10
# Wall parameters
wall_x = width - 500
# Bounciness factor
bounciness = 0.25
# Time step
time_step = 0.05
# Calculate the momentum of the ball
initial_momentum = ball_mass * ball_velocity.x
# Calculate the force required to stop the ball
force = -initial_momentum / time_step
# Game loop
running = True
clock = pygame.time.Clock()
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update ball position
ball_x += ball_velocity.x * time_step
# Check if ball hits the wall
if ball_x >= wall_x - ball_radius:
ball_x = wall_x - ball_radius
ball_velocity.x *= -bounciness # Reverse velocity direction with reduced magnitude
# Draw the scene
screen.fill(BLACK)
# Draw wall
pygame.draw.line(screen, WHITE, (wall_x, 0), (wall_x, height), 3)
# Draw ball
pygame.draw.circle(screen, BLUE, (int(ball_x), int(ball_y)), ball_radius)
# Update the display
pygame.display.flip()
# Limit the frame rate
clock.tick(60)
# Quit the game
pygame.quit()