-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter4_BouningBall.py
More file actions
51 lines (38 loc) · 963 Bytes
/
Chapter4_BouningBall.py
File metadata and controls
51 lines (38 loc) · 963 Bytes
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
import pygame
import random
pygame.init()
WIDTH=800
HEIGHT=600
screen=pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Bouning Ball")
running=True
clock=pygame.time.Clock()
#Step-2 Ball Variables
x=100
y=100
radius=20
#speed of ball
x_speed=5
y_speed=5
while running:
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
#moving logic
x=x+x_speed
y=y+y_speed
#Bouncing Logic
#left/right handle
if x-radius <= 0 or x+radius>=WIDTH:
x_speed=-x_speed
#top/bottom handle
if y-radius <=0 or y+radius >=HEIGHT:
y_speed=-y_speed
#draw circle
color=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
pygame.draw.rect(screen, (255, 255, 255), (350, 550, 100, 20), 2)
pygame.draw.circle(screen,color,(x,y),radius)
pygame.display.update()
clock.tick(60) #fps
pygame.quit()