def setup(force: bool = False): global socket_conn if force and socket_conn is not None: socket_conn.disconnect() with open(root.joinpath('running'), 'w') as f: f.write(str(os.getpid())) socket_conn = Connector('https://io.gbsl.website', '__GAME_RUNNER__') socket_conn.on_devices = on_client_devices socket_conn.on_data = on_highscore socket_conn.on_timer = on_timer
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector, Colors from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') score = 0 device.clear_playground() device.configure_playground( width=100, height=60, images='images', color=Colors.ALICEBLUE, image='waterfall' ) device.add_sprite( id='cookie', width=20, height=20, pos_x=10, pos_y=10, image='cookie' ) for rotation in range(360): device.update_sprite(id='cookie', rotate=rotation) device.sleep(0.1) device.disconnect()
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector, AccMsg, KeyMsg, Colors from random import randint from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') WIDTH = 400 HEIGHT = 300 SHIFT_X = 0 SHIFT_Y = 0 QUEUE_SIZE = 30 positions = [(0.0, 0.0)] form = 'round' def update_position(dx: float, dy: float): new_x = positions[-1][0] - dx * 2 new_y = positions[-1][1] - dy * 2 if new_x > WIDTH - 2: new_x = WIDTH - 2 elif new_x < 0: new_x = 0 if new_y > HEIGHT - 2: new_y = HEIGHT - 2 elif new_y < 0: new_y = 0 positions.append((new_x, new_y))
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector, AccMsg, KeyMsg, Colors from random import randint from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') device.clear_playground() device.configure_playground( width=100, height=100, origin_x=50, origin_y=50 ) id = device.add_circle( pos_x=0, pos_y=40, radius=5, color=Colors.RED, direction=[0, -1], speed=1, time_span=4 ) device.sleep(3) speed = 1 device.update_circle(
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector, SpriteClickedMsg, Colors, time_s from examples.server_address import SERVER_ADDRESS from math import cos, sin, pi, radians device = Connector(SERVER_ADDRESS, 'FooBar') RADIUS = 40 device.clear_playground() device.configure_playground(width=100, height=100, shift_x=-50, shift_y=-50, color=Colors.WHITE) device.add_circle(radius=RADIUS, pos_x=0, pos_y=0, border_color="black") for deg in range(0, 360, 6): device.add_line(x1=sin(radians(deg)) * (RADIUS - 3), y1=cos(radians(deg)) * (RADIUS - 3), y2=cos(radians(deg)) * RADIUS, x2=sin(radians(deg)) * RADIUS, line_width=0.3) for deg in range(0, 360, 30): device.add_line(x1=sin(radians(deg)) * (RADIUS - 5), y1=cos(radians(deg)) * (RADIUS - 5), y2=cos(radians(deg)) * RADIUS, x2=sin(radians(deg)) * RADIUS, line_width=1)
import turtle import random import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector from smartphone_connector.types import KeyMsg from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') turtle.home() def on_f1(): for _ in range(4): turtle.forward(100) turtle.left(90) def on_f2(): for _ in range(9): turtle.forward(30) turtle.left(360 / 9) def on_f3(): for _ in range(5): turtle.forward(100) turtle.left(144)
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector, DictX, KeyMsg, Colors from examples.server_address import SERVER_ADDRESS DT = 2 device = Connector(SERVER_ADDRESS, 'FooBar') player = DictX({ 'color': 'yellow', 'form': 'round', 'height': 10, 'width': 10, 'id': 'player1', 'collision_detection': True, 'pos_x': 0, 'pos_y': 0, 'clickable': True }) def on_key(data: KeyMsg): if data.key == 'right': player.pos_x += DT device.update_sprite(id='player1', pos_x=player.pos_x) if data.key == 'left': player.pos_x -= DT device.update_sprite(id='player1', pos_x=player.pos_x) if data.key == 'up':
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector, AccMsg, KeyMsg, Colors from random import randint from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') device.clear_playground() device.configure_playground(width=100, height=100, origin_x=50, origin_y=50) with device.add_objects() as add: for i in range(22): add(form='round', pos_x=50 - i * 5, pos_y=-40, radius=5, color=Colors.next(), movements={ 'cycle': True, 'movements': [ { 'movement': 'absolute', 'to': [-50, -40], 'speed': 10 }, { 'movement': 'absolute', 'to': [50, -40],
from examples.server_address import SERVER_ADDRESS from smartphone_connector import Connector, GridPointerMsg import os import sys from pprint import pprint sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) GRID_W = 20 GRID_H = 20 device = Connector(SERVER_ADDRESS, 'FooBar') # set up white grid device.setup_grid(GRID_H, GRID_W, enumerate=True) def on_pointer(data: GridPointerMsg): # this is the eratosthenes logic... # ignore 1 if data.number == 1: return # ignore when already red since it is not prime if data.color == 'red': return # update all grid cells being a multiplicative multiple of the number # e.g for 2: 4,6,8,10,12 updates = 0 for num in range(data.number * 2, GRID_H * GRID_W + 1, data.number): if device.get_grid_at(cell_number=num) != 'red': updates = updates + 1
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector, SpriteClickedMsg, Colors from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') device.clear_playground() device.configure_playground(width=100, height=50, origin_x=50, origin_y=25) device.add_line(-50, -25, 50, 25, color="purple", line_width=0.2) lw = 0.1 for x in range(-50, 100, 25): device.add_circle(pos_x=x, pos_y=x / 2, radius=2, color='red', border_width=lw, border_color='black', border_style='dashed') device.add_line(x1=-50, y1=x / 2, x2=50, y2=x / 2) device.add_line(x, -25, x, 25) lw = lw + 0.3 device.disconnect()
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from examples.server_address import SERVER_ADDRESS from smartphone_connector import Connector from smartphone_connector.types import AccMsg import matplotlib.pyplot as plt device = Connector(SERVER_ADDRESS, 'FooBar') MAX_SAMPLES = 300 x = [] y = [] plt.show() def on_acc(data: AccMsg): if len(x) > MAX_SAMPLES: x.pop(0) y.pop(0) x.append(data.time_stamp) y.append([data.x, data.y, data.z]) def on_intervall(): plt.clf() plt.plot(x, y) plt.pause(0.005)
import os import sys from pprint import pprint sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') device.set_grid(""" 99999999999999 99999....99999 99999....99999 99999....99999 99999....99999 9............9 9............9 9............9 9............9 99999....99999 99999....99999 99999....99999 99999....99999 99999999999999 """, base_color='red') device.sleep(1) pprint(device.get_grid) device.set_grid_at(0, 0, 'red') pprint(device.get_grid)
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from examples.server_address import SERVER_ADDRESS from smartphone_connector import Connector from math import sin, cos, pi device = Connector(SERVER_ADDRESS, 'FooBar') device.clear_playground() device.configure_playground( width=100, height=100, origin_x=50, origin_y=50, color="#FF3366" ) device.add_line(0, 0, 20, 20, line_width=2) device.add_line(0, 0, 0, 20, line_width=2) device.add_line(-20, 0, 0, 0, color="white") device.add_line(0, 20, -20, 20, color="white") device.add_line(0, 0, 10, 10, color="white") id = device.add_line(0, 0, 10, 0, color="yellow") device.sleep(1) device.update_line(id, x2=0, y2=-10) with device.add_lines() as add: for i in range(30): add(x1=-25, y1=i * 2, x2=-45, y2=i * 2, line_width=1, color="lightblue") device.add_line(0, 0, 10, 0, line_width=2, color='hsla(0, 100%, 50%, 0.5)')
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from examples.server_address import SERVER_ADDRESS from smartphone_connector import Connector device = Connector(SERVER_ADDRESS, 'FooBar') device.clear_playground() device.configure_playground(100, 100, 50, 50, color="white") device.add_circle(pos_x=0, pos_y=0, radius=5, color='red') device.add_square(pos_x=0, pos_y=0, size=10, color='yellow', text='hi', clickable=True) device.add_rectangle(pos_x=-20, pos_y=-40, width=10, height=5, color='orange', text='hi', clickable=True, z_index=999) device.add_circle(pos_x=0, pos_y=0, radius=13, color='green', clickable=True) device.add_square(pos_x=15, pos_y=0, size=10, color='red', anchor=[0.5, 0.5]) device.add_text(text='1', pos_x=-20, pos_y=10) device.add_text(text='1', pos_x=-20, pos_y=10, background_color='unset') device.add_text(text='Hello', pos_x=-20, pos_y=0, border_color='red') device.add_text(text='Hello', pos_x=-20, pos_y=0, border_color='red', rotate=90, anchor=[0, 1]) device.add_text(text='Hello My Friend', pos_x=-20, pos_y=-10, clickable=True) device.add_text(text='Hello There', pos_x=-40, pos_y=-40, font_color='white', font_size=10) device.add_text(text='Hello There', pos_x=-40, pos_y=-40, font_color='white', font_size=10, rotate=-90) device.on_sprite_clicked = lambda x: print(x) device.sleep(10) device.disconnect()
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector, Colors from examples.server_address import SERVER_ADDRESS from math import sin, cos DT = 2 device = Connector(SERVER_ADDRESS, 'FooBar') device.clear_playground() device.configure_playground(width=100, height=100, shift_x=-50, shift_y=-50, color='lightblue') device.add_sprite(id='circler', height=10, width=10, form='round', speed=1, color='yellow') angle = 0 while True: device.update_sprite(id='circler', direction=[cos(angle), sin(angle)], color=Colors.next())
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector import numpy as np from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') grid1 = np.transpose([[1, 2, 3], [1, 2, 3], [1, 2, 3]]) device.set_grid(grid1, base_color='red' ) assert grid1.tolist() == device.get_grid print('ok') grid2 = np.transpose([[1, 2, 3], [1, 2, 3]]) device.set_grid(grid2, base_color='red' ) assert grid2.tolist() == device.get_grid print('ok2') device.disconnect()
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector, SpriteClickedMsg, Colors from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') score = 0 device.clear_playground() device.configure_playground(width=100, height=180, color=Colors.ALICEBLUE, images='images') device.add_sprite(id='score', pos_x=0, pos_y=160, height=20, width=30, text=f'Score: {score}', form='rectangle', color=Colors.LIGHTBLUE) device.add_sprite(id='cookie', pos_x=30, pos_y=40, height=30, width=30, form='rectanle', color=Colors.BROWN, clickable=True, image='firefox')
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector, AccMsg, KeyMsg, Colors from random import randint from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') device.clear_playground() device.configure_playground( width=100, height=100, origin_x=50, origin_y=50 ) id = device.add_circle( pos_x=0, pos_y=0, radius=5, color=Colors.RED ) pos_x = 0 def on_key(data: KeyMsg): global pos_x if (data.key == 'right'): device.move_to(id, [pos_x + 5, 0], time=1, via=[pos_x + 2.5, 30]) pos_x = pos_x + 5
import os import sys from pprint import pprint sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') device.set_grid(""" 99999999999999 99999....99999 99999....99999 99999....99999 99999....99999 9............9 9............9 9............9 9............9 99999....99999 99999....99999 99999....99999 99999....99999 99999999999999 """, base_color='blue', enumerate=True) device.sleep(1) device.set_grid_at(20, 20, 'red') device.sleep(1)
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector, AccMsg, KeyMsg, Colors from random import randint from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') device.clear_playground() id = device.add_circle(pos_x=0, pos_y=0, radius=5, color=Colors.RED) device.sleep(3) speed = 3 device.update_circle(id=id, movements={ 'repeat': 10, 'movements': [{ 'movement': 'relative', 'direction': [0, 1], 'distance': 50, 'speed': speed }, { 'movement': 'relative', 'direction': [0, -1], 'distance': 50, 'speed': speed
from turtle import * import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector, DataFrame, time_s from smartphone_connector.types import KeyMsg from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') Screen().tracer(1, 0) def on_interval(data: DataFrame): if data.acceleration.x < -2: right(2) elif data.acceleration.x > 2: left(2) forward(1) # OR.... # def on_interval(): # print(time_s(), device.latest_data(device_id='__ALL_DEVICES__')) # if device.acceleration.x < -2: # right(2) # elif device.acceleration.x > 2: # left(2) # forward(1) device.subscribe(on_interval, interval=0.01)
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') device.set_color('green') device.sleep(0.5) device.set_color('rgb(255,0,0)') device.sleep(0.5) device.set_color('rgba(255,0,0, 0.5)') device.sleep(0.5) device.set_color(1) device.sleep(0.5) device.set_color('6') device.disconnect()
import os import sys from pprint import pprint sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') device.set_grid([1, 2, 3, 4], base_color='blue') device.sleep(1) device.disconnect()
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') d = device.latest_acceleration() drink = device.select('Was wosch trinkä?', ['Pingusirup', 'Wasser', 'Orangensaft']) food = device.select('Was wosch ässä?', ['Fischstäbli', 'Cordonblö', 'Zürigschnätzlets']) device.print(f'Bestellung: {food} mit {drink}', display_time=3) device.sleep(0.3) device.disconnect()
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') while True: device.set_color('yellow') device.sleep(0.5) device.set_color('black') device.sleep(0.5)
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') dimension = 1 device.set_grid([['white']]) while True: for i in range(dimension): device.set_grid_at(i, dimension, 'white' if dimension % 2 == 0 else 'black') device.set_grid_at(dimension, i, 'black' if dimension % 2 == 0 else 'white') device.sleep(0.05) dimension = dimension + 1 if dimension > 50: device.set_grid([['white']]) dimension = 1 device.sleep(1)
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector, Colors from typing import Union, Literal from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') device.clear_playground() device.configure_playground( width=180, height=180, origin_x=10, origin_y=10, color=Colors.BISQUE, ) def color(i: int, j: int) -> Union[Literal['white'], Literal['black']]: if i % 2 == 0: if j % 2 == 0: return 'black' else: return 'white' else: if j % 2 == 0: return 'white' else: return 'black'
import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from smartphone_connector import Connector, DictX, KeyMsg, AccMsg, random_color, BorderOverlapMsg, SpriteCollisionMsg, Colors from random import randint from examples.server_address import SERVER_ADDRESS device = Connector(SERVER_ADDRESS, 'FooBar') WIDTH = 100 HEIGHT = 100 SHIFT_X = 0 SHIFT_Y = 0 positions = [(0.0 + SHIFT_X, 0.0 + SHIFT_Y)] form = 'round' def on_key(data: KeyMsg): global form x = positions[-1][0] y = positions[-1][1] if data.key == 'right': positions.append((x + 5, y)) if data.key == 'left': positions.append((x - 5, y)) if data.key == 'up': positions.append((x, y + 5)) if data.key == 'down': positions.append((x, y - 5)) if data.key == 'home':