added neighbours list for bfs and fields to search #14
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
51
bfs.py
Normal file
51
bfs.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import settings
|
||||||
|
import math
|
||||||
|
|
||||||
|
|
||||||
|
def generate_neighbours_list():
|
||||||
|
width = settings.Field.horizontal_count()
|
||||||
|
height = settings.Field.vertical_count()
|
||||||
|
keys = [width for width in range(height * width)]
|
||||||
|
neighbours_list = {key: [] for key in keys}
|
||||||
|
for i in range(height):
|
||||||
|
for j in range(width):
|
||||||
|
point = i * width + j
|
||||||
|
# point_row = math.floor(point/width)
|
||||||
|
# point_column = point % width
|
||||||
|
point_left = i * width + j - 1
|
||||||
|
point_left_column = point_left - width * i
|
||||||
|
point_right = i * width + j + 1
|
||||||
|
point_right_column = point_right % width
|
||||||
|
point_up = (i - 1) * width + j
|
||||||
|
point_up_row = math.floor(point_up / width)
|
||||||
|
point_down = (i + 1) * width + j
|
||||||
|
point_down_row = math.floor(point_down / width)
|
||||||
|
if point_left_column >= 0:
|
||||||
|
neighbours_list[point].append(point_left)
|
||||||
|
if point_right_column > 0:
|
||||||
|
neighbours_list[point].append(point_right)
|
||||||
|
if point_up_row >= 0:
|
||||||
|
neighbours_list[point].append(point_up)
|
||||||
|
if point_down_row < height:
|
||||||
|
neighbours_list[point].append(point_down)
|
||||||
|
return neighbours_list
|
||||||
|
|
||||||
|
|
||||||
|
def generate_fields_lists(fields):
|
||||||
|
width = settings.Field.horizontal_count()
|
||||||
|
height = settings.Field.vertical_count()
|
||||||
|
fields_to_visit = []
|
||||||
|
for i in range(height):
|
||||||
|
for j in range(width):
|
||||||
|
point = i * width + j
|
||||||
|
if fields[j][i] == 'dirt':
|
||||||
|
fields_to_visit.append(point)
|
||||||
|
return fields_to_visit
|
||||||
|
|
||||||
|
|
||||||
|
def generate_all(fields):
|
||||||
|
neighbours_list = generate_neighbours_list()
|
||||||
|
fields_to_visit = generate_fields_lists(fields)
|
||||||
|
print(neighbours_list)
|
||||||
|
print(fields_to_visit)
|
||||||
|
|
17
main.py
17
main.py
@ -1,6 +1,6 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import random
|
import random
|
||||||
|
import bfs
|
||||||
import field
|
import field
|
||||||
import settings
|
import settings
|
||||||
import common
|
import common
|
||||||
@ -21,21 +21,25 @@ def randomize_map():
|
|||||||
fields_array = []
|
fields_array = []
|
||||||
for i in possibleFields:
|
for i in possibleFields:
|
||||||
fields_array.append(possibleFields[i].tile.object)
|
fields_array.append(possibleFields[i].tile.object)
|
||||||
|
|
||||||
field_array_big = []
|
field_array_big = []
|
||||||
field_array_small = []
|
field_array_small = []
|
||||||
|
field_array_big_2 = []
|
||||||
|
field_array_small_2 = []
|
||||||
for i in range(settings.Field.horizontal_count()):
|
for i in range(settings.Field.horizontal_count()):
|
||||||
for j in range(settings.Field.vertical_count()):
|
for j in range(settings.Field.vertical_count()):
|
||||||
field_array_small.append(random.choice(fields_array))
|
k = random.choice(list(possibleFields.keys()))
|
||||||
|
field_array_small.append(possibleFields[k].tile.object)
|
||||||
|
field_array_small_2.append(k)
|
||||||
field_array_big.append(field_array_small)
|
field_array_big.append(field_array_small)
|
||||||
|
field_array_big_2.append(field_array_small_2)
|
||||||
field_array_small = []
|
field_array_small = []
|
||||||
return field_array_big
|
field_array_small_2 = []
|
||||||
|
return field_array_big, field_array_big_2
|
||||||
|
|
||||||
|
|
||||||
def read_img(agent, fields):
|
def read_img(agent, fields):
|
||||||
window = common.get('window')
|
window = common.get('window')
|
||||||
current_field = fields[agent.x()][agent.y()]
|
current_field = fields[agent.x()][agent.y()]
|
||||||
|
|
||||||
if current_field == possibleFields['grass'].tile.object:
|
if current_field == possibleFields['grass'].tile.object:
|
||||||
window.blit(possibleFields['grass'].block.object, (0, 0))
|
window.blit(possibleFields['grass'].block.object, (0, 0))
|
||||||
elif current_field == possibleFields['dirt'].tile.object:
|
elif current_field == possibleFields['dirt'].tile.object:
|
||||||
@ -80,7 +84,8 @@ def main():
|
|||||||
|
|
||||||
pygame.display.set_caption(settings.Pygame.display_name())
|
pygame.display.set_caption(settings.Pygame.display_name())
|
||||||
|
|
||||||
fields = randomize_map()
|
fields, fields_2 = randomize_map()
|
||||||
|
bfs.generate_all(fields_2)
|
||||||
|
|
||||||
while common.get('game_running'):
|
while common.get('game_running'):
|
||||||
pygame.time.Clock().tick(settings.Pygame.fps())
|
pygame.time.Clock().tick(settings.Pygame.fps())
|
||||||
|
Loading…
Reference in New Issue
Block a user