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 random
import Flower
import Beehive
import Frames
@ -77,65 +78,122 @@ dis = pygame.display.set_mode((disX, disY))
clock = pygame.time.Clock()
#position of the bee and pos changings
XChanges = 0
XPosBee = 64
bee_x = 1
bee_y = 0
bee_dir = 'west'
YChanges = 0
YPosBee = 64
# tall grass counter
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:
# check for events
for event in pygame.event.get():
#check to quit
if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
# game_over = True
if event.type == pygame.QUIT:
pygame.quit()
quit()
#printing map
for i in range(len(tilemap)):
for j in range(len(tilemap[i])):
dis.blit(tiles[tilemap[i][j]], (j * tileSize, i * tileSize))
# get next node in path if bee has reached current node
if bee_x == path[current_node][0] and bee_y == path[current_node][1]:
current_node += 1
if current_node >= len(path):
current_node = 0
#movement speed
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
XChanges = 0
YChanges = -16
if event.key == pygame.K_DOWN:
XChanges = 0
YChanges = 16
if event.key == pygame.K_LEFT:
XChanges = -16
YChanges = 0
if event.key == pygame.K_RIGHT:
XChanges = 16
YChanges = 0
#stopmovement when a key is up
if event.type == pygame.KEYUP:
XChanges = 0
YChanges = 0
#calculating position of the bee
if disX >= XPosBee >= 0:
XPosBee = XPosBee + XChanges
if disY >= YPosBee >= 0:
YPosBee = YPosBee + YChanges
# move bee towards next node in path
next_node = path[current_node]
if bee_x < next_node[0]:
bee_x += 1
bee_dir = 'east'
print(bee_x, bee_y)
elif bee_x > next_node[0]:
bee_x -= 1
bee_dir = 'west'
print(bee_x, bee_y)
elif bee_y < next_node[1]:
bee_y += 1
bee_dir = 'south'
print(bee_x, bee_y)
elif bee_y > next_node[1]:
bee_y -= 1
bee_dir = 'north'
print(bee_x, bee_y)
# check for collision with the screen edges
if bee_x < 0:
bee_x = 0
bee_dir = 'east'
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
if XPosBee <= 0:
XPosBee = 0
elif XPosBee >= disX:
XPosBee = disX
elif YPosBee <= 0:
YPosBee = 0
elif YPosBee >= disY:
YPosBee = disY
# render the bee and the tilemap
dis.fill(white)
for y in range(tilemapSizeY):
for x in range(tilemapSizeX):
tile = tilemap[y][x]
dis.blit(tiles[tile], (x * tileSize, y * tileSize))
#beatiful bee
dis.blit(BEE, (XPosBee, YPosBee))
# rotate and render the bee
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
# pygame.display.flip()
dis.blit(bee_image, (bee_x * tileSize, bee_y * tileSize))
pygame.display.update()
clock.tick(10000)
clock.tick(1)