Compare commits
6 Commits
3a8a7fdb14
...
e60d18d3f6
Author | SHA1 | Date | |
---|---|---|---|
e60d18d3f6 | |||
eaf7ed46fe | |||
2cb44dcb01 | |||
c5d86faade | |||
24894482e4 | |||
425a9bf3e2 |
@ -6,22 +6,36 @@ class Node:
|
||||
|
||||
|
||||
class Search:
|
||||
def __init__(self, cell_size):
|
||||
def __init__(self, cell_size, cell_number):
|
||||
self.cell_size = cell_size
|
||||
self.cell_number = cell_number
|
||||
|
||||
# WARNING! IT EXCEEDS THE PLANE!!!
|
||||
def succ(self, state): # successor function
|
||||
def succ(self, state):
|
||||
x = state[0]
|
||||
y = state[1]
|
||||
angle = state[2]
|
||||
if angle == 0:
|
||||
angles = {0: 'UP', 90: 'RIGHT', 270: 'LEFT', 180: 'DOWN'}
|
||||
match(angles[angle]):
|
||||
case 'UP':
|
||||
if y != 0:
|
||||
return [['move', x, y - self.cell_size, 0], ['left', x, y, 270], ['right', x, y, 90]]
|
||||
if angle == 90:
|
||||
else:
|
||||
return [['left', x, y, 270], ['right', x, y, 90]]
|
||||
case 'RIGHT':
|
||||
if x != self.cell_size*(self.cell_number-1):
|
||||
return [['move', x + self.cell_size, y, 90], ['left', x, y, 0], ['right', x, y, 180]]
|
||||
if angle == 180:
|
||||
else:
|
||||
return [['left', x, y, 0], ['right', x, y, 180]]
|
||||
case 'DOWN':
|
||||
if y != self.cell_size*(self.cell_number-1):
|
||||
return [['move', x, y + self.cell_size, 180], ['left', x, y, 90], ['right', x, y, 270]]
|
||||
if angle == 270:
|
||||
else:
|
||||
return [['left', x, y, 90], ['right', x, y, 270]]
|
||||
case 'LEFT':
|
||||
if x != 0:
|
||||
return [['move', x - self.cell_size, y, 270], ['left', x, y, 180], ['right', x, y, 0]]
|
||||
else:
|
||||
return [['left', x, y, 180], ['right', x, y, 0]]
|
||||
|
||||
def graphsearch(self, istate, goaltest):
|
||||
x = istate[0]
|
||||
@ -55,8 +69,6 @@ class Search:
|
||||
explored.append(elem.state)
|
||||
|
||||
for (action, state_x, state_y, state_angle) in self.succ(elem.state):
|
||||
if state_x < 0 or state_y < 0: # check if any of the values are negative
|
||||
continue
|
||||
if [state_x, state_y, state_angle] not in fringe_state and \
|
||||
[state_x, state_y, state_angle] not in explored:
|
||||
x = Node([state_x, state_y, state_angle])
|
||||
@ -64,7 +76,3 @@ class Search:
|
||||
x.action = action
|
||||
fringe.append(x)
|
||||
fringe_state.append(x.state)
|
||||
|
||||
|
||||
se = Search(50)
|
||||
se.graphsearch(istate=[50, 50, 0], goaltest=[150, 250])
|
||||
|
22
main.py
22
main.py
@ -1,12 +1,11 @@
|
||||
import os
|
||||
|
||||
import pygame
|
||||
import random
|
||||
import land
|
||||
import tractor
|
||||
import blocks
|
||||
import graph_search
|
||||
from pygame.locals import *
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class Game:
|
||||
@ -66,12 +65,14 @@ class Game:
|
||||
# print(self.potato.get_soil_info().get_irrigation())
|
||||
running = True
|
||||
clock = pygame.time.Clock()
|
||||
# last_time = datetime.now()
|
||||
|
||||
move_tractor_event = pygame.USEREVENT + 1
|
||||
pygame.time.set_timer(move_tractor_event, 1000) # tractor moves every 1000 ms
|
||||
tractor_next_moves = []
|
||||
graph_search_object = graph_search.Search(self.cell_size, self.cell_number)
|
||||
|
||||
while running:
|
||||
clock.tick(60) # manual fps control not to overwork the computer
|
||||
# time_now = datetime.now()
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == KEYDOWN:
|
||||
if pygame.key.get_pressed()[K_ESCAPE]:
|
||||
@ -92,8 +93,15 @@ class Game:
|
||||
if pygame.key.get_pressed()[K_q]:
|
||||
self.tractor.harvest(self.fawn_seed_body, self.fawn_wheat_body, self.cell_size)
|
||||
self.tractor.put_seed(self.fawn_soil_body, self.fawn_seed_body, self.cell_size)
|
||||
|
||||
|
||||
if event.type == move_tractor_event:
|
||||
if len(tractor_next_moves) == 0:
|
||||
random_x = random.randrange(0, self.cell_number * self.cell_size, 50)
|
||||
random_y = random.randrange(0, self.cell_number * self.cell_size, 50)
|
||||
print("Generated target: ",random_x, random_y)
|
||||
tractor_next_moves = graph_search_object.graphsearch(
|
||||
[self.tractor.x, self.tractor.y, self.tractor.angle], [random_x, random_y])
|
||||
else:
|
||||
self.tractor.move(tractor_next_moves.pop(0)[0], self.cell_size, self.cell_number)
|
||||
elif event.type == QUIT:
|
||||
running = False
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user