-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-client.py
214 lines (148 loc) · 4.79 KB
/
game-client.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
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/python
#coding: utf-8
import pygame
import model
from math import ceil
from random import random
import socket
import sys
import json
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = ('127.0.0.1', 8765)
sock.sendto("hello", server_address)
msg_server, address = sock.recvfrom(1024)
game_id = 0
if(msg_server == "server-full"):
print "Server is fulling, return back tomorrow"
sys.exit()
else:
game_id = int(msg_server)
class Game(pygame.Rect):
speed = 8
stage_blocks = []
pause_game = True
config = {}
snakes = []
def __init__(self, size, block_s, winstyle=0):
pygame.init()
## init
pygame.Rect.__init__(self, (0, 0) , (size[0], size[1]))
## init windows
self.winstyle = winstyle
bestdepth = pygame.display.mode_ok(self.size, self.winstyle, 32)
self.screen = pygame.display.set_mode(self.size, self.winstyle, bestdepth)
## init and running
self.running = True
self.clock = pygame.time.Clock()
self.snake = model.Snake(self, block_s)
self.snake.setId(game_id)
self.eventManager = model.EventManager(self);
self.eventManager.start()
self.screenBuffer = pygame.Surface(self.size);
self.snake.addBlock(model.Block(self.snake, (3 * block_s, 6 * block_s)) )
self.snake.addBlock(model.Block(self.snake, (3 * block_s, 5 * block_s)) )
self.snake.addBlock(model.Block(self.snake, (3 * block_s, 4 * block_s)) )
self.snake.addBlock(model.Block(self.snake, (3 * block_s, 3 * block_s)) )
self.snake.addBlock(model.Block(self.snake, (3 * block_s, 2 * block_s)) )
self.snakes.append(self.snake);
# load stage
self.load_stage(0, 20)
block_free = False
count = 0
while(self.running):
self.update()
self.screenBuffer.fill([0,0,0])
if(block_free == False):
pos = ( int(random() * (self.size[0] / block_s)) * block_s , int(random() * (self.size[1] / block_s)) * block_s )
block_free = model.BlockFree(False, pos)
while(block_free.collidelist(self.stage_blocks) != -1):
pos = ( int(random() * (self.size[0] / block_s)) * block_s , int(random() * (self.size[1] / block_s)) * block_s )
block_free = model.BlockFree(False, pos)
for snake in self.snakes:
if(block_free != False):
block_free.paint(self.screenBuffer)
if(snake.blocks[0].colliderect(block_free)):
snake.addBlock(model.Block([], block_free.getPos()))
block_free = False
for block in self.stage_blocks:
block.paint(self.screenBuffer)
snake.paint(self.screenBuffer)
# pass screenBuffer for real screen
self.screen.blit(self.screenBuffer, (0, 0))
# update display
pygame.display.flip();
if(self.pause_game): continue;
# move snaker
snake.move(snake.direct)
# test collision
index = snake.blocks[0].collidelist(self.stage_blocks)
index2 = snake.blocks[0].collidelist(self.snake.blocks[1:-1])
if index != -1 or index2 != -1:
if(isinstance(self.stage_blocks[index], model.Brick)):
self.running = False
self.clock.tick(self.speed)
# endwhile
def update(self):
# manda as informações
encoder = json.encoder.JSONEncoder()
decoder = json.decoder.JSONDecoder()
msg = encoder.encode(['set-update', {'snake': self.snake.getInfo()}]);
sock.sendto(msg, server_address)
# get updates
updates = []
msg = encoder.encode(['get-updates']);
sock.sendto(msg, server_address)
# get
updates, address = sock.recvfrom(1024)
update = decoder.decode(updates)
total = int(update['total']);
if(total > 0):
i = 0
while( i < total):
update, address = sock.recvfrom(1024)
info = decoder.decode(update);
encontrou = False
for snake in self.snakes:
if(snake.getId() == info["snake"]["id"] ):
encontrou = True
snake.setInfo(info["snake"])
break
if(not encontrou):
snake = model.Snake(self, 20);
snake.setInfo(info["snake"])
self.snakes.append(snake)
print self.snakes
i += 1
def __done__(self):
pygame.quit()
def load_stage(self, stage_number, block_s):
file_name = "stages/stage%d" % (stage_number)
########################################################################
# linha 1 - snake init: posx, posy, direction, total blocks init #
# linha 2 - speed game
# linha > 2 - map elements
#
########################################################################
f = file(file_name)
line1 = f.readline().split(',')
line2 = f.readline().split(',')
self.config['snake'] = line1
self.config['game'] = line2
content = f.read()
f.close()
# @ -> brick
x = 0
y = 0
i = 0
while i < len(content):
ch = content[i]
if(ch == "@"):
self.stage_blocks.append(model.Brick(self, (x * block_s, y * block_s), (block_s, block_s)))
x += 1
if(ch == '\n'):
y += 1
x = 0
i += 1
## init game
if __name__ == '__main__':
game = Game((500, 500), 20)