2022-04-03 19:57:22 +02:00
|
|
|
from asyncio.windows_events import NULL
|
2022-04-03 22:40:55 +02:00
|
|
|
from collections import deque
|
2022-04-03 19:57:22 +02:00
|
|
|
from lib2to3.pgen2.token import CIRCUMFLEX
|
|
|
|
from queue import Queue
|
2022-03-08 11:25:50 +01:00
|
|
|
import sys
|
2022-04-03 19:57:22 +02:00
|
|
|
from unicodedata import name
|
2022-03-08 11:25:50 +01:00
|
|
|
import pygame
|
2022-04-07 23:32:39 +02:00
|
|
|
import random
|
|
|
|
import time
|
2022-03-08 11:25:50 +01:00
|
|
|
|
|
|
|
screen = []
|
|
|
|
objectArray = []
|
2022-04-07 17:45:26 +02:00
|
|
|
pathToPrint = []
|
2022-03-08 11:25:50 +01:00
|
|
|
|
2022-04-07 23:32:39 +02:00
|
|
|
|
2022-04-03 19:57:22 +02:00
|
|
|
class Object:
|
2022-03-08 11:25:50 +01:00
|
|
|
def __init__(self, name, xPos, yPos):
|
|
|
|
self.name = name
|
|
|
|
self.xPos = xPos
|
|
|
|
self.yPos = yPos
|
2022-04-07 23:32:39 +02:00
|
|
|
|
2022-04-03 19:57:22 +02:00
|
|
|
class Field:
|
2022-04-07 23:32:39 +02:00
|
|
|
def __init__(self, xPos, yPos, isObject):
|
2022-04-03 19:57:22 +02:00
|
|
|
self.xPos = xPos
|
|
|
|
self.yPos = yPos
|
|
|
|
self.visited = False
|
|
|
|
self.parent = None
|
2022-04-07 23:32:39 +02:00
|
|
|
number = random.randint(0, 9)
|
|
|
|
if number > 7 and not isObject:
|
|
|
|
self.isBlock = True
|
|
|
|
else:
|
|
|
|
self.isBlock = False
|
|
|
|
|
2022-04-03 19:57:22 +02:00
|
|
|
def printXandY(self):
|
|
|
|
print("X: " + str(self.xPos) + ". Y: " + str(self.yPos))
|
2022-03-08 11:25:50 +01:00
|
|
|
|
|
|
|
|
2022-04-07 23:32:39 +02:00
|
|
|
def draw(square_num, objectArr, pathToTarget, num):
|
|
|
|
grid_color = (0, 0, 0)
|
|
|
|
grid_size = 500
|
|
|
|
square = grid_size/square_num
|
2022-03-08 11:25:50 +01:00
|
|
|
a = 50
|
2022-04-07 23:32:39 +02:00
|
|
|
b = 10
|
2022-03-08 11:25:50 +01:00
|
|
|
|
|
|
|
for i in range(square_num):
|
2022-04-07 23:32:39 +02:00
|
|
|
pygame.draw.line(screen, grid_color, (a + i*square, b),
|
|
|
|
(a + i*square, b + grid_size), 2)
|
2022-03-08 11:25:50 +01:00
|
|
|
|
2022-04-07 23:32:39 +02:00
|
|
|
pygame.draw.line(screen, grid_color, (a, b + i*square),
|
|
|
|
(a + grid_size, b + i*square), 2)
|
2022-03-08 11:25:50 +01:00
|
|
|
|
|
|
|
pygame.draw.line(screen, grid_color, (a, b + grid_size), (a + grid_size,
|
|
|
|
b + grid_size), 2)
|
|
|
|
pygame.draw.line(screen, grid_color, (a + grid_size, b),
|
|
|
|
(a + grid_size, b + grid_size), 2)
|
|
|
|
|
2022-04-07 23:32:39 +02:00
|
|
|
circleX = objectArr[0].xPos * square + square + square / 2
|
|
|
|
circleY = objectArr[0].yPos * square + square / 2
|
|
|
|
|
|
|
|
a = (10 + circleX, 5 + circleY)
|
|
|
|
b = (20 + circleX, 5 + circleY)
|
|
|
|
c = (15 + circleX, 20 + circleY)
|
|
|
|
|
|
|
|
|
|
|
|
polygon = pygame.draw.polygon(screen, (0, 0, 255), (b, a, c))
|
|
|
|
print(polygon)
|
|
|
|
|
2022-04-03 19:57:22 +02:00
|
|
|
|
2022-04-07 23:32:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
circleX = objectArray[1].xPos * square + square * 2
|
|
|
|
circleY = objectArr[1].yPos * square + square
|
2022-04-07 17:45:26 +02:00
|
|
|
radius = 8
|
2022-04-03 19:57:22 +02:00
|
|
|
grid_color = (255, 0, 0)
|
|
|
|
pygame.draw.circle(screen, grid_color, (circleX, circleY), radius)
|
2022-03-08 11:25:50 +01:00
|
|
|
|
2022-04-07 23:32:39 +02:00
|
|
|
|
2022-03-08 14:30:25 +01:00
|
|
|
def kb_listen(objectArray, gridLength):
|
2022-03-08 11:25:50 +01:00
|
|
|
for event in pygame.event.get():
|
2022-03-08 11:42:06 +01:00
|
|
|
if event.type == pygame.KEYDOWN:
|
2022-03-08 14:30:25 +01:00
|
|
|
if event.key == pygame.K_LEFT and objectArray[0].xPos > 0:
|
2022-03-08 11:42:06 +01:00
|
|
|
objectArray[0].xPos = objectArray[0].xPos - 1
|
2022-04-03 19:57:22 +02:00
|
|
|
print(objectArray[0].xPos)
|
2022-03-08 14:30:25 +01:00
|
|
|
if event.key == pygame.K_RIGHT and objectArray[0].xPos < gridLength - 1:
|
2022-03-08 11:42:06 +01:00
|
|
|
objectArray[0].xPos = objectArray[0].xPos + 1
|
2022-04-03 19:57:22 +02:00
|
|
|
print(objectArray[0].xPos)
|
2022-04-07 23:32:39 +02:00
|
|
|
if event.key == pygame.K_UP and objectArray[0].yPos > 0:
|
2022-03-08 11:42:06 +01:00
|
|
|
objectArray[0].yPos = objectArray[0].yPos - 1
|
2022-04-03 19:57:22 +02:00
|
|
|
print(objectArray[0].yPos)
|
2022-04-07 23:32:39 +02:00
|
|
|
if event.key == pygame.K_DOWN and objectArray[0].yPos < gridLength - 1:
|
2022-03-08 11:42:06 +01:00
|
|
|
objectArray[0].yPos = objectArray[0].yPos + 1
|
2022-04-03 19:57:22 +02:00
|
|
|
print(objectArray[0].yPos)
|
2022-03-08 11:25:50 +01:00
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
sys.exit()
|
2022-04-07 23:32:39 +02:00
|
|
|
|
|
|
|
|
2022-04-03 19:57:22 +02:00
|
|
|
fieldQueue = []
|
|
|
|
isGoalAchieved = False
|
|
|
|
|
|
|
|
def startQueue(agentX, agentY, fieldList, gridNum):
|
|
|
|
currentX = agentX
|
|
|
|
currentY = agentY
|
|
|
|
currentIndex = currentY * gridNum + currentX
|
|
|
|
|
|
|
|
fieldQueue.append(fieldList[currentIndex])
|
2022-04-03 21:09:15 +02:00
|
|
|
|
|
|
|
checkFlag = False
|
2022-04-07 23:32:39 +02:00
|
|
|
|
2022-04-03 21:09:15 +02:00
|
|
|
while not checkFlag:
|
|
|
|
result = checkGoal(fieldList, gridNum)
|
|
|
|
if result:
|
|
|
|
checkFlag = True
|
2022-04-03 22:40:55 +02:00
|
|
|
# print("NEXT CHECK GOAL")
|
2022-04-03 21:09:15 +02:00
|
|
|
return result
|
2022-04-07 23:32:39 +02:00
|
|
|
|
|
|
|
|
2022-04-03 21:09:15 +02:00
|
|
|
def checkGoal(fieldList, gridNum):
|
2022-04-03 19:57:22 +02:00
|
|
|
currentField = fieldQueue.pop(0)
|
2022-04-07 23:32:39 +02:00
|
|
|
|
2022-04-03 21:09:15 +02:00
|
|
|
currentY = currentField.yPos
|
|
|
|
currentX = currentField.xPos
|
|
|
|
currentIndex = currentY * gridNum + currentX
|
2022-04-03 22:40:55 +02:00
|
|
|
# print("POPPED!!!")
|
|
|
|
# currentField.printXandY()
|
2022-04-03 19:57:22 +02:00
|
|
|
##print(str(currentField.xPos) + " " + str(objectArray[1].xPos) + " " + str(currentField.yPos) + " " + str(objectArray[1].yPos))
|
2022-04-07 23:32:39 +02:00
|
|
|
|
|
|
|
# SPRAWDZAMY CZY DANE POLE JEST POLEM W KTORYM ZNAJDUJE SIE NASZ CEL
|
|
|
|
if currentField.xPos == objectArray[1].xPos and currentField.yPos == objectArray[1].yPos:
|
2022-04-03 21:09:15 +02:00
|
|
|
return currentField
|
2022-04-07 23:32:39 +02:00
|
|
|
|
|
|
|
# SPRAWDZAMY CZY DANE POLE ZOSTALO JUZ WCZESNIEJ ODWIEDZONE - JEZELI NIE TO USTAWIAMY STATUS
|
2022-04-03 19:57:22 +02:00
|
|
|
if not currentField.visited:
|
|
|
|
currentField.visited = True
|
2022-04-07 23:32:39 +02:00
|
|
|
|
|
|
|
# JEZELI DANE POLE NIE ZOSTALO ODWIEDZONE, TO DODAJEMY DO KOLEJKI WSZYSTKIE POLA OBOK I LECIMY DALEJ
|
|
|
|
if currentIndex + 15 <= 224 and fieldList[currentIndex + 15].visited == False and fieldList[currentIndex + 15].isBlock == False:
|
2022-04-03 19:57:22 +02:00
|
|
|
fieldQueue.append(fieldList[currentIndex + 15])
|
|
|
|
fieldList[currentIndex + 15].parent = fieldList[currentIndex]
|
2022-04-03 22:40:55 +02:00
|
|
|
# print("APPENDED DOWN!!!")
|
|
|
|
# fieldList[currentIndex + 15].printXandY()
|
2022-04-07 23:32:39 +02:00
|
|
|
if currentIndex - 15 > -1 and fieldList[currentIndex - 15].visited == False and fieldList[currentIndex - 15].isBlock == False:
|
2022-04-03 19:57:22 +02:00
|
|
|
fieldQueue.append(fieldList[currentIndex - 15])
|
|
|
|
fieldList[currentIndex - 15].parent = fieldList[currentIndex]
|
2022-04-03 22:40:55 +02:00
|
|
|
# print("APPENDED UP!!!")
|
|
|
|
# fieldList[currentIndex - 15].printXandY()
|
2022-04-07 23:32:39 +02:00
|
|
|
if (currentIndex + 1) % 15 != 0 and fieldList[currentIndex + 1].visited == False and fieldList[currentIndex + 1].isBlock == False:
|
2022-04-03 19:57:22 +02:00
|
|
|
fieldQueue.append(fieldList[currentIndex + 1])
|
|
|
|
fieldList[currentIndex + 1].parent = fieldList[currentIndex]
|
2022-04-03 22:40:55 +02:00
|
|
|
# print("APPENDED RIGHT!!!")
|
|
|
|
# fieldList[currentIndex + 1].printXandY()
|
2022-04-07 23:32:39 +02:00
|
|
|
if (currentIndex - 1) % 15 != 14 and not currentIndex - 1 < 0 and fieldList[currentIndex - 1].visited == False and fieldList[currentIndex - 1].isBlock == False:
|
2022-04-03 19:57:22 +02:00
|
|
|
fieldQueue.append(fieldList[currentIndex - 1])
|
|
|
|
fieldList[currentIndex - 1].parent = fieldList[currentIndex]
|
2022-04-03 22:40:55 +02:00
|
|
|
# print("APPENDED LEFT!!!")
|
|
|
|
# fieldList[currentIndex - 1].printXandY()
|
2022-03-08 11:25:50 +01:00
|
|
|
|
2022-04-07 23:32:39 +02:00
|
|
|
def generateStartEndPos():
|
|
|
|
startX = random.randint(0, 14)
|
|
|
|
startY = random.randint(0, 14)
|
|
|
|
print(startX)
|
|
|
|
targetX = None
|
|
|
|
targetY = None
|
2022-03-08 11:25:50 +01:00
|
|
|
|
2022-04-07 23:32:39 +02:00
|
|
|
while targetX == None and targetY == None or targetX == startX and targetY == startY:
|
|
|
|
targetX = random.randint(0, 14)
|
|
|
|
targetY = random.randint(0, 14)
|
|
|
|
|
|
|
|
return [startX, startY, targetX, targetY]
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
pygame.init() # inicjalizacja modułów, na razie niepotrzebna
|
2022-03-08 11:25:50 +01:00
|
|
|
|
|
|
|
width = 600
|
|
|
|
height = 530
|
2022-04-07 23:32:39 +02:00
|
|
|
screen = pygame.display.set_mode(
|
|
|
|
(width, height)) # ustalanie rozmiarów okna
|
|
|
|
|
|
|
|
# OBLICZANIE RANDOMOWYCH POL START I END
|
|
|
|
[sX, sY, eX, eY] = generateStartEndPos()
|
|
|
|
|
|
|
|
print(sX)
|
|
|
|
print(sY)
|
|
|
|
print(sX)
|
|
|
|
print(eY)
|
|
|
|
|
|
|
|
# GENEROWANIE WSZYSTKICH POL - FIELDS
|
2022-04-03 19:57:22 +02:00
|
|
|
fields = []
|
|
|
|
for y in range(15):
|
|
|
|
for x in range(15):
|
2022-04-07 23:32:39 +02:00
|
|
|
if x == sX and y == sY or x == eX and y == eY:
|
|
|
|
newField = Field(x, y, True)
|
|
|
|
else:
|
|
|
|
newField = Field(x, y, False)
|
2022-04-03 22:40:55 +02:00
|
|
|
# newField.printXandY()
|
2022-04-03 19:57:22 +02:00
|
|
|
fields.append(newField)
|
2022-04-07 23:32:39 +02:00
|
|
|
|
|
|
|
agent = Object("AGENT", sX, sY)
|
|
|
|
target = Object("TARGET", eX, eY)
|
|
|
|
|
|
|
|
# if fields[agent.getX - 1 *
|
|
|
|
|
|
|
|
objectArray.append(agent)
|
|
|
|
objectArray.append(target)
|
|
|
|
|
|
|
|
result = startQueue(objectArray[0].xPos,
|
|
|
|
objectArray[0].yPos, fields, 15)
|
2022-04-03 21:09:15 +02:00
|
|
|
|
2022-04-03 21:53:24 +02:00
|
|
|
|
2022-04-03 22:40:55 +02:00
|
|
|
print("RED DOT X AND Y POSITION ============================")
|
2022-04-07 23:32:39 +02:00
|
|
|
print("X: " + str(objectArray[1].xPos) + " Y: " + str(objectArray[1].yPos))
|
2022-04-03 22:40:55 +02:00
|
|
|
print("TRUCK X AND Y POSITION ============================")
|
2022-04-07 17:45:26 +02:00
|
|
|
print("X: " + str(objectArray[0].xPos) + " Y: " + str(objectArray[0].yPos))
|
2022-04-03 21:53:24 +02:00
|
|
|
|
|
|
|
result_parent = result.parent
|
|
|
|
|
2022-04-07 23:32:39 +02:00
|
|
|
currentIndex = (objectArray[0].yPos) * 15 + objectArray[0].xPos
|
2022-04-03 22:40:55 +02:00
|
|
|
pathToTarget = deque()
|
|
|
|
pathToTarget.appendleft(result)
|
|
|
|
|
|
|
|
while result_parent.parent != None:
|
|
|
|
pathToTarget.appendleft(result_parent)
|
2022-04-03 21:53:24 +02:00
|
|
|
result_parent = result_parent.parent
|
2022-04-03 22:40:55 +02:00
|
|
|
pathToTarget.appendleft(fields[currentIndex])
|
|
|
|
|
|
|
|
print("PRINTING PATH TO TARGET ============================")
|
|
|
|
for node in pathToTarget:
|
|
|
|
node.printXandY()
|
|
|
|
|
2022-03-08 11:25:50 +01:00
|
|
|
while 1:
|
2022-04-07 23:32:39 +02:00
|
|
|
# tymczasowy kolor tła - do usunięcia, jak już będzie zdjęcie
|
|
|
|
c = (255, 255, 255)
|
2022-03-08 11:42:06 +01:00
|
|
|
screen.fill(c)
|
2022-04-07 23:32:39 +02:00
|
|
|
draw(15, objectArray, pathToTarget, 1)
|
|
|
|
|
|
|
|
# TUTAJ RYSUJEMY SCIEZKE
|
2022-04-07 17:45:26 +02:00
|
|
|
for node in pathToTarget:
|
2022-04-07 23:32:39 +02:00
|
|
|
if node == pathToTarget[0] or node == pathToTarget[-1]:
|
2022-04-07 17:45:26 +02:00
|
|
|
continue
|
|
|
|
green = (0, 255, 0)
|
2022-04-07 23:32:39 +02:00
|
|
|
pygame.draw.circle(screen, green, ((node.xPos + 1)
|
|
|
|
* 500 / 15 + 30, (node.yPos + 1) * 500 / 15), 5)
|
|
|
|
|
|
|
|
for node in fields:
|
|
|
|
## DRAWING BLOCKS
|
|
|
|
if node.isBlock == True:
|
|
|
|
black = (0, 0, 0)
|
|
|
|
pygame.draw.rect(screen, black, pygame.Rect((node.xPos + 1) * 500 / 15 + 30, (node.yPos + 1) * 500 / 15 - 10, 10, 10))
|
2022-04-07 17:45:26 +02:00
|
|
|
|
2022-03-08 14:30:25 +01:00
|
|
|
kb_listen(objectArray, 15)
|
2022-04-07 23:32:39 +02:00
|
|
|
pygame.display.update() # by krata pojawiła się w okienku - update powierzchni
|