-
Notifications
You must be signed in to change notification settings - Fork 2
/
game_example.py
executable file
·150 lines (129 loc) · 5.64 KB
/
game_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
pymlgame - example game
=======================
This example shows how a simple pymlgame could be written. You also need a connected controller to actually see
something happening. You can use the controller example for this.
"""
__author__ = 'Ricardo Band'
__copyright__ = 'Ricardo Band'
__credits__ = ['Ricardo Band']
__license__ = 'MIT'
__version__ = '0.3.1'
__maintainer__ = 'Ricardo Band'
__email__ = '[email protected]'
__status__ = 'Development'
from datetime import datetime
import pymlgame
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
class Game(object):
"""
The main game class that holds the gameloop.
"""
def __init__(self, host, port, width, height):
"""
Create a screen and define some game specific things.
"""
self.host = host
self.port = port
self.width = width
self.height = height
self.players = {}
pymlgame.init()
self.screen = Screen(self.host, self.port,
self.width, self.height)
self.clock = Clock(15)
self.running = True
self.colors = [WHITE, BLUE, GREEN, CYAN, MAGENTA, YELLOW, RED]
# surfaces
self.corners = Surface(self.screen.width, self.screen.height)
self.lines = Surface(int(self.screen.width / 2) - 2,
int(self.screen.height / 2) - 2)
self.rects = Surface(int(self.screen.width / 2) - 2,
int(self.screen.height / 2) - 2)
self.circle = Surface(int(self.screen.width / 2) - 2,
int(self.screen.height / 2) - 2)
self.filled = Surface(int(self.screen.width / 2) - 2,
int(self.screen.height / 2) - 2)
def update(self):
"""
Update the screens contents in every loop.
"""
# this is not really neccesary because the surface is black after initializing
self.corners.fill(BLACK)
self.corners.draw_dot((0, 0), self.colors[0])
self.corners.draw_dot((self.screen.width - 1, 0), self.colors[0])
self.corners.draw_dot((self.screen.width - 1, self.screen.height - 1),
self.colors[0])
self.corners.draw_dot((0, self.screen.height - 1), self.colors[0])
self.lines.fill(BLACK)
self.lines.draw_line((1, 0), (self.lines.width - 1, 0), self.colors[1])
self.lines.draw_line((0, 1), (0, self.lines.height - 1), self.colors[3])
self.lines.draw_line((0, 0), (self.lines.width - 1,
self.lines.height - 1), self.colors[2])
self.rects.fill(BLACK)
self.rects.draw_rect((0, 0), (int(self.rects.width / 2) - 1,
self.rects.height),
self.colors[2], self.colors[3])
self.rects.draw_rect((int(self.rects.width / 2) + 1, 0),
(int(self.rects.width / 2) - 1,
self.rects.height),
self.colors[3], self.colors[2])
self.circle.fill(BLACK)
radius = int(min(self.circle.width, self.circle.height) / 2) - 1
self.circle.draw_circle((int(self.circle.width / 2) - 1,
int(self.circle.height / 2) - 1), radius,
self.colors[4], self.colors[5])
self.filled.fill(self.colors[6])
def render(self):
"""
Send the current screen content to Mate Light.
"""
self.screen.reset()
self.screen.blit(self.corners)
self.screen.blit(self.lines, (1, 1))
self.screen.blit(self.rects, (int(self.screen.width / 2) + 1, 1))
self.screen.blit(self.circle, (0, int(self.screen.height / 2) + 1))
self.screen.blit(self.filled, (int(self.screen.width / 2) + 1,
int(self.screen.height / 2) + 1))
self.screen.update()
self.clock.tick()
def handle_events(self):
"""
Loop through all events.
"""
for event in pymlgame.get_events():
if event.type == E_NEWCTLR:
#print(datetime.now(), '### new player connected with uid', event.uid)
self.players[event.uid] = {'name': 'alien_{}'.format(event.uid), 'score': 0}
elif event.type == E_DISCONNECT:
#print(datetime.now(), '### player with uid {} disconnected'.format(event.uid))
self.players.pop(event.uid)
elif event.type == E_KEYDOWN:
#print(datetime.now(), '###', self.players[event.uid]['name'], 'pressed', event.button)
self.colors.append(self.colors.pop(0))
elif event.type == E_KEYUP:
#print(datetime.now(), '###', self.players[event.uid]['name'], 'released', event.button)
self.colors.append(self.colors.pop(0))
elif event.type == E_PING:
#print(datetime.now(), '### ping from', self.players[event.uid]['name'])
pass
def gameloop(self):
"""
A game loop that circles through the methods.
"""
try:
while True:
self.handle_events()
self.update()
self.render()
except KeyboardInterrupt:
pass
if __name__ == '__main__':
GAME = Game('127.0.0.1', 1337, 40, 16)
#GAME = Game('matelight.cbrp3.c-base.org', 1337, 40, 16)
GAME.gameloop()