-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
executable file
·142 lines (96 loc) · 2.64 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
#!/usr/bin/python
#coding: utf-8
import pygame
# mvc
import controller
import model
from math import ceil
import socket
import sys
import json
import lib
class Game(pygame.Rect):
DEFAULT_BLOCK_SIZE = 20
speed = 8
stage_blocks = []
pause_game = True
game_id = 0
config = {}
def __init__(self, size, block_s, winstyle=0):
pygame.init()
self.block_s = block_s;
self.controllers = []
## 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)
## surfaces
self.screen = pygame.display.set_mode(self.size, self.winstyle, bestdepth)
self.screenBuffer = pygame.Surface(self.size);
## init and running
self.running = True
self.clock = pygame.time.Clock()
self.snakeController = controller.SnakeController(self)
self.blockFreeController = controller.BlockFreeController(self)
self.controllers.append(self.snakeController)
self.controllers.append(self.blockFreeController)
self.eventManager = model.EventManager(self.snakeController)
self.eventManager.start()
self.serverComunicate = lib.ServerComunicate(self)
self.serverComunicate.start()
# contador de iterações
count = 0
while(self.running):
# apaga tudo
self.eraser()
# atualiza
self.update()
# pinta
self.paint()
# atrasa um certo tempo
self.clock.tick(self.speed)
def eraser(self):
self.screenBuffer.fill([0,0,0])
def update(self):
# update controllers
for control in self.controllers:
control.update()
def paint(self):
# pass screenBuffer for real screen
self.screen.blit(self.screenBuffer, (0, 0))
# update display
pygame.display.flip();
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((400, 400), Game.DEFAULT_BLOCK_SIZE)