-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygame_bouncy_balls.py
91 lines (65 loc) · 1.89 KB
/
pygame_bouncy_balls.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Pygame example
Do no use jupyter notebook. Call this code from console (or any IDE you like).
"""
import time
import pygame
import os
import numpy as np
from pygame import gfxdraw
class Item():
def __init__(self, position, speed):
self.mx, self.my = position
self.mx_1, self.my_1 = self.mx, self.my
self.vx, self.vy = speed
self.vx_1, self.vy_1 = self.vx, self.vy
rc = int(abs(vx / 3. * 255))
gc = int(abs(vy / 3. * 255))
bc = 0 # np.random.randint(0, 255)
self.color = (rc, gc, bc)
self.list_x = np.array([])
self.list_y = np.array([])
self.list_x_1 = np.array([])
self.list_y_1 = np.array([])
def update(self):
self.vx = (0.999 * self.vx)
self.vy = (0.999 * self.vy) + 0.003
self.mx += self.vx
self.my += self.vy
if self.my >= GROUND_LEVEL:
self.vy = -0.8 * self.vy
#self.vy = 0
self.vx = 0.5 * self.vx
self.my = GROUND_LEVEL
pygame.draw.circle(screen, self.color, (int(self.mx), int(self.my)), 20)
self.list_x_1 = np.append(self.list_x_1, self.mx)
self.list_y_1 = np.append(self.list_y_1, self.my)
WINDOW_X = 0
WINDOW_Y = 0
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (WINDOW_X, WINDOW_Y)
WIDTH = 1000
HEIGHT = 700
GROUND_LEVEL = HEIGHT - 50
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
done = False
mx = 50
my = GROUND_LEVEL
items = []
for idx in range(10):
vy = -np.random.random() * 3
vx = np.random.random() * 3
item = Item((mx, my), (vx, vy))
items.append(item)
while not done:
screen.fill((0, 0, 0))
pygame.draw.line(screen, (150, 75, 0), (0, GROUND_LEVEL), (WIDTH, GROUND_LEVEL), 10)
for item in items:
item.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
done = True
time.sleep(0.01)
pygame.display.flip()