-
Notifications
You must be signed in to change notification settings - Fork 5
/
bouncing_ball.py
56 lines (43 loc) · 1.12 KB
/
bouncing_ball.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
import time
import cv2
import numpy as np
import random
WHITE = 255, 255, 255
SIZE = 700
class BouncingBall:
radius = 20
color = WHITE
def __init__(self):
self.x = 100
self.y = 100
self.dx = 1
self.dy = 1
def move(self):
self.x += self.dx
self.y += self.dy
if self.x < 0:
self.x = 0
self.dx = random.randint(1, 3)
if self.y < 0:
self.y = 0
self.dy = random.randint(1, 3)
if self.x >= SIZE:
self.dx = -random.randint(1, 3)
if self.y >= SIZE:
self.dy = -random.randint(1, 3)
def draw(self, frame):
Y, X = np.ogrid[0:SIZE, 0:SIZE]
square_dist = (X - self.x) ** 2 + (Y - self.y) ** 2
mask = square_dist < self.radius ** 2
frame[mask] = self.color
bg = np.zeros((SIZE, SIZE, 3), dtype=np.uint8)
ball = BouncingBall()
while True:
frame = bg.copy() // 2
ball.move()
ball.draw(frame)
cv2.imshow('bouncing ball', frame)
key = chr(cv2.waitKey(1) & 0xFF)
if key == 'q':
break
cv2.destroyAllWindows()