Runnable Examples¶
OAuth Flow¶
Note
The runnable source for this can be found in the examples/2_shortcode_oauth folder of this repo.
This example shows you how to use the shortcode OAuth flow to get an access token to connect to Interactive. For development you can manually request a token, but the shortcode flow provides a better experience to users!
import asyncio
from sys import argv
async def get_access_token(client):
code = await client.get_code()
print("Go to mixer.com/go and enter {}".format(code.code))
try:
return await code.accepted()
except interactive.ShortCodeAccessDeniedError:
print("The user denied access to our client")
except interactive.ShortCodeTimeoutError:
print("Yo, you're too slow! Let's try again...")
return await get_access_token(client)
async def run():
try:
with interactive.OAuthClient(argv[1]) as client:
token = await get_access_token(client)
print("Access token: {}".format(token.access))
except interactive.UnknownShortCodeError as e:
print("An unknown error occurred in Mixer: {}".format(e))
Pong¶
Note
The runnable source for this can be found in the examples/1_viewer_controlled folder of this repo.
For an initial example, we’ll create a small pong game. We already wrote a tiny game engine that can be used to create a two player game like so:
def __init__(self):
super().__init__()
self._player_1 = self._create_paddle(x=0, height=self._screen_height//6)
self._player_2 = self._create_paddle(x=self._screen_width-1,
height=self._screen_height//6)
def update(self, pressed_key=None):
if pressed_key == ord('s'):
self._player_1.move(1)
elif pressed_key == ord('w'):
self._player_1.move(-1)
if pressed_key == ord('k'):
self._player_2.move(1)
elif pressed_key == ord('i'):
self._player_2.move(-1)
self._ball.step(self._player_1, self._player_2)
Let’s make it Interactive!
def __init__(self):
super().__init__()
self._player_1 = self._create_paddle(x=0, height=self._screen_height//6)
self._player_2 = self._create_paddle(x=self._screen_width-1,
height=self._screen_height//4)
self._interactive = None
async def setup(self):
"""
Called automatically by our game engine to boot the game. We'll create
an interactive connection here! I've hard-coded a blank project to use.
"""
try:
interactive = await State.connect(authorization="Bearer " + argv[1],
project_version_id=42489,
project_sharecode='rheo1hre')
except Exception as e:
print("Error connecting to interactive", e)
return
self._interactive = interactive
interactive.on('error', lambda e: self.fatal_error(e))
interactive.pump_async()
await self._setup_controls()
async def _setup_controls(self):
"""
All the control setup! Alternately, you can design the controls in
the Interactive Studio, but we'll do them programmatically
for demonstration purposes.
"""
up = Button(
control_id='up',
text='Up',
keycode=keycode.up,
position=[
{'size': 'large', 'width': 5, 'height': 5, 'x': 0, 'y': 0},
],
)
up.on('mousedown', lambda call: self._player_2.move(-1))
down = Button(
control_id='down',
text='Down',
keycode=keycode.down,
position=[
{'size': 'large', 'width': 5, 'height': 5, 'x': 0, 'y': 6},
],
)
down.on('mousedown', lambda call: self._player_2.move(1))
await self._interactive.scenes['default'].create_controls(up, down)
await self._interactive.set_ready()
def update(self, pressed_key=None):
if pressed_key == ord('s'):
self._player_1.move(1)
elif pressed_key == ord('w'):
self._player_1.move(-1)
self._ball.step(self._player_1, self._player_2)