MCPcopy
hub / github.com/PySimpleGUI/PySimpleGUI / pong

Function pong

DemoPrograms/Demo_Pong_Multiple_Platforms.py:143–192  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

141
142
143def pong():
144 layout = [[sg.Graph(GAMEPLAY_SIZE,
145 (0, GAMEPLAY_SIZE[1]),
146 (GAMEPLAY_SIZE[0], 0),
147 background_color=BACKGROUND_COLOR,
148 key='-GRAPH-')],
149 [sg.Text(''),
150 sg.Button('Exit'),
151 sg.Text('Speed'),
152 sg.Slider((0, 20),
153 default_value=10,
154 orientation='h',
155 enable_events=True,
156 key='-SPEED-')]
157 ]
158
159 window = sg.Window(
160 'Pong', layout, return_keyboard_events=True, finalize=True)
161
162 graph_elem = window['-GRAPH-'] # type: sg.Graph
163
164 bat_1 = PongBall(graph_elem, 'red', 30)
165 bat_2 = PongBall(graph_elem, 'blue', 670)
166 ball_1 = Ball(graph_elem, bat_1, bat_2, 'green1')
167 sleep_time = 10
168
169 while True:
170 ball_1.draw()
171 bat_1.draw()
172 bat_2.draw()
173
174 event, values = window.read(
175 timeout=sleep_time) # type: str, str
176 if event in (sg.WIN_CLOSED, 'Exit'):
177 break
178 elif event.startswith('Up') or event.endswith('Up'):
179 bat_2.up(5)
180 elif event.startswith('Down') or event.endswith('Down'):
181 bat_2.down(5)
182 elif event == 'w':
183 bat_1.up(5)
184 elif event == 's':
185 bat_1.down(5)
186 elif event == '-SPEED-':
187 sleep_time = int(values['-SPEED-'])
188
189 if ball_1.win_loss_check():
190 sg.popup('Game Over', ball_1.win_loss_check() + ' won!!')
191 break
192 window.close()
193
194
195if __name__ == '__main__':

Callers 1

Calls 9

drawMethod · 0.95
drawMethod · 0.95
readMethod · 0.95
upMethod · 0.95
downMethod · 0.95
win_loss_checkMethod · 0.95
closeMethod · 0.95
PongBallClass · 0.85
BallClass · 0.70

Tested by

no test coverage detected