Skip to content

Commit 14e40e9

Browse files
author
XenGi
committed
Merge branch 'feature/controller' into develop
2 parents 2fe5d02 + 73e50a7 commit 14e40e9

11 files changed

+383
-59
lines changed

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()

emu.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33

44
"""
55
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.
610
"""
711

812
__author__ = "Ricardo Band"
913
__copyright__ = "Copyright 2013, Ricardo Band"
1014
__credits__ = ["Ricardo Band"]
1115
__license__ = "MIT"
12-
__version__ = "0.0.1"
16+
__version__ = "0.1.0"
1317
__maintainer__ = "Ricardo Band"
1418
__email__ = "[email protected]"
1519
__status__ = "Development"
@@ -21,7 +25,14 @@
2125

2226

2327
class Emu(object):
28+
"""
29+
The Emulator is a simple pygame game.
30+
"""
2431
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+
"""
2536
self.width = width
2637
self.height = height
2738
pygame.init()
@@ -37,26 +48,39 @@ def __init__(self, width=40, height=40, ip='127.0.0.1', port=1337):
3748
self.sock.bind((ip, port))
3849

3950
def recv_data(self):
51+
"""
52+
Grab the next frame and put it on the matrix.
53+
"""
4054
data, addr = self.sock.recvfrom(self.width * self.height * 3 + 4)
4155
self.matrix = map(ord, data.strip())[:-4]
4256

4357
def update(self):
58+
"""
59+
Generate the output from the matrix.
60+
"""
4461
pixels = len(self.matrix)
4562
for x in range(self.width):
4663
for y in range(self.height):
4764
pixel = y * self.width * 3 + x * 3
48-
# sometimes the matrix is not as big as it should
65+
#TODO: sometimes the matrix is not as big as it should
4966
if pixel < pixels:
5067
pygame.draw.circle(self.screen, (self.matrix[pixel],
5168
self.matrix[pixel + 1],
5269
self.matrix[pixel + 2]),
5370
(x * 10 + 5, y * 10 + 5), 5, 0)
5471

5572
def render(self):
73+
"""
74+
Output the current screen.
75+
"""
5676
pygame.display.update()
5777
pygame.display.flip()
5878

5979
def gameloop(self):
80+
"""
81+
Loop through all the necessary stuff and end execution when Ctrl+C
82+
was hit.
83+
"""
6084
try:
6185
while True:
6286
for event in pygame.event.get():

example.py renamed to game_example.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
# -*- coding: utf-8 -*-
33

44
"""
5-
pymlgame example game
5+
pymlgame - example game
6+
=======================
7+
8+
This example shows how a simple pymlgame could be written. You also need a
9+
connected controller to actually see something happening. You can use the
10+
controller example for this.
611
"""
712

813
__author__ = "Ricardo Band"
914
__copyright__ = "Copyright 2013, Ricardo Band"
1015
__credits__ = ["Ricardo Band"]
1116
__license__ = "MIT"
12-
__version__ = "0.0.1"
17+
__version__ = "0.1.0"
1318
__maintainer__ = "Ricardo Band"
1419
__email__ = "[email protected]"
1520
__status__ = "Development"
@@ -19,13 +24,18 @@
1924

2025
class Game(object):
2126
"""
22-
The main game class that holds the gameloop
27+
The main game class that holds the gameloop.
2328
"""
2429
def __init__(self, host, port, width, height):
2530
"""
26-
Create a screen and define some game specific things
31+
Create a screen and define some game specific things.
2732
"""
28-
self.screen = pymlgame.Screen(host, port, width, height)
33+
self.host = host
34+
self.port = port
35+
self.width = width
36+
self.height = height
37+
self.screen = pymlgame.Screen(self.host, self.port,
38+
self.width, self.height)
2939
self.clock = pymlgame.Clock()
3040
self.running = True
3141
self.colors = [pymlgame.WHITE,
@@ -47,9 +57,11 @@ def __init__(self, host, port, width, height):
4757
self.filled = pymlgame.Surface(int(self.screen.width / 2) - 2,
4858
int(self.screen.height / 2) - 2)
4959

60+
self.ctlr = pymlgame.Controller(self.host, self.port + 1)
61+
5062
def update(self):
5163
"""
52-
Update the screens contents in every loop
64+
Update the screens contents in every loop.
5365
"""
5466
# this is not really neccesary because the surface is black after
5567
# initializing
@@ -86,7 +98,7 @@ def update(self):
8698

8799
def render(self):
88100
"""
89-
Send the current screen content to Mate Light
101+
Send the current screen content to Mate Light.
90102
"""
91103
self.screen.reset()
92104
self.screen.blit(self.corners)
@@ -101,13 +113,23 @@ def render(self):
101113

102114
def handle_events(self):
103115
"""
104-
Loop through all events
116+
Loop through all events.
105117
"""
106-
self.colors.append(self.colors.pop(0))
118+
for event in self.ctlr.get_events():
119+
if event.type == pymlgame.NEWCTLR:
120+
print('new ctlr with uid:', event.uid)
121+
elif event.type == pymlgame.KEYDOWN:
122+
print('key', event.button, 'down on', event.uid)
123+
self.colors.append(self.colors.pop(0))
124+
elif event.type == pymlgame.KEYUP:
125+
print('key', event.button, 'up on', event.uid)
126+
self.colors.append(self.colors.pop(0))
127+
elif event.type == pymlgame.PING:
128+
print('ping from', event.uid)
107129

108130
def gameloop(self):
109131
"""
110-
A game loop that circles through the methods
132+
A game loop that circles through the methods.
111133
"""
112134
try:
113135
while True:

pymlgame/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
__status__ = "Development"
1111

1212
from pymlgame.locals import *
13-
from pymlgame.events import EventListener
1413
from pymlgame.screen import Screen
1514
from pymlgame.clock import Clock
1615
from pymlgame.surface import Surface
17-
18-
events = EventListener()
16+
from pymlgame.controller import Controller

0 commit comments

Comments
 (0)