-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_pygame_bouncing_ball.py
More file actions
59 lines (47 loc) · 1.48 KB
/
13_pygame_bouncing_ball.py
File metadata and controls
59 lines (47 loc) · 1.48 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
import pygame
# color definition //
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
yellow = (255, 255, 0)
blue = (0, 0, 255)
# pygame window initilizion
bg_color = white
window_width = 800 # width in pixels
window_height = 600 # height in pixels
window_x_center = int(window_width / 2)
window_y_center = int(window_height / 2)
frameRate = 60 # FPS
pygame.init()
gameClock = pygame.time.Clock()
display_surface = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('Bouncing ball')
# GameParameters
block_size = 40
speed_x = 8
speed_y = 8
position_x = window_width // 2
position_y = window_height // 2
# gameLOOP
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
exit()
# Rendering
display_surface.fill(bg_color)
position_x = position_x + speed_x
position_y = position_y + speed_y
# boucing the ball
if position_x + block_size >= window_width: # right edge
speed_x = -(speed_x)
if position_x - block_size <= 0: # left edge
speed_x = -(speed_x)
if position_y + block_size >= window_height: # Bottom edge
speed_y = -(speed_y)
if position_y - block_size <= 0: # Top edge
speed_y = -(speed_y)
pygame.draw.circle(display_surface, blue, (position_x, position_y), block_size)
pygame.display.flip()
gameClock.tick(frameRate)