-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path017_MovingBall.py
48 lines (41 loc) · 1023 Bytes
/
017_MovingBall.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
import cv2
import numpy as np
image_w = 640
image_h = 480
ball_r = 50
ball_x = image_w // 2
ball_y = image_h // 2
ball_v = 10
ball_vy = 1
ball_vx = 1
while True:
image = np.zeros((image_h, image_w), dtype=np.uint8)
image[:, :] = 255
cv2.circle(image, (ball_x, ball_y), ball_r, 0)
cv2.imshow("image", image)
key_code = cv2.waitKey(1)
if key_code == 27:
break
elif key_code == ord('w'):
ball_vy -= 1
elif key_code == ord('s'):
ball_vy += 1
elif key_code == ord('a'):
ball_vx -= 1
elif key_code == ord('d'):
ball_vx += 1
else:
ball_y += ball_vy
ball_x += ball_vx
if ball_y < 0 + ball_r:
ball_y = 0 + ball_r
ball_vy = -ball_vy
if ball_y > image_h - ball_r:
ball_y = image_h - ball_r
ball_vy = -ball_vy
if ball_x < 0 + ball_r:
ball_x = 0 + ball_r
ball_vx = -ball_vx
if ball_x > image_w - ball_r:
ball_x = image_w - ball_r
ball_vx = -ball_vx