-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpitonAuto.py
More file actions
284 lines (225 loc) · 7.98 KB
/
pitonAuto.py
File metadata and controls
284 lines (225 loc) · 7.98 KB
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
# # Importing the keyboard module
import keyboard as kb
import os
import time
import threading
import random
from color import Colors
def clear_console():
if os.name == "nt": # windows:
os.system("cls")
else:
os.system("clear")
def mostrar():
for i in space:
print("\t\t", end="")
for j in i:
print(j, end="")
print()
def crear_space(ancho, alto):
return [[' ' for _ in range(ancho)] for _ in range(alto)]
def paredes():
for i in range(alto):
space[i][0] = '|'
space[i][ancho-1] = '|'
for i in range(ancho):
space[0][i] = '~'
space[alto-1][i] = '~'
def mover_snake(X, Y):
snake.insert(0, (X, Y))
snake.pop()
def crecer_snake(x, y):
snake.append((x, y))
def poner_manzana():
while True:
numX = random.randint(0, ancho-1)
numY = random.randint(0, alto-1)
if (numX, numY) not in snake and numX not in (0, ancho-1) and numY not in (0, alto-1):
space[numY][numX] = Colors.RED+"$"+Colors.RESET
return numX, numY
def mostrar_key(e):
global teclado
teclado = e.name
def update_screen():
global x, y, space, snake, alto, ancho, manzanaExiste, manzanaX, manzanaY, estaViva, stop_threads, gano, puntaje, sigue, banner, teclado
global derecha, izquierda, abajo, arriba
if not gano:
# CONDICIONES PARA QUE SE MUERA LA SERPIENTE
if y in (alto-1, 0) or x in (ancho-1, 0) or (x, y) in snake:
estaViva = False
# CONDICION PARA GANAR
if puntaje+2 == (ancho-2)*(alto-2):
gano = True
space = crear_space(ancho, alto)
# INGRESA LOS PAREDES AL ESPACIO
paredes()
# PONER MANZANA [SI LA MANZANA YA EXISTE SIGUE PONIENDO LA MANZANA EN LA POSCION]
# SI GANA YA NO TIENE QUE PONER MAS MANZANAS
if not gano:
if not manzanaExiste:
manzanaX, manzanaY = poner_manzana()
manzanaExiste = True
else:
space[manzanaY][manzanaX] = Colors.RED+"$"+Colors.RESET
# MOVER LA SERPIENTE
mover_snake(x, y)
# IMPRIME 'X' SI ESTA MUERTO
for i in snake:
space[i[1]][i[0]] = Colors.GREEN+"o"+Colors.RESET
if estaViva:
space[snake[0][1]][snake[0][0]] = Colors.GREEN+"@"+Colors.RESET
else:
space[snake[0][1]][snake[0][0]] = Colors.RED + "X" + Colors.RESET
# DETECTAR LA MANZANA Y LA CABEZA
if snake[0] == (manzanaX, manzanaY):
space[snake[0][1]][snake[0][0]] = Colors.GREEN+"C"+Colors.RESET
crecer_snake(x, y)
puntaje += 1
manzanaExiste = False
if not estaViva:
banner = Colors.BLUE+bannerPerder + Colors.RESET
else:
# ESTO ES CUANDO GANA
banner = Colors.BLUE+bannerGanar + Colors.RESET
space = crear_space(ancho, alto)
for i in snake:
space[i[1]][i[0]] = Colors.RED+"o"+Colors.RESET
space[snake[0][1]][snake[0][0]] = Colors.RED+"@"+Colors.RESET
if len(snake) != 1:
snake.pop()
else:
stop_threads = True
sigue = False
time.sleep(0.25)
clear_console()
kb.on_press(mostrar_key)
print(banner)
mostrar()
print("Head = x:", x, "y: ", y)
print("Score:", puntaje)
print("Keyboard:", teclado)
print("Snake:", snake)
if not gano:
time.sleep(0.5)
if derecha:
x += 1
if izquierda:
x -= 1
if abajo:
y += 1
if arriba:
y -= 1
def comenzar():
while not stop_threads:
update_screen()
if not estaViva:
print("YOU LOSE!")
elif gano:
print("YOU WIN!!")
print("END!")
bannerPerder = """
** ** ******* ** ** ** ******* ******** ********
//** ** **/////** /** /** /** **/////** **////// /**/////
//**** ** //**/** /** /** ** //**/** /**
//** /** /**/** /** /** /** /**/*********/*******
/** /** /**/** /** /** /** /**////////**/**////
/** //** ** /** /** /** //** ** /**/**
/** //******* //******* /******** //******* ******** /********
// /////// /////// //////// /////// //////// ////////
"""
bannerGanar = """
** ** ******* ** ** ** ** ** **** **
//** ** **/////** /** /** /** /**/**/**/** /**
//**** ** //**/** /** /** * /**/**/**//** /**
//** /** /**/** /** /** *** /**/**/** //** /**
/** /** /**/** /** /** **/**/**/**/** //**/**
/** //** ** /** /** /**** //****/**/** //****
/** //******* //******* /**/ ///**/**/** //***
// /////// /////// // // // // ///
"""
bannerSnake = Colors.BLUE + """
******** **** ** ** ** ** ********
**////// /**/** /** **** /** ** /**/////
/** /**//** /** **//** /** ** /**
/*********/** //** /** ** //** /**** /*******
////////**/** //**/** **********/**/** /**////
/**/** //****/**//////**/**//** /**
******** /** //***/** /**/** //**/********
//////// // /// // // // // ////////
""" + Colors.RESET
banner = ""
def main():
global x, y, space, snake, alto, ancho, manzanaExiste, manzanaX, manzanaY, estaViva, stop_threads, gano, puntaje, sigue, banner, teclado
global derecha, izquierda, abajo, arriba
print("\t[\tEnter the dimensions of the grid\t]")
while True:
ancho = int(input("Write the width: "))+2
alto = int(input("Write the height: "))+2
if ancho > 3 and alto > 3:
break
else:
print(Colors.RED + "\tERROR: Enter a value greater than 1" + Colors.RESET)
estaViva = True
gano = False
stop_threads = False
manzanaExiste = False
derecha, izquierda, abajo, arriba = True, False, False, False
sigue = True
banner = bannerSnake
puntaje = 0
x, y = 1, 1
snake = [(0, 0), (0, 0)]
space = crear_space(ancho, alto)
teclado = ""
thread = threading.Thread(target=comenzar)
thread.start()
try:
while sigue:
if kb.is_pressed("right"):
derecha, izquierda, abajo, arriba = True, False, False, False
if kb.is_pressed("left"):
derecha, izquierda, abajo, arriba = False, True, False, False
if kb.is_pressed("down"):
derecha, izquierda, abajo, arriba = False, False, True, False
if kb.is_pressed("up"):
derecha, izquierda, abajo, arriba = False, False, False, True
if kb.is_pressed("esc"):
stop_threads = True
break
if not estaViva:
stop_threads = True
break
except KeyboardInterrupt:
stop_threads = True
thread.join()
if __name__ == "__main__":
print(Colors.GREEN + """
__
_______ /*_>-<
___/ _____ \__/ /
<____/ \____/
""" + Colors.RESET)
print(Colors.CYAN + "Welcome to Snake Console" + Colors.RESET)
print(Colors.CYAN + "Author: Wil-Go" + Colors.RESET)
iniciado = True
while iniciado:
main()
while True:
try:
print(Colors.CYAN +
"Press 'esc' to exit or 'space' to restart"+Colors.RESET)
event = kb.read_event()
if event.name == 'esc':
iniciado = False
break
elif event.name == 'space':
clear_console()
print("Lets start again!")
break
else:
print("Press a valid input!!")
except KeyboardInterrupt:
iniciado = False
break
print("bye")
time.sleep(1)