Skip to content

Instantly share code, notes, and snippets.

@quantumjim
Last active April 3, 2020 12:23
Show Gist options
  • Save quantumjim/0717496e471200c20e35314dfbbfe157 to your computer and use it in GitHub Desktop.
Save quantumjim/0717496e471200c20e35314dfbbfe157 to your computer and use it in GitHub Desktop.
Game engine with jupyter widgets
from ipywidgets import widgets
from ipywidgets import Layout, HBox, VBox
from IPython.display import display
class jupyter_widget_engine():
def __init__(self,start,next_frame,parameters):
self.next_frame = next_frame
self.parameters = parameters
L = 8
layout = Layout(width='50px', height='50px')
screen = {}
for X in range(L):
for Y in range(L):
screen[X,Y] = widgets.ToggleButton(description='',button_style='',layout=layout,disabled=True)
controller = {}
controller['blank'] = widgets.ToggleButton(description='',button_style='',layout=layout)
controller['up'] = widgets.ToggleButton(description='▲',button_style='',layout=layout)
controller['down'] = widgets.ToggleButton(description='▼',button_style='',layout=layout)
controller['left'] = widgets.ToggleButton(description='◀︎',button_style='',layout=layout)
controller['right'] = widgets.ToggleButton(description='►',button_style='',layout=layout)
controller['O'] = widgets.ToggleButton(description='O',button_style='',layout=layout)
controller['X'] = widgets.ToggleButton(description='X',button_style='',layout=layout)
controller['continue'] = widgets.ToggleButton(description='Continue',button_style='',layout=Layout(width='320px', height='50px'))
[b,u,d,l,r,o,x,c] = [controller['blank'],
controller['up'],
controller['down'],
controller['left'],
controller['right'],
controller['O'],
controller['X'],
controller['continue']]
interface = []
interface.append( widgets.HBox([screen[X,0] for X in range(L)]+[b,u,b,b,b,o]) )
interface.append( widgets.HBox([screen[X,1] for X in range(L)]+[l,b,r,b,x,b]) )
interface.append( widgets.HBox([screen[X,2] for X in range(L)]+[b,d,b,b,b,b]) )
interface.append( widgets.HBox([screen[X,3] for X in range(L)]+[c]) )
for Y in range(4,L):
interface.append( widgets.HBox([screen[X,Y] for X in range(L)]) )
self.screen = screen
self.controller = controller
start(self)
display(widgets.VBox(interface))
b.observe(self.given_b)
self.controller['continue'].observe(self._next_frame)
def given_b(self,obs_b):
if self.controller['blank'].value:
self.controller['blank'].value = False
def _next_frame(self,obs_n):
self.next_frame(self)
for button in self.controller.values():
button.value = False
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Copyright IBM - Research 2020
Apache 2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment