Coimment about installing pymunk
This commit is contained in:
parent
3717b318b0
commit
225eada259
|
@ -5,6 +5,7 @@ import random
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
python -m pip install pymunk==5.7.0
|
||||||
Demo that shows integrating PySimpleGUI with the pymunk library. This combination
|
Demo that shows integrating PySimpleGUI with the pymunk library. This combination
|
||||||
of PySimpleGUI and pymunk could be used to build games.
|
of PySimpleGUI and pymunk could be used to build games.
|
||||||
Note this exact same demo runs with PySimpleGUIWeb by changing the import statement
|
Note this exact same demo runs with PySimpleGUIWeb by changing the import statement
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import PySimpleGUIWeb as sg
|
import PySimpleGUI as sg
|
||||||
|
# import PySimpleGUIWeb as sg
|
||||||
import pymunk
|
import pymunk
|
||||||
import random
|
import random
|
||||||
import socket
|
import socket
|
||||||
|
@ -7,6 +8,8 @@ import socket
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
To get a good version of pymunk that works with this code:
|
||||||
|
python -m pip install pymunk==5.7.0
|
||||||
Demo that shows integrating PySimpleGUI with the pymunk library. This combination
|
Demo that shows integrating PySimpleGUI with the pymunk library. This combination
|
||||||
of PySimpleGUI and pymunk could be used to build games.
|
of PySimpleGUI and pymunk could be used to build games.
|
||||||
Note this exact same demo runs with PySimpleGUIWeb by changing the import statement
|
Note this exact same demo runs with PySimpleGUIWeb by changing the import statement
|
||||||
|
@ -28,7 +31,8 @@ class Ball():
|
||||||
|
|
||||||
|
|
||||||
class Playfield():
|
class Playfield():
|
||||||
def __init__(self):
|
def __init__(self, graph_elem):
|
||||||
|
self.graph_elem = graph_elem
|
||||||
self.space = pymunk.Space()
|
self.space = pymunk.Space()
|
||||||
self.space.gravity = 0, 200
|
self.space.gravity = 0, 200
|
||||||
self.add_wall((0, 400), (600, 400)) # ground
|
self.add_wall((0, 400), (600, 400)) # ground
|
||||||
|
@ -50,29 +54,33 @@ class Playfield():
|
||||||
r = random.randint(1, 10)
|
r = random.randint(1, 10)
|
||||||
ball = Ball(x, y, r)
|
ball = Ball(x, y, r)
|
||||||
self.arena_balls.append(ball)
|
self.arena_balls.append(ball)
|
||||||
area.space.add(ball.body, ball.shape)
|
self.space.add(ball.body, ball.shape)
|
||||||
ball.gui_circle_figure = graph_elem.draw_circle(
|
ball.gui_circle_figure = self.graph_elem.draw_circle(
|
||||||
(x, y), r, fill_color='black', line_color='red')
|
(x, y), r, fill_color='black', line_width=0)
|
||||||
|
|
||||||
|
|
||||||
# ------------------- Build and show the GUI Window -------------------
|
def main():
|
||||||
graph_elem = sg.Graph((600, 400), (0, 400), (600, 0),
|
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------- Build and show the GUI Window -------------------
|
||||||
|
graph_elem = sg.Graph((600, 400), (0, 400), (600, 0),
|
||||||
enable_events=True, key='-GRAPH-', background_color='lightblue')
|
enable_events=True, key='-GRAPH-', background_color='lightblue')
|
||||||
|
|
||||||
hostname = socket.gethostbyname(socket.gethostname())
|
hostname = socket.gethostbyname(socket.gethostname())
|
||||||
layout = [[sg.Text('Ball Test'), sg.Text('My IP '+hostname)],
|
layout = [[sg.Text('Ball Test'), sg.Text('My IP '+hostname)],
|
||||||
[graph_elem],
|
[graph_elem],
|
||||||
# [sg.Up(), sg.Down()],
|
# [sg.Up(), sg.Down()],
|
||||||
[sg.Button('Kick'), sg.Button('Exit')]]
|
[sg.Button('Kick'), sg.Button('Exit')]]
|
||||||
|
|
||||||
window = sg.Window('Window Title', layout, finalize=True)
|
window = sg.Window('Window Title', layout, finalize=True)
|
||||||
|
|
||||||
area = Playfield()
|
area = Playfield(graph_elem)
|
||||||
area.add_balls()
|
area.add_balls()
|
||||||
|
|
||||||
# ------------------- GUI Event Loop -------------------
|
# ------------------- GUI Event Loop -------------------
|
||||||
while True: # Event Loop
|
while True: # Event Loop
|
||||||
event, values = window.read(timeout=0)
|
event, values = window.read(timeout=1)
|
||||||
# print(event, values)
|
# print(event, values)
|
||||||
if event in (sg.WIN_CLOSED, 'Exit'):
|
if event in (sg.WIN_CLOSED, 'Exit'):
|
||||||
break
|
break
|
||||||
|
@ -85,4 +93,7 @@ while True: # Event Loop
|
||||||
graph_elem.RelocateFigure(
|
graph_elem.RelocateFigure(
|
||||||
ball.gui_circle_figure, ball.body.position[0], ball.body.position[1])
|
ball.gui_circle_figure, ball.body.position[0], ball.body.position[1])
|
||||||
|
|
||||||
window.close()
|
window.close()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
Loading…
Reference in New Issue