-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap.py
156 lines (137 loc) · 6.74 KB
/
map.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
import pygame
from node import Node
from map_generator import Map_generator
from pathfinder import Pathfinder
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
GREY = (128, 128, 128)
# This sets the WIDTH and HEIGHT of each grid location
WIDTH = 20
HEIGHT = 20
# HOW MANY NODES WE WANT
ROWS = 30
COLUMNS = 30
# This sets the margin between each cell
MARGIN = 5
# Create a 2 dimensional array. A two dimensional
# array is simply a list of lists.
grid = []
map_generator = Map_generator()
pathfinder = Pathfinder()
def init_map():
del grid[:]
for row in range(ROWS):
# Add an empty array that will hold each node
# in this row
grid.append([])
for column in range(COLUMNS):
node = Node()
node.x = row
node.y = column
node.color = "white"
node.name = "My name is: ", node.x, node.y
grid[row].append(node) # Append a cell
pathfinder.update_neighbors(grid)
init_map()
map_generator.generate_map(grid)
# Initialize pygame
pygame.init()
# Set the HEIGHT and WIDTH of the screen
WINDOW_SIZE = [((WIDTH + MARGIN) * ROWS) + MARGIN, ((HEIGHT + MARGIN) * COLUMNS) + MARGIN]
screen = pygame.display.set_mode(WINDOW_SIZE)
# Set title of screen
pygame.display.set_caption("Pathfinding and Random levels")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
pathfinder.clear_color_and_path(grid)
# User clicks the mouse. Get the position
pos = pygame.mouse.get_pos()
# Change the x/y screen coordinates to grid coordinates
column = pos[0] // (WIDTH + MARGIN)
row = pos[1] // (HEIGHT + MARGIN)
start_x = row
start_y = column
if grid[row][column].color == "red":
# print("Grid coordinates: ", row, column, "Node color: ", grid[row][column].color, "Node is passable: ", grid[row][column].passable,
# "Node name is: ", grid[row][column].name, "My neighbors are :", "My neighbors are: ", print_out_neighbor(grid[row][column].neighbor))
next
elif grid[row][column].color != 'grey':
grid[row][column].color = "green"
# print("Grid coordinates: ", row, column, "Node color: ", grid[row][column].color, "Node is passable: ", grid[row][column].passable,
# "Node name is: ", grid[row][column].name, "My neighbors are :", "My neighbors are: ", print_out_neighbor(grid[row][column].neighbor))
elif event.button == 2:
# User clicks the mouse. Get the position
pos = pygame.mouse.get_pos()
# Change the x/y screen coordinates to grid coordinates
column = pos[0] // (WIDTH + MARGIN)
row = pos[1] // (HEIGHT + MARGIN)
if grid[row][column].color == "red":
grid[row][column].color == "white"
# print("Grid coordinates: ", row, column, "Node color: ", grid[row][column].color, "Node is passable: ",grid[row][column].passable, "Node name is: ", grid[row][column].name, "My neighbors are :" , "My neighbors are: ", print_out_neighbor(grid[row][column].neighbor))
elif grid[row][column].color != 'grey':
grid[row][column].color = "red"
grid[row][column].passable = False
# print("Grid coordinates: ", row, column, "Node color: ", grid[row][column].color, "Node is passable: ",grid[row][column].passable, "Node name is: ", grid[row][column].name, "My neighbors are :" , "My neighbors are: ", print_out_neighbor(grid[row][column].neighbor))
elif event.button == 3:
# User clicks the mouse. Get the position
pos = pygame.mouse.get_pos()
# Change the x/y screen coordinates to grid coordinates
column = pos[0] // (WIDTH + MARGIN)
row = pos[1] // (HEIGHT + MARGIN)
end_x = row
end_y = column
# Set that location to one
if grid[row][column].color == "red":
# print("Grid coordinates: ", row, column, "Node color: ", grid[row][column].color, "Node is passable: ", grid[row][column].passable,
# "Node name is: ", grid[row][column].name, "My neighbors are :", "My neighbors are: ", print_out_neighbor(grid[row][column].neighbor))
next
elif grid[row][column].color != 'grey':
grid[row][column].color = "green"
# print("Grid coordinates: ", row, column, "Node color: ", grid[row][column].color, "Node is passable: ", grid[row][column].passable,
# "Node name is: ", grid[row][column].name, "My neighbors are :", "My neighbors are: ", print_out_neighbor(grid[row][column].neighbor))
pathfinder.find_path(grid, start_x, start_y, end_x, end_y)
# Set the screen background
screen.fill(BLACK)
# Draw the grid
for row in range(ROWS):
for column in range(COLUMNS):
color = WHITE
if grid[row][column].color == "green":
color = GREEN
elif grid[row][column].color == "red":
color = RED
elif grid[row][column].color == "blue":
color = BLUE
elif grid[row][column].color == "yellow":
color = YELLOW
elif grid[row][column].color == "white":
color = WHITE
elif grid[row][column].color == "grey":
color = GREY
pygame.draw.rect(screen,
color,
[(MARGIN + WIDTH) * column + MARGIN,
(MARGIN + HEIGHT) * row + MARGIN,
WIDTH,
HEIGHT])
# Limit to 60 frames per second
clock.tick(60)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()