Compare commits

..

2 Commits

Author SHA1 Message Date
Aga
a9cdee96e0 Merge remote-tracking branch 'origin/master'
# Conflicts:
#	bfs.py
#	main.py
#	screen.py
2023-05-03 23:19:16 +02:00
Aga
8f296005d1 poprawienie bfs, start A* 2023-05-03 23:12:41 +02:00
6 changed files with 741 additions and 171 deletions

201
astar.py Normal file
View File

@ -0,0 +1,201 @@
from operator import itemgetter
import cart
import copy
from classes import Field
class Istate:
def __init__(self, direction, x, y):
self.direction = direction
self.x = x
self.y = y
def get_direction(self):
return self.direction
def set_direction(self, direction):
self.direction = direction
def get_x(self):
return self.x
def set_x(self, x):
self.x = x
def get_y(self):
return self.y
def set_y(self, y):
self.y = y
class Node:
def __init__(self, action, direction, parent, x, y):
self.action = action
self.direction = direction
self.parent = parent
self.x = x
self.y = y
def get_action(self):
return self.action
def set_action(self, action):
self.action = action
def get_direction(self):
return self.direction
def set_direction(self, direction):
self.direction = direction
def get_parent(self):
return self.parent
def set_parent(self, parent):
self.parent = parent
def get_x(self):
return self.x
def set_x(self, x):
self.x = x
def get_y(self):
return self.y
def set_y(self, y):
self.y = y
def fieldCost(T,node):
c = 0
if T[node.x-1][node.y-1].plantType == 1:
c =2
elif T[node.x-1][node.y-1].plantType == 2:
c =5
elif T[node.x-1][node.y-1].plantType == 3:
c =13
elif T[node.x-1][node.y-1].plantType == 4:
c =100000
else:
c=0
if T[node.x-1][node.y-1].isWet == 1:
c = c + 4
else:
c=c+1
return c
def cost(T, node):
cost = 0
while (node.get_parent() != None):
cost = cost + fieldCost(T, node)
node = node.get_parent()
return cost
def f(goaltest, map, node):
return cost(map, node) + heuristic(goaltest, node)
def goal_test(elem,goaltest):
if elem.get_x() == goaltest[0] and elem.get_y() == goaltest[1]:
return True
else:
return False
def graphsearch(explored, f, fringe, goaltest, istate, map, succ): # przeszukiwanie grafu wszerz
node = Node(None, istate.get_direction(), None, istate.get_x(), istate.get_y())
fringe.append((node, 0)) # wierzchołki do odwiedzenia z priorytetem
while True:
if not fringe:
return False
elem = fringe.pop(0) # zdejmujemy wierzchołek z kolejki fringe i rozpatrujemy go
temp = copy.copy(elem[0])
if goal_test(elem[0], goaltest) is True: # jeżeli osiągniemy cel w trakcie przeszukiwania grafu wszerz (wjedziemy na pole docelowe) : zwracamy listę ruchów, po których wykonaniu dotrzemy na miejsce
return print_moves(elem[0])
explored.append(elem) # dodajemy wierzchołek do listy wierzchołków odwiedzonych
for (action, state) in succ(temp): # iterujemy po wszystkich możliwych akcjach i stanach otrzymanych dla danego wierzchołka grafu
fringe_tuple = []
fringe_tuple_prio = []
explored_tuple = []
for (x, y) in fringe:
fringe_tuple.append((x.get_direction(), x.get_x(), x.get_y()))
fringe_tuple_prio.append(((x.get_direction(), x.get_x(), x.get_y()), y))
for (x, y) in explored:
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
p = f(goaltest, map, x) # liczy priorytet
#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
fringe.append((x, p)) # dodanie wierzchołka na fringe
fringe = sorted(fringe, key=itemgetter(1)) # sortowanie fringe'a według priorytetu
elif state in fringe_tuple:
i = 0
for (state_prio, r) in fringe_tuple_prio:
if str(state_prio) == str(state):
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.pop(i + 1)
fringe = sorted(fringe, key=itemgetter(1)) # sortowanie fringe'a według priorytetu
break
i = i + 1
def heuristic(goaltest, node):
return abs(node.get_x() - goaltest[0]) + abs(node.get_y() - goaltest[1])
def print_moves(elem):
moves_list = []
while (elem.get_parent() != None):
moves_list.append(elem.get_action())
elem = elem.get_parent()
moves_list.reverse()
return moves_list
def succ(elem):
actions_list = []
temp = copy.copy(elem.get_direction())
if temp == 1:
temp = 4
else:
temp = temp - 1
actions_list.append(("rotate_right", (temp, elem.get_x(), elem.get_y())))
temp = copy.copy(elem.get_direction())
if temp == 4:
temp = 1
else:
temp = temp + 1
actions_list.append(("rotate_left", (temp, elem.get_x(), elem.get_y())))
temp_move_south = elem.get_y() - 1
temp_move_west = elem.get_x() - 1
temp_move_east = elem.get_x() + 1
temp_move_north = elem.get_y() + 1
if cart.Cart.is_move_allowed_succ(elem) == "x + 1":
actions_list.append(("move", (elem.get_direction(), temp_move_east, elem.get_y())))
elif cart.Cart.is_move_allowed_succ(elem) == "y + 1":
actions_list.append(("move", (elem.get_direction(), elem.get_x(), temp_move_north)))
elif cart.Cart.is_move_allowed_succ(elem) == "y - 1":
actions_list.append(("move", (elem.get_direction(), elem.get_x(), temp_move_south)))
elif cart.Cart.is_move_allowed_succ(elem) == "x - 1":
actions_list.append(("move", (elem.get_direction(), temp_move_west, elem.get_y())))
return actions_list

185
bfs.py
View File

@ -1,45 +1,156 @@
class BFS: import sys
# Finds a suitable path from point A to point B using Breadth-First-Search Algorithm import cart
def __init__(self, graph, start, goal): import copy
self.graph = graph
self.start = start
self.goal = goal
def solve(self):
print('Start\n\n')
print(self.graph)
print('\n\n')
# keep track of explored nodes
explored = []
# keep track of all paths to be checked class Istate:
queue = [[self.start]] def __init__(self, direction, x, y):
self.direction = direction
self.x = x
self.y = y
# return path if start is goal def get_direction(self):
if self.start == self.goal: return self.direction
return 'That was easy. Start == Goal'
# keep looping until all possible paths are explored def set_direction(self, direction):
while queue: self.direction = direction
# pop the first path from the queue
path = queue.pop(0)
# get the last node from the path
node = path[-1]
if node not in explored: def get_x(self):
neighbors = self.graph[node] return self.x
# go through all neighbor nodes
# push it into the queue
for neighbor in neighbors:
new_path = list(path)
new_path.append(neighbor)
queue.append(new_path)
if neighbor == self.goal: def set_x(self, x):
return new_path self.x = x
# mark node as explored def get_y(self):
explored.append(node) return self.y
# in case there is no path def set_y(self, y):
return "path not accessible" self.y = y
class Node:
def __init__(self, action, direction, parent, x, y):
self.action = action
self.direction = direction
self.parent = parent
self.x = x
self.y = y
def get_action(self):
return self.action
def set_action(self, action):
self.action = action
def get_direction(self):
return self.direction
def set_direction(self, direction):
self.direction = direction
def get_parent(self):
return self.parent
def set_parent(self, parent):
self.parent = parent
def get_x(self):
return self.x
def set_x(self, x):
self.x = x
def get_y(self):
return self.y
def set_y(self, y):
self.y = y
def goal_test(goaltest,elem):
if elem.get_x() == goaltest[0] and elem.get_y() == goaltest[1]:
return True
else:
return False
# def graphsearch(explored, fringe, goaltest, istate, succ): # 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())
fringe = []
#elem = []
explored = []
#action = []
fringe.append(node) # wierzchołki do odwiedzenia
# fringe = [node]
while True:
if not fringe:
return False
elem = fringe.pop(0) # zdejmujemy wierzchołek z kolejki fringe i rozpatrujemy go
temp = copy.copy(elem)
if goal_test(goaltest,
elem) is True: # jeżeli osiągniemy cel w trakcie przeszukiwania grafu wsszerz, zwracamy listę ruchów, po których wykonaniu dotrzemy na miejsce
return print_moves(elem)
explored.append(elem) # dodajemy wierzchołek do listy wierzchołków odwiedzonych
for action, state in succ(temp): # iterujemy po wszystkich możliwych akcjach i stanach otrzymanych dla danego wierzchołka grafu
fringe_tuple = []
explored_tuple = []
for x in fringe:
fringe_tuple.append((x.get_direction(), x.get_x(), x.get_y()))
for x in explored:
explored_tuple.append((x.get_direction(), x.get_x(), x.get_y()))
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
x = Node(action, state[0], elem, state[1], state[2]) # stworzenie nowego wierzchołka, którego rodzicem jest elem
fringe.append(x) # dodanie wierzchołka na fringe
def print_moves(elem):
moves_list = []
while (elem.get_parent() != None):
moves_list.append(elem.get_action())
elem = elem.get_parent()
moves_list.reverse()
return moves_list
def succ(elem):
actions_list = []
temp = copy.copy(elem.get_direction())
if temp == 1:
temp = 4
else:
temp = temp - 1
actions_list.append(("rotate_right", (temp, elem.get_x(), elem.get_y())))
temp = copy.copy(elem.get_direction())
if temp == 4:
temp = 1
else:
temp = temp + 1
actions_list.append(("rotate_left", (temp, elem.get_x(), elem.get_y())))
temp_move_south = elem.get_y() - 1
temp_move_west = elem.get_x() - 1
temp_move_east = elem.get_x() + 1
temp_move_north = elem.get_y() + 1
if cart.Cart.is_move_allowed_succ(elem) == "x + 1":
actions_list.append(("move", (elem.get_direction(), temp_move_east, elem.get_y())))
elif cart.Cart.is_move_allowed_succ(elem) == "y + 1":
actions_list.append(("move", (elem.get_direction(), elem.get_x(), temp_move_north)))
elif cart.Cart.is_move_allowed_succ(elem) == "y - 1":
actions_list.append(("move", (elem.get_direction(), elem.get_x(), temp_move_south)))
elif cart.Cart.is_move_allowed_succ(elem) == "x - 1":
actions_list.append(("move", (elem.get_direction(), temp_move_west, elem.get_y())))
return actions_list

77
cart.py Normal file
View File

@ -0,0 +1,77 @@
import definitions
class Cart:
def __init__(self, direction, x, y):
self.direction = direction # w którą stronę patrzy, zgodnie ze wskazówkami zegara (1 -: godzina 12, 2 : godzina 3, 3 : godzina 6, 4 : godzina 9)
self.x = x
self.y = y
def get_direction(self):
return self.direction
def set_direction(self, direction):
self.direction = direction
def get_x(self):
return self.x
def set_x(self, x):
self.x = x
def get_y(self):
return self.y
def set_y(self, y):
self.y = y
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
if self.direction == definitions.CART_DIRECTION_EAST and cart_rect.x + definitions.BLOCK_SIZE < definitions.WIDTH_MAP:
return True
elif self.direction == definitions.CART_DIRECTION_SOUTH and cart_rect.y - definitions.BLOCK_SIZE >= 0:
return True
elif self.direction == definitions.CART_DIRECTION_NORTH and cart_rect.y + definitions.BLOCK_SIZE < definitions.HEIGHT_MAP:
return True
elif self.direction == definitions.CART_DIRECTION_WEST and cart_rect.x - definitions.BLOCK_SIZE >= 0:
return True
else:
return False
@staticmethod
def is_move_allowed_succ(
node): # sprawdza czy dany ruch, który chce wykonać wózek jest możliwy, zwraca pozycje po wykonaniu ruchu, wersja node
if node.get_direction() == definitions.CART_DIRECTION_EAST and node.get_x() * definitions.BLOCK_SIZE + definitions.BLOCK_SIZE < definitions.WIDTH_MAP:
return "x + 1"
elif node.get_direction() == definitions.CART_DIRECTION_NORTH and node.get_y() * definitions.BLOCK_SIZE - definitions.BLOCK_SIZE >= 0:
return "y - 1"
elif node.get_direction() == definitions.CART_DIRECTION_SOUTH and node.get_y() * definitions.BLOCK_SIZE + definitions.BLOCK_SIZE < definitions.HEIGHT_MAP:
return "y + 1"
elif node.get_direction() == definitions.CART_DIRECTION_WEST and node.get_x() * definitions.BLOCK_SIZE - definitions.BLOCK_SIZE >= 0:
return "x - 1"
else:
return False
def move(self):
if self.direction == definitions.CART_DIRECTION_EAST:
self.x = self.x + definitions.BLOCK_SIZE
elif self.direction == definitions.CART_DIRECTION_NORTH:
self.y = self.y + definitions.BLOCK_SIZE
elif self.direction == definitions.CART_DIRECTION_SOUTH:
self.y = self.y - definitions.BLOCK_SIZE
elif self.direction == definitions.CART_DIRECTION_WEST:
self.x = self.x - definitions.BLOCK_SIZE
def rotate_right(self):
if self.direction == 1:
self.direction = 4
else:
self.direction = self.direction - 1
def rotate_left(self):
if self.direction == 4:
self.direction = 1
else:
self.direction = self.direction + 1

24
definitions.py Normal file
View File

@ -0,0 +1,24 @@
# definicje
import os
import pygame
pygame.init()
BLOCK_SIZE = 50
WHEAT_COST = 2
CARROT_COST = 5
CABBAGE_COST = 13
TREE_COST = 100000
DIRT_COST = 1
WET_DIRT_COST = 4
CART_DIRECTION_EAST = 1
CART_DIRECTION_NORTH = 2
CART_DIRECTION_SOUTH = 4
CART_DIRECTION_WEST = 3
HEIGHT_AMOUNT, WIDTH_AMOUNT = 11, 11
HEIGHT_MAP, WIDTH_MAP = BLOCK_SIZE * HEIGHT_AMOUNT, BLOCK_SIZE * WIDTH_AMOUNT
HEIGHT, WIDTH = HEIGHT_MAP + BLOCK_SIZE, WIDTH_MAP
IMAGE_SIZE_NEURAL_NETWORK = 16
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))

423
main.py
View File

@ -1,7 +1,10 @@
import pygame import pygame
import astar
from classes import Field, Plant, Fertilizer, Player from classes import Field, Plant, Fertilizer, Player
from bfs import BFS from bfs import Node
from bfs import Istate, print_moves, succ
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
@ -39,8 +42,12 @@ boxes = 1
obstacles = 1 obstacles = 1
# BFS Variables # BFS Variables
startNode = 0
goalNode = 0
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
@ -70,7 +77,7 @@ def generateGraph(row,col):
miniG = {} miniG = {}
for grid in range(len(gridObjects)): for grid in range(len(gridObjects)):
grid += 1 # Synchronize index grid += 1 # Synchronize index
mod = grid % col # Used to check the Top and Bottom Grid Boxes mod = grid % col # Used to check the Top and Bottom Grid Boxes!
gN = grid - 1 gN = grid - 1
gS = grid + 1 gS = grid + 1
gE = grid + col gE = grid + col
@ -81,7 +88,6 @@ 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, gW] miniG[grid] = [gN, gW]
miniG[grid] = [gN, 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, gE, gW] miniG[grid] = [gN, 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
@ -127,119 +133,142 @@ def generateGraph(row,col):
return miniG2 return miniG2
def refreshScreen():
#pygame.display.update()
#SCREEN.blit(tmpImg, (55 + 50 * player.x, 55 + 50 * player.y))
# -----------------------------
i = 0
while i < len(T):
j = 0
while j < len(T[i]):
#color = (255, 255, 255, 0)
if T[i][j].isWet == 0:
# a = 1
color = (160, 80, 40, 0)
else:
# a = 1
color = (50, 25, 0, 0)
#Covers 'player' on the way
pygame.draw.rect(SCREEN, color, pygame.Rect(50 + 50 * i, 50 + 50 * j, 50, 50))
if T[i][j].plantType == 1:
SCREEN.blit(imgWheat, (50 + 50 * i, 50 + 50 * j))
if T[i][j].plantType == 2:
SCREEN.blit(imgCarrot, (50 + 50 * i, 50 + 50 * j))
if T[i][j].plantType == 3:
SCREEN.blit(imgCabbage, (50 + 50 * i, 50 + 50 * j))
if T[i][j].plantType == 4:
SCREEN.blit(imgTree, (50 + 50 * i, 50 + 50 * j))
j = j + 1
i = i + 1
# Render the trees
for obs in obstacleObjects:
obstacleObjects[obs].draw()
for bx in boxObjects:
boxObjects[bx].draw()
i = 0
while i < len(T)+1:
pygame.draw.line(SCREEN, (0, 0, 0), (50 + i * 50, 50), (50 + i * 50, 50 + len(T) * 50), 1)
pygame.draw.line(SCREEN, (0, 0, 0), (50, 50 + i * 50), (50 + len(T) * 50, 50 + i * 50), 1)
i = i + 1
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))
# --------------------------------------
# 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.time.wait(300)
SCREEN.fill((WHITE))
def drawGraph(pathF): def drawGraph(pathF):
#Draws the path given the path-list #Draws the path given the path-list
global Ucelu global Ucelu
print(pathF) #print(pathF)
if Ucelu == False:
for grid in pathF:
g = gridObjects[grid] # Get the grid-box object mentioned in the path if (Ucelu == False):
x = g.x for grid in pathF:
y = g.y # g = gridObjects[grid] # Get the grid-box object mentioned in the path
sx = g.sx # x = g.x
sy = g.sy # y = g.y
a = 0 # sx = g.sx
# sy = g.sy
# a = 0
# pygame.draw.rect(SCREEN, GREEN, pygame.Rect(x, y, sx, sy)) # pygame.draw.rect(SCREEN, GREEN, pygame.Rect(x, y, sx, sy))
if player.x < (x/50 - 1): if grid == 'rotate_right':
a = 1 player.rotation = (player.rotation - 90) % 360
if player.x > (x/50 - 1): if grid == 'rotate_left':
a =2 player.rotation = (player.rotation + 90) %360
if player.y < (y/50 - 1):
a =3
if player.y > (y/50 - 1):
a =4
if a==1: #( player.rotation)
# 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
refreshScreen() if grid == 'move':
if player.rotation == 0:
if player.x < 9:
player.x = player.x + 1
if player.rotation == 180:
if player.x > 0:
player.x = player.x - 1
if player.rotation == 270:
if player.y < 9:
player.y = player.y + 1
if player.rotation == 90:
if player.y > 0:
player.y = player.y - 1
#pygame.time.wait(2000)
player.y = y/50 - 1 # if player.x < (x/50 - 1):
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
refreshScreen()
# 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
while i < len(T):
j = 0
while j < len(T[i]):
#color = (255, 255, 255, 0)
if T[i][j].isWet == 0:
# a = 1
color = (160, 80, 40, 0)
else:
# a = 1
color = (50, 25, 0, 0)
#Covers 'player' on the way
pygame.draw.rect(SCREEN, color, pygame.Rect(50 + 50 * i, 50 + 50 * j, 50, 50))
if T[i][j].plantType == 1:
SCREEN.blit(imgWheat, (50 + 50 * i, 50 + 50 * j))
if T[i][j].plantType == 2:
SCREEN.blit(imgCarrot, (50 + 50 * i, 50 + 50 * j))
if T[i][j].plantType == 3:
SCREEN.blit(imgCabbage, (50 + 50 * i, 50 + 50 * j))
if T[i][j].plantType == 4:
SCREEN.blit(imgTree, (50 + 50 * i, 50 + 50 * j))
j = j + 1
i = i + 1
# Render the trees
for obs in obstacleObjects:
obstacleObjects[obs].draw()
for bx in boxObjects:
boxObjects[bx].draw()
i = 0
while i < len(T)+1:
pygame.draw.line(SCREEN, (0, 0, 0), (50 + i * 50, 50), (50 + i * 50, 50 + len(T) * 50), 1)
pygame.draw.line(SCREEN, (0, 0, 0), (50, 50 + i * 50), (50 + len(T) * 50, 50 + i * 50), 1)
i = i + 1
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))
# --------------------------------------
# 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.time.wait(300)
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
@ -270,11 +299,10 @@ def eventHandler(kbdObj,mouseObj):
global Ucelu global Ucelu
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
exit("Thank you for new plants <3") running = False
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
pygame.time.wait(DELAY) pygame.time.wait(DELAY)
if event.key == pygame.K_LEFT: if event.key == pygame.K_LEFT:
if player.x > 0: if player.x > 0:
player.x = player.x - 1 player.x = player.x - 1
@ -295,7 +323,7 @@ def eventHandler(kbdObj,mouseObj):
player.y = player.y + 1 player.y = player.y + 1
player.rotation = 270 player.rotation = 270
#start lewo prawo, naprzód # Aga start lewo prawo, naprzód
if event.key == pygame.K_a: if event.key == pygame.K_a:
player.rotation = (player.rotation + 90) % 360 player.rotation = (player.rotation + 90) % 360
if event.key == pygame.K_d: if event.key == pygame.K_d:
@ -314,15 +342,13 @@ def eventHandler(kbdObj,mouseObj):
if player.y > 0: if player.y > 0:
player.y = player.y - 1 player.y = player.y - 1
# 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() # gBox = getGridBoxes()
x = mouseObj[0] #x = mouseObj[0]
y = mouseObj[1] #y = mouseObj[1]
# x = gBox.x # x = gBox.x
# y = gBox.y # y = gBox.y
sx = gBox.sx sx = gBox.sx
@ -352,7 +378,20 @@ def eventHandler(kbdObj,mouseObj):
boxes = 1 boxes = 1
# goalNode = GRIDX*GRIDX # goalNode = GRIDX*GRIDX
# goalNode = (10 * (x + 1) + (y + 1) - 10) # goalNode = (10 * (x + 1) + (y + 1) - 10)
goalNode = (10 * (posX/50 ) + (posY/50) - 10)
# goalNode.state = int(10 * (posX/50 ) + (posY/50) - 10)
# goalNode[0] = int((posX/50)
# goalNode[1] = int(posY/50) - 10
goalNode = [int(posX/50), int(posY/50)]
# goalNode = [10,10]
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)
@ -368,6 +407,24 @@ def eventHandler(kbdObj,mouseObj):
# print(obstacleObjects) # print(obstacleObjects)
gridObstacle[obs.gridBox] = obstacles gridObstacle[obs.gridBox] = obstacles
# Delay to avoid multiple spawning of objects # Delay to avoid multiple spawning of objects
mseX = mouseObj[0]
mseY = mouseObj[1]
for grid in gridObjects:
g = getGridBoxes(grid)
x = g.x
y = g.y
sx = g.sx
sy = g.sy
if mseX > x and mseX < x + sx:
if mseY > y and mseY < y + sy:
posX = x
posY = y
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)
@ -395,7 +452,83 @@ def eventHandler(kbdObj,mouseObj):
# boxes += 1 # boxes += 1
boxes = 1 boxes = 1
startNode = (10 * (player.x + 1) + (player.y + 1) - 10)
# startNode.state = (10 * (player.x + 1) + (player.y + 1) - 10)
startNode.x = player.x + 1
startNode.y = player.y + 1
if player.rotation == 0:
startNode.direction = 1
elif player.rotation == 90:
startNode.direction = 2
elif player.rotation == 180:
startNode.direction = 3
elif player.rotation == 270:
startNode.direction = 4
print(' startNode x=', startNode.x, 'startNode y= ', startNode.y, 'startNode direction =', startNode.direction)
graph = generateGraph(GRIDY,GRIDX)
print(graph)
# if startNode != goalNode:
if startNode.x != goalNode[0] or startNode.y != goalNode[1]:
elem = []
move_list = (graphsearch([], [], goalNode, startNode)) # przeszukiwanie grafu wszerz
pathFound = move_list
# pathFound = bfs.graphsearch()
print('akcje które wykonuję by znalezc sie u celu')
print(move_list)
print('\n')
# Delay to avoid multiple spawning of objects
pygame.time.wait(DELAY)
# startNode = goalNode
if kbdObj[pygame.K_b]:
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
# startNode.state = (10 * (player.x + 1) + (player.y + 1) - 10)
startNode.x = player.x + 1
startNode.y = player.y + 1
if player.rotation == 0:
startNode.direction = 1
elif player.rotation == 90:
startNode.direction = 2
elif player.rotation == 180:
startNode.direction = 3
elif player.rotation == 270:
startNode.direction = 4
print(' startNode x=', startNode.x, 'startNode y= ', startNode.y, 'startNode direction =', startNode.direction)
# startNode = (((player.x + 1)*10 - 9) * (player.y + 1) ) # startNode = (((player.x + 1)*10 - 9) * (player.y + 1) )
# startNode = 2 # startNode = 2
@ -407,39 +540,60 @@ def eventHandler(kbdObj,mouseObj):
#pygame.time.wait(DELAY) #pygame.time.wait(DELAY)
graph = generateGraph(GRIDY,GRIDX) graph = generateGraph(GRIDY,GRIDX)
print(graph)
# if startNode != goalNode:
if startNode.x != goalNode[0] or startNode.y != goalNode[1]:
elem = []
move_list = (astar.graphsearch([], astar.f, [], goalNode, startNode, T, succ)) # przeszukiwanie grafu wszerz
pathFound = move_list
# pathFound = bfs.graphsearch()
print('akcje które wykonuję by znalezc sie u celu')
print(move_list)
print('\n')
if startNode != goalNode:
bfs = BFS(graph, startNode, goalNode)
# print(bfs.solve())
pathFound = bfs.solve()
# else: # else:
# startNode = (10 * (player.x + 1) + (player.y + 1) - 10) # startNode = (10 * (player.x + 1) + (player.y + 1) - 10)
# Ucelu = True # Ucelu = True
# Delay to avoid multiple spawning of objects # Delay to avoid multiple spawning of objects
pygame.time.wait(DELAY) pygame.time.wait(DELAY)
# startNode = goalNode
#With it it keeps going, if without it turns off #With it it keeps going, if without it turns off
# Ucelu = False # 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)],
[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(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,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,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,3,1,0,0,0),Field(1,0,0,0,0,0),Field(1,0,1,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,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,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,0,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,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,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,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)]]
T = [[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,3,1,0,0,0),Field(0,0,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,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,0,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)], #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,1,1,0,0,0),Field(1,2,0,0,0,0),Field(0,3,1,0,0,0),Field(0,0,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,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(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,3,1,0,0,0),Field(0,0,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(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,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,3,1,0,0,0),Field(0,0,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,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,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,3,1,0,0,0),Field(0,0,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,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,3,1,0,0,0),Field(0,0,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,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,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,0,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,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,1,1,0,0,0),Field(1,2,0,0,0,0),Field(0,3,1,0,0,0),Field(0,0,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,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,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,0,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,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)]]
# ========================================================================================= # =========================================================================================
# main # no i tutaj mamy główna pętlę programu
pygame.init() pygame.init()
@ -505,7 +659,8 @@ while running:
for obs in obstacleObjects: for obs in obstacleObjects:
obstacleObjects[obs].draw() obstacleObjects[obs].draw()
if startNode != goalNode: # if startNode.state != goalNode.state:
if startNode.x != goalNode[0] or startNode.y != goalNode[1] :
for bx in boxObjects: for bx in boxObjects:
boxObjects[bx].draw() boxObjects[bx].draw()
@ -529,9 +684,11 @@ while running:
label = font.render('f- punkt końcowy, x- drzewa, spacja- uruchomienie', 1, (0, 0, 0)) 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)) 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)) 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(label, (10, 570))
SCREEN.blit(label1, (10, 590)) SCREEN.blit(label1, (10, 590))
SCREEN.blit(label2, (10, 610)) SCREEN.blit(label2, (10, 610))
SCREEN.blit(label3, (10, 630))
# pygame.display.flip() # pygame.display.flip()

View File

@ -1,2 +1,2 @@
import pygame import pygame
SCREEN = pygame.display.set_mode([600,650]) SCREEN = pygame.display.set_mode([600,665])