small fix + pygad install
This commit is contained in:
parent
72cc8dcac3
commit
acafb05ae0
@ -15,7 +15,6 @@ b = [Image.open("b1.png").convert('RGBA'), Image.open("b2.png").convert('RGBA'),
|
|||||||
|
|
||||||
|
|
||||||
def generate(water, fertilizer, plantf):
|
def generate(water, fertilizer, plantf):
|
||||||
new_im = None
|
|
||||||
if water == 1:
|
if water == 1:
|
||||||
new_im = Image.new('RGB', (100, 100),
|
new_im = Image.new('RGB', (100, 100),
|
||||||
(160 + random.randint(-10, 10), 80 + random.randint(-10, 10), 40 + random.randint(-10, 10)))
|
(160 + random.randint(-10, 10), 80 + random.randint(-10, 10), 40 + random.randint(-10, 10)))
|
||||||
@ -81,4 +80,3 @@ for x in range(0, 1000):
|
|||||||
generate(0, 1, random.randint(0, 2)).save('datasets/01/' + str(x) + '.png')
|
generate(0, 1, random.randint(0, 2)).save('datasets/01/' + str(x) + '.png')
|
||||||
for x in range(0, 1000):
|
for x in range(0, 1000):
|
||||||
generate(1, 1, random.randint(0, 2)).save('datasets/11/' + str(x) + '.png')
|
generate(1, 1, random.randint(0, 2)).save('datasets/11/' + str(x) + '.png')
|
||||||
|
|
||||||
|
@ -2,13 +2,14 @@ import pathlib
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
from PIL.Image import Image
|
|
||||||
from torch import nn
|
from torch import nn
|
||||||
from torch.utils.data import DataLoader
|
from torch.utils.data import DataLoader
|
||||||
from torchvision import datasets, transforms
|
from torchvision import datasets, transforms
|
||||||
from torchvision.transforms import Lambda
|
from torchvision.transforms import Lambda
|
||||||
|
|
||||||
device = torch.device('cuda')
|
|
||||||
|
device = torch.device('cpu')
|
||||||
|
|
||||||
|
|
||||||
def train(model, dataset, n_iter=100, batch_size=2560000):
|
def train(model, dataset, n_iter=100, batch_size=2560000):
|
||||||
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
|
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
|
||||||
@ -40,8 +41,8 @@ train_data = datasets.ImageFolder(root="./datasets",
|
|||||||
transform=data_transform,
|
transform=data_transform,
|
||||||
target_transform=None)
|
target_transform=None)
|
||||||
|
|
||||||
model1=nn.Sequential(nn.Linear(30000, 10000),nn.ReLU(),nn.Linear(10000,10000),nn.ReLU(),nn.Linear(10000,10000),nn.Linear(10000,4),nn.LogSoftmax(dim=-1)).to(device)
|
model1 = nn.Sequential(nn.Linear(30000, 10000), nn.ReLU(), nn.Linear(10000, 10000), nn.ReLU(), nn.Linear(10000, 0000), nn.Linear(10000, 4), nn.LogSoftmax(dim=-1)).to(device)
|
||||||
model1.load_state_dict(torch.load("./trained"))
|
model1.load_state_dict(torch.load("./trained"))
|
||||||
train(model1,train_data)
|
train(model1, train_data)
|
||||||
|
|
||||||
torch.save(model1.state_dict(), "./trained")
|
torch.save(model1.state_dict(), "./trained")
|
||||||
|
32
astar.py
32
astar.py
@ -1,7 +1,6 @@
|
|||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
import cart
|
import cart
|
||||||
import copy
|
import copy
|
||||||
from classes import Field
|
|
||||||
|
|
||||||
|
|
||||||
class Istate:
|
class Istate:
|
||||||
@ -68,23 +67,23 @@ class Node:
|
|||||||
self.y = y
|
self.y = y
|
||||||
|
|
||||||
|
|
||||||
def fieldCost(T,node):
|
def fieldCost(T, node):
|
||||||
c = 0
|
c = 0
|
||||||
if T[node.x-1][node.y-1].plantType == 1:
|
if T[node.x-1][node.y-1].plantType == 1:
|
||||||
c =2
|
c = 2
|
||||||
elif T[node.x-1][node.y-1].plantType == 2:
|
elif T[node.x-1][node.y-1].plantType == 2:
|
||||||
c =5
|
c = 5
|
||||||
elif T[node.x-1][node.y-1].plantType == 3:
|
elif T[node.x-1][node.y-1].plantType == 3:
|
||||||
c =13
|
c = 13
|
||||||
elif T[node.x-1][node.y-1].plantType == 4:
|
elif T[node.x-1][node.y-1].plantType == 4:
|
||||||
c =100000
|
c = 100000
|
||||||
else:
|
else:
|
||||||
c=0
|
c = 0
|
||||||
|
|
||||||
if T[node.x-1][node.y-1].isWet == 1:
|
if T[node.x-1][node.y-1].isWet == 1:
|
||||||
c = c + 4
|
c = c + 4
|
||||||
else:
|
else:
|
||||||
c=c+1
|
c = c+1
|
||||||
|
|
||||||
return c
|
return c
|
||||||
|
|
||||||
@ -92,7 +91,7 @@ def fieldCost(T,node):
|
|||||||
def cost(T, node):
|
def cost(T, node):
|
||||||
cost = 0
|
cost = 0
|
||||||
|
|
||||||
while (node.get_parent() != None):
|
while node.get_parent() is not None:
|
||||||
cost = cost + fieldCost(T, node)
|
cost = cost + fieldCost(T, node)
|
||||||
node = node.get_parent()
|
node = node.get_parent()
|
||||||
|
|
||||||
@ -103,7 +102,7 @@ def f(goaltest, map, node):
|
|||||||
return cost(map, node) + heuristic(goaltest, node)
|
return cost(map, node) + heuristic(goaltest, node)
|
||||||
|
|
||||||
|
|
||||||
def goal_test(elem,goaltest):
|
def goal_test(elem, goaltest):
|
||||||
if elem.get_x() == goaltest[0] and elem.get_y() == goaltest[1]:
|
if elem.get_x() == goaltest[0] and elem.get_y() == goaltest[1]:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
@ -132,7 +131,7 @@ def graphsearch(explored, f, fringe, goaltest, istate, map, succ): # przeszukiw
|
|||||||
explored_tuple.append((x.get_direction(), x.get_x(), x.get_y()))
|
explored_tuple.append((x.get_direction(), x.get_x(), x.get_y()))
|
||||||
x = Node(action, state[0], elem[0], state[1], state[2]) # stworzenie nowego wierzchołka, którego rodzicem jest elem
|
x = Node(action, state[0], elem[0], state[1], state[2]) # stworzenie nowego wierzchołka, którego rodzicem jest elem
|
||||||
p = f(goaltest, map, x) # liczy priorytet
|
p = f(goaltest, map, x) # liczy priorytet
|
||||||
#print('Koszt =', p)
|
# print('Koszt =', p)
|
||||||
if state not in fringe_tuple and state not in explored_tuple: # jeżeli stan nie znajduje się na fringe oraz nie znajduje się w liście wierzchołków odwiedzonych
|
if state not in fringe_tuple and state not in explored_tuple: # jeżeli stan nie znajduje się na fringe oraz nie znajduje się w liście wierzchołków odwiedzonych
|
||||||
fringe.append((x, p)) # dodanie wierzchołka na fringe
|
fringe.append((x, p)) # dodanie wierzchołka na fringe
|
||||||
fringe = sorted(fringe, key=itemgetter(1)) # sortowanie fringe'a według priorytetu
|
fringe = sorted(fringe, key=itemgetter(1)) # sortowanie fringe'a według priorytetu
|
||||||
@ -141,7 +140,7 @@ def graphsearch(explored, f, fringe, goaltest, istate, map, succ): # przeszukiw
|
|||||||
for (state_prio, r) in fringe_tuple_prio:
|
for (state_prio, r) in fringe_tuple_prio:
|
||||||
if str(state_prio) == str(state):
|
if str(state_prio) == str(state):
|
||||||
if r > p:
|
if r > p:
|
||||||
fringe.insert(i, (x,p)) # zamiana state, który należy do fringe z priorytetem r na state z priorytetem p (niższym)
|
fringe.insert(i, (x, p)) # zamiana state, który należy do fringe z priorytetem r na state z priorytetem p (niższym)
|
||||||
fringe.pop(i + 1)
|
fringe.pop(i + 1)
|
||||||
fringe = sorted(fringe, key=itemgetter(1)) # sortowanie fringe'a według priorytetu
|
fringe = sorted(fringe, key=itemgetter(1)) # sortowanie fringe'a według priorytetu
|
||||||
break
|
break
|
||||||
@ -154,7 +153,7 @@ def heuristic(goaltest, node):
|
|||||||
|
|
||||||
def print_moves(elem):
|
def print_moves(elem):
|
||||||
moves_list = []
|
moves_list = []
|
||||||
while (elem.get_parent() != None):
|
while elem.get_parent() is not None:
|
||||||
moves_list.append(elem.get_action())
|
moves_list.append(elem.get_action())
|
||||||
elem = elem.get_parent()
|
elem = elem.get_parent()
|
||||||
moves_list.reverse()
|
moves_list.reverse()
|
||||||
@ -184,7 +183,6 @@ def succ(elem):
|
|||||||
temp_move_east = elem.get_x() + 1
|
temp_move_east = elem.get_x() + 1
|
||||||
temp_move_north = elem.get_y() + 1
|
temp_move_north = elem.get_y() + 1
|
||||||
|
|
||||||
|
|
||||||
if cart.Cart.is_move_allowed_succ(elem) == "x + 1":
|
if cart.Cart.is_move_allowed_succ(elem) == "x + 1":
|
||||||
actions_list.append(("move", (elem.get_direction(), temp_move_east, elem.get_y())))
|
actions_list.append(("move", (elem.get_direction(), temp_move_east, elem.get_y())))
|
||||||
elif cart.Cart.is_move_allowed_succ(elem) == "y + 1":
|
elif cart.Cart.is_move_allowed_succ(elem) == "y + 1":
|
||||||
@ -194,8 +192,4 @@ def succ(elem):
|
|||||||
elif cart.Cart.is_move_allowed_succ(elem) == "x - 1":
|
elif cart.Cart.is_move_allowed_succ(elem) == "x - 1":
|
||||||
actions_list.append(("move", (elem.get_direction(), temp_move_west, elem.get_y())))
|
actions_list.append(("move", (elem.get_direction(), temp_move_west, elem.get_y())))
|
||||||
|
|
||||||
|
return actions_list
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return actions_list
|
|
||||||
|
18
bfs.py
18
bfs.py
@ -1,4 +1,3 @@
|
|||||||
import sys
|
|
||||||
import cart
|
import cart
|
||||||
import copy
|
import copy
|
||||||
|
|
||||||
@ -67,23 +66,19 @@ class Node:
|
|||||||
self.y = y
|
self.y = y
|
||||||
|
|
||||||
|
|
||||||
def goal_test(goaltest,elem):
|
def goal_test(goaltest, elem):
|
||||||
if elem.get_x() == goaltest[0] and elem.get_y() == goaltest[1]:
|
if elem.get_x() == goaltest[0] and elem.get_y() == goaltest[1]:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
# def graphsearch(explored, fringe, goaltest, istate, succ): # przeszukiwanie grafu wszerz
|
def graphsearch(goaltest, istate): # przeszukiwanie grafu wszerz
|
||||||
def graphsearch(explored, fringe, goaltest, istate): # przeszukiwanie grafu wszerz
|
|
||||||
node = Node(None, istate.get_direction(), None, istate.get_x(), istate.get_y())
|
node = Node(None, istate.get_direction(), None, istate.get_x(), istate.get_y())
|
||||||
|
|
||||||
fringe = []
|
fringe = []
|
||||||
#elem = []
|
|
||||||
explored = []
|
explored = []
|
||||||
#action = []
|
|
||||||
fringe.append(node) # wierzchołki do odwiedzenia
|
fringe.append(node) # wierzchołki do odwiedzenia
|
||||||
# fringe = [node]
|
|
||||||
while True:
|
while True:
|
||||||
if not fringe:
|
if not fringe:
|
||||||
return False
|
return False
|
||||||
@ -109,7 +104,7 @@ def graphsearch(explored, fringe, goaltest, istate): # przeszukiwanie grafu wsz
|
|||||||
|
|
||||||
def print_moves(elem):
|
def print_moves(elem):
|
||||||
moves_list = []
|
moves_list = []
|
||||||
while (elem.get_parent() != None):
|
while elem.get_parent() is not None:
|
||||||
moves_list.append(elem.get_action())
|
moves_list.append(elem.get_action())
|
||||||
elem = elem.get_parent()
|
elem = elem.get_parent()
|
||||||
moves_list.reverse()
|
moves_list.reverse()
|
||||||
@ -139,7 +134,6 @@ def succ(elem):
|
|||||||
temp_move_east = elem.get_x() + 1
|
temp_move_east = elem.get_x() + 1
|
||||||
temp_move_north = elem.get_y() + 1
|
temp_move_north = elem.get_y() + 1
|
||||||
|
|
||||||
|
|
||||||
if cart.Cart.is_move_allowed_succ(elem) == "x + 1":
|
if cart.Cart.is_move_allowed_succ(elem) == "x + 1":
|
||||||
actions_list.append(("move", (elem.get_direction(), temp_move_east, elem.get_y())))
|
actions_list.append(("move", (elem.get_direction(), temp_move_east, elem.get_y())))
|
||||||
elif cart.Cart.is_move_allowed_succ(elem) == "y + 1":
|
elif cart.Cart.is_move_allowed_succ(elem) == "y + 1":
|
||||||
@ -149,8 +143,4 @@ def succ(elem):
|
|||||||
elif cart.Cart.is_move_allowed_succ(elem) == "x - 1":
|
elif cart.Cart.is_move_allowed_succ(elem) == "x - 1":
|
||||||
actions_list.append(("move", (elem.get_direction(), temp_move_west, elem.get_y())))
|
actions_list.append(("move", (elem.get_direction(), temp_move_west, elem.get_y())))
|
||||||
|
|
||||||
|
return actions_list
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return actions_list
|
|
||||||
|
10
board.py
10
board.py
@ -1,6 +1,5 @@
|
|||||||
import pygame
|
import pygame
|
||||||
from screen import SCREEN
|
from screen import SCREEN
|
||||||
global BLACK
|
|
||||||
|
|
||||||
# global SCREEN
|
# global SCREEN
|
||||||
global BLACK
|
global BLACK
|
||||||
@ -8,9 +7,9 @@ global gridObjects
|
|||||||
global imgTree
|
global imgTree
|
||||||
global imgTree
|
global imgTree
|
||||||
imgTree = pygame.image.load('img/tree.png')
|
imgTree = pygame.image.load('img/tree.png')
|
||||||
|
|
||||||
gridObjects = {} # Store grid-box objects from Grid Class
|
gridObjects = {} # Store grid-box objects from Grid Class
|
||||||
|
|
||||||
|
|
||||||
class Grid(object):
|
class Grid(object):
|
||||||
# ta klasa rysuje kratę na ekranie
|
# ta klasa rysuje kratę na ekranie
|
||||||
def __init__(self, x, y, sx, sy):
|
def __init__(self, x, y, sx, sy):
|
||||||
@ -27,6 +26,7 @@ class Grid(object):
|
|||||||
BLACK = (0, 0, 0)
|
BLACK = (0, 0, 0)
|
||||||
pygame.draw.rect(SCREEN, BLACK, (self.x, self.y, self.sx, self.sy), self.width)
|
pygame.draw.rect(SCREEN, BLACK, (self.x, self.y, self.sx, self.sy), self.width)
|
||||||
|
|
||||||
|
|
||||||
class Box(object):
|
class Box(object):
|
||||||
# global SCREEN
|
# global SCREEN
|
||||||
|
|
||||||
@ -43,6 +43,7 @@ class Box(object):
|
|||||||
# global BLACK
|
# global BLACK
|
||||||
pygame.draw.rect(SCREEN, self.color, pygame.Rect(self.x, self.y, self.sx, self.sy))
|
pygame.draw.rect(SCREEN, self.color, pygame.Rect(self.x, self.y, self.sx, self.sy))
|
||||||
|
|
||||||
|
|
||||||
class Obstacle(object):
|
class Obstacle(object):
|
||||||
def __init__(self, mouseObj):
|
def __init__(self, mouseObj):
|
||||||
self.mseX = mouseObj[0]
|
self.mseX = mouseObj[0]
|
||||||
@ -66,6 +67,7 @@ class Obstacle(object):
|
|||||||
SCREEN.blit(imgTree, (self.posX, self.posY))
|
SCREEN.blit(imgTree, (self.posX, self.posY))
|
||||||
# pygame.display.update()
|
# pygame.display.update()
|
||||||
|
|
||||||
|
|
||||||
def getGridBoxes(grid_box):
|
def getGridBoxes(grid_box):
|
||||||
global gridObjects
|
global gridObjects
|
||||||
return gridObjects[grid_box]
|
return gridObjects[grid_box]
|
||||||
|
2
cart.py
2
cart.py
@ -25,7 +25,6 @@ class Cart:
|
|||||||
def set_y(self, y):
|
def set_y(self, y):
|
||||||
self.y = y
|
self.y = y
|
||||||
|
|
||||||
|
|
||||||
def is_move_allowed(self,
|
def is_move_allowed(self,
|
||||||
cart_rect): # sprawdza czy dany ruch, który chce wykonać wózek jest możliwy, zwraca prawdę lub fałsz
|
cart_rect): # sprawdza czy dany ruch, który chce wykonać wózek jest możliwy, zwraca prawdę lub fałsz
|
||||||
if self.direction == definitions.CART_DIRECTION_EAST and cart_rect.x + definitions.BLOCK_SIZE < definitions.WIDTH_MAP:
|
if self.direction == definitions.CART_DIRECTION_EAST and cart_rect.x + definitions.BLOCK_SIZE < definitions.WIDTH_MAP:
|
||||||
@ -74,4 +73,3 @@ class Cart:
|
|||||||
self.direction = 1
|
self.direction = 1
|
||||||
else:
|
else:
|
||||||
self.direction = self.direction + 1
|
self.direction = self.direction + 1
|
||||||
|
|
||||||
|
15
classes.py
15
classes.py
@ -1,11 +1,12 @@
|
|||||||
class Field:
|
class Field:
|
||||||
def __init__(self, fieldType, plantType, isWet, wetTime, isFertilized, fertilizedTime):
|
def __init__(self, fieldType, plantType, isWet, wetTime, isFertilized, fertilizedTime):
|
||||||
self.fieldType =fieldType # good/bad
|
self.fieldType = fieldType # good/bad
|
||||||
self.plantType =plantType # wheat/carrot/cabbage
|
self.plantType = plantType # wheat/carrot/cabbage
|
||||||
self.isWet =isWet # yes/no
|
self.isWet = isWet # yes/no
|
||||||
self.wetTime =wetTime # number
|
self.wetTime = wetTime # number
|
||||||
self.isFertilized =isFertilized # yes/no
|
self.isFertilized = isFertilized # yes/no
|
||||||
self.fertilizedTime =fertilizedTime # number
|
self.fertilizedTime = fertilizedTime # number
|
||||||
|
|
||||||
|
|
||||||
class Plant:
|
class Plant:
|
||||||
def __init__(self, plantType, growthState):
|
def __init__(self, plantType, growthState):
|
||||||
@ -21,4 +22,4 @@ class Fertilizer:
|
|||||||
class Player:
|
class Player:
|
||||||
x = 0
|
x = 0
|
||||||
y = 0
|
y = 0
|
||||||
rotation = 0
|
rotation = 0
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
# Generowanie unikalnej losowej linii tekstu
|
# Generowanie unikalnej losowej linii tekstu
|
||||||
def generate_unique_line(existing_lines):
|
def generate_unique_line(existing_lines):
|
||||||
while True:
|
while True:
|
||||||
@ -9,6 +10,7 @@ def generate_unique_line(existing_lines):
|
|||||||
if line not in existing_lines:
|
if line not in existing_lines:
|
||||||
return line
|
return line
|
||||||
|
|
||||||
|
|
||||||
# Generowanie 200 unikalnych linii tekstu
|
# Generowanie 200 unikalnych linii tekstu
|
||||||
lines = []
|
lines = []
|
||||||
while len(lines) < 200:
|
while len(lines) < 200:
|
||||||
@ -18,4 +20,4 @@ while len(lines) < 200:
|
|||||||
# Zapisywanie linii tekstu do pliku
|
# Zapisywanie linii tekstu do pliku
|
||||||
with open('decisionTree/database.txt', 'w') as file:
|
with open('decisionTree/database.txt', 'w') as file:
|
||||||
for line in lines:
|
for line in lines:
|
||||||
file.write(line + '\n')
|
file.write(line + '\n')
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
|
# from sklearn.datasets import load_iris
|
||||||
|
|
||||||
#from sklearn.datasets import load_iris
|
|
||||||
from sklearn.tree import export_text
|
from sklearn.tree import export_text
|
||||||
|
|
||||||
from sklearn.tree import DecisionTreeClassifier
|
from sklearn.tree import DecisionTreeClassifier
|
||||||
@ -52,14 +50,14 @@ with open("decisionTree/database.txt", 'r') as f:
|
|||||||
view.append(x)
|
view.append(x)
|
||||||
X1.append(test_list)
|
X1.append(test_list)
|
||||||
|
|
||||||
f = open("decisionTree/learning_set.txt", "w") #zapisuje atrybuty s³ownie
|
f = open("decisionTree/learning_set.txt", "w") # zapisuje atrybuty s³ownie
|
||||||
for i in view:
|
for i in view:
|
||||||
f.write(str(i)+"\n")
|
f.write(str(i)+"\n")
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
Y1 = []
|
Y1 = []
|
||||||
with open("decisionTree/decissions.txt", 'r') as f: #czyta decyzje
|
with open("decisionTree/decissions.txt", 'r') as f: # czyta decyzje
|
||||||
for line in f:
|
for line in f:
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
test = int(line)
|
test = int(line)
|
||||||
@ -67,7 +65,7 @@ with open("decisionTree/decissions.txt", 'r') as f: #czyta decyzje
|
|||||||
|
|
||||||
dataset = X1
|
dataset = X1
|
||||||
decision = Y1
|
decision = Y1
|
||||||
labels = ['Rain','Plant','Temperature','Sun','Snow','Moisture','Rotten','Time']
|
labels = ['Rain', 'Plant', 'Temperature', 'Sun', 'Snow', 'Moisture', 'Rotten', 'Time']
|
||||||
model = DecisionTreeClassifier(random_state=0, max_depth=20).fit(dataset, decision)
|
model = DecisionTreeClassifier(random_state=0, max_depth=20).fit(dataset, decision)
|
||||||
filename = 'decisionTree/decisionTree.sav'
|
filename = 'decisionTree/decisionTree.sav'
|
||||||
print("Model trained")
|
print("Model trained")
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
# definicje
|
|
||||||
import os
|
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
@ -21,4 +19,4 @@ HEIGHT_AMOUNT, WIDTH_AMOUNT = 11, 11
|
|||||||
HEIGHT_MAP, WIDTH_MAP = BLOCK_SIZE * HEIGHT_AMOUNT, BLOCK_SIZE * WIDTH_AMOUNT
|
HEIGHT_MAP, WIDTH_MAP = BLOCK_SIZE * HEIGHT_AMOUNT, BLOCK_SIZE * WIDTH_AMOUNT
|
||||||
HEIGHT, WIDTH = HEIGHT_MAP + BLOCK_SIZE, WIDTH_MAP
|
HEIGHT, WIDTH = HEIGHT_MAP + BLOCK_SIZE, WIDTH_MAP
|
||||||
IMAGE_SIZE_NEURAL_NETWORK = 16
|
IMAGE_SIZE_NEURAL_NETWORK = 16
|
||||||
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
|
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
|
||||||
|
BIN
img/player.png
BIN
img/player.png
Binary file not shown.
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 2.9 KiB |
BIN
img/tree.png
BIN
img/tree.png
Binary file not shown.
Before Width: | Height: | Size: 2.0 KiB After Width: | Height: | Size: 3.9 KiB |
295
main.py
295
main.py
@ -1,18 +1,15 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import pathlib
|
|
||||||
import random
|
import random
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
from torch import nn
|
from torch import nn
|
||||||
from torch.utils.data import DataLoader
|
|
||||||
from torchvision import datasets, transforms
|
from torchvision import datasets, transforms
|
||||||
from torchvision.transforms import Lambda
|
from torchvision.transforms import Lambda
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
import astar
|
import astar
|
||||||
from classes import Field, Plant, Fertilizer, Player
|
from classes import Field, Player
|
||||||
from bfs import Node
|
from bfs import Istate, succ
|
||||||
from bfs import Istate, print_moves, succ
|
|
||||||
from bfs import graphsearch
|
from bfs import graphsearch
|
||||||
from board import Grid, Box, Obstacle, getGridBoxes, gridObjects
|
from board import Grid, Box, Obstacle, getGridBoxes, gridObjects
|
||||||
from screen import SCREEN
|
from screen import SCREEN
|
||||||
@ -24,7 +21,7 @@ Ucelu = False
|
|||||||
SCREENX = 500
|
SCREENX = 500
|
||||||
SCREENY = 500
|
SCREENY = 500
|
||||||
device = torch.device('cpu')
|
device = torch.device('cpu')
|
||||||
model1=nn.Sequential(nn.Linear(30000, 10000),nn.ReLU(),nn.Linear(10000,10000),nn.ReLU(),nn.Linear(10000,10000),nn.Linear(10000,4),nn.LogSoftmax(dim=-1)).to(device)
|
model1 = nn.Sequential(nn.Linear(30000, 10000), nn.ReLU(), nn.Linear(10000, 10000), nn.ReLU(), nn.Linear(10000, 10000), nn.Linear(10000, 4), nn.LogSoftmax(dim=-1)).to(device)
|
||||||
model1.load_state_dict(torch.load("./NN/trained"))
|
model1.load_state_dict(torch.load("./NN/trained"))
|
||||||
|
|
||||||
pygame.display.set_caption('Inteligentny Traktor')
|
pygame.display.set_caption('Inteligentny Traktor')
|
||||||
@ -40,8 +37,8 @@ plants[2].append(Image.open("NN/ca2.png"))
|
|||||||
plants[2].append(Image.open("NN/ca3.png"))
|
plants[2].append(Image.open("NN/ca3.png"))
|
||||||
b = [Image.open("NN/b1.png").convert('RGBA'), Image.open("NN/b2.png").convert('RGBA'), Image.open("NN/b3.png").convert('RGBA')]
|
b = [Image.open("NN/b1.png").convert('RGBA'), Image.open("NN/b2.png").convert('RGBA'), Image.open("NN/b3.png").convert('RGBA')]
|
||||||
|
|
||||||
|
|
||||||
def generate(water, fertilizer, plantf):
|
def generate(water, fertilizer, plantf):
|
||||||
new_im = None
|
|
||||||
if water == 1:
|
if water == 1:
|
||||||
new_im = Image.new('RGB', (100, 100),
|
new_im = Image.new('RGB', (100, 100),
|
||||||
(160 + random.randint(-10, 10), 80 + random.randint(-10, 10), 40 + random.randint(-10, 10)))
|
(160 + random.randint(-10, 10), 80 + random.randint(-10, 10), 40 + random.randint(-10, 10)))
|
||||||
@ -98,6 +95,7 @@ def generate(water, fertilizer, plantf):
|
|||||||
|
|
||||||
return new_im
|
return new_im
|
||||||
|
|
||||||
|
|
||||||
# COLORS
|
# COLORS
|
||||||
WHITE = (255, 255, 255)
|
WHITE = (255, 255, 255)
|
||||||
BLACK = (0, 0, 0)
|
BLACK = (0, 0, 0)
|
||||||
@ -124,18 +122,16 @@ obstacles = 1
|
|||||||
|
|
||||||
# BFS Variables
|
# BFS Variables
|
||||||
|
|
||||||
|
startNode = Istate(1, 1, 1)
|
||||||
|
goalNode = [1, 1]
|
||||||
startNode = Istate( 1,1,1)
|
|
||||||
goalNode = [1,1]
|
|
||||||
|
|
||||||
graph = dict()
|
graph = dict()
|
||||||
pathFound = [] # Store the path in a list box index to draw on later
|
pathFound = [] # Store the path in a list box index to draw on later
|
||||||
|
|
||||||
def drawGrid(sizex,sizey):
|
|
||||||
|
def drawGrid(sizex, sizey):
|
||||||
spaceX = SCREENX // sizex
|
spaceX = SCREENX // sizex
|
||||||
spaceY = SCREENY // sizey
|
spaceY = SCREENY // sizey
|
||||||
width = 2
|
|
||||||
|
|
||||||
counter = 1
|
counter = 1
|
||||||
for i in range(sizex):
|
for i in range(sizex):
|
||||||
@ -144,16 +140,18 @@ def drawGrid(sizex,sizey):
|
|||||||
g = Grid(50 + i*50, 50 + j*50, spaceX, spaceY)
|
g = Grid(50 + i*50, 50 + j*50, spaceX, spaceY)
|
||||||
gridObjects[counter] = g
|
gridObjects[counter] = g
|
||||||
counter += 1
|
counter += 1
|
||||||
def generateGraph(row,col):
|
|
||||||
|
|
||||||
|
def generateGraph(row, col):
|
||||||
# This function generates a graph based on the gridObjects instantiated!
|
# This function generates a graph based on the gridObjects instantiated!
|
||||||
sample_graph = {'A':['B','C','E'],
|
# sample_graph = {'A': ['B', 'C', 'E'],
|
||||||
'B':['A','D','E'],
|
# 'B': ['A', 'D', 'E'],
|
||||||
'C':['A','F','G'],
|
# 'C': ['A', 'F', 'G'],
|
||||||
'D':['B'],
|
# 'D': ['B'],
|
||||||
'E':['A','B','D'],
|
# 'E': ['A', 'B', 'D'],
|
||||||
'F':['C'],
|
# 'F': ['C'],
|
||||||
'G':['C']
|
# 'G': ['C']
|
||||||
}
|
# }
|
||||||
|
|
||||||
miniG = {}
|
miniG = {}
|
||||||
for grid in range(len(gridObjects)):
|
for grid in range(len(gridObjects)):
|
||||||
@ -187,9 +185,9 @@ def generateGraph(row,col):
|
|||||||
if grid > col: # Away from the Left Border of the Screen
|
if grid > col: # Away from the Left Border of the Screen
|
||||||
if grid > (col*row)-col: # You are on the Right Border of the screen - You can't go East
|
if grid > (col*row)-col: # You are on the Right Border of the screen - You can't go East
|
||||||
miniG[grid] = [gN, gS, gW]
|
miniG[grid] = [gN, gS, gW]
|
||||||
else: # Away from the Right Border of the Screen - You can go East
|
else: # Away from the Right Border of the Screen - You can go East
|
||||||
miniG[grid] = [gN, gS, gE, gW]
|
miniG[grid] = [gN, gS, gE, gW]
|
||||||
else: # You are on the Left Edge of the screen - You can't go West
|
else: # You are on the Left Edge of the screen - You can't go West
|
||||||
miniG[grid] = [gN, gS, gE]
|
miniG[grid] = [gN, gS, gE]
|
||||||
|
|
||||||
# FILTER OUT OBSTACLES FROM THE GRAPH
|
# FILTER OUT OBSTACLES FROM THE GRAPH
|
||||||
@ -199,7 +197,7 @@ def generateGraph(row,col):
|
|||||||
if grid not in gridObstacle:
|
if grid not in gridObstacle:
|
||||||
# gridObjects.remove(grid) # Dict object has no attribute : 'remove'
|
# gridObjects.remove(grid) # Dict object has no attribute : 'remove'
|
||||||
# HACK
|
# HACK
|
||||||
miniG2[grid] = miniG[grid] # Created a new dictionary that stored the values required
|
miniG2[grid] = miniG[grid] # Created a new dictionary that stored the values required
|
||||||
# IN-DEPTH FILTER - Filter out obstacles from the neighbors-list
|
# IN-DEPTH FILTER - Filter out obstacles from the neighbors-list
|
||||||
for neigbor in miniG2[grid]:
|
for neigbor in miniG2[grid]:
|
||||||
if neigbor in gridObstacle:
|
if neigbor in gridObstacle:
|
||||||
@ -214,12 +212,13 @@ def generateGraph(row,col):
|
|||||||
|
|
||||||
return miniG2
|
return miniG2
|
||||||
|
|
||||||
def drawGraph(pathF):
|
|
||||||
#Draws the path given the path-list
|
|
||||||
global Ucelu
|
|
||||||
#print(pathF)
|
|
||||||
|
|
||||||
if (Ucelu == False):
|
def drawGraph(pathF):
|
||||||
|
# Draws the path given the path-list
|
||||||
|
global Ucelu
|
||||||
|
# print(pathF)
|
||||||
|
|
||||||
|
if not Ucelu:
|
||||||
for grid in pathF:
|
for grid in pathF:
|
||||||
# g = gridObjects[grid] # Get the grid-box object mentioned in the path
|
# g = gridObjects[grid] # Get the grid-box object mentioned in the path
|
||||||
# x = g.x
|
# x = g.x
|
||||||
@ -232,9 +231,9 @@ def drawGraph(pathF):
|
|||||||
if grid == 'rotate_right':
|
if grid == 'rotate_right':
|
||||||
player.rotation = (player.rotation - 90) % 360
|
player.rotation = (player.rotation - 90) % 360
|
||||||
if grid == 'rotate_left':
|
if grid == 'rotate_left':
|
||||||
player.rotation = (player.rotation + 90) %360
|
player.rotation = (player.rotation + 90) % 360
|
||||||
|
|
||||||
#( player.rotation)
|
# (player.rotation)
|
||||||
|
|
||||||
if grid == 'move':
|
if grid == 'move':
|
||||||
if player.rotation == 0:
|
if player.rotation == 0:
|
||||||
@ -250,49 +249,11 @@ def drawGraph(pathF):
|
|||||||
if player.y > 0:
|
if player.y > 0:
|
||||||
player.y = player.y - 1
|
player.y = player.y - 1
|
||||||
|
|
||||||
|
|
||||||
# if player.x < (x/50 - 1):
|
|
||||||
# a = 1
|
|
||||||
# if player.x > (x/50 - 1):
|
|
||||||
# a =2
|
|
||||||
# if player.y < (y/50 - 1):
|
|
||||||
# a =3
|
|
||||||
# if player.y > (y/50 - 1):
|
|
||||||
# a =4
|
|
||||||
#
|
|
||||||
# if a==1:
|
|
||||||
# # player.x = x/50 - 1
|
|
||||||
# player.rotation = 0
|
|
||||||
# if a==2:
|
|
||||||
# # player.x = x/50 - 1
|
|
||||||
# player.rotation = 180
|
|
||||||
# if a==3:
|
|
||||||
# # player.y = y/50 - 1
|
|
||||||
# player.rotation = 270
|
|
||||||
# if a==4:
|
|
||||||
# # player.y = y/50 - 1
|
|
||||||
# player.rotation = 90
|
|
||||||
|
|
||||||
|
|
||||||
# tmpImg = pygame.transform.rotate(imgPlayer, player.rotation)
|
|
||||||
# if player.rotation == 180:
|
|
||||||
# tmpImg = pygame.transform.flip(tmpImg, True, True)
|
|
||||||
# tmpImg = pygame.transform.flip(tmpImg, True, False)
|
|
||||||
#
|
|
||||||
# #player is seen on the way
|
|
||||||
# SCREEN.blit(tmpImg, (55 + 50 * player.x, 55 + 50 * player.y))
|
|
||||||
# pygame.display.update()
|
|
||||||
# # pygame.time.wait(300)
|
|
||||||
|
|
||||||
# player.y = y/50 - 1
|
|
||||||
# player.x = x/50 - 1
|
|
||||||
|
|
||||||
# -----------------------------
|
|
||||||
i = 0
|
i = 0
|
||||||
while i < len(T):
|
while i < len(T):
|
||||||
j = 0
|
j = 0
|
||||||
while j < len(T[i]):
|
while j < len(T[i]):
|
||||||
#color = (255, 255, 255, 0)
|
# color = (255, 255, 255, 0)
|
||||||
if T[i][j].isWet == 0:
|
if T[i][j].isWet == 0:
|
||||||
# a = 1
|
# a = 1
|
||||||
color = (160, 80, 40, 0)
|
color = (160, 80, 40, 0)
|
||||||
@ -300,7 +261,7 @@ def drawGraph(pathF):
|
|||||||
# a = 1
|
# a = 1
|
||||||
color = (50, 25, 0, 0)
|
color = (50, 25, 0, 0)
|
||||||
|
|
||||||
#Covers 'player' on the way
|
# Covers 'player' on the way
|
||||||
pygame.draw.rect(SCREEN, color, pygame.Rect(50 + 50 * i, 50 + 50 * j, 50, 50))
|
pygame.draw.rect(SCREEN, color, pygame.Rect(50 + 50 * i, 50 + 50 * j, 50, 50))
|
||||||
if T[i][j].plantType == 1:
|
if T[i][j].plantType == 1:
|
||||||
SCREEN.blit(imgWheat, (50 + 50 * i, 50 + 50 * j))
|
SCREEN.blit(imgWheat, (50 + 50 * i, 50 + 50 * j))
|
||||||
@ -318,7 +279,6 @@ def drawGraph(pathF):
|
|||||||
for obs in obstacleObjects:
|
for obs in obstacleObjects:
|
||||||
obstacleObjects[obs].draw()
|
obstacleObjects[obs].draw()
|
||||||
|
|
||||||
|
|
||||||
for bx in boxObjects:
|
for bx in boxObjects:
|
||||||
boxObjects[bx].draw()
|
boxObjects[bx].draw()
|
||||||
|
|
||||||
@ -333,30 +293,21 @@ def drawGraph(pathF):
|
|||||||
tmpImg = pygame.transform.flip(tmpImg, True, True)
|
tmpImg = pygame.transform.flip(tmpImg, True, True)
|
||||||
tmpImg = pygame.transform.flip(tmpImg, True, False)
|
tmpImg = pygame.transform.flip(tmpImg, True, False)
|
||||||
|
|
||||||
#player is seen on the way
|
# player is seen on the way
|
||||||
SCREEN.blit(tmpImg, (55 + 50 * player.x, 55 + 50 * player.y))
|
SCREEN.blit(tmpImg, (55 + 50 * player.x, 55 + 50 * player.y))
|
||||||
|
|
||||||
# --------------------------------------
|
|
||||||
|
|
||||||
# tmpImg = pygame.transform.rotate(imgPlayer, player.rotation)
|
|
||||||
# # if flip:
|
|
||||||
# # if flip == True:
|
|
||||||
# if player.rotation == 180:
|
|
||||||
# tmpImg = pygame.transform.flip(tmpImg, True, True)
|
|
||||||
# tmpImg = pygame.transform.flip(tmpImg, True, False)
|
|
||||||
#
|
|
||||||
# SCREEN.blit(tmpImg, (55 + 50 * player.x, 55 + 50 * player.y))
|
|
||||||
|
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
pygame.time.wait(300)
|
pygame.time.wait(300)
|
||||||
SCREEN.fill((WHITE))
|
SCREEN.fill(WHITE)
|
||||||
# pygame.time.wait(50)
|
# pygame.time.wait(50)
|
||||||
# pygame.draw.rect(SCREEN, WHITE, pygame.Rect(x, y, sx, sy))
|
# pygame.draw.rect(SCREEN, WHITE, pygame.Rect(x, y, sx, sy))
|
||||||
Ucelu = True
|
Ucelu = True
|
||||||
def UIHandler(mouseObj):
|
|
||||||
|
|
||||||
|
def UIHandler():
|
||||||
# drawGrid(GRIDX, GRIDY)
|
# drawGrid(GRIDX, GRIDY)
|
||||||
global Ucelu
|
global Ucelu
|
||||||
drawGrid(10,10)
|
drawGrid(10, 10)
|
||||||
|
|
||||||
for grid in gridObjects:
|
for grid in gridObjects:
|
||||||
gridObjects[grid].draw()
|
gridObjects[grid].draw()
|
||||||
@ -368,10 +319,10 @@ def UIHandler(mouseObj):
|
|||||||
obstacleObjects[obs].draw()
|
obstacleObjects[obs].draw()
|
||||||
|
|
||||||
if pathFound:
|
if pathFound:
|
||||||
drawGraph(pathFound)
|
drawGraph(pathFound)
|
||||||
# Ucelu = False
|
|
||||||
|
|
||||||
def eventHandler(kbdObj,mouseObj):
|
|
||||||
|
def eventHandler(kbdObj, mouseObj):
|
||||||
global boxes
|
global boxes
|
||||||
global obstacles
|
global obstacles
|
||||||
global startNode
|
global startNode
|
||||||
@ -380,7 +331,7 @@ def eventHandler(kbdObj,mouseObj):
|
|||||||
global Ucelu
|
global Ucelu
|
||||||
|
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
running = False
|
pygame.quit()
|
||||||
|
|
||||||
if event.type == pygame.KEYDOWN:
|
if event.type == pygame.KEYDOWN:
|
||||||
pygame.time.wait(DELAY)
|
pygame.time.wait(DELAY)
|
||||||
@ -426,15 +377,10 @@ def eventHandler(kbdObj,mouseObj):
|
|||||||
# If Key_f is pressed, set goal node
|
# If Key_f is pressed, set goal node
|
||||||
if kbdObj[pygame.K_f]:
|
if kbdObj[pygame.K_f]:
|
||||||
gBox = getGridBoxes(int(len(gridObjects)))
|
gBox = getGridBoxes(int(len(gridObjects)))
|
||||||
# gBox = getGridBoxes()
|
|
||||||
|
|
||||||
#x = mouseObj[0]
|
|
||||||
#y = mouseObj[1]
|
|
||||||
# x = gBox.x
|
|
||||||
# y = gBox.y
|
|
||||||
sx = gBox.sx
|
sx = gBox.sx
|
||||||
sy = gBox.sy
|
sy = gBox.sy
|
||||||
# ----------------------------------------
|
|
||||||
mseX = mouseObj[0]
|
mseX = mouseObj[0]
|
||||||
mseY = mouseObj[1]
|
mseY = mouseObj[1]
|
||||||
|
|
||||||
@ -444,15 +390,11 @@ def eventHandler(kbdObj,mouseObj):
|
|||||||
y = g.y
|
y = g.y
|
||||||
sx = g.sx
|
sx = g.sx
|
||||||
sy = g.sy
|
sy = g.sy
|
||||||
if mseX > x and mseX < x + sx:
|
if x < mseX < x + sx:
|
||||||
if mseY > y and mseY < y + sy:
|
if y < mseY < y + sy:
|
||||||
posX = x
|
posX = x
|
||||||
posY = y
|
posY = y
|
||||||
gridBox = grid
|
|
||||||
|
|
||||||
# SCREEN.blit(imgTree, (posX, posY))
|
|
||||||
|
|
||||||
# ---------------------------------------
|
|
||||||
bo = Box(posX, posY, sx, sy, BLUE)
|
bo = Box(posX, posY, sx, sy, BLUE)
|
||||||
boxObjects[boxes] = bo
|
boxObjects[boxes] = bo
|
||||||
# boxes += 1
|
# boxes += 1
|
||||||
@ -470,9 +412,6 @@ def eventHandler(kbdObj,mouseObj):
|
|||||||
|
|
||||||
print(' goalNode x=', goalNode[0], 'goalNode y=', goalNode[1])
|
print(' goalNode x=', goalNode[0], 'goalNode y=', goalNode[1])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# pygame.display.update()
|
# pygame.display.update()
|
||||||
|
|
||||||
# goalNode = (x/sx) * (y/sy)
|
# goalNode = (x/sx) * (y/sy)
|
||||||
@ -482,10 +421,10 @@ def eventHandler(kbdObj,mouseObj):
|
|||||||
# If Key_x is pressed, spawn tree
|
# If Key_x is pressed, spawn tree
|
||||||
if kbdObj[pygame.K_t]:
|
if kbdObj[pygame.K_t]:
|
||||||
w = random.randint(0, 1)
|
w = random.randint(0, 1)
|
||||||
f=random.randint(0, 1)
|
f = random.randint(0, 1)
|
||||||
print(w)
|
print(w)
|
||||||
print(f)
|
print(f)
|
||||||
img = generate(w,f,random.randint(0,2))
|
img = generate(w, f, random.randint(0, 2))
|
||||||
img.save('./test/00/test.png')
|
img.save('./test/00/test.png')
|
||||||
|
|
||||||
data_transform = transforms.Compose([
|
data_transform = transforms.Compose([
|
||||||
@ -500,15 +439,15 @@ def eventHandler(kbdObj,mouseObj):
|
|||||||
target_transform=None)
|
target_transform=None)
|
||||||
model1.eval()
|
model1.eval()
|
||||||
res = model1(train_data[0][0])
|
res = model1(train_data[0][0])
|
||||||
if res[0] ==res.max():
|
if res[0] == res.max():
|
||||||
print("0 0")
|
print("0 0")
|
||||||
if res[1] ==res.max():
|
if res[1] == res.max():
|
||||||
print("0 1")
|
print("0 1")
|
||||||
if res[2] ==res.max():
|
if res[2] == res.max():
|
||||||
print("1 0")
|
print("1 0")
|
||||||
if res[3] ==res.max():
|
if res[3] == res.max():
|
||||||
print("1 1")
|
print("1 1")
|
||||||
#img.show()
|
# img.show()
|
||||||
if kbdObj[pygame.K_x]:
|
if kbdObj[pygame.K_x]:
|
||||||
obs = Obstacle(mouseObj)
|
obs = Obstacle(mouseObj)
|
||||||
obstacleObjects[obstacles] = obs
|
obstacleObjects[obstacles] = obs
|
||||||
@ -527,44 +466,22 @@ def eventHandler(kbdObj,mouseObj):
|
|||||||
y = g.y
|
y = g.y
|
||||||
sx = g.sx
|
sx = g.sx
|
||||||
sy = g.sy
|
sy = g.sy
|
||||||
if mseX > x and mseX < x + sx:
|
if x < mseX < x + sx:
|
||||||
if mseY > y and mseY < y + sy:
|
if y < mseY < y + sy:
|
||||||
posX = x
|
posX = x
|
||||||
posY = y
|
posY = y
|
||||||
|
|
||||||
T[int((posX/50)-1)][int((posY/50)-1)].plantType=4
|
T[int((posX/50)-1)][int((posY/50)-1)].plantType = 4
|
||||||
|
|
||||||
|
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
pygame.time.wait(DELAY)
|
pygame.time.wait(DELAY)
|
||||||
|
|
||||||
|
|
||||||
# if Key_SPACE is pressed, start the magic
|
# if Key_SPACE is pressed, start the magic
|
||||||
if kbdObj[pygame.K_SPACE]:
|
if kbdObj[pygame.K_SPACE]:
|
||||||
Ucelu = False
|
Ucelu = False
|
||||||
gBox = getGridBoxes(1)
|
|
||||||
|
|
||||||
x = gBox.x
|
|
||||||
y = gBox.y
|
|
||||||
sx = gBox.sx
|
|
||||||
sy = gBox.sy
|
|
||||||
|
|
||||||
x = (player.x +1) * 50
|
|
||||||
y = (player.y +1) * 50
|
|
||||||
|
|
||||||
# tmpImg = pygame.transform.rotate(imgPlayer, player.rotation)
|
|
||||||
# SCREEN.blit(tmpImg, (50 + 50 * player.x, 50 + 50 * player.y))
|
|
||||||
# pygame.display.update()
|
|
||||||
|
|
||||||
#when on it keeps flashing - among others
|
|
||||||
#bo = Box(x, y, sx, sy, RED)
|
|
||||||
#boxObjects[boxes] = bo
|
|
||||||
|
|
||||||
# boxes += 1
|
|
||||||
boxes = 1
|
boxes = 1
|
||||||
|
|
||||||
# startNode.state = (10 * (player.x + 1) + (player.y + 1) - 10)
|
|
||||||
|
|
||||||
startNode.x = player.x + 1
|
startNode.x = player.x + 1
|
||||||
startNode.y = player.y + 1
|
startNode.y = player.y + 1
|
||||||
|
|
||||||
@ -579,15 +496,12 @@ def eventHandler(kbdObj,mouseObj):
|
|||||||
|
|
||||||
print(' startNode x=', startNode.x, 'startNode y= ', startNode.y, 'startNode direction =', startNode.direction)
|
print(' startNode x=', startNode.x, 'startNode y= ', startNode.y, 'startNode direction =', startNode.direction)
|
||||||
|
|
||||||
graph = generateGraph(GRIDY,GRIDX)
|
graph = generateGraph(GRIDY, GRIDX)
|
||||||
print(graph)
|
print(graph)
|
||||||
|
|
||||||
# if startNode != goalNode:
|
|
||||||
if startNode.x != goalNode[0] or startNode.y != goalNode[1]:
|
if startNode.x != goalNode[0] or startNode.y != goalNode[1]:
|
||||||
elem = []
|
|
||||||
|
|
||||||
|
move_list = (graphsearch(goalNode, startNode)) # przeszukiwanie grafu wszerz
|
||||||
move_list = (graphsearch([], [], goalNode, startNode)) # przeszukiwanie grafu wszerz
|
|
||||||
|
|
||||||
pathFound = move_list
|
pathFound = move_list
|
||||||
|
|
||||||
@ -599,32 +513,11 @@ def eventHandler(kbdObj,mouseObj):
|
|||||||
pygame.time.wait(DELAY)
|
pygame.time.wait(DELAY)
|
||||||
# startNode = goalNode
|
# startNode = goalNode
|
||||||
|
|
||||||
|
|
||||||
if kbdObj[pygame.K_b]:
|
if kbdObj[pygame.K_b]:
|
||||||
Ucelu = False
|
Ucelu = False
|
||||||
gBox = getGridBoxes(1)
|
|
||||||
|
|
||||||
x = gBox.x
|
|
||||||
y = gBox.y
|
|
||||||
sx = gBox.sx
|
|
||||||
sy = gBox.sy
|
|
||||||
|
|
||||||
x = (player.x +1) * 50
|
|
||||||
y = (player.y +1) * 50
|
|
||||||
|
|
||||||
# tmpImg = pygame.transform.rotate(imgPlayer, player.rotation)
|
|
||||||
# SCREEN.blit(tmpImg, (50 + 50 * player.x, 50 + 50 * player.y))
|
|
||||||
# pygame.display.update()
|
|
||||||
|
|
||||||
#when on it keeps flashing - among others
|
|
||||||
#bo = Box(x, y, sx, sy, RED)
|
|
||||||
#boxObjects[boxes] = bo
|
|
||||||
|
|
||||||
# boxes += 1
|
|
||||||
boxes = 1
|
boxes = 1
|
||||||
|
|
||||||
# startNode.state = (10 * (player.x + 1) + (player.y + 1) - 10)
|
|
||||||
|
|
||||||
startNode.x = player.x + 1
|
startNode.x = player.x + 1
|
||||||
startNode.y = player.y + 1
|
startNode.y = player.y + 1
|
||||||
|
|
||||||
@ -639,22 +532,11 @@ def eventHandler(kbdObj,mouseObj):
|
|||||||
|
|
||||||
print(' startNode x=', startNode.x, 'startNode y= ', startNode.y, 'startNode direction =', startNode.direction)
|
print(' startNode x=', startNode.x, 'startNode y= ', startNode.y, 'startNode direction =', startNode.direction)
|
||||||
|
|
||||||
# startNode = (((player.x + 1)*10 - 9) * (player.y + 1) )
|
graph = generateGraph(GRIDY, GRIDX)
|
||||||
# startNode = 2
|
|
||||||
|
|
||||||
# tmpImg = pygame.transform.rotate(imgPlayer, player.rotation)
|
|
||||||
# SCREEN.blit(tmpImg, (55 + 50 * player.x, 55 + 50 * player.y))
|
|
||||||
# pygame.display.update()
|
|
||||||
|
|
||||||
# Delay to avoid multiple spawning of objects
|
|
||||||
#pygame.time.wait(DELAY)
|
|
||||||
|
|
||||||
graph = generateGraph(GRIDY,GRIDX)
|
|
||||||
print(graph)
|
print(graph)
|
||||||
|
|
||||||
# if startNode != goalNode:
|
# if startNode != goalNode:
|
||||||
if startNode.x != goalNode[0] or startNode.y != goalNode[1]:
|
if startNode.x != goalNode[0] or startNode.y != goalNode[1]:
|
||||||
elem = []
|
|
||||||
|
|
||||||
move_list = (astar.graphsearch([], astar.f, [], goalNode, startNode, T, succ)) # przeszukiwanie grafu wszerz
|
move_list = (astar.graphsearch([], astar.f, [], goalNode, startNode, T, succ)) # przeszukiwanie grafu wszerz
|
||||||
|
|
||||||
@ -665,17 +547,11 @@ def eventHandler(kbdObj,mouseObj):
|
|||||||
print(move_list)
|
print(move_list)
|
||||||
print('\n')
|
print('\n')
|
||||||
|
|
||||||
|
|
||||||
# else:
|
|
||||||
# startNode = (10 * (player.x + 1) + (player.y + 1) - 10)
|
|
||||||
# Ucelu = True
|
|
||||||
|
|
||||||
# Delay to avoid multiple spawning of objects
|
# Delay to avoid multiple spawning of objects
|
||||||
pygame.time.wait(DELAY)
|
pygame.time.wait(DELAY)
|
||||||
|
|
||||||
#With it it keeps going, if without it turns off
|
# With it it keeps going, if without it turns off
|
||||||
|
|
||||||
# Ucelu = False
|
|
||||||
|
|
||||||
T = [[Field(1,0,0,0,0,0),Field(0,0,1,0,0,0),Field(1,2,1,0,0,0),Field(1,3,0,0,0,0),Field(0,3,0,0,0,0),Field(0,0,1,0,0,0),Field(0,0,0,0,0,0),Field(1,3,1,0,0,0),Field(1,0,0,0,0,0),Field(1,0,1,0,0,0)],
|
T = [[Field(1,0,0,0,0,0),Field(0,0,1,0,0,0),Field(1,2,1,0,0,0),Field(1,3,0,0,0,0),Field(0,3,0,0,0,0),Field(0,0,1,0,0,0),Field(0,0,0,0,0,0),Field(1,3,1,0,0,0),Field(1,0,0,0,0,0),Field(1,0,1,0,0,0)],
|
||||||
[Field(1,0,0,0,0,0),Field(0,0,1,0,0,0),Field(1,0,1,0,0,0),Field(1,3,1,0,0,0),Field(0,0,0,0,0,0),Field(0,0,0,0,0,0),Field(0,0,0,0,0,0),Field(1,3,1,0,0,0),Field(1,0,0,0,0,0),Field(1,0,1,0,0,0)],
|
[Field(1,0,0,0,0,0),Field(0,0,1,0,0,0),Field(1,0,1,0,0,0),Field(1,3,1,0,0,0),Field(0,0,0,0,0,0),Field(0,0,0,0,0,0),Field(0,0,0,0,0,0),Field(1,3,1,0,0,0),Field(1,0,0,0,0,0),Field(1,0,1,0,0,0)],
|
||||||
@ -688,20 +564,6 @@ T = [[Field(1,0,0,0,0,0),Field(0,0,1,0,0,0),Field(1,2,1,0,0,0),Field(1,3,0,0,0,0
|
|||||||
[Field(1,0,0,0,0,0),Field(0,2,0,0,0,0),Field(1,1,0,0,0,0),Field(1,0,1,0,0,0),Field(0,2,1,0,0,0),Field(0,3,0,0,0,0),Field(0,0,0,0,0,0),Field(1,0,1,0,0,0),Field(1,0,0,0,0,0),Field(1,0,1,0,0,0)],
|
[Field(1,0,0,0,0,0),Field(0,2,0,0,0,0),Field(1,1,0,0,0,0),Field(1,0,1,0,0,0),Field(0,2,1,0,0,0),Field(0,3,0,0,0,0),Field(0,0,0,0,0,0),Field(1,0,1,0,0,0),Field(1,0,0,0,0,0),Field(1,0,1,0,0,0)],
|
||||||
[Field(1,0,1,0,0,0),Field(0,0,0,0,0,0),Field(1,1,1,0,0,0),Field(1,0,0,0,0,0),Field(0,1,1,0,0,0),Field(0,0,1,0,0,0),Field(0,0,0,0,0,0),Field(1,0,1,0,0,0),Field(1,0,0,0,0,0),Field(1,0,1,0,0,0)]]
|
[Field(1,0,1,0,0,0),Field(0,0,0,0,0,0),Field(1,1,1,0,0,0),Field(1,0,0,0,0,0),Field(0,1,1,0,0,0),Field(0,0,1,0,0,0),Field(0,0,0,0,0,0),Field(1,0,1,0,0,0),Field(1,0,0,0,0,0),Field(1,0,1,0,0,0)]]
|
||||||
|
|
||||||
|
|
||||||
#T = [[Field(1,0,0,0,0,0),Field(0,0,1,0,0,0),Field(1,2,1,0,0,0),Field(1,3,0,0,0,0),Field(0,3,0,0,0,0),Field(0,0,1,0,0,0),Field(0,3,0,0,0,0),Field(1,0,1,0,0,0),Field(1,3,0,0,0,0),Field(1,2,1,0,0,0)],
|
|
||||||
# [Field(1,0,0,0,0,0),Field(0,0,1,0,0,0),Field(1,0,1,0,0,0),Field(1,3,1,0,0,0),Field(0,0,0,0,0,0),Field(0,0,0,0,0,0),Field(0,2,0,0,0,0),Field(1,1,0,0,0,0),Field(1,0,1,0,0,0),Field(1,1,0,0,0,0)],
|
|
||||||
# [Field(0,2,1,0,0,0),Field(0,0,1,0,0,0),Field(1,0,0,0,0,0),Field(1,0,0,0,0,0),Field(0,2,1,0,0,0),Field(0,1,1,0,0,0),Field(0,2,0,0,0,0),Field(1,0,0,0,0,0),Field(1,0,0,0,0,0),Field(1,1,0,0,0,0)],
|
|
||||||
# [Field(1,0,1,0,0,0),Field(0,0,1,0,0,0),Field(1,1,1,0,0,0),Field(1,2,0,0,0,0),Field(0,3,1,0,0,0),Field(0,1,0,0,0,0),Field(0,0,0,0,0,0),Field(1,2,0,0,0,0),Field(1,0,0,0,0,0),Field(1,0,0,0,0,0)],
|
|
||||||
# [Field(1,3,0,0,0,0),Field(0,3,1,0,0,0),Field(1,2,1,0,0,0),Field(1,1,1,0,0,0),Field(0,1,1,0,0,0),Field(0,0,0,0,0,0),Field(0,0,1,0,0,0),Field(1,0,1,0,0,0),Field(1,3,0,0,0,0),Field(1,0,1,0,0,0)],
|
|
||||||
# [Field(1,0,0,0,0,0),Field(0,0,1,0,0,0),Field(1,1,1,0,0,0),Field(1,2,0,0,0,0),Field(0,1,0,0,0,0),Field(0,2,0,0,0,0),Field(0,1,0,0,0,0),Field(1,0,1,0,0,0),Field(1,0,0,0,0,0),Field(1,1,1,0,0,0)],
|
|
||||||
# [Field(1,0,0,0,0,0),Field(0,0,0,0,0,0),Field(1,1,1,0,0,0),Field(1,2,0,0,0,0),Field(0,0,1,0,0,0),Field(0,0,1,0,0,0),Field(0,1,0,0,0,0),Field(1,2,1,0,0,0),Field(1,2,1,0,0,0),Field(1,0,0,0,0,0)],
|
|
||||||
# [Field(1,0,0,0,0,0),Field(0,0,1,0,0,0),Field(1,3,1,0,0,0),Field(1,2,1,0,0,0),Field(0,0,1,0,0,0),Field(0,0,0,0,0,0),Field(0,1,1,0,0,0),Field(1,0,0,0,0,0),Field(1,1,1,0,0,0),Field(1,1,1,0,0,0)],
|
|
||||||
# [Field(1,0,0,0,0,0),Field(0,2,0,0,0,0),Field(1,1,0,0,0,0),Field(1,0,1,0,0,0),Field(0,2,1,0,0,0),Field(0,3,0,0,0,0),Field(0,0,0,0,0,0),Field(1,0,0,0,0,0),Field(1,2,1,0,0,0),Field(1,2,1,0,0,0)],
|
|
||||||
# [Field(1,0,1,0,0,0),Field(0,0,0,0,0,0),Field(1,1,1,0,0,0),Field(1,0,0,0,0,0),Field(0,1,1,0,0,0),Field(0,0,1,0,0,0),Field(0,1,0,0,0,0),Field(1,1,1,0,0,0),Field(1,0,1,0,0,0),Field(1,0,0,0,0,0)]]
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# =========================================================================================
|
# =========================================================================================
|
||||||
# no i tutaj mamy główna pętlę programu
|
# no i tutaj mamy główna pętlę programu
|
||||||
|
|
||||||
@ -718,12 +580,12 @@ while running:
|
|||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
kbd = pygame.key.get_pressed()
|
kbd = pygame.key.get_pressed()
|
||||||
mse = pygame.mouse.get_pos()
|
mse = pygame.mouse.get_pos()
|
||||||
UIHandler(mse)
|
UIHandler()
|
||||||
eventHandler(kbd, mse)
|
eventHandler(kbd, mse)
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
# CLOCK.tick(FPS)
|
# CLOCK.tick(FPS)
|
||||||
|
|
||||||
#screen.fill((175, 255, 50, 0))
|
# screen.fill((175, 255, 50, 0))
|
||||||
|
|
||||||
# SCREEN.fill((WHITE))
|
# SCREEN.fill((WHITE))
|
||||||
imgWheat = pygame.image.load('img/wheat.png')
|
imgWheat = pygame.image.load('img/wheat.png')
|
||||||
@ -745,7 +607,7 @@ while running:
|
|||||||
else:
|
else:
|
||||||
# a = 1
|
# a = 1
|
||||||
color = (50, 25, 0, 0)
|
color = (50, 25, 0, 0)
|
||||||
#colour from the beginning
|
# colour from the beginning
|
||||||
pygame.draw.rect(SCREEN, color, pygame.Rect(50 + 50 * i, 50 + 50 * j, 50, 50))
|
pygame.draw.rect(SCREEN, color, pygame.Rect(50 + 50 * i, 50 + 50 * j, 50, 50))
|
||||||
if T[i][j].plantType == 1:
|
if T[i][j].plantType == 1:
|
||||||
SCREEN.blit(imgWheat, (50 + 50 * i, 50 + 50 * j))
|
SCREEN.blit(imgWheat, (50 + 50 * i, 50 + 50 * j))
|
||||||
@ -756,7 +618,6 @@ while running:
|
|||||||
if T[i][j].plantType == 4:
|
if T[i][j].plantType == 4:
|
||||||
SCREEN.blit(imgTree, (50 + 50 * i, 50 + 50 * j))
|
SCREEN.blit(imgTree, (50 + 50 * i, 50 + 50 * j))
|
||||||
|
|
||||||
|
|
||||||
j = j + 1
|
j = j + 1
|
||||||
i = i + 1
|
i = i + 1
|
||||||
|
|
||||||
@ -770,34 +631,26 @@ while running:
|
|||||||
obstacleObjects[obs].draw()
|
obstacleObjects[obs].draw()
|
||||||
|
|
||||||
# if startNode.state != goalNode.state:
|
# if startNode.state != goalNode.state:
|
||||||
if startNode.x != goalNode[0] or startNode.y != goalNode[1] :
|
if startNode.x != goalNode[0] or startNode.y != goalNode[1]:
|
||||||
for bx in boxObjects:
|
for bx in boxObjects:
|
||||||
boxObjects[bx].draw()
|
boxObjects[bx].draw()
|
||||||
|
|
||||||
|
|
||||||
tmpImg = pygame.transform.rotate(imgPlayer, player.rotation)
|
tmpImg = pygame.transform.rotate(imgPlayer, player.rotation)
|
||||||
if player.rotation == 180:
|
if player.rotation == 180:
|
||||||
tmpImg = pygame.transform.flip(tmpImg, True, True)
|
tmpImg = pygame.transform.flip(tmpImg, True, True)
|
||||||
tmpImg = pygame.transform.flip(tmpImg, True, False)
|
tmpImg = pygame.transform.flip(tmpImg, True, False)
|
||||||
|
|
||||||
#player seen at the beginning
|
# player seen at the beginning
|
||||||
SCREEN.blit(tmpImg, (55 + 50 * player.x, 55 + 50 * player.y))
|
SCREEN.blit(tmpImg, (55 + 50 * player.x, 55 + 50 * player.y))
|
||||||
|
|
||||||
|
font = pygame.font.SysFont('comicsans', 22)
|
||||||
|
label = font.render('F - cel | X - drzewo', True, (0, 0, 0))
|
||||||
# if Ucelu == False:
|
label1 = font.render('ARROWS - ręczne poruszanie', True, (0, 0, 0))
|
||||||
# for bx in boxObjects:
|
label2 = font.render('A - lewo | D - prawo | W - ruch', True, (0, 0, 0))
|
||||||
# boxObjects[bx].draw()
|
label3 = font.render('SPACE - start BFS | B - start A*', True, (0, 0, 0))
|
||||||
|
SCREEN.blit(label, (10, 555))
|
||||||
|
SCREEN.blit(label1, (10, 580))
|
||||||
font = pygame.font.SysFont('comicsans', 18)
|
SCREEN.blit(label2, (10, 605))
|
||||||
label = font.render('f- punkt końcowy, x- drzewa, spacja- uruchomienie', 1, (0, 0, 0))
|
|
||||||
label1 = font.render('strzałki-ręczne poruszanie traktorem,', 1, (0, 0, 0))
|
|
||||||
label2 = font.render('a- obrót w lewo, d- w prawo, w-ruch naprzód', 1, (0, 0, 0))
|
|
||||||
label3 = font.render('b - uruchom A*', 1, (0, 0, 0))
|
|
||||||
SCREEN.blit(label, (10, 570))
|
|
||||||
SCREEN.blit(label1, (10, 590))
|
|
||||||
SCREEN.blit(label2, (10, 610))
|
|
||||||
SCREEN.blit(label3, (10, 630))
|
SCREEN.blit(label3, (10, 630))
|
||||||
|
|
||||||
# pygame.display.flip()
|
# pygame.display.flip()
|
||||||
@ -807,4 +660,4 @@ while running:
|
|||||||
|
|
||||||
# Done! Time to quit.
|
# Done! Time to quit.
|
||||||
|
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
Loading…
Reference in New Issue
Block a user