generowanie nastepnikow

This commit is contained in:
Zofia Lorenc 2024-04-15 09:36:04 +02:00
parent f6d9fa609e
commit ac6a8771e6
2 changed files with 71 additions and 9 deletions

View File

@ -36,12 +36,3 @@ class Tile(pygame.sprite.Sprite):
else:
self.image = pygame.image.load("images/grass.png").convert()
self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
'''
def set_type(self, type):
self.type = type
# if self.type == 'grass':
# self.image = pygame.image.load("images/grass.png").convert()
self.image = pygame.image.load("images/grass.png").convert()
self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
'''

View File

@ -3,6 +3,7 @@ import os
from kb import ile_podlac, multi_sasiedzi
from tile import Tile
from config import TILE_SIZE
from collections import deque
class Tractor(pygame.sprite.Sprite):
def __init__(self, field):
@ -74,6 +75,8 @@ class Tractor(pygame.sprite.Sprite):
modifier = multi_sasiedzi(current_tile.type, neighbors)[0]['Mul']
print(f"🌱 the growth modifier for {current_tile.type} on this tile is ~{modifier:.2f} based on its neighbors: {', '.join(neighbors)}")
self.BFS((14,14))
print() # empty line at end of log statement
@ -136,3 +139,71 @@ class Tractor(pygame.sprite.Sprite):
self.image = pygame.image.load('images/tractor/north.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
self.direction = 'north'
def turn_right(self, direction):
directions = ["east", "south", "west", "north"]
current_index = directions.index(direction)
new_index = (current_index + 1) % 4
return directions[new_index]
def turn_left(self, direction):
directions = ["east", "south", "west", "north"]
current_index = directions.index(direction)
new_index = (current_index - 1) % 4
return directions[new_index]
def generate_succesors(self, state):
x, y, direction = state
successors = []
if direction == "east" and x < 15:
successors.append(((x + 1, y, direction), "forward"))
elif direction == "west" and x > 0:
successors.append(((x - 1, y, direction), "forward"))
elif direction == "north" and y > 0:
successors.append(((x, y - 1, direction), "forward"))
elif direction == "south" and y < 15:
successors.append(((x, y + 1, direction), "forward"))
if direction == "east" and y > 0:
successors.append(((x, y, self.turn_left(direction)), "left"))
elif direction == "west" and y < 15:
successors.append(((x, y, self.turn_left(direction)), "left"))
elif direction == "north" and x > 0:
successors.append(((x, y, self.turn_left(direction)), "left"))
elif direction == "south" and x < 15:
successors.append(((x, y, self.turn_left(direction)), "left"))
if direction == "east" and y < 15:
successors.append(((x, y, self.turn_right(direction)), "right"))
elif direction == "west" and y > 0:
successors.append(((x, y, self.turn_right(direction)), "right"))
elif direction == "north" and x < 15:
successors.append(((x, y, self.turn_right(direction)), "right"))
elif direction == "south" and x > 0:
successors.append(((x, y, self.turn_right(direction)), "right"))
return successors
def BFS(self, end):
x = self.rect.x // TILE_SIZE
y = self.rect.y // TILE_SIZE
start = (x, y, self.direction)
fringe = deque()
path = []
fringe.append(start)
while fringe:
if (fringe[0])[0] == end[0] and (fringe[0])[1] == end[1]:
return path
successors = self.generate_succesors(fringe[0])
print(fringe[0])
print(successors, '<-----tutaj następniki')
break