-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze_game.py
More file actions
106 lines (85 loc) · 3.45 KB
/
Copy pathMaze_game.py
File metadata and controls
106 lines (85 loc) · 3.45 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
import os
import random
from readchar import readchar
POS_X = 0
POS_Y = 1
NUM_MAP_OBJECTS = 11
obstacle_definition = """\
############ ########
## ##
## ##
############ ################## ###
############ #######
## ################## ###
## ##
####### ##
############### ########
"""
my_position = [8, 8] # [x, y]
tail = []
map_objects = []
tail_length = 0
end_game = False
died = False
# Convertir a lista de listas y normalizar filas
rows = obstacle_definition.split("\n")
MAP_WIDTH = max(len(row) for row in rows)
MAP_HEIGHT = len(rows)
obstacle_definition = [list(row.ljust(MAP_WIDTH)) for row in rows]
while not end_game:
os.system("cls" if os.name == "nt" else "clear")
# Crear objetos en el mapa
while len(map_objects) < NUM_MAP_OBJECTS:
new_position = [random.randint(0, MAP_WIDTH - 1), random.randint(0, MAP_HEIGHT - 1)]
if (new_position not in map_objects and
new_position != my_position and
obstacle_definition[new_position[POS_Y]][new_position[POS_X]] != "#"):
map_objects.append(new_position)
print("+" + "-" * (MAP_WIDTH * 2) + "+")
for y in range(MAP_HEIGHT):
print("|", end="")
for x in range(MAP_WIDTH):
char_to_draw = " "
object_in_cell = None
tail_in_cell = None
for item in map_objects:
if item[POS_X] == x and item[POS_Y] == y:
char_to_draw = "* "
object_in_cell = item
for tail_piece in tail:
if tail_piece[POS_X] == x and tail_piece[POS_Y] == y:
char_to_draw = "o "
tail_in_cell = tail_piece
if my_position[POS_X] == x and my_position[POS_Y] == y:
char_to_draw = "@ "
if object_in_cell:
map_objects.remove(object_in_cell)
tail_length += 1
if tail_in_cell:
end_game = True
died = True
if obstacle_definition[y][x] == "#":
char_to_draw = "XX"
print(char_to_draw, end="")
print("|")
print("+" + "-" * (MAP_WIDTH * 2) + "+")
print("Tamaño de la cola: {}".format(tail_length))
direction = readchar()
new_position = None
if direction == "w":
new_position = [my_position[POS_X], (my_position[POS_Y] - 1) % MAP_HEIGHT]
elif direction == "s":
new_position = [my_position[POS_X], (my_position[POS_Y] + 1) % MAP_HEIGHT]
elif direction == "a":
new_position = [(my_position[POS_X] - 1) % MAP_WIDTH, my_position[POS_Y]]
elif direction == "d":
new_position = [(my_position[POS_X] + 1) % MAP_WIDTH, my_position[POS_Y]]
elif direction == "q":
end_game = True
if new_position:
if obstacle_definition[new_position[POS_Y]][new_position[POS_X]] != "#":
tail.insert(0, my_position.copy())
tail = tail[:tail_length]
my_position = new_position
if died:
print("Has muerto porque te has mordido la cola de {} metros".format(tail_length))