Skip to content

Commit 92524a4

Browse files
author
XenGi
committed
oh look some files in an unknown state
1 parent 0610edb commit 92524a4

File tree

7 files changed

+226
-0
lines changed

7 files changed

+226
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ nosetests.xml
3434
.mr.developer.cfg
3535
.project
3636
.pydevproject
37+
38+
# PyCharm files
39+
.idea

example.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python2.7
2+
# -*- coding: utf-8 -*-
3+
4+
import pymlgame
5+
from pymlgame.locals import *
6+
7+
8+
class Game(object):
9+
"""
10+
The main game class that holds the gameloop
11+
"""
12+
def __init__(self):
13+
"""
14+
create a screen and define some game specific things
15+
"""
16+
self.screen = pymlgame.Screen()
17+
self.running = True
18+
self.colors = [GREEN, WHITE, RED, YELLOW, BLUE, CYAN]
19+
20+
def update(self):
21+
"""
22+
update the screens contents in every loop
23+
"""
24+
self.screen.fill(BLACK)
25+
self.screen.draw_dot((0, 0), self.colors[0])
26+
self.screen.draw_line((3, 0), (20, 3), self.colors[1])
27+
self.screen.draw_rect((1, 5), (10, 8), self.colors[2], self.colors[3])
28+
self.screen.draw_circle((20, 5), (8, 8), self.colors[4], self.colors[5])
29+
30+
def render(self):
31+
"""
32+
send the current screen content to Mate Light
33+
"""
34+
self.screen.update()
35+
36+
def handle_events(self):
37+
"""
38+
Loop through all events and react to them
39+
"""
40+
for event in pymlgame.events.get_events():
41+
if event.type == KEYUP:
42+
if event.key == K_Arrow_Up or event.key == K_Arrow_Right:
43+
self.colors.append(self.colors.pop(0))
44+
elif event.key == K_Arrow_Down or event.key == K_Arrow_Left:
45+
self.colors.insert(0, self.colors.pop())
46+
47+
def gameloop(self):
48+
"""
49+
A game loop that circles through the methods
50+
"""
51+
while self.running:
52+
self.handle_events()
53+
self.update()
54+
self.render()
55+
56+
57+
if __name__ == '__main__':
58+
GAME = Game()
59+
GAME.gameloop()

pymlgame/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import pymlgame.events

pymlgame/clock.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
3+
__author__ = 'xengi'
4+
5+
import time
6+
7+
8+
class Clock(object):
9+
"""
10+
Measure the time to adjust drawing rates.
11+
"""
12+
def __init__(self):
13+
"""
14+
Get a fresh Clock.
15+
"""
16+
self.__last_tick = False
17+
18+
def tick(self, fps):
19+
"""
20+
Let the Clock tick x times per second.
21+
"""
22+
tick = time.time()
23+
if self.__last_tick:
24+
time.sleep(float(1)/fps)
25+
self.__last_tick = tick

pymlgame/events.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
3+
__author__ = 'xengi'
4+
5+
6+
def get_events():
7+
"""
8+
Return all events that happened since last time we checked
9+
"""
10+
return []

pymlgame/locals.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# event types
4+
KEYUP = 0
5+
KEYDOWN = 1
6+
7+
# keys
8+
K_Arrow_Up = 0
9+
K_Arrow_Down = 1
10+
K_Arrow_Left = 2
11+
K_Arrow_Right = 3
12+
K_Return = 4
13+
K_Escape = 5
14+
15+
# predefined colors
16+
RED = (255, 0, 0)
17+
MAGENTA = (255, 0, 255)
18+
BLUE = (0, 0, 255)
19+
CYAN = (0, 255, 255)
20+
GREEN = (0, 255, 0)
21+
YELLOW = (255, 255, 0)
22+
DARKRED = (127, 0, 0)
23+
DARKMAGENTA = (127, 0, 127)
24+
DARKBLUE = (0, 0, 127)
25+
DARKCYAN = (0, 127, 127)
26+
DARKGREEN = (0, 127, 0)
27+
DARKYELLOW = (127, 127, 0)
28+
BLACK = (0, 0, 0)
29+
GREY9 = (25, 25, 25)
30+
GREY8 = (51, 51, 51)
31+
GREY7 = (76, 76, 76)
32+
GREY6 = (102, 102, 102)
33+
GREY5 = (127, 127, 127)
34+
GREY4 = (153, 153, 153)
35+
GREY3 = (178, 178, 178)
36+
GREY2 = (204, 204, 204)
37+
GREY1 = (229, 229, 229)
38+
WHITE = (255, 255, 255)
39+
40+
41+

pymlgame/screen.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import socket
4+
5+
from pymlgame.locals import *
6+
7+
8+
class Screen(object):
9+
"""
10+
Represents the Mate Light screen and has all the drawing methods.
11+
"""
12+
def __init__(self, width=40, height=16):
13+
"""
14+
Create a screen with default size and fill it with black pixels.
15+
"""
16+
self.width = width
17+
self.height = height
18+
self.__matrix = []
19+
self.fill(BLACK)
20+
21+
def fill(self, color):
22+
"""
23+
Fill the whole screen with the given color.
24+
"""
25+
self.__matrix = []
26+
for x in range(self.width):
27+
column = []
28+
for y in range(self.height):
29+
column.append(color)
30+
self.__matrix.append(column)
31+
32+
def draw_dot(self, pos, color):
33+
"""
34+
Draw one single dot with the given color on the screen.
35+
"""
36+
if 0 <= pos[0] < self.width and 0 <= pos[1] < self.height:
37+
self.__matrix[pos[0]][pos[1]] = color
38+
else:
39+
print('{:d},{:d} is outside the screen'.format(pos[0], pos[1]))
40+
41+
def draw_line(self, start, end, color):
42+
"""
43+
Draw a line with the given color on the screen.
44+
"""
45+
pass
46+
47+
def draw_rect(self, pos, size, color, fillcolor=None):
48+
"""
49+
Draw a rectangle with the given color on the screen and optionally fill it with fillcolor.
50+
"""
51+
# draw top and botton line
52+
for x in range(size[0]):
53+
self.draw_dot((pos[0] + x, pos[1]), color)
54+
self.draw_dot((pos[0] + x, pos[1] + size[1]), color)
55+
# draw left and right side
56+
for y in range(size[1]):
57+
self.draw_dot((pos[0], pos[1] + y), color)
58+
self.draw_dot((pos[0] + size[0], pos[1] + y), color)
59+
# draw filled rect
60+
if fillcolor:
61+
for x in range(size[0] - 2):
62+
for y in range(size[1] - 2):
63+
self.draw_dot((pos[0] + 1 + x, pos[1] + 1 + y), fillcolor)
64+
65+
def draw_circle(self, pos, size, color, fillcolor):
66+
"""
67+
Draw a circle with the given color on the screen and optionally fill it with fillcolor.
68+
"""
69+
pass
70+
71+
def update(self):
72+
"""
73+
Sends the current screen contents to Mate Light
74+
"""
75+
display_data = []
76+
for y in range(self.width):
77+
for x in range(self.heigth):
78+
for color in self.__matrix[x][y]:
79+
display_data.append(color)
80+
81+
checksum = bytearray([0, 0, 0, 0])
82+
data_as_bytes = bytearray(display_data)
83+
data = data_as_bytes + checksum
84+
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
85+
sock.sendto(data, (self.ip, self.port))

0 commit comments

Comments
 (0)