demo ruch

This commit is contained in:
s464923 2024-04-20 02:03:42 +02:00
parent 1315b33894
commit fb4cd44b61
2 changed files with 62 additions and 54 deletions

View File

@ -1,4 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.9 (traktor)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (traktor)" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (traktor)" project-jdk-type="Python SDK" />
</project> </project>

49
main.py
View File

@ -45,13 +45,13 @@ def graphsearch(istate, goaltest, board):
def main(): def main():
rotation = ["left", "up", "right", "down"] rotation = ["left", "up", "right", "down"]
istate = Stan(4,4, "down") initial_state = Stan(4,4, "down")
goaltest = Stan(1,1, "up") goaltest = Stan(1,1, "up")
run = True run = True
clock = pygame.time.Clock() clock = pygame.time.Clock()
board = Board() board = Board()
board.load_images() board.load_images()
actions = graphsearch(istate, goaltest, board) actions = graphsearch(initial_state, goaltest, board)
print("akcje: >",actions ) print("akcje: >",actions )
tractor = Tractor(4, 4) tractor = Tractor(4, 4)
while run: while run:
@ -61,9 +61,27 @@ def main():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
run = False run = False
keys = pygame.key.get_pressed() if actions:
action = actions.pop() # Pobierz kolejną akcję z listy
if keys[pygame.K_UP]: 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 (tractor.direction == "up" and tractor.row > 0):
if board.is_weed(tractor.col, tractor.row - 1): if board.is_weed(tractor.col, tractor.row - 1):
board.set_grass(tractor.col, tractor.row - 1) board.set_grass(tractor.col, tractor.row - 1)
@ -73,7 +91,7 @@ def main():
tractor.row -= 1 tractor.row -= 1
elif not board.is_rock(tractor.col, tractor.row - 1): elif not board.is_rock(tractor.col, tractor.row - 1):
tractor.row -= 1 tractor.row -= 1
if(tractor.direction == "left" and tractor.col > 0): elif (tractor.direction == "left" and tractor.col > 0):
if board.is_weed(tractor.col - 1, tractor.row): if board.is_weed(tractor.col - 1, tractor.row):
board.set_grass(tractor.col - 1, tractor.row) board.set_grass(tractor.col - 1, tractor.row)
tractor.col -= 1 tractor.col -= 1
@ -82,7 +100,7 @@ def main():
tractor.col -= 1 tractor.col -= 1
elif not board.is_rock(tractor.col - 1, tractor.row): elif not board.is_rock(tractor.col - 1, tractor.row):
tractor.col -= 1 tractor.col -= 1
if(tractor.direction == "down" and tractor.row < rows - 1): elif (tractor.direction == "down" and tractor.row < rows - 1):
if board.is_weed(tractor.col, tractor.row + 1): if board.is_weed(tractor.col, tractor.row + 1):
board.set_grass(tractor.col, tractor.row + 1) board.set_grass(tractor.col, tractor.row + 1)
tractor.row += 1 tractor.row += 1
@ -91,7 +109,7 @@ def main():
tractor.row += 1 tractor.row += 1
elif not board.is_rock(tractor.col, tractor.row + 1): elif not board.is_rock(tractor.col, tractor.row + 1):
tractor.row += 1 tractor.row += 1
if(tractor.direction == "right" and tractor.col < cols - 1): elif (tractor.direction == "right" and tractor.col < cols - 1):
if board.is_weed(tractor.col + 1, tractor.row): if board.is_weed(tractor.col + 1, tractor.row):
board.set_grass(tractor.col + 1, tractor.row) board.set_grass(tractor.col + 1, tractor.row)
tractor.col += 1 tractor.col += 1
@ -100,20 +118,7 @@ def main():
tractor.col += 1 tractor.col += 1
elif not board.is_rock(tractor.col + 1, tractor.row): elif not board.is_rock(tractor.col + 1, tractor.row):
tractor.col += 1 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) board.draw_cubes(WIN)
tractor.draw(WIN) tractor.draw(WIN)