From 5479f8cce95ad9623454917588f628bd24af845c Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Sun, 28 Apr 2019 14:47:26 -0400 Subject: [PATCH 1/3] NEW demo programs using PyGame --- DemoPrograms/Demo_PyGame_Integration.py | 39 +++++++ DemoPrograms/Demo_PyGame_Snake_Game.py | 138 ++++++++++++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 DemoPrograms/Demo_PyGame_Integration.py create mode 100644 DemoPrograms/Demo_PyGame_Snake_Game.py diff --git a/DemoPrograms/Demo_PyGame_Integration.py b/DemoPrograms/Demo_PyGame_Integration.py new file mode 100644 index 00000000..dae260ff --- /dev/null +++ b/DemoPrograms/Demo_PyGame_Integration.py @@ -0,0 +1,39 @@ +import pygame +import PySimpleGUI as sg +import os + +""" + Demo of integrating PyGame with PySimpleGUI, the tkinter version + A similar technique may be possible with WxPython + Only works on windows from what I've read +""" +# --------------------- PySimpleGUI window layout and creation -------------------- +layout = [[sg.T('Test of PySimpleGUI with PyGame')], + [sg.Graph((500,500), (0,0), (500,500), background_color='lightblue', key='_GRAPH_' )], + [sg.B('Draw'), sg.Exit()]] + +window = sg.Window('PySimpleGUI + PyGame', layout).Finalize() +graph = window.Element('_GRAPH_') + +# -------------- Magic code to integrate PyGame with tkinter ------- +embed = graph.TKCanvas +os.environ['SDL_WINDOWID'] = str(embed.winfo_id()) +os.environ['SDL_VIDEODRIVER'] = 'windib' + +# ----------------------------- PyGame Code ----------------------------- + +screen = pygame.display.set_mode((500,500)) +screen.fill(pygame.Color(255,255,255)) + +pygame.display.init() +pygame.display.update() + +while True: + event, values = window.Read(timeout=10) + if event in (None, 'Exit'): + break + elif event == 'Draw': + pygame.draw.circle(screen, (0, 0, 0), (250, 250), 125) + pygame.display.update() + +window.Close() \ No newline at end of file diff --git a/DemoPrograms/Demo_PyGame_Snake_Game.py b/DemoPrograms/Demo_PyGame_Snake_Game.py new file mode 100644 index 00000000..6d1485e7 --- /dev/null +++ b/DemoPrograms/Demo_PyGame_Snake_Game.py @@ -0,0 +1,138 @@ +import pygame +import PySimpleGUI as sg +import os + +""" + Demo - Simple Snake Game using PyGame and PySimpleGUI + This demo may not be fully functional in terms of getting the coordinate + systems right or other problems due to a lack of understanding of PyGame + The purpose of the demo is to show one way of adding a PyGame window into your PySimpleGUI window + Note, you must click on the game area in order for PyGame to get keyboard strokes, etc. + Tried using set_focus to switch to the PyGame canvas but still needed to click on game area +""" + +# --- Globals --- +# Colors +BLACK = (0, 0, 0) +WHITE = (255, 255, 255) + +# Set the width and height of each snake segment +segment_width = 15 +segment_height = 15 +# Margin between each segment +segment_margin = 3 + +# Set initial speed +x_change = segment_width + segment_margin +y_change = 0 + + +class Segment(pygame.sprite.Sprite): + """ Class to represent one segment of the snake. """ + # -- Methods + # Constructor function + def __init__(self, x, y): + # Call the parent's constructor + super().__init__() + + # Set height, width + self.image = pygame.Surface([segment_width, segment_height]) + self.image.fill(WHITE) + + # Make our top-left corner the passed-in location. + self.rect = self.image.get_rect() + self.rect.x = x + self.rect.y = y + +# --------------------------- GUI Setup & Create Window ------------------------------- + +layout = [[sg.T('Snake Game - PySimpleGUI + PyGame')], + [sg.Graph((800,600), (0,0), (800,600), background_color='lightblue', key='_GRAPH_')], + [sg.Exit()]] + +window = sg.Window('Snake Game using PySimpleGUI and PyGame', layout).Finalize() + +# ------------------------ Do the magic that integrates PyGame and Graph Element ------------------ +graph = window.Element('_GRAPH_') # type: sg.Graph +embed = graph.TKCanvas +os.environ['SDL_WINDOWID'] = str(embed.winfo_id()) +os.environ['SDL_VIDEODRIVER'] = 'windib' + +# ----------------------------- PyGame Code ----------------------------- +# Call this function so the Pygame library can initialize itself +# pygame.init() +screen = pygame.display.set_mode((800,600)) +screen.fill(pygame.Color(255,255,255)) + +pygame.display.init() +pygame.display.update() + +# Set the title of the window +pygame.display.set_caption('Snake Example') + +allspriteslist = pygame.sprite.Group() + +# Create an initial snake +snake_segments = [] +for i in range(15): + x = 250 - (segment_width + segment_margin) * i + y = 30 + segment = Segment(x, y) + snake_segments.append(segment) + allspriteslist.add(segment) + +clock = pygame.time.Clock() + +while True: + event, values = window.Read(timeout=10) + if event in (None, 'Exit'): + break + pygame.display.update() + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + break + # Set the speed based on the key pressed + # We want the speed to be enough that we move a full + # segment, plus the margin. + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_LEFT: + x_change = (segment_width + segment_margin) * -1 + y_change = 0 + if event.key == pygame.K_RIGHT: + x_change = (segment_width + segment_margin) + y_change = 0 + if event.key == pygame.K_UP: + x_change = 0 + y_change = (segment_height + segment_margin) * -1 + if event.key == pygame.K_DOWN: + x_change = 0 + y_change = (segment_height + segment_margin) + + # Get rid of last segment of the snake + # .pop() command removes last item in list + old_segment = snake_segments.pop() + allspriteslist.remove(old_segment) + + # Figure out where new segment will be + x = snake_segments[0].rect.x + x_change + y = snake_segments[0].rect.y + y_change + segment = Segment(x, y) + + # Insert new segment into the list + snake_segments.insert(0, segment) + allspriteslist.add(segment) + + # -- Draw everything + # Clear screen + screen.fill(BLACK) + + allspriteslist.draw(screen) + + # Flip screen + pygame.display.flip() + + # Pause + clock.tick(5) + +window.Close() From fc7a382af1c4b97deda7470e7cf909e92ad7b6a6 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Sun, 28 Apr 2019 14:50:03 -0400 Subject: [PATCH 2/3] Added Output Element to window demonstraint multiple threads printing --- DemoPrograms/Demo_Multithreaded_Queued.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/DemoPrograms/Demo_Multithreaded_Queued.py b/DemoPrograms/Demo_Multithreaded_Queued.py index 9ea968e4..66e09e1c 100644 --- a/DemoPrograms/Demo_Multithreaded_Queued.py +++ b/DemoPrograms/Demo_Multithreaded_Queued.py @@ -55,7 +55,7 @@ def worker_thread(thread_name, run_freq, gui_queue): for i in itertools.count(): # loop forever, keeping count in i as it loops time.sleep(run_freq/1000) # sleep for a while gui_queue.put('{} - {}'.format(thread_name, i)) # put a message into queue for GUI - + print('..') ###### ## ## #### ## ## ## ## ## @@ -76,12 +76,13 @@ def the_gui(gui_queue): """ layout = [ [sg.Text('Multithreaded Window Example')], [sg.Text('', size=(15,1), key='_OUTPUT_')], + [sg.Output(size=(40,6))], [sg.Button('Exit')],] window = sg.Window('Multithreaded Window').Layout(layout) # --------------------- EVENT LOOP --------------------- while True: - event, values = window.Read(timeout=100) # wait for up to 100 ms for a GUI event from user + event, values = window.Read(timeout=100) # wait for up to 100 ms for a GUI event if event is None or event == 'Exit': break #--------------- Loop through all messages coming in from threads --------------- From f3387fa186054e92af30773eb5d35adbb357e35a Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Sun, 28 Apr 2019 14:50:18 -0400 Subject: [PATCH 3/3] Removed up and down arrows --- DemoPrograms/Demo_Graph_pymunk_2D_Graphics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DemoPrograms/Demo_Graph_pymunk_2D_Graphics.py b/DemoPrograms/Demo_Graph_pymunk_2D_Graphics.py index 79d70ff0..9dcd8e9c 100644 --- a/DemoPrograms/Demo_Graph_pymunk_2D_Graphics.py +++ b/DemoPrograms/Demo_Graph_pymunk_2D_Graphics.py @@ -53,7 +53,7 @@ graph_elem = sg.Graph((600, 400), (0, 400), (600, 0), enable_events=True, key='_ layout = [[sg.Text('Ball Test'), sg.T('My IP {}'.format(socket.gethostbyname(socket.gethostname())))], [graph_elem], - [sg.Up(), sg.Down()], + # [sg.Up(), sg.Down()], [sg.B('Kick'), sg.Button('Exit')]] window = sg.Window('Window Title', layout, ).Finalize()