Fixed flicker problem!!! Updated all of the PySimpleGUIWeb demos
This commit is contained in:
parent
f0e1642437
commit
a2443c63ad
14 changed files with 413 additions and 298 deletions
|
@ -1,5 +1,4 @@
|
|||
# import PySimpleGUIWeb as sg
|
||||
import PySimpleGUI as sg
|
||||
import PySimpleGUIWeb as sg
|
||||
import pymunk
|
||||
import random
|
||||
import socket
|
||||
|
@ -10,17 +9,20 @@ import socket
|
|||
Note this exact same demo runs with PySimpleGUIWeb by changing the import statement
|
||||
"""
|
||||
|
||||
|
||||
class Ball():
|
||||
def __init__(self, x, y, r, *args, **kwargs):
|
||||
mass = 10
|
||||
self.body = pymunk.Body(mass,
|
||||
pymunk.moment_for_circle(mass, 0, r, (0, 0))) # Create a Body with mass and moment
|
||||
self.body.position = x, y
|
||||
self.shape = pymunk.Circle(self.body, r, offset=(0, 0)) # Create a box shape and attach to body
|
||||
# Create a box shape and attach to body
|
||||
self.shape = pymunk.Circle(self.body, r, offset=(0, 0))
|
||||
self.shape.elasticity = 0.99999
|
||||
self.shape.friction = 0.8
|
||||
self.gui_circle_figure = None
|
||||
|
||||
|
||||
class Playfield():
|
||||
def __init__(self):
|
||||
self.space = pymunk.Space()
|
||||
|
@ -30,7 +32,7 @@ class Playfield():
|
|||
self.add_wall(self.space, (600, 0), (600, 400)) # right side
|
||||
|
||||
def add_wall(self, space, pt_from, pt_to):
|
||||
body = pymunk.Body(body_type=pymunk.Body.STATIC)
|
||||
body = pymunk.Body(body_type=pymunk.Body.STATIC)
|
||||
ground_shape = pymunk.Segment(body, pt_from, pt_to, 0.0)
|
||||
ground_shape.friction = 0.8
|
||||
ground_shape.elasticity = .99
|
||||
|
@ -45,24 +47,25 @@ class Playfield():
|
|||
ball = Ball(x, y, r)
|
||||
self.arena_balls.append(ball)
|
||||
area.space.add(ball.body, ball.shape)
|
||||
ball.gui_circle_figure = graph_elem.DrawCircle((x, y), r, fill_color='black', line_color='red')
|
||||
ball.gui_circle_figure = graph_elem.draw_circle(
|
||||
(x, y), r, fill_color='black', line_color='red')
|
||||
|
||||
|
||||
# ------------------- Build and show the GUI Window -------------------
|
||||
graph_elem = sg.Graph((600, 400), (0, 400), (600, 0), enable_events=True, key='_GRAPH_', background_color='lightblue')
|
||||
graph_elem = sg.Graph((600, 400), (0, 400), (600, 0),
|
||||
enable_events=True, key='_GRAPH_', background_color='lightblue')
|
||||
|
||||
layout = [[sg.Text('Ball Test'), sg.T('My IP {}'.format(socket.gethostbyname(socket.gethostname())))],
|
||||
layout = [[sg.Text('Ball Test'), sg.Text('My IP {}'.format(socket.gethostbyname(socket.gethostname())))],
|
||||
[graph_elem],
|
||||
[sg.B('Kick'), sg.Button('Exit')]]
|
||||
|
||||
window = sg.Window('Window Title', layout, ).Finalize()
|
||||
[sg.Button('Kick'), sg.Button('Exit')]]
|
||||
|
||||
window = sg.Window('Window Title', layout, finalize=True)
|
||||
area = Playfield()
|
||||
area.add_balls()
|
||||
|
||||
# ------------------- GUI Event Loop -------------------
|
||||
while True: # Event Loop
|
||||
event, values = window.Read(timeout=0)
|
||||
event, values = window.read(timeout=0)
|
||||
# print(event, values)
|
||||
if event in (None, 'Exit'):
|
||||
break
|
||||
|
@ -70,7 +73,9 @@ while True: # Event Loop
|
|||
|
||||
for ball in area.arena_balls:
|
||||
if event == 'Kick':
|
||||
ball.body.position = ball.body.position[0], ball.body.position[1]-random.randint(1,200)
|
||||
graph_elem.RelocateFigure(ball.gui_circle_figure, ball.body.position[0], ball.body.position[1])
|
||||
ball.body.position = ball.body.position[0], ball.body.position[1]-random.randint(
|
||||
1, 200)
|
||||
graph_elem.relocate_figure(
|
||||
ball.gui_circle_figure, ball.body.position[0], ball.body.position[1])
|
||||
|
||||
window.Close()
|
||||
window.close()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue