repl.it
@hjvijchhgjvyjnv/

O barqueiro

Python

No description

fork
loading
Files
  • main.py
main.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env python3
# -*- coding:UTF-8 -*-

#coding: cp1252
#coding: latin1

import sys
import time
import curses
from random import randrange
from threading import Timer

# janela principal
screen = curses.initscr()

# tamanho da janela principal
screen_x, screen_y = screen.getmaxyx()

if screen_x < 24 or screen_y < 80:
	screen.clear()
	curses.endwin()

	print('\nO seu terminal tem que ter no minimo 24 linhas por 80 colunas.\n')
	print('Reajuste o tamanho do seu terminal para poder jogar... \n')
	sys.exit(0)


screen.keypad(True)
#screen.notimeout(False)
curses.cbreak()
# Não retorna caracteres na tela
curses.noecho()

# esconde o cursor do mouse e do terminal
curses.curs_set(0)

# limpa a tela
screen.clear()

# atualiza a tela automaticamente mais causa perda de performance
# pode ser usado no lugar das chamadas da funcao  screen.refresh()
screen.immedok(True)

# iniciando cores
curses.start_color()
curses.init_pair( 1, curses.COLOR_RED, curses.COLOR_WHITE)
curses.init_pair( 2, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair( 3, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair( 4, curses.COLOR_WHITE, curses.COLOR_BLUE)
curses.init_pair( 5, curses.COLOR_WHITE, curses.COLOR_BLUE)
curses.init_pair( 6, curses.COLOR_RED, curses.COLOR_BLUE)

curses.init_pair(11, curses.COLOR_BLUE, curses.COLOR_WHITE)
curses.init_pair(13, curses.COLOR_GREEN, curses.COLOR_WHITE)
curses.init_pair(18, curses.COLOR_BLACK, curses.COLOR_WHITE)

# Define Constantes usadas como caracteres especiais
# Single-line
# Chr( 218 ) + Chr( 196 ) + Chr( 191 ) ┌ ─ ┐
# Chr( 179 ) + Chr(  32 ) + Chr( 179 ) │   │
# Chr( 192 ) + Chr( 196 ) + Chr( 217 ) └ ─ ┘

c_032 =   32
c_168 = 9608
c_177 = 9617
c_178 = 9618
c_179 = 9474
c_191 = 9488
c_192 = 9492
c_196 = 9472
c_217 = 9496
c_218 = 9484
c_219 = 9604
c_223 = 9600


# Executando Funções em Intervalos de Tempo
# Tirado do link:
# https://wiki.python.org.br/ExecutandoEmIntervalos
def setInterval(function, interval, *params, **kwparams):
    def setTimer(wrapper):
        wrapper.timer = Timer(interval, wrapper)
        wrapper.timer.start()

    def wrapper():
        function(*params, **kwparams)
        setTimer(wrapper)
    
    setTimer(wrapper)
    return wrapper

def clearInterval(wrapper):
    wrapper.timer.cancel()



# Esta é uma tentativa de manter o prg compativel com o python 2.7
# para isto comente as duas linhas abaixo
def unichr(ch):
	return chr(ch)



def pause(tempo):
	# Atualiza a tela
	#screen.refresh()
	# Pausa por um tempo
	time.sleep(tempo)



# Funcao para imprimir a tela de abertura
def autor():

	DispBox_Shadow(2,10,21,67, 11)

	screen.addstr( 5, 12, '  ___    ____                             _           ', curses.color_pair(13) | curses.A_BOLD)
	screen.addstr( 6, 12, ' / _ \  | __ )  __ _ _ __ __ _ _   _  ___(_)_ __ ___  ', curses.color_pair(13) | curses.A_BOLD)
	screen.addstr( 7, 12, '| | | | |  _ \ / _` | \'__/ _` | | | |/ _ \ | \'__/ _ \ ', curses.color_pair(13) | curses.A_BOLD)
	screen.addstr( 8, 12, '| |_| | | |_) | (_| | | | (_| | |_| |  __/ | | | (_) |', curses.color_pair(13) | curses.A_BOLD)
	screen.addstr( 9, 12, ' \___/  |____/ \__,_|_|  \__, |\__,_|\___|_|_|  \___/ ', curses.color_pair(13) | curses.A_BOLD)
	screen.addstr(10, 12, '                            |_|                       ', curses.color_pair(13) | curses.A_BOLD)

	screen.addstr(14, 52, 'Versão 1.0', curses.color_pair(18))

	screen.addstr(18, 15, 'Autor: Washington Luis de Oliveira Santos', curses.color_pair(11))
	screen.addstr(19, 15, 'End. : Av. Campinas, 749 - Chácara do Visconde', curses.color_pair(11))
	screen.addstr(20, 28, 'Taubaté - São Paulo', curses.color_pair(11))


def Box(lt,ce,lb,cd, cor):
	
	for x in range(lt,lb):
		screen.addstr(x, ce, ' ' * (cd-ce+1), curses.color_pair(cor))


def DispBox(lt,ce,lb,cd, cor):
	
	Box(lt,ce,lb,cd, cor)

	screen.addstr(lt, ce, unichr(c_196) * (cd-ce), curses.color_pair(cor))
	screen.addstr(lt, ce, unichr(c_218), curses.color_pair(cor))
	screen.addstr(lt, cd, unichr(c_191), curses.color_pair(cor))
	
	screen.addstr(lb, ce, unichr(c_196) * (cd-ce), curses.color_pair(cor))
	screen.addstr(lb, ce, unichr(c_192), curses.color_pair(cor))
	screen.addstr(lb, cd, unichr(c_217), curses.color_pair(cor))

	for x in range(lt+1,lb):
		screen.addstr(x, ce, unichr(c_179), curses.color_pair(cor))
		screen.addstr(x, cd, unichr(c_179), curses.color_pair(cor))


def DispBox_Shadow(lt,ce,lb,cd, cor):
	
	DispBox(lt,ce,lb,cd, cor)

	#Desenha a Sombra da Caixa
	for x in range(lt+1,lb+1):
		screen.addstr(x, cd+1, unichr(c_168), curses.color_pair(18))
	
	screen.addstr(lb+1, ce+1, unichr(c_168) * (cd-ce+1), curses.color_pair(18))


def ENCERRA():
	#Restaura a cor do terminal
	screen.refresh()
	screen.clear()
	screen.keypad(True)
	curses.nocbreak()
	curses.echo()
	curses.endwin()
	sys.exit(0)

def water_display(l_ini=13, l_end=19):
	
	for line in range(l_ini, l_end):
		water_line = ''
		
		while len(water_line) <= 80:
			water_line += ('^' * randrange(0,6)) + (' ' * randrange(1,6))
		
		screen.addstr(line, 0, water_line[:80], curses.color_pair(4))


def chk_cais(cais):
		cais = ''.join(cais)

		quem   = 'O  Lobo' if cais == 'LC ' else 'A Cabra' if cais == ' CP' else ''
		#'ninguem'
		vitima = 'a Cabra' if cais == 'LC ' else 'o Pasto' if cais == ' CP' else ''
		#'nada  '

		if quem+vitima:
			screen.addstr(21, 1, 'Sinto muito...', curses.color_pair(1))
			screen.addstr(22, 1, quem + ' comeu ' + vitima, curses.color_pair(1))
			pause(1)
			return True


def chk_lotacao(carga):
	if carga.count(' ') == 1:
		screen.addstr(21, 1, 'Sinto muito... Lotado', curses.color_pair(1))
		screen.addstr(22, 1, 'O Barco afundou...', curses.color_pair(1))
		pause(1)

		screen.addstr(10,38, '     ', curses.color_pair(2)) # f preto
		screen.addstr(11,38, '     ', curses.color_pair(1)) # f branco

		for  line in range(13,18):
			water_display()
			screen.addstr(line+0,37, '  M  ', curses.color_pair(6))
			screen.addstr(line+1,37, '\~~~/', curses.color_pair(6)) # | curses.A_UNDERLINE)
			pause(.2)

		return True


def chk_fim(cais_2):
	if ''.join(cais_2) == 'LCP':
		screen.addstr(21, 1, ' Muito Bem !!!', curses.color_pair(1))
		return True


def embarque(cais, carga):
	if ''.join(cais) != '   ':
		screen.addstr(21, 1, 'Por no Barco?  ', curses.color_pair(3) | curses.A_BOLD | curses.A_BLINK)
		key = chr(screen.getch()).upper()

		screen.addstr(21, 1, ' '*15, curses.color_pair(3))

		if  key == chr(27):
			return True

		elif key != chr(32):
			try:
				i = cais.index(key)
				cais  [i] = ' '
				carga [i] = key
			except:
				pass


def desembarque(cais, carga):
	if ''.join(carga) != '   ':
		screen.addstr(21, 1, 'Tirar do Barco?', curses.color_pair(3) | curses.A_BOLD | curses.A_BLINK)
		key = chr(screen.getch()).upper()

		screen.addstr(21, 1, ' '*15, curses.color_pair(3))

		if  key == chr(27):
			return True

		elif key != chr(32):
			try:
				i = carga.index(key)
				cais  [i] = key
				carga [i] = ' '
			except:
				pass


def gameloop():
	# Define Variaveis usadas no jogo

	cais_1 = ['L','C','P']
	cais_2 = [' ',' ',' ']
	carga  = [' ',' ',' ']

	barco  = ' \___/ '
	nv     = 0 # numero de viagens

	screen.addstr(21,62, ' Nº de Viagens: {:2}'.format(nv), curses.color_pair(4))

	while True:
		if desembarque(cais_1, carga): return
		screen.addstr(10, 2, ''.join(cais_1), curses.color_pair(2))
		screen.addstr(10, 9, ''.join(carga),  curses.color_pair(2))

		if embarque(cais_1, carga): return
		screen.addstr(10, 2, ''.join(cais_1), curses.color_pair(2))
		screen.addstr(10, 9, ''.join(carga),  curses.color_pair(2))

		# Movimenta o barco de C1 para C2 (atravessa)
		aux = '  '+''.join(carga)+'  '
		for col in range(7,67):
			#water_display()
			screen.addstr(10, col, aux,   curses.color_pair(2))
			screen.addstr(11, col, barco, curses.color_pair(1))
			pause(.1)
			if col == 37 and chk_lotacao(carga): return

		nv += 1
		screen.addstr(21,62, ' Nº de Viagens: {:2}'.format(nv), curses.color_pair(4))

		if chk_cais(cais_1): return

		if desembarque(cais_2, carga): return
		screen.addstr(10,75, ''.join(cais_2), curses.color_pair(2))
		screen.addstr(10,68, ''.join(carga),  curses.color_pair(2))

		if chk_fim(cais_2): return

		if embarque(cais_2, carga): return
		screen.addstr(10,75, ''.join(cais_2), curses.color_pair(2))
		screen.addstr(10,68, ''.join(carga),  curses.color_pair(2))

		# Movimenta o barco de C2 para C1 (volta)
		aux = '  '+''.join(carga)+'  '
		for col in range(66,6,-1):
			#water_display()
			screen.addstr(10, col, aux,   curses.color_pair(2))
			screen.addstr(11, col, barco, curses.color_pair(1))
			pause(.1)
			if col == 37 and chk_lotacao(carga): return

		nv += 1
		screen.addstr(21,62, ' Nº de Viagens: {:2}'.format(nv), curses.color_pair(4))

		if chk_cais(cais_2): return


def main():
	while True:
		screen.clear()
		#Imprimi a tela de abertura
		water_display(1,24)
		autor()
		pause(4)

		screen.clear()

		screen.addstr( 2,30, ' O   B A R Q U E I R O ', curses.color_pair(4) | curses.A_BOLD)
		screen.addstr(10, 2, 'LCP', curses.color_pair(2))
		screen.addstr(10, 9, '   ', curses.color_pair(2))
		screen.addstr(11, 0, '|-----. \   /' + (' ' * 60)+'.-----|', curses.color_pair(1))
		screen.addstr(12, 0, ('~' * 80), curses.color_pair(4))
		screen.addstr(12, 0, '|', curses.color_pair(6) | curses.A_UNDERLINE)
		screen.addstr(12, 6, '|', curses.color_pair(6) | curses.A_UNDERLINE)
		screen.addstr(12,73, '|', curses.color_pair(6) | curses.A_UNDERLINE)
		screen.addstr(12,79, '|', curses.color_pair(6) | curses.A_UNDERLINE)
		screen.addstr(19, 0, ('~' * 80), curses.color_pair(4))

		interval_monitor = setInterval(water_display, .5)
		
		gameloop()
		
		clearInterval(interval_monitor)

		# Verifica o que fazer ao final do jogo
		screen.addstr(23, 1, 'Quer tentar outra vez? (S/N)', curses.color_pair(1))

		key = chr(screen.getch()).upper()

		if key != 'S':
			ENCERRA()


if __name__ == '__main__':
	try:
		curses.wrapper(main())
	except KeyboardInterrupt:
		curses.endwin()

?