2022-03-09 19:51:09 +01:00
|
|
|
import pygame
|
|
|
|
|
2022-03-22 21:34:50 +01:00
|
|
|
from src.world import World
|
|
|
|
from src.tractor import Tractor
|
|
|
|
from src.settings import Settings
|
2022-05-18 20:05:47 +02:00
|
|
|
from utils.astar import a_star_search
|
|
|
|
|
2022-03-09 19:51:09 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
pygame.init()
|
|
|
|
|
2022-05-18 20:05:47 +02:00
|
|
|
settings = Settings() # ustawienia pygame
|
|
|
|
world = World(settings) # stworzenie mapy na bazie ustawień pygame
|
|
|
|
tractor = Tractor("Spalinowy", "Nawóz 1", settings, 8 * settings.tile_size, 8 * settings.tile_size) # stworzenie traktora z podanymi argumentami
|
|
|
|
obstacles = [tile for tile in world.tiles if tile.type == 'rock'] # stworzenie listy z przeszkodami, kamień = przeszkoda
|
2022-04-06 18:04:23 +02:00
|
|
|
clock = pygame.time.Clock() # FPS purpose
|
2022-03-22 21:34:50 +01:00
|
|
|
|
2022-05-18 20:05:47 +02:00
|
|
|
screen = pygame.display.set_mode((settings.screen_width, settings.screen_height)) # tworzenie ekranu
|
|
|
|
pygame.display.set_caption('TRAKTOHOLIK') # nazwa okna
|
2022-03-09 19:51:09 +01:00
|
|
|
|
2022-04-22 13:00:58 +02:00
|
|
|
start_cords = (8, 1)
|
2022-05-19 17:36:23 +02:00
|
|
|
goals = [(8, 7), (7, 7), (0, 0)]
|
2022-04-22 13:00:58 +02:00
|
|
|
end_cords = goals[0]
|
2022-05-18 20:05:47 +02:00
|
|
|
start_dir = tractor.curr_direction # przypisanie początkowego ustawienia traktora do zmiennej
|
|
|
|
# path = BFSSearcher().search(start_cords, end_cords, start_dir) # wygenerowanie listy ruchów na bazie BFS
|
2022-05-19 17:36:23 +02:00
|
|
|
path = a_star_search(start_cords, end_cords, start_dir, world) # generowanie ścieżki za pomocą A*
|
|
|
|
|
2022-04-20 18:58:43 +02:00
|
|
|
|
2022-03-09 19:51:09 +01:00
|
|
|
run = True
|
|
|
|
while run:
|
2022-04-06 18:04:23 +02:00
|
|
|
clock.tick(settings.fps)
|
2022-03-31 13:13:43 +02:00
|
|
|
world.draw_tiles(screen)
|
|
|
|
world.draw_lines(screen)
|
2022-04-06 18:04:23 +02:00
|
|
|
tractor.draw(screen)
|
2022-03-09 19:51:09 +01:00
|
|
|
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
run = False
|
|
|
|
|
2022-05-18 20:05:47 +02:00
|
|
|
# iteracja przez listę ruchów
|
|
|
|
if path:
|
|
|
|
action = path.pop(0) # pobranie pierwszego ruchu z listy
|
|
|
|
tractor.update(action) # wykonanie ruchu przez traktor
|
|
|
|
else:
|
|
|
|
if len(goals) > 1: # sprawdzenie czy są inne cele
|
|
|
|
new_start = goals.pop(0) # pobierz współrzędne pierwszego celu i ustaw jako początkowe
|
|
|
|
end_cords = goals[0] # ustaw kolejny cel
|
|
|
|
start_dir = tractor.curr_direction # aktualizacja kierunku traktora
|
|
|
|
# path = BFSSearcher().search(start_cord, end_cords, start_dir) # generacja nowej ścieżki
|
2022-05-19 17:36:23 +02:00
|
|
|
path = a_star_search(new_start, end_cords, start_dir, world)
|
2022-04-22 13:00:58 +02:00
|
|
|
|
2022-04-20 18:58:43 +02:00
|
|
|
pygame.time.wait(settings.freeze_time)
|
2022-03-09 19:51:09 +01:00
|
|
|
pygame.display.update()
|
|
|
|
pygame.quit()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-05-16 18:59:54 +02:00
|
|
|
main()
|