From fb4cd44b61817332f8098134a71b735faf90ff3c Mon Sep 17 00:00:00 2001 From: s464923 Date: Sat, 20 Apr 2024 02:03:42 +0200 Subject: [PATCH] demo ruch --- .idea/misc.xml | 3 ++ main.py | 113 ++++++++++++++++++++++++++----------------------- 2 files changed, 62 insertions(+), 54 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index e5c1b90a2..ff140041e 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,7 @@ + + \ No newline at end of file diff --git a/main.py b/main.py index 1925cdfcc..dfb08ff1e 100644 --- a/main.py +++ b/main.py @@ -45,13 +45,13 @@ def graphsearch(istate, goaltest, board): def main(): rotation = ["left", "up", "right", "down"] - istate = Stan(4,4, "down") + initial_state = Stan(4,4, "down") goaltest = Stan(1,1, "up") run = True clock = pygame.time.Clock() board = Board() board.load_images() - actions = graphsearch(istate, goaltest, board) + actions = graphsearch(initial_state, goaltest, board) print("akcje: >",actions ) tractor = Tractor(4, 4) while run: @@ -61,59 +61,64 @@ def main(): if event.type == pygame.QUIT: run = False - keys = pygame.key.get_pressed() + if actions: + action = actions.pop() # Pobierz kolejną akcję z listy + if action == "left": + if tractor.direction == "up": + tractor.direction = "left" + elif tractor.direction == "down": + tractor.direction = "right" + elif tractor.direction == "left": + tractor.direction = "down" + elif tractor.direction == "right": + tractor.direction = "up" + elif action == "right": + if tractor.direction == "up": + tractor.direction = "right" + elif tractor.direction == "down": + tractor.direction = "left" + elif tractor.direction == "left": + tractor.direction = "up" + elif tractor.direction == "right": + tractor.direction = "down" + elif action == "up": + if (tractor.direction == "up" and tractor.row > 0): + if board.is_weed(tractor.col, tractor.row - 1): + board.set_grass(tractor.col, tractor.row - 1) + tractor.row -= 1 + elif board.is_dirt(tractor.col, tractor.row - 1): + board.set_soil(tractor.col, tractor.row - 1) + tractor.row -= 1 + elif not board.is_rock(tractor.col, tractor.row - 1): + tractor.row -= 1 + elif (tractor.direction == "left" and tractor.col > 0): + if board.is_weed(tractor.col - 1, tractor.row): + board.set_grass(tractor.col - 1, tractor.row) + tractor.col -= 1 + elif board.is_dirt(tractor.col - 1, tractor.row): + board.set_soil(tractor.col - 1, tractor.row) + tractor.col -= 1 + elif not board.is_rock(tractor.col - 1, tractor.row): + tractor.col -= 1 + elif (tractor.direction == "down" and tractor.row < rows - 1): + if board.is_weed(tractor.col, tractor.row + 1): + board.set_grass(tractor.col, tractor.row + 1) + tractor.row += 1 + elif board.is_dirt(tractor.col, tractor.row + 1): + board.set_soil(tractor.col, tractor.row + 1) + tractor.row += 1 + elif not board.is_rock(tractor.col, tractor.row + 1): + tractor.row += 1 + elif (tractor.direction == "right" and tractor.col < cols - 1): + if board.is_weed(tractor.col + 1, tractor.row): + board.set_grass(tractor.col + 1, tractor.row) + tractor.col += 1 + elif board.is_dirt(tractor.col + 1, tractor.row): + board.set_soil(tractor.col + 1, tractor.row) + tractor.col += 1 + elif not board.is_rock(tractor.col + 1, tractor.row): + tractor.col += 1 - if keys[pygame.K_UP]: - if(tractor.direction == "up" and tractor.row > 0 ): - if board.is_weed(tractor.col, tractor.row - 1): - board.set_grass(tractor.col, tractor.row - 1) - tractor.row -= 1 - elif board.is_dirt(tractor.col, tractor.row - 1): - board.set_soil(tractor.col, tractor.row - 1) - tractor.row -= 1 - elif not board.is_rock(tractor.col, tractor.row - 1): - tractor.row -= 1 - if(tractor.direction == "left" and tractor.col > 0): - if board.is_weed(tractor.col - 1, tractor.row): - board.set_grass(tractor.col - 1, tractor.row) - tractor.col -= 1 - elif board.is_dirt(tractor.col - 1, tractor.row): - board.set_soil(tractor.col - 1, tractor.row) - tractor.col -= 1 - elif not board.is_rock(tractor.col - 1, tractor.row): - tractor.col -= 1 - if(tractor.direction == "down" and tractor.row < rows - 1): - if board.is_weed(tractor.col, tractor.row + 1): - board.set_grass(tractor.col, tractor.row + 1) - tractor.row += 1 - elif board.is_dirt(tractor.col, tractor.row + 1): - board.set_soil(tractor.col, tractor.row + 1) - tractor.row += 1 - elif not board.is_rock(tractor.col, tractor.row + 1): - tractor.row += 1 - if(tractor.direction == "right" and tractor.col < cols - 1): - if board.is_weed(tractor.col + 1, tractor.row): - board.set_grass(tractor.col + 1, tractor.row) - tractor.col += 1 - elif board.is_dirt(tractor.col + 1, tractor.row): - board.set_soil(tractor.col + 1, tractor.row) - tractor.col += 1 - elif not board.is_rock(tractor.col + 1, tractor.row): - tractor.col += 1 - if keys[pygame.K_LEFT]: - for i in range(0, 4): - if(tractor.direction == rotation[i]): - if(i == 0): - i = 4 - tractor.direction = rotation[i-1] - break - if keys[pygame.K_RIGHT]: - for i in range(0, 4): - if(tractor.direction == rotation[i]): - if(i == 3): - i = -1 - tractor.direction = rotation[i+1] - break board.draw_cubes(WIN) tractor.draw(WIN)