-
Notifications
You must be signed in to change notification settings - Fork 0
/
ahorcapy.py
executable file
·273 lines (220 loc) · 5.91 KB
/
ahorcapy.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Ahorcapy r11 (0.9.1)
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
# Versión del juego del ahorcado.
# (c) 2011-2012 Alfonso Saavedra "Son Link"
# http://sonlinkblog.blogspot.com/p/ahorcapy.html
import curses, curses.panel, gettext
from random import randint
from sys import argv
from optparse import OptionParser
from os.path import isfile
from os import access, R_OK
APP = 'ahorcapy'
gettext.textdomain (APP)
gettext.bindtextdomain (APP, 'lang')
_ = gettext.gettext
gettext.install(APP, 'lang')
# Iniciamos Curses
stdscr = curses.initscr()
curses.start_color()
stdscr.keypad(1)
# Definimos los pares de color que usaremos
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_BLUE, curses.COLOR_BLACK)
curses.noecho()
curses.cbreak()
# Configuramos e iniciamos la ventana del juego
begin_x = 1 ; begin_y = 1
height = 20 ; width = 20
win = curses.newwin(height, width, begin_y, begin_x)
class Ahorcapy():
def __init__(self):
"""
Iniciamos el juego
"""
self.positions = []
self.letters = []
self.errors = 0
self.wordslist = ''
if len(argv) == 2:
self.wordslist = argv[1]
else:
self.wordslist = 'words.txt'
if not isfile(self.wordslist) or not access(self.wordslist, R_OK):
curses.endwin()
print (_('The file %s don\'t exists or you not have read permissions') % self.wordslist)
exit(-1)
f = open(self.wordslist, 'r')
words = f.readlines()
f.close()
n = randint(0, len(words)-1)
self.word = words[n].split()[0]
self.word2 = ''
i = 1
while i <= len(self.word):
self.word2 += '*'
i += 1
stdscr.addstr(1, 4, ' ______')
stdscr.addstr(2, 4, '/ |')
stdscr.addstr(3, 4, '|')
stdscr.addstr(4, 4, '|')
stdscr.addstr(5, 4, '|')
stdscr.addstr(6, 4, '|')
stdscr.addstr(9, 4, 'Palabra: %s' % self.word2)
panel = self.show_letters()
panel.show()
curses.panel.update_panels()
stdscr.refresh()
self.checkLetter()
def checkLetter(self):
"""
Comprueba si la letra esta en la palabra.
"""
k = stdscr.getch()
if k >= 97 and k <= 122 or k >= 65 and k <= 90:
key = chr(k).lower()
if not key in self.letters:
self.letters.append(key)
self.letters.sort()
if key in self.word:
i = -1
try:
while 1:
i = self.word.index(key, i+1)
self.positions.append(i)
self.positions.sort()
except ValueError:
pass
else:
curses.beep()
self.errors += 1
i = 0
word2 = ''
while i < len(self.word):
if i in self.positions:
word2 += self.word[i]
else:
word2 += '*'
i += 1
self.word2 = word2
self.redraw()
elif k == 27:
# Si se pulsa la tecla Esc se cierra el juego
self.salir()
else:
self.redraw()
def redraw(self):
"""
Repinta la ventana
"""
letters = ''
if self.word2 != self.word:
if self.errors == 1:
stdscr.addstr(3, 10, 'O')
if self.errors == 2:
stdscr.addstr(4, 10, '|')
if self.errors == 3:
stdscr.addstr(4, 9, '/|')
if self.errors == 4:
stdscr.addstr(4, 9, '/|\\')
if self.errors == 5:
stdscr.addstr(5, 9, '/')
if self.errors == 6:
stdscr.addstr(5, 9, '/ \\')
stdscr.addstr(9, 4, _('Word: %s') % self.word)
stdscr.addstr(11, 4, _('YOU FAIL!'), curses.color_pair(1) )
self.retry()
stdscr.addstr(9, 4, _('Word: %s') % self.word2)
for k in self.letters:
letters += ' ' + k
panel = self.show_letters()
panel.show()
self.checkLetter()
else:
stdscr.addstr(9, 4, _('Word: %s') % self.word2)
stdscr.addstr(11, 4, _('YOU WIN!'), curses.color_pair(2) )
self.retry()
def retry(self):
"""
Al terminar la partida nos preguntara si queremos volver a jugar
"""
stdscr.addstr(12, 4, _('Press any key to start new game or press ESC to exit'))
key = stdscr.getch()
if key < 256:
if key == 27:
self.salir()
else:
self.positions = []
self.letters = []
self.errors = 0
stdscr.clear()
self.word2 = ''
self.show_letters(True)
f = open(self.wordslist, 'r')
words = f.readlines()
f.close()
n = randint(0, len(words)-1)
self.word = words[n].split()[0]
self.word2 = ''
i = 1
while i <= len(self.word):
self.word2 += '*'
i += 1
stdscr.addstr(1, 4, ' ______')
stdscr.addstr(2, 4, '/ |')
stdscr.addstr(3, 4, '|')
stdscr.addstr(4, 4, '|')
stdscr.addstr(5, 4, '|')
stdscr.addstr(6, 4, '|')
stdscr.addstr(9, 4, 'Palabra: %s' % self.word2)
panel = self.show_letters()
panel.show()
self.redraw()
else:
self.retry()
def show_letters(self, clear=False):
letters = ''
win = curses.newwin(6, 14, 1, 20)
if clear:
win.clear()
win.addstr(0, 0, _('Letters:'))
for l in self.letters:
letters += l + ' '
win.addstr(1, 0, letters)
win.bkgdset(ord(' '), curses.color_pair(0))
win.refresh()
pan = curses.panel.new_panel(win)
return pan
def salir(self):
"""
Salimos del programa
"""
curses.endwin()
exit()
if __name__ == '__main__':
try:
Ahorcapy()
except KeyboardInterrupt:
curses.endwin()
exit()