Skip to content

Commit 05246e1

Browse files
author
XenGi
committed
Merge branch 'release/0.1.0'
2 parents 92524a4 + efd3aac commit 05246e1

16 files changed

+1088
-141
lines changed

README.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
1-
pymlgame
2-
========
1+
# pymlgame
32

4-
pymlgame is an abstraction layer to easily build games for Mate Light inspired by pygame.
3+
pymlgame is an abstraction layer to easily build games for Mate Light inspired
4+
by pygame.
5+
6+
You need 3 parts to actually have a running game on Mate Light.
7+
8+
## The game
9+
10+
You can build a game using the pymlgame library. If you know the pygame
11+
library this should meen nothing new to you. Use the game_example.py to find
12+
out how to to so.
13+
14+
## A controller
15+
16+
If you want players, your game needs a controller to have some inputs. You
17+
can use anything as a controller that can trigger the JSONRPC calls in your
18+
game. As an example you can use the controller_example.py and adapt it to
19+
your controller.
20+
21+
## Mate Light
22+
23+
Last but not least you need Mate Light as your display. If you are not at
24+
c-base space station but still want to tinker around with this you can use
25+
the emulator.py to do so.
26+
27+
---
28+
29+
Have fun while playing pymlgames on Mate Light! :D

controller_example.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env python2.7
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
pymlgame - controller example
6+
=============================
7+
8+
This example shows how you can use a controller connected to a laptop or any
9+
other machine capable of pygame to connect to a pymlgame instance.
10+
"""
11+
12+
__author__ = 'Ricardo Band'
13+
__copyright__ = 'Copyright 2013, Ricardo Band'
14+
__credits__ = ['Ricardo Band']
15+
__license__ = 'MIT'
16+
__version__ = '0.1.0'
17+
__maintainer__ = 'Ricardo Band'
18+
__email__ = '[email protected]'
19+
__status__ = 'Development'
20+
21+
import sys
22+
23+
import pygame
24+
import jsonrpclib
25+
26+
27+
class Controller(object):
28+
def __init__(self, host, port):
29+
self.host = host
30+
self.port = port
31+
pygame.init()
32+
self.joysticks = [[pygame.joystick.Joystick(j), None, None]
33+
for j in range(pygame.joystick.get_count())]
34+
self.screen = pygame.display.set_mode((100, 10))
35+
pygame.display.set_caption("pymlgame_ctlr")
36+
self.clock = pygame.time.Clock()
37+
self.server = jsonrpclib.Server('http://' + self.host + ':' +
38+
str(self.port))
39+
for joy in self.joysticks:
40+
joy[0].init()
41+
joy[1] = self.server.init()
42+
if joy[0].get_name() == 'Xbox 360 Wireless Receiver':
43+
joy[2] = {0: 'A',
44+
1: 'B',
45+
2: 'X',
46+
3: 'Y',
47+
4: 'L1',
48+
5: 'R1',
49+
6: 'Select',
50+
7: 'Start',
51+
11: 'Left',
52+
12: 'Right',
53+
13: 'Up',
54+
14: 'Down'}
55+
56+
def handle_events(self):
57+
for event in pygame.event.get():
58+
if event.type == pygame.QUIT:
59+
pygame.joystick.quit()
60+
pygame.quit()
61+
sys.exit()
62+
if event.type == pygame.KEYDOWN:
63+
if event.key == pygame.K_ESCAPE:
64+
pygame.event.post(pygame.event.Event(pygame.QUIT))
65+
if event.type == pygame.JOYBUTTONDOWN:
66+
print('joy', self.joysticks[event.joy][0].get_name(),
67+
'(uid:', self.joysticks[event.joy][1], ')button pressed:',
68+
event.button)
69+
self.server.trigger_button(self.joysticks[event.joy][1],
70+
'KeyDown',
71+
self.joysticks[event.joy][2][event.button])
72+
if event.type == pygame.JOYBUTTONUP:
73+
print('joy', self.joysticks[event.joy][0].get_name(),
74+
'(uid:', self.joysticks[event.joy][1], ')button released:',
75+
event.button)
76+
self.server.trigger_button(self.joysticks[event.joy][1],
77+
'KeyUp',
78+
self.joysticks[event.joy][2][event.button])
79+
80+
def update(self):
81+
pass
82+
83+
def render(self):
84+
pygame.display.update()
85+
pygame.display.flip()
86+
87+
def gameloop(self):
88+
try:
89+
while True:
90+
self.handle_events()
91+
self.update()
92+
self.render()
93+
except KeyboardInterrupt:
94+
pass
95+
96+
97+
if __name__ == '__main__':
98+
CTLR = Controller('127.0.0.1', 1338)
99+
CTLR.gameloop()

emulator.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env python2.7
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
pymlgame - Mate Light emulator
6+
==============================
7+
8+
This little program emulates the awesome Mate Light, just in case you're not
9+
at c-base but want to code something for it.
10+
"""
11+
12+
__author__ = 'Ricardo Band'
13+
__copyright__ = 'Copyright 2013, Ricardo Band'
14+
__credits__ = ['Ricardo Band']
15+
__license__ = 'MIT'
16+
__version__ = '0.1.0'
17+
__maintainer__ = 'Ricardo Band'
18+
__email__ = '[email protected]'
19+
__status__ = 'Development'
20+
21+
import sys
22+
import socket
23+
24+
import pygame
25+
26+
27+
class Emu(object):
28+
"""
29+
The Emulator is a simple pygame game.
30+
"""
31+
def __init__(self, width=40, height=40, ip='127.0.0.1', port=1337):
32+
"""
33+
Creates a screen with the given size, generates the matrix for the
34+
Mate bottles and binds the socket for incoming frames.
35+
"""
36+
self.width = width
37+
self.height = height
38+
pygame.init()
39+
self.screen = pygame.display.set_mode([self.width * 10,
40+
self.height * 10])
41+
pygame.display.set_caption("Mate Light Emu")
42+
self.clock = pygame.time.Clock()
43+
self.matrix = []
44+
for c in range(self.width * self.height * 3):
45+
self.matrix.append(0)
46+
47+
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
48+
self.sock.bind((ip, port))
49+
50+
def recv_data(self):
51+
"""
52+
Grab the next frame and put it on the matrix.
53+
"""
54+
data, addr = self.sock.recvfrom(self.width * self.height * 3 + 4)
55+
self.matrix = map(ord, data.strip())[:-4]
56+
57+
def update(self):
58+
"""
59+
Generate the output from the matrix.
60+
"""
61+
pixels = len(self.matrix)
62+
for x in range(self.width):
63+
for y in range(self.height):
64+
pixel = y * self.width * 3 + x * 3
65+
#TODO: sometimes the matrix is not as big as it should
66+
if pixel < pixels:
67+
pygame.draw.circle(self.screen, (self.matrix[pixel],
68+
self.matrix[pixel + 1],
69+
self.matrix[pixel + 2]),
70+
(x * 10 + 5, y * 10 + 5), 5, 0)
71+
72+
def render(self):
73+
"""
74+
Output the current screen.
75+
"""
76+
pygame.display.update()
77+
pygame.display.flip()
78+
79+
def gameloop(self):
80+
"""
81+
Loop through all the necessary stuff and end execution when Ctrl+C
82+
was hit.
83+
"""
84+
try:
85+
while True:
86+
for event in pygame.event.get():
87+
if event.type == pygame.QUIT:
88+
sys.exit()
89+
if event.type == pygame.KEYDOWN:
90+
if event.key == pygame.K_ESCAPE:
91+
pygame.event.post(pygame.event.Event(pygame.QUIT))
92+
93+
self.recv_data()
94+
self.update()
95+
self.render()
96+
except KeyboardInterrupt:
97+
pass
98+
99+
100+
if __name__ == '__main__':
101+
EMU = Emu()
102+
EMU.gameloop()

example.py

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)