| 24 | |
| 25 | |
| 26 | class Playfield(): |
| 27 | def __init__(self, graph_elem, screensize): |
| 28 | self.graph_elem = graph_elem # type: sg.Graph |
| 29 | self.space = pymunk.Space() |
| 30 | self.space.gravity = 0, 200 |
| 31 | self.screensize = screensize |
| 32 | self.add_wall((0, screensize[1]), (screensize[0],screensize[1])) # ground |
| 33 | self.add_wall((0, 0), (0, screensize[1])) # Left side |
| 34 | self.add_wall((screensize[0], 0), (screensize[0], screensize[1])) # right side |
| 35 | self.arena_balls = [] # type: List[Ball] |
| 36 | |
| 37 | def add_wall(self, pt_from, pt_to): |
| 38 | body = pymunk.Body(body_type=pymunk.Body.STATIC) |
| 39 | ground_shape = pymunk.Segment(body, pt_from, pt_to, 1.0) |
| 40 | ground_shape.friction = 0.8 |
| 41 | ground_shape.elasticity = .99 |
| 42 | self.space.add(ground_shape) |
| 43 | |
| 44 | def add_balls(self, num_balls = 30): |
| 45 | for i in range(1, num_balls): |
| 46 | x = random.randint(0, self.screensize[0]) |
| 47 | y = random.randint(0, self.screensize[1]) |
| 48 | r = random.randint(5, 10) |
| 49 | ball = Ball(x, y, r) |
| 50 | self.arena_balls.append(ball) |
| 51 | self.space.add(ball.body, ball.shape) |
| 52 | ball.gui_circle_figure = self.graph_elem.draw_circle( |
| 53 | (x, y), r, fill_color=random.choice(('#23a0a0', '#56d856', '#be45be', '#5681d8', '#d34545', '#BE7C29')), line_width=0) |
| 54 | |
| 55 | |
| 56 | def main(): |