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"?>
<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" />
</project>

113
main.py
View File

@ -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)