-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.py
240 lines (220 loc) · 12 KB
/
interface.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#################################################################################
# #
# Interface.py #
# #
#################################################################################
# #
# Esse arquivo contem as definicoes da classe Interface. #
# #
#################################################################################
import os
import sys
import pygame
from game import *
from colors import *
from pygame.locals import *
class Interface:
width = 800
height = 600
caption = ""
margin_left = 20
margin_right = 20
margin_top = 100
margin_bottom = 20
background_color = white
buttons = [] # inicializa o vetor de botoes
# Inicializa a classe
#
# @in width: largura do tabuleiro
# @in height: altura do tabuleiro
# @in caption: titulo da janela
def __init__(self, width, height, caption):
self.height = height
self.width = width
self.caption = caption
self.create()
# Cria a janela e o objeto to tabuleiro.
def create(self):
pygame.init() # Inicializa os modulos do pygame
pygame.display.set_caption(self.caption) # Ajusta o titulo
self.window = pygame.display.set_mode((self.width, self.height), 0, 32) # Cria a janela
self.log = Log(self.window, pygame.Rect(280, 20, 240, 50), pygame.Rect(290, 25, 200, 20))
self.game = Game(self.window, self.log, self.width - self.margin_left - self.margin_right, self.height - self.margin_top - self.margin_bottom)
self.buttons.append(Button(self.window, pygame.Rect(20, 20, 250, 50), pygame.Rect(70, 25, 200, 20), "Calcular", self.game.start))
self.buttons.append(Button(self.window, pygame.Rect(530, 20, 250, 50), pygame.Rect(605, 25, 200, 20), "Limpar", self.game.clear))
#escreve o log do inicio
self.log.update("Inicio")
# Limpa a janela, pinta o tabuleiro e seus objetos e depois atualiza a janela.
def paint(self):
self.window.fill(self.background_color)
# Atualiza o tabuleiro
self.game.paint(pygame.Rect(self.margin_left, self.margin_top, self.game.width, self.game.height))
# Atualiza os botoes
for i in range(len(self.buttons)):
self.buttons[i].paint()
#Atualiza o log screen
self.log.paint()
pygame.display.update()
# Funcao que roda o tempo todo para determinar os eventos.
def update(self):
# Pinta a janela.
self.paint()
# Atualiza o tabuleiro.
self.game.update()
# Verifica eventos.
for event in pygame.event.get():
if event.type == QUIT: # Caso haja uma notificacao para fechar.
pygame.quit()
return False
elif event.type == MOUSEBUTTONUP: # Caso haja uma notificacao de clique.
pos = pygame.mouse.get_pos() # Obtem onde foi efetuado o clique.
# Verificamos se o clique foi sobre um botao
if event.button == 1:
for i in range(len(self.buttons)):
if self.buttons[i].rect.collidepoint(pos):
self.buttons[i].onClick() # chamamos a funcao associada ao botao
break
# Ajusta a posicao e tenta obter a celula sobre este.
cell = self.game.getCellByPosition([pos[0] - self.margin_left, pos[1] - self.margin_top])
if cell: # Caso encontrada
if event.button == 1:
self.game.updateCell(cell) # Atualiza esta.
if event.button == 3:
self.game.text = "Cell: " + str(cell.id) + ". Cost G: " + str(cell.cost_g) + ". Cost H: " + str(cell.cost_h)
elif event.type == KEYDOWN: # Caso seja uma tecla pressionada.
if event.key == K_F1:
self.game.clear()
self.log.update("Carregando mapa 1")
self.game.getCellById(55).color = gray
self.game.getCellById(206).color = yellow
elif event.key == K_F2:
self.game.clear()
self.log.update("Carregando mapa 2")
self.game.getCellById(108).color = gray
self.game.getCellById(112).color = yellow
self.game.getCellById(90).color = black
self.game.getCellById(110).color = black
self.game.getCellById(130).color = black
pass
elif event.key == K_F3:
self.game.clear()
self.log.update("Carregando mapa 3")
self.game.getCellById(221).color = gray
self.game.getCellById(20).color = yellow
self.game.getCellById(201).color = black
self.game.getCellById(202).color = black
self.game.getCellById(224).color = black
self.game.getCellById(204).color = black
self.game.getCellById(184).color = black
self.game.getCellById(162).color = black
self.game.getCellById(142).color = black
self.game.getCellById(122).color = black
self.game.getCellById(123).color = black
self.game.getCellById(124).color = black
self.game.getCellById(125).color = black
self.game.getCellById(126).color = black
self.game.getCellById(127).color = black
self.game.getCellById(164).color = black
self.game.getCellById(225).color = black
self.game.getCellById(205).color = black
self.game.getCellById(185).color = black
self.game.getCellById(165).color = black
self.game.getCellById(81).color = black
self.game.getCellById(82).color = black
self.game.getCellById(83).color = black
self.game.getCellById(84).color = black
self.game.getCellById(85).color = black
self.game.getCellById(86).color = black
self.game.getCellById(87).color = black
self.game.getCellById(88).color = black
self.game.getCellById(89).color = black
self.game.getCellById(147).color = black
self.game.getCellById(167).color = black
self.game.getCellById(187).color = black
self.game.getCellById(207).color = black
self.game.getCellById(227).color = black
self.game.getCellById(109).color = black
self.game.getCellById(129).color = black
self.game.getCellById(149).color = black
self.game.getCellById(169).color = black
self.game.getCellById(189).color = black
self.game.getCellById(209).color = black
self.game.getCellById(231).color = black
self.game.getCellById(211).color = black
self.game.getCellById(191).color = black
self.game.getCellById(171).color = black
self.game.getCellById(151).color = black
self.game.getCellById(131).color = black
self.game.getCellById(111).color = black
self.game.getCellById(91).color = black
self.game.getCellById(71).color = black
self.game.getCellById(51).color = black
self.game.getCellById(50).color = black
self.game.getCellById(49).color = black
self.game.getCellById(48).color = black
self.game.getCellById(47).color = black
self.game.getCellById(46).color = black
self.game.getCellById(26).color = black
self.game.getCellById(44).color = black
self.game.getCellById(43).color = black
self.game.getCellById(42).color = black
self.game.getCellById(1).color = black
self.game.getCellById(2).color = black
self.game.getCellById(3).color = black
self.game.getCellById(4).color = black
self.game.getCellById(8).color = black
self.game.getCellById(30).color = black
self.game.getCellById(12).color = black
self.game.getCellById(13).color = black
self.game.getCellById(232).color = black
self.game.getCellById(212).color = black
self.game.getCellById(192).color = black
self.game.getCellById(172).color = black
self.game.getCellById(152).color = black
self.game.getCellById(132).color = black
self.game.getCellById(34).color = black
self.game.getCellById(55).color = black
self.game.getCellById(76).color = black
self.game.getCellById(97).color = black
self.game.getCellById(118).color = black
self.game.getCellById(139).color = black
self.game.getCellById(156).color = black
self.game.getCellById(135).color = black
self.game.getCellById(93).color = black
self.game.getCellById(72).color = black
self.game.getCellById(52).color = black
self.game.getCellById(73).color = black
self.game.getCellById(94).color = black
self.game.getCellById(136).color = black
self.game.getCellById(157).color = black
self.game.getCellById(159).color = black
self.game.getCellById(179).color = black
self.game.getCellById(199).color = black
self.game.getCellById(233).color = black
self.game.getCellById(134).color = black
self.game.getCellById(154).color = black
self.game.getCellById(174).color = black
self.game.getCellById(194).color = black
self.game.getCellById(195).color = black
self.game.getCellById(197).color = black
self.game.getCellById(198).color = black
self.game.getCellById(215).color = black
self.game.getCellById(216).color = black
self.game.getCellById(217).color = black
self.game.getCellById(218).color = black
self.game.getCellById(219).color = black
self.game.getCellById(14).color = black
self.game.getCellById(15).color = black
self.game.getCellById(35).color = black
self.game.getCellById(37).color = black
self.game.getCellById(38).color = black
self.game.getCellById(39).color = black
self.game.getCellById(40).color = black
self.game.getCellById(58).color = black
self.game.getCellById(60).color = black
self.game.getCellById(80).color = black
self.game.getCellById(100).color = black
return True
def onExit(self):
#escreve o log do fim
self.log.update("Fim")