bfs added

This commit is contained in:
s473594 2023-04-21 02:23:27 +02:00
parent 60c12596af
commit 40b5ce787d
2 changed files with 108 additions and 50 deletions

View File

@ -1 +1 @@
pygame=2.3.0 pygame~=2.3.0

View File

@ -1,4 +1,5 @@
import pygame import pygame
import random
import Flower import Flower
import Beehive import Beehive
import Frames import Frames
@ -77,65 +78,122 @@ dis = pygame.display.set_mode((disX, disY))
clock = pygame.time.Clock() clock = pygame.time.Clock()
#position of the bee and pos changings #position of the bee and pos changings
XChanges = 0 bee_x = 1
XPosBee = 64 bee_y = 0
bee_dir = 'west'
YChanges = 0 # tall grass counter
YPosBee = 64 tall_grass_counter = 0
def createGraph(tilemap):
graph = {}
for y in range(tilemapSizeY):
for x in range(tilemapSizeX):
if tilemap[y][x] != TREE:
node = {}
if y > 0 and tilemap[y-1][x] != TREE:
node[(x,y-1)] = 1
if y < tilemapSizeY-1 and tilemap[y+1][x] != TREE:
node[(x,y+1)] = 1
if x > 0 and tilemap[y][x-1] != TREE:
node[(x-1,y)] = 1
if x < tilemapSizeX-1 and tilemap[y][x+1] != TREE:
node[(x+1,y)] = 1
graph[(x,y)] = node
return graph
graph = createGraph(tilemap)
def bfs(graph, start, end):
queue = [(start, [start])]
visited = set()
while queue:
(node, path) = queue.pop(0)
if node == end:
return path
if node not in visited:
visited.add(node)
for neighbor in graph[node]:
if neighbor not in visited and neighbor not in path and tilemap[neighbor[1]][neighbor[0]] != TREE:
queue.append((neighbor, path + [neighbor]))
return None
random_x = random.randint(0, tilemapSizeX-1)
random_y = random.randint(0, tilemapSizeY-1)
print(random_x, random_y)
path = bfs(graph, (bee_x, bee_y), (random_x, random_y))
# set starting node to first node in path
current_node = 0
while True: while True:
# check for events
for event in pygame.event.get(): for event in pygame.event.get():
#check to quit if event.type == pygame.QUIT:
if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
# game_over = True
pygame.quit() pygame.quit()
quit() quit()
#printing map # get next node in path if bee has reached current node
for i in range(len(tilemap)): if bee_x == path[current_node][0] and bee_y == path[current_node][1]:
for j in range(len(tilemap[i])): current_node += 1
dis.blit(tiles[tilemap[i][j]], (j * tileSize, i * tileSize)) if current_node >= len(path):
current_node = 0
#movement speed # move bee towards next node in path
if event.type == pygame.KEYDOWN: next_node = path[current_node]
if event.key == pygame.K_UP: if bee_x < next_node[0]:
XChanges = 0 bee_x += 1
YChanges = -16 bee_dir = 'east'
if event.key == pygame.K_DOWN: print(bee_x, bee_y)
XChanges = 0 elif bee_x > next_node[0]:
YChanges = 16 bee_x -= 1
if event.key == pygame.K_LEFT: bee_dir = 'west'
XChanges = -16 print(bee_x, bee_y)
YChanges = 0 elif bee_y < next_node[1]:
if event.key == pygame.K_RIGHT: bee_y += 1
XChanges = 16 bee_dir = 'south'
YChanges = 0 print(bee_x, bee_y)
#stopmovement when a key is up elif bee_y > next_node[1]:
if event.type == pygame.KEYUP: bee_y -= 1
XChanges = 0 bee_dir = 'north'
YChanges = 0 print(bee_x, bee_y)
#calculating position of the bee # check for collision with the screen edges
if disX >= XPosBee >= 0: if bee_x < 0:
XPosBee = XPosBee + XChanges bee_x = 0
if disY >= YPosBee >= 0: bee_dir = 'east'
YPosBee = YPosBee + YChanges elif bee_x >= tilemapSizeX:
bee_x = tilemapSizeX - 1
bee_dir = 'west'
elif bee_y < 0:
bee_y = 0
bee_dir = 'south'
elif bee_y >= tilemapSizeY:
bee_y = tilemapSizeY - 1
bee_dir = 'north'
#left and up restriction # render the bee and the tilemap
if XPosBee <= 0: dis.fill(white)
XPosBee = 0 for y in range(tilemapSizeY):
elif XPosBee >= disX: for x in range(tilemapSizeX):
XPosBee = disX tile = tilemap[y][x]
elif YPosBee <= 0: dis.blit(tiles[tile], (x * tileSize, y * tileSize))
YPosBee = 0
elif YPosBee >= disY:
YPosBee = disY
#beatiful bee # rotate and render the bee
dis.blit(BEE, (XPosBee, YPosBee)) if bee_dir == 'west':
bee_image = BEE
elif bee_dir == 'east':
bee_image = pygame.transform.rotate(BEE, 180)
elif bee_dir == 'north':
bee_image = pygame.transform.rotate(BEE, -90)
elif bee_dir == 'south':
bee_image = pygame.transform.rotate(BEE, 90)
#updating dis.blit(bee_image, (bee_x * tileSize, bee_y * tileSize))
# pygame.display.flip()
pygame.display.update() pygame.display.update()
clock.tick(10000)
clock.tick(1)