forked from stldavids/E05b-Physics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main1.py
89 lines (66 loc) · 2.24 KB
/
main1.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
import sys, logging, random, open_color, arcade
#check to make sure we are running the right version of Python
version = (3,7)
assert sys.version_info >= version, "This script requires at least Python {0}.{1}".format(version[0],version[1])
#turn on logging, in case we have to leave ourselves debugging messages
logging.basicConfig(format='[%(filename)s:%(lineno)d] %(message)s', level=logging.DEBUG)
logger = logging.getLogger(__name__)
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
MARGIN = 10
NUM_BALLS = 20
SCREEN_TITLE = "Gravity Exercise"
GRAVITY = - 9.8 #update this value
class Ball():
def __init__(self, x, y, color):
self.x = x
self.y = y
self.radius = 10
self.color = color
self.dx = 0
self.dy = 0
def draw(self):
arcade.draw_circle_filled(self.x, self.y, self.radius, self.color)
def update(self):
self.x += self.dx
self.y += self.dy
if self.x <= MARGIN:
self.x = MARGIN
if self.x >= SCREEN_WIDTH - MARGIN:
self.x = SCREEN_WIDTH - MARGIN
if self.y <= MARGIN:
self.y = MARGIN
if self.y >= SCREEN_HEIGHT - MARGIN:
self.y = SCREEN_HEIGHT - MARGIN
def accelerate(self,dx,dy):
self.dx += dx
self.dy += dy
class Window(arcade.Window):
def __init__(self, width, height, title):
super().__init__(width, height, title)
self.set_mouse_visible(True)
arcade.set_background_color(open_color.black)
self.ball_list = []
def setup(self):
for i in range(NUM_BALLS):
x = random.randint(MARGIN,SCREEN_WIDTH-MARGIN)
y = random.randint(MARGIN,SCREEN_HEIGHT-MARGIN)
color = random.choice(open_color.yellows)
self.ball = Ball(x,y,color)
self.ball_list.append(self.ball)
def update(self, delta_time,):
for b in self.ball_list:
x = 0
y = GRAVITY
b.accelerate(x,y)
b.update()
def on_draw(self):
arcade.start_render()
for b in self.ball_list:
b.draw()
def main():
window = Window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
window.setup()
arcade.run()
if __name__ == "__main__":
main()