This commit is contained in:
s473603 2023-04-21 16:02:18 +02:00
parent ca3df94bce
commit 476944a6df
3 changed files with 27 additions and 30 deletions

View File

@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4"> <module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager"> <component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" /> <content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.9" jdkType="Python SDK" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10" project-jdk-type="Python SDK" />
</project> </project>

View File

@ -72,7 +72,6 @@ def draw_interface():
startpoint = (0, 0) startpoint = (0, 0)
endpoint = startpoint endpoint = startpoint
fl_running = True fl_running = True
while fl_running: while fl_running:
draw_grid() draw_grid()
@ -98,9 +97,8 @@ def draw_interface():
if (tractor.x, tractor.y) == SPAWN_POINT: if (tractor.x, tractor.y) == SPAWN_POINT:
tractor.water = WATER_TANK_CAPACITY tractor.water = WATER_TANK_CAPACITY
tractor.gas = GAS_TANK_CAPACITY tractor.gas = GAS_TANK_CAPACITY
elif event.type == pygame.MOUSEWHEEL:
print(1)
elif event.type == pygame.MOUSEBUTTONDOWN: elif event.type == pygame.MOUSEBUTTONDOWN:
startpoint = (tractor.x, tractor.y)
endpoint = get_click_mouse_pos() endpoint = get_click_mouse_pos()
print(endpoint) print(endpoint)
a = graph1.dijkstra(startpoint, endpoint) a = graph1.dijkstra(startpoint, endpoint)
@ -208,9 +206,11 @@ class Grid:
i -= 1 i -= 1
def get_next_nodes(self, x, y): def get_next_nodes(self, x, y):
check_next_node = lambda x, y: True if 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE and (self.grid[x][y] != types.ROCK) else False check_next_node = lambda x, y: True if 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE and (
self.grid[x][y] != types.ROCK) else False
ways = [-1, 0], [1, 0], [0, -1], [0, 1] ways = [-1, 0], [1, 0], [0, -1], [0, 1]
return [(1 if self.grid[x][y] != types.ROCK else 5, (x + dx, y + dy)) for dx, dy in ways if check_next_node(x + dx, y + dy)] return [(1 if self.grid[x][y] != types.ROCK else 5, (x + dx, y + dy)) for dx, dy in ways if
check_next_node(x + dx, y + dy)]
class Graph: class Graph:
@ -221,11 +221,11 @@ class Graph:
def initialize_graph(self, grid: Grid): def initialize_graph(self, grid: Grid):
for y, row in enumerate(grid.grid): for y, row in enumerate(grid.grid):
for x, col in enumerate(row): for x, col in enumerate(row):
if col != types.ROCK: self.graph[(x, y)] = self.graph.get((x, y), []) + grid.get_next_nodes(x, y)
self.graph[(x, y)] = self.graph.get((x, y), []) + grid.get_next_nodes(x, y)
def dijkstra(self, start, goal): def dijkstra(self, start, goal):
#not finished yet https://www.youtube.com/watch?v=abHftC1GU6w # not finished yet https://www.youtube.com/watch?v=abHftC1GU6w
queue = [] queue = []
heappush(queue, (0, start)) heappush(queue, (0, start))
cost_visited = {start: 0} cost_visited = {start: 0}
@ -249,6 +249,7 @@ class Graph:
# print(visited) # print(visited)
return visited return visited
class Tractor: class Tractor:
def __init__(self, x, y, direction: Direction): def __init__(self, x, y, direction: Direction):
self.x = x self.x = x
@ -286,36 +287,38 @@ class Tractor:
return return
def movement(traktor:Tractor, grid:Grid, road): def movement(traktor: Tractor, grid: Grid, road):
n = len(road) n = len(road)
for i in range(n-1): for i in range(n - 1):
if road[i][0]!=road[i+1][0]: time.sleep(0.3)
if road[i][0]>road[i+1][0]: if road[i][0] != road[i + 1][0]:
if traktor.direction!=3: if road[i][0] > road[i + 1][0]:
while traktor.direction!=3: if traktor.direction != 3:
while traktor.direction != 3:
traktor.rot_center(Direction.LEFT) traktor.rot_center(Direction.LEFT)
traktor.move(grid=grid) traktor.move(grid=grid)
print("move left") print("move left")
elif road[i][0]<road[i+1][0]: elif road[i][0] < road[i + 1][0]:
if traktor.direction!=1: if traktor.direction != 1:
while traktor.direction!=1: while traktor.direction != 1:
traktor.rot_center(Direction.LEFT) traktor.rot_center(Direction.LEFT)
traktor.move(grid=grid) traktor.move(grid=grid)
print("move right") print("move right")
else: else:
if road[i][1] > road[i + 1][1]: if road[i][1] > road[i + 1][1]:
if traktor.direction!=0: if traktor.direction != 0:
while traktor.direction!=0: while traktor.direction != 0:
traktor.rot_center(Direction.LEFT) traktor.rot_center(Direction.LEFT)
traktor.move(grid=grid) traktor.move(grid=grid)
print("move up") print("move up")
elif road[i][1] < road[i + 1][1]: elif road[i][1] < road[i + 1][1]:
if traktor.direction!=2: if traktor.direction != 2:
while traktor.direction!=2: while traktor.direction != 2:
traktor.rot_center(Direction.LEFT) traktor.rot_center(Direction.LEFT)
traktor.move(grid=grid) traktor.move(grid=grid)
print("move down") print("move down")
def getRoad(start, goal, visited): def getRoad(start, goal, visited):
arr = [] arr = []
aFrom = goal aFrom = goal
@ -323,7 +326,7 @@ def getRoad(start, goal, visited):
arr.append(aFrom) arr.append(aFrom)
aFrom = visited[aFrom] aFrom = visited[aFrom]
arr.append(start) arr.append(start)
brr=arr[::-1] brr = arr[::-1]
return brr return brr
# grid = Grid(BOARD_SIZE, BOARD_SIZE, BLOCK_SIZE) # grid = Grid(BOARD_SIZE, BOARD_SIZE, BLOCK_SIZE)
@ -336,9 +339,3 @@ def getRoad(start, goal, visited):
# a = graph1.dijkstra(startpoint, endpoint) # a = graph1.dijkstra(startpoint, endpoint)
# b = getRoad(startpoint, endpoint, a) # b = getRoad(startpoint, endpoint, a)
# print(b) # print(b)