-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.py
202 lines (147 loc) · 5.87 KB
/
game.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""
game.py - base file for the tetris game
author : Benjamin Blundell
email : [email protected]
"""
import tetris, argparse, time, sys, socket
class Game:
''' A game class that controls the timing and viewing of the game state
This class also communicates with the server side '''
def __init__(self, game, fps=2, local=True, server_address="192.168.1.1", port=9001, pygame=False):
self.fps = fps;
self.interval = 1 / fps;
self.game = game
self.pygame = pygame
self.server_address = server_address
self.port = port
self.local = local
self.buffer_palette = {"I" : 1, "J" : 2, "L" : 3, "O" : 4, "Z" : 5, "S" : 6 , "T" : 7 }
if self.pygame:
self.pygame.init()
if not local:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def bufferToServer(self):
''' send the game buffer to the server via udp.
We prepare the buffer by converting down into numnbers 0-8.'''
send_buffer = []
for item in self.game.getLinearBufferReversed():
if item in self.buffer_palette.keys():
send_buffer.append( int(self.buffer_palette[item]) )
else:
send_buffer.append(int(0))
msg = ''.join(chr(x) for x in send_buffer)
try:
self.socket.sendto(msg, (self.server_address, self.port))
# TODO - dont break on all exceptions! Its a bit naff
except:
import traceback
print("Error connecting to server: " + self.server_address + ":" + str(self.port))
print(traceback.print_exc())
def loop(self):
start_time = time.time()
self.running = True
while self.running:
# Cx pygame - kill it but keep the game running
if self.pygame:
if self.pygame.running:
self.pygame.frame()
else:
self.pygame.cleanup()
self.pygame = False
now = time.time()
dt = now - start_time
if dt >= self.interval:
self.game.frame(self.fps)
#print("---")
#self.game.prettyPrint()
start_time = now
if not self.local:
self.bufferToServer()
def quit(self):
print("Quitting...")
if self.pygame:
self.pygame.cleanup()
self.running = False
sys.exit()
if __name__ == "__main__" :
import signal
def signal_handler(sig, frame):
print("")
game.quit()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
print ('Launching Tetris...')
print ('Press Ctrl+C to exit')
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--fps', metavar='N', type=float, help='overall framerate.')
parser.add_argument('--pygame', help='launch a pygame window.', action='store_true')
parser.add_argument('--server', metavar='ip address', help='server string.')
parser.add_argument('--port', metavar='N', type=int, help='port to connect to.')
parser.add_argument('--local', help='run locally with no server.', action='store_true')
parser.add_argument('--message', metavar='scroll message', help='scroll a message then quit.')
parser.add_argument('--file', metavar='filename', help='load a file and go on repeat.')
parser.add_argument('--scrollspeed', metavar='N', type=float, help='How man steps a second for the scolling message.')
parser.add_argument('--width', metavar='N', type=int, help='buffer width.')
parser.add_argument('--height', metavar='N', type=int, help='buffer height.')
argz = vars(parser.parse_args())
# Basic options - defaults
fps = 1
address = "localhost"
port = 9001
local = False
width = 14
height = 13
# Parse arguments
if argz["fps"]:
fps = argz["fps"]
if argz["server"]:
address = argz["server"]
if argz["port"]:
port = argz["port"]
if argz["local"]:
local = True
if argz["width"]:
width = argz["width"]
if argz["height"]:
height = argz["height"]
# game choice options
if argz["pygame"]:
tetris = tetris.Tetris(width, height)
import pygame_wrapper, pygame
wrapper = pygame_wrapper.Wrapper(tetris.buffer, tetris.boardX, tetris.boardY)
game = Game(tetris,fps,local,address,port,wrapper)
# Bind pygame keyboard events to tetris game logic
wrapper.register_event( pygame.KEYDOWN, pygame.K_LEFT, tetris.goLeft )
wrapper.register_event( pygame.KEYDOWN, pygame.K_RIGHT, tetris.goRight )
wrapper.register_event( pygame.KEYDOWN, pygame.K_SPACE, tetris.goRotate )
wrapper.register_event( pygame.KEYDOWN, pygame.K_DOWN, tetris.goDown )
wrapper.register_event( pygame.KEYDOWN, pygame.K_SPACE, tetris.goStart )
# Makey Makey bindings on the mac
wrapper.register_event( pygame.KEYDOWN, pygame.K_b, tetris.goDown )
wrapper.register_event( pygame.KEYDOWN, pygame.K_n, tetris.goLeft )
wrapper.register_event( pygame.KEYDOWN, pygame.K_v, tetris.goRight )
wrapper.register_event( pygame.KEYDOWN, pygame.K_m, tetris.goStart )
wrapper.register_event( pygame.KEYDOWN, pygame.K_m, tetris.goRotate )
# TODO makey makey on the RI machine. Is it the same as above?
game.loop()
elif argz["file"]:
# Display contents from one of our files
import pygame_wrapper, pygame, screensaver
crashburn = screensaver.FileToBuffer(width, height, argz["file"])
wrapper = pygame_wrapper.Wrapper(crashburn.buffer, crashburn.boardX, crashburn.boardY)
game = Game(crashburn,fps,local,address,port,wrapper)
game.loop()
elif argz["message"]:
# Show messages
import pygame_wrapper, pygame, message
scrollspeed = 1
if argz["scrollspeed"]:
scrollspeed = argz["scrollspeed"]
messenger = message.MessageWriter(width, height, "glyphs.txt", argz["message"],scrollspeed)
wrapper = pygame_wrapper.Wrapper(messenger.buffer, messenger.boardX, messenger.boardY)
game = Game(messenger,fps,local,address,port,wrapper)
game.loop()
else:
tetris = tetris.Tetris(width, height)
game = Game(tetris,fps,local,address,port)
game.loop()