Skip to content

Commit

Permalink
Merge branch 'release/v0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
XenGi committed Dec 29, 2014
2 parents 1cc0b95 + cc20d8f commit 45d83e2
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 83 deletions.
35 changes: 20 additions & 15 deletions controller_example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2.7
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

"""
Expand All @@ -18,6 +18,8 @@

import pygame

DEBUG = True

# define some constants
E_UID = pygame.USEREVENT + 1
E_DOWNLOAD = pygame.USEREVENT + 2
Expand All @@ -29,7 +31,7 @@ class ReceiverThread(Thread):
"""
This thread will listen on a UDP port for packets from the game.
"""
def __init__(self, host='0.0.0.0', port=11337):
def __init__(self, host='0.0.0.0', port=1338):
"""
Creates the socket and binds it to the given host and port.
"""
Expand All @@ -48,24 +50,26 @@ def run(self):
logging.warning('example warning')
logging.error('example error')
logging.critical('example critical')
#print(datetime.now(), '<<< {}'.format(data))
if data.startswith('/uid/'):
#print(datetime.now(), '### uid', data[5:], 'received')
e = pygame.event.Event(E_UID, {'uid': int(data[5:])})
pygame.event.post(e)
if DEBUG: logging.info('uid received: {}'.format(data[5:]))
elif data.startswith('/download/'):
e = pygame.event.Event(E_DOWNLOAD, {'url': str(data[10:])})
pygame.event.post(e)
if DEBUG: logging.info('download of {} triggered'.format(data[10:]))
elif data.startswith('/play/'):
e = pygame.event.Event(E_PLAY, {'filename': str(data[6:])})
pygame.event.post(e)
if DEBUG: logging.info('playback of {} triggered'.format(data[6:]))
elif data.startswith('/rumble/'):
e = pygame.event.Event(E_RUMBLE, {'duration': float(data[8:].replace(',', '.'))})
e = pygame.event.Event(E_RUMBLE, {'duration': int(data[8:])})
pygame.event.post(e)
if DEBUG: logging.info('request rumble for {}ms'.format(data[8:]))


class Controller(object):
def __init__(self, game_host='127.0.0.1', game_port=1338, host='0.0.0.0', port=11337):
def __init__(self, game_host='127.0.0.1', game_port=1338, host='0.0.0.0', port=1338):
self.game_host = game_host # Host of Mate Light
self.game_port = game_port # Port of Mate Light
self.host = host # Host of ReceiverThread
Expand Down Expand Up @@ -98,41 +102,47 @@ def __init__(self, game_host='127.0.0.1', game_port=1338, host='0.0.0.0', port=1
self.rumble_active = False
self.uid = None

self._receiver = ReceiverThread(host, port)
self._receiver = ReceiverThread(self.host, self.port)
self._receiver.setDaemon(True)
self._receiver.start()

def ping(self):
if self.uid:
if DEBUG: logging.info('sending ping')
msg = '/controller/{}/ping/{}'.format(self.uid, self._receiver.port)
self.sock.sendto(msg.encode('utf-8'), (self.game_host, self.game_port))
#print(datetime.now(), '>>>', msg)

def send_keys(self):
# alternative states creation: [1 if k else 0 for k in self.keys]
states = '/controller/{}/states/{}'.format(self.uid, ''.join([str(k) for k in self.keys]))
if DEBUG: logging.info('sending states {}'.format(''.join([str(k) for k in self.keys])))
self.sock.sendto(states.encode('utf-8'), (self.game_host, self.game_port))
#print(datetime.now(), '>>>' + states)
self.timeout = time.time()

def send_message(self, msg):
if DEBUG: logging.info('sending of messages not yet implemented')
pass

def disconnect(self):
if DEBUG: logging.info('disconnecting from game')
msg = '/controller/{}/kthxbye'.format(self.uid)
self.sock.sendto(msg.encode('utf-8'), (self.game_host, self.game_port))

def connect(self):
if DEBUG: logging.info('connecting to game')
msg = '/controller/new/{}'.format(self.port)
self.sock.sendto(msg.encode('utf-8'), (self.game_host, self.game_port))

def rumble(self, duration):
if DEBUG: logging.info('rumble not yet implemented')
pass

def download_sound(self, url):
if DEBUG: logging.info('downloading of media files not yet implemented')
pass

def play_sound(self, filename):
if DEBUG: logging.info('playing media files not yet implemented')
pass

def handle_inputs(self):
Expand All @@ -147,11 +157,9 @@ def handle_inputs(self):
elif event.type == pygame.MOUSEBUTTONUP:
pygame.event.post(pygame.event.Event(pygame.QUIT))
elif event.type == E_UID:
#print(datetime.now(), '### UID event received', event.uid)
self.uid = event.uid

if self.uid is not None:
#print(datetime.now(), '### UID set. checking other events')
if event.type == E_DOWNLOAD:
self.download_sound(event.url)
elif event.type == E_PLAY:
Expand All @@ -162,22 +170,19 @@ def handle_inputs(self):
try:
button = self.mapping[event.key]
if event.type == pygame.KEYDOWN:
#print('{} | {}'.format(event.key, button))
self.keys[button] = 1
elif event.type == pygame.KEYUP:
#print('{} | {}'.format(event.key, button))
self.keys[button] = 0
self.send_keys()
except KeyError:
break
else:
#print(datetime.now(), '### UID not set. connecting to game.')
self.connect()
time.sleep(1)


if __name__ == '__main__':
ctlr = Controller('127.0.0.1', 1338, '0.0.0.0', 11337)
ctlr = Controller('127.0.0.1', 1338, '0.0.0.0', 1338)
try:
while True:
ctlr.handle_inputs()
Expand Down
31 changes: 23 additions & 8 deletions emulator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2.7
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

"""
Expand All @@ -7,13 +7,27 @@
This little program emulates the awesome Mate Light, just in case you're not on c-base space station but want to send
something to it.
Usage:
emulator.py [-w=<px>] [-h=<px>] [--host=<ip>] [--port=<num>] [--dot=<size>]
emulator.py --help
emulator.py --version
Options:
--help Show this screen.
--version Show version.
-w=<px> Width in pixels [default: 40].
-h=<px> Height in pixels [default: 16].
--host=<host> Bind to IP address [default: 127.0.0.1].
--port=<port> Bind to Port [default: 1337].
--dot=<px> Size of dots in pixels [default: 10].
"""

__author__ = 'Ricardo Band'
__copyright__ = 'Copyright 2014, Ricardo Band'
__credits__ = ['Ricardo Band']
__license__ = 'MIT'
__version__ = '0.2.0'
__version__ = '0.3.0'
__maintainer__ = 'Ricardo Band'
__email__ = '[email protected]'
__status__ = 'Development'
Expand All @@ -22,27 +36,27 @@
import socket

import pygame
from docopt import docopt


class Emu(object):
"""
The Emulator is a simple pygame game.
"""
def __init__(self, width=40, height=16, ip='127.0.0.1', port=1337):
def __init__(self, width=40, height=16, ip='127.0.0.1', port=1337, dotsize=10):
"""
Creates a screen with the given size, generates the matrix for the Mate bottles and binds the socket for
incoming frames.
"""
self.width = width
self.height = height
self.dotsize = dotsize
pygame.init()
# one mate bottle is 10x10px
self.screen = pygame.display.set_mode([self.width * 10, self.height * 10])
self.screen = pygame.display.set_mode([self.width * self.dotsize, self.height * self.dotsize])
pygame.display.set_caption("Mate Light Emu")
self.clock = pygame.time.Clock()
self.matrix = []
for c in range(self.width * self.height * 3):
# fill matrix with black color
self.matrix.append(0)

self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Expand Down Expand Up @@ -71,7 +85,7 @@ def update(self):
if pixel < pixels:
pygame.draw.circle(self.screen,
(self.matrix[pixel], self.matrix[pixel + 1], self.matrix[pixel + 2]),
(x * 10 + 5, y * 10 + 5), 5, 0)
(x * self.dotsize + self.dotsize / 2, y * self.dotsize + self.dotsize / 2), self.dotsize / 2, 0)

def render(self):
"""
Expand Down Expand Up @@ -101,5 +115,6 @@ def gameloop(self):


if __name__ == '__main__':
EMU = Emu(40, 16, '127.0.0.1', 1337)
ARGS = docopt(__doc__, version=__version__)
EMU = Emu(int(ARGS['-w']), int(ARGS['-h']), ARGS['--host'], int(ARGS['--port']), int(ARGS['--dot']))
EMU.gameloop()
4 changes: 2 additions & 2 deletions game_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
__copyright__ = 'Copyright 2014, Ricardo Band'
__credits__ = ['Ricardo Band']
__license__ = 'MIT'
__version__ = '0.2.0'
__version__ = '0.3.0'
__maintainer__ = 'Ricardo Band'
__email__ = '[email protected]'
__status__ = 'Development'

from datetime import datetime

import pymlgame
from pymlgame.locals import *
from pymlgame.locals import WHITE, BLUE, GREEN, CYAN, MAGENTA, YELLOW, RED, BLACK, E_NEWCTLR, E_DISCONNECT, E_KEYDOWN, E_KEYUP, E_PING
from pymlgame.screen import Screen
from pymlgame.clock import Clock
from pymlgame.surface import Surface
Expand Down
Binary file added icons_android.xcf
Binary file not shown.
36 changes: 20 additions & 16 deletions pymlgame/__init__.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
# -*- coding: utf-8 -*-

"""
PyMLGame
"""

__author__ = 'Ricardo Band'
__copyright__ = 'Copyright 2014, Ricardo Band'
__credits__ = ['Ricardo Band']
__license__ = 'MIT'
__version__ = '0.2.0'
__version__ = '0.3.0'
__maintainer__ = 'Ricardo Band'
__email__ = '[email protected]'
__status__ = 'Development'

# from pymlgame.locals import *
# from pymlgame.screen import Screen
# from pymlgame.clock import Clock
# from pymlgame.surface import Surface
from pymlgame.locals import *
from pymlgame.screen import Screen
from pymlgame.clock import Clock
from pymlgame.surface import Surface
from pymlgame.controller import Controller

_ctlr = Controller()
CONTROLLER = Controller()


def init(host='0.0.0.0', port=1338):
"""
Initialize pymlgame. This creates a controller thread that listens for game controllers and events.
Initialize PyMLGame. This creates a controller thread that listens for game controllers and events.
"""
_ctlr.host = host
_ctlr.port = port
_ctlr.setDaemon(True) # because it's a deamon it will exit together with the main thread
_ctlr.start()
CONTROLLER.host = host
CONTROLLER.port = port
CONTROLLER.setDaemon(True) # because it's a deamon it will exit together with the main thread
CONTROLLER.start()


def get_events(maximum=10):
Expand All @@ -35,10 +39,10 @@ def get_events(maximum=10):
events = []
for ev in range(0, maximum):
try:
if _ctlr.queue.empty():
if CONTROLLER.queue.empty():
break
else:
events.append(_ctlr.queue.get_nowait())
events.append(CONTROLLER.queue.get_nowait())
except NameError:
not_initialized()
events = False
Expand All @@ -50,11 +54,11 @@ def get_event():
"""
Get the next event in the queue if there is one.
"""
if not _ctlr.queue.empty():
return _ctlr.queue.get_nowait()
if not CONTROLLER.queue.empty():
return CONTROLLER.queue.get_nowait()
else:
return False


def not_initialized():
print('pymlgame is not initialized correctly. Use pymlgame.init() first.')
print('PyMLGame is not initialized correctly. Use pymlgame.init() first.')
6 changes: 3 additions & 3 deletions pymlgame/clock.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

"""
pymlgame - Clock
PyMLGame - Clock
"""

import time
Expand All @@ -21,5 +21,5 @@ def tick(self):
"""
Let the Clock tick.
"""
#TODO: I think this is not the correct way. Should think about this again..
time.sleep(1.0/self.fps)
#TODO: I think this is not the correct way. I should think about this again..
time.sleep(1.0/self.fps)
Loading

0 comments on commit 45d83e2

Please sign in to comment.