Skip to content

Commit 70f0e4b

Browse files
committed
multiplos saves
1 parent cd81115 commit 70f0e4b

9 files changed

Lines changed: 201 additions & 85 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Tamacat 🐱
2+
=============
23

4+
Jogo muito fofo
35

46

57

TODO.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# FAZER AGORA!!!!!!
2-
- Animacao comida
32
- Documentar o código!!!!
3+
- Tirar classe Main para poder voltar ao menu
44
- Tempo
5+
- Mudar tamaedit para save
56

67
# FAZER DEPOIS
78
- Sistema de dinheiro
@@ -10,3 +11,4 @@
1011
- Comida favorita
1112
- Computador
1213
- Interagir com outros gatos do jogo
14+
- Animacao comida

config/caminhos.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from os import mkdir, path
2+
3+
# Diretório principal
4+
path_dir = path.join(path.expanduser('~'), '.tamacat')
5+
6+
# Diretório dos saves
7+
path_save = path.join(path_dir,'saves')
8+
9+
10+
# Criando as pastas caso elas não existam
11+
if not path.isdir(path_dir):
12+
mkdir(path_dir)
13+
mkdir(path_save)
14+
15+
else:
16+
if not path.isdir(path_save):
17+
mkdir(path_save)

config/funcoes.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66

77
def limpar_tela():
8+
89
if 'nt' in name:
910
system('cls')
1011
else:
1112
system('clear')
1213

1314

1415
def ajustes_iniciais():
16+
1517
if 'nt' in name:
1618
system('color f0') # define a cor do terminal (branco)
1719
system('mode con: cols=81 lines=24') # ajuda o tamanho do terminal
@@ -24,15 +26,19 @@ def mudar_titulo(titulo):
2426
system(f'TITLE Tamacat - {titulo}')
2527

2628

29+
def existe_save(nome):
30+
return nome in [save.split('.')[0] for save in csave.listar_saves()]
31+
32+
2733
def verificar_nome(nome):
28-
if len(nome) > 32 or nome == '_pE_dRo_' or nome.isspace() or nome == '':
34+
35+
if len(nome) > 32 or nome == '_pE_dRo_' or nome.isspace() or nome == '' or existe_save(nome):
2936
return False
3037

3138
return True
3239

3340

3441
def janela_sair(salvo, gato, gela, bau):
35-
3642
mudar_titulo('Sair do jogo')
3743

3844
janela = cjane.Janela()
@@ -61,7 +67,7 @@ def janela_sair(salvo, gato, gela, bau):
6167
if int(esc) in range(1, 3):
6268
esc = 's'
6369
if esc == '1':
64-
csave.salvar_jogo(gato, gela, bau)
70+
csave.salvar_jogo([gato, gela, bau])
6571

6672
if 's' in esc:
6773
janela.limpar_janela()
@@ -75,7 +81,6 @@ def janela_sair(salvo, gato, gela, bau):
7581

7682

7783
def janela_deletar():
78-
7984
mudar_titulo('Abandonar gato')
8085

8186
janela = cjane.Janela()
@@ -96,7 +101,6 @@ def janela_deletar():
96101

97102

98103
def janela_carregar():
99-
100104
mudar_titulo('Carregando jogo')
101105

102106
janela = cjane.Janela()
@@ -105,7 +109,6 @@ def janela_carregar():
105109

106110

107111
def janela_salvar():
108-
109112
mudar_titulo('Salvar jogo')
110113

111114
janela = cjane.Janela()

config/janela.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def mostrar_janela(self, show_input=True):
9898

9999
print(s + self.linha_top)
100100
if show_input:
101-
input(f'(Página {i + 1}/{len(self.itens) // 19 + 1}) Aperte ENTER para continuar...')
101+
input(f'Página {i + 1}/{len(self.itens) // 19 + 1} (Aperte ENTER para continuar...)')
102102

103103
def add_linha(self, lista):
104104
lista = [str(i) for i in lista]

config/saveload.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
from pickle import dump, loads
2-
from os import remove, path
3-
4-
path_save = path.join(path.expanduser('~'), 'tamacat.save')
1+
import config.caminhos as cpath
52

3+
from pickle import dump, loads
4+
from os import remove, path, listdir
5+
66

7-
def salvar_jogo(*objs):
7+
def salvar_jogo(objs:list):
88
try:
9+
save_file = path.join(cpath.path_save, objs[0].nome + '.tamacat')
10+
911
for i in range(len(objs)):
1012
t = 'ab'
1113
if i == 0:
1214
t = 'wb'
1315

14-
with open(path_save, t) as file:
16+
with open(save_file, t) as file:
1517
dump(objs[i], file)
1618
file.write(bytes('_pE_dRo_'.encode('utf8')))
1719

1820
except Exception as e:
1921
print(e)
2022

2123

22-
def carregar_jogo():
24+
def carregar_jogo(nome):
2325
try:
24-
with open(path_save, 'rb') as file:
26+
save_file = path.join(cpath.path_save, nome + '.tamacat')
27+
28+
with open(save_file, 'rb') as file:
2529
objs = file.read()
2630
objs = objs.split('_pE_dRo_'.encode('utf8'))
2731

@@ -31,12 +35,16 @@ def carregar_jogo():
3135

3236
return list_objs
3337

34-
except FileNotFoundError:
38+
except Exception:
3539
return False
3640

3741

38-
def deletar_jogo():
42+
def deletar_jogo(nome):
3943
try:
40-
remove(path_save)
44+
remove(path.join(cpath.path_save, nome + '.tamacat'))
4145
except Exception as e:
4246
print(e)
47+
48+
49+
def listar_saves():
50+
return [file for file in listdir(cpath.path_save) if file.endswith('.tamacat')]

main.py

Lines changed: 93 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,26 @@
1313

1414

1515
class Main:
16+
1617
def __init__(self):
1718
cfunc.ajustes_iniciais()
18-
self.tela_inicial()
19-
20-
if csave.carregar_jogo():
21-
self.gato, self.gela, self.bau = csave.carregar_jogo()
22-
cfunc.janela_carregar()
23-
sleep(1)
24-
19+
op = self.tela_inicial()
20+
self.voltar = False
21+
22+
if op == '1':
23+
objs = self.novo_gato()
24+
elif op == '2':
25+
objs = self.tela_carregar_gato()
26+
elif op == '3':
27+
exit()
28+
29+
if objs:
30+
self.gato, self.gela, self.bau = objs
31+
csave.salvar_jogo([self.gato, self.gela, self.bau])
32+
2533
else:
26-
self.gato, self.gela, self.bau = self.novo_gato()
27-
csave.salvar_jogo(self.gato, self.gela, self.bau)
28-
34+
self.voltar = True
35+
2936
self.salvo = True
3037

3138
@staticmethod
@@ -56,11 +63,69 @@ def tela_inicial():
5663
pass
5764

5865
janela.muda_linha(11, 'O MELHOR JOGO DO MUNDO!')
59-
janela.muda_linha(20, '© RaGhu 2021 ', alin='rjust')
66+
janela.muda_linha(21, '© RaGhu 2021 ', alin='rjust')
6067

6168
print(janela)
6269
input() # para não dar para digitar nada no input além de enter
63-
70+
71+
janela.muda_linha(15, '(1) Novo Jogo ')
72+
janela.muda_linha(16, '(2) Carregar Jogo ')
73+
janela.muda_linha(17, '(3) Sair ')
74+
75+
print(janela)
76+
op = input('Digite a opção desejada: ')
77+
78+
while op not in ['1', '2', '3']:
79+
print(janela)
80+
op = input('Digite uma opção válida: ')
81+
82+
return op
83+
84+
def tela_carregar_gato(self):
85+
gatos = csave.listar_saves()
86+
87+
if len(gatos) == 0:
88+
janela = cjane.Janela()
89+
janela.muda_linha(11, 'Você não possui nenhum gato, deseja criar um? (S)im ou (N)ão')
90+
91+
print(janela)
92+
93+
esc = input('>>> ').lower()
94+
while esc != 's' and esc != 'n' and esc != 'sim' and esc != 'não' and esc != 'nao':
95+
janela.muda_linha(12, 'Digite uma opção válida!')
96+
print(janela)
97+
esc = input('>>> ').lower()
98+
99+
if 's' in esc:
100+
return self.novo_gato()
101+
elif 'n':
102+
return None
103+
104+
elif len(gatos) == 1:
105+
save = csave.carregar_jogo(gatos[0].split(".")[0])
106+
return save
107+
108+
elif len(gatos) > 1:
109+
janela = cjane.JanelaTable({'##': 4, 'Gato': 54, 'Idade': 18})
110+
gatitos = []
111+
112+
for i in range(len(gatos)):
113+
ga, ge, ba = csave.carregar_jogo(gatos[i].split(".")[0])
114+
gatitos.append([ga, ge, ba])
115+
janela.add_linha([i+1, ga.nome, ga.mostrar_idade()])
116+
117+
janela.mostrar_janela(False)
118+
esc = input('Digite o número do gato para carregar (ENTER para voltar): ').lower()
119+
120+
while esc != '' and (not esc.isnumeric() or int(esc) not in range(1, len(gatos)+1)):
121+
janela.mostrar_janela(False)
122+
esc = input('Digite uma opção válida: ').lower()
123+
124+
if esc != '':
125+
return gatitos[int(esc)-1]
126+
else:
127+
return None
128+
64129
@staticmethod
65130
def novo_gato():
66131
"""Retorna um Gatinho, Geladeira e Bau para um gato inicial."""
@@ -215,15 +280,24 @@ def novo_gato():
215280
input('(Aperte ENTER para continuar...)')
216281

217282
l = ga.gens['letra']
218-
janela.muda_linha(3+v, f' Hora de uma decisão difícil... Qual vai ser o nome d{l} gat{l}?', 'ljust')
283+
p = ga.gens['pron']
284+
janela.muda_linha(3+v, f' Hora de uma decisão difícil... Qual vai ser o nome del{p}?', 'ljust')
219285
print(janela)
220286
nome = input('>>> ')
221287

222288
while not cfunc.verificar_nome(nome):
223-
janela.muda_linha(4+v, ' Digite um nome válido (e com tamanho menor que 32)!', 'ljust')
289+
290+
if cfunc.existe_save(nome):
291+
gatolino = csave.carregar_jogo(nome)[0]
292+
l_antigo = gatolino.gens['letra']
293+
p_antigo = gatolino.gens['pron']
294+
janela.muda_linha(4+v, f' Ess{p_antigo} gatinh{l_antigo} já existe! Escolha outro nome.', 'ljust')
295+
else:
296+
janela.muda_linha(4+v, ' Digite um nome válido (e com tamanho menor que 32)!', 'ljust')
297+
224298
print(janela)
225299
nome = input('>>> ')
226-
300+
227301
ga.nome = nome
228302
ge = ogela.Geladeira()
229303
ba = obau.Bau()
@@ -403,7 +477,7 @@ def run_game(self):
403477
elif esc == '5':
404478
# Salvar jogo
405479
cfunc.limpar_tela()
406-
csave.salvar_jogo(self.gato, self.gela, self.bau)
480+
csave.salvar_jogo([self.gato, self.gela, self.bau])
407481

408482
self.salvo = True
409483
cfunc.janela_salvar()
@@ -432,4 +506,7 @@ def run_game(self):
432506
if __name__ == '__main__':
433507

434508
game = Main()
509+
while game.voltar:
510+
game = Main()
511+
435512
game.run_game()

objs/gatinho.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ def mostrar_idade(self):
9393

9494
mes = self.idade % 12
9595
ano = self.idade // 12
96-
96+
9797
if ano == 0:
9898
return f'{mes} meses'
99-
99+
100100
elif mes == 0:
101101
if ano == 1:
102102
return '1 ano'
@@ -108,6 +108,9 @@ def mostrar_idade(self):
108108
return f'1 ano e 1 mês'
109109
else:
110110
return f'{ano} anos e 1 mês'
111+
112+
elif ano == 1:
113+
return f'1 ano e {mes} meses'
111114

112115
return f'{ano} anos e {mes} meses'
113116

0 commit comments

Comments
 (0)