MCPcopy Index your code
hub / github.com/PySimpleGUI/PySimpleGUI / Playfield

Class Playfield

DemoPrograms/Demo_Graph_Ball_Game.py:38–75  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

36
37
38class Playfield():
39 def __init__(self, graph_elem):
40 self.space = pymunk.Space()
41 self.space.gravity = 0, 200
42 self.add_wall((0, 400), (600, 400)) # ground
43 self.add_wall((0, 0), (0, 600)) # Left side
44 self.add_wall((600, 0), (600, 400)) # right side
45 self.arena_balls = [] # type: List[Ball]
46 self.graph_elem = graph_elem # type: sg.Graph
47
48 def add_wall(self, pt_from, pt_to):
49 body = pymunk.Body(body_type=pymunk.Body.STATIC)
50 ground_shape = pymunk.Segment(body, pt_from, pt_to, 0.0)
51 ground_shape.friction = 0.8
52 ground_shape.elasticity = .99
53 ground_shape.mass = pymunk.inf
54 self.space.add(ground_shape)
55
56 def add_random_balls(self):
57 for i in range(1, 200):
58 x = random.randint(0, 600)
59 y = random.randint(0, 400)
60 r = random.randint(1, 10)
61 self.add_ball(x, y, r)
62
63 def add_ball(self, x, y, r, fill_color='black', line_color='red'):
64 ball = Ball(x, y, r, self.graph_elem)
65 self.arena_balls.append(ball)
66 area.space.add(ball.body, ball.shape)
67 ball.gui_circle_figure = self.graph_elem.draw_circle(
68 (x, y), r, fill_color=fill_color, line_color=line_color)
69 return ball
70
71 def shoot_a_ball(self, x, y, r, vector=(-10, 0), fill_color='black', line_color='red'):
72 ball = self.add_ball(
73 x, y, r, fill_color=fill_color, line_color=line_color)
74 # ball.shape.surface_velocity=10
75 ball.body.apply_impulse_at_local_point(100*pymunk.Vec2d(vector))
76
77
78# ------------------- Build and show the GUI Window -------------------

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected