.
This commit is contained in:
ddamiankowalski 2022-04-08 00:22:27 +02:00
parent a920f446b5
commit 2dadd020c6

114
board
View File

@ -56,17 +56,8 @@ def draw(square_num, objectArr, pathToTarget, num):
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)
pygame.draw.circle(screen, (0, 0, 255), (circleX + 15, circleY + 15), 8)
circleX = objectArray[1].xPos * square + square * 2
circleY = objectArr[1].yPos * square + square
@ -110,7 +101,6 @@ def startQueue(agentX, agentY, fieldList, gridNum):
result = checkGoal(fieldList, gridNum)
if result:
checkFlag = True
# print("NEXT CHECK GOAL")
return result
@ -120,9 +110,6 @@ def checkGoal(fieldList, gridNum):
currentY = currentField.yPos
currentX = currentField.xPos
currentIndex = currentY * gridNum + currentX
# print("POPPED!!!")
# currentField.printXandY()
##print(str(currentField.xPos) + " " + str(objectArray[1].xPos) + " " + str(currentField.yPos) + " " + str(objectArray[1].yPos))
# SPRAWDZAMY CZY DANE POLE JEST POLEM W KTORYM ZNAJDUJE SIE NASZ CEL
if currentField.xPos == objectArray[1].xPos and currentField.yPos == objectArray[1].yPos:
@ -136,28 +123,19 @@ def checkGoal(fieldList, gridNum):
if currentIndex + 15 <= 224 and fieldList[currentIndex + 15].visited == False and fieldList[currentIndex + 15].isBlock == False:
fieldQueue.append(fieldList[currentIndex + 15])
fieldList[currentIndex + 15].parent = fieldList[currentIndex]
# print("APPENDED DOWN!!!")
# fieldList[currentIndex + 15].printXandY()
if currentIndex - 15 > -1 and fieldList[currentIndex - 15].visited == False and fieldList[currentIndex - 15].isBlock == False:
fieldQueue.append(fieldList[currentIndex - 15])
fieldList[currentIndex - 15].parent = fieldList[currentIndex]
# print("APPENDED UP!!!")
# fieldList[currentIndex - 15].printXandY()
if (currentIndex + 1) % 15 != 0 and fieldList[currentIndex + 1].visited == False and fieldList[currentIndex + 1].isBlock == False:
fieldQueue.append(fieldList[currentIndex + 1])
fieldList[currentIndex + 1].parent = fieldList[currentIndex]
# print("APPENDED RIGHT!!!")
# fieldList[currentIndex + 1].printXandY()
if (currentIndex - 1) % 15 != 14 and not currentIndex - 1 < 0 and fieldList[currentIndex - 1].visited == False and fieldList[currentIndex - 1].isBlock == False:
fieldQueue.append(fieldList[currentIndex - 1])
fieldList[currentIndex - 1].parent = fieldList[currentIndex]
# print("APPENDED LEFT!!!")
# fieldList[currentIndex - 1].printXandY()
def generateStartEndPos():
startX = random.randint(0, 14)
startY = random.randint(0, 14)
print(startX)
targetX = None
targetY = None
@ -167,23 +145,40 @@ def generateStartEndPos():
return [startX, startY, targetX, targetY]
def drawFull(index):
c = (255, 255, 255)
screen.fill(c)
for node in pathToTarget:
if node == pathToTarget[0] or node == pathToTarget[-1]:
continue
if pathToTarget.index(node) <= index:
continue
green = (0, 255, 0)
pygame.draw.circle(screen, green, ((node.xPos + 1)
* 500 / 15 + 30, (node.yPos + 1) * 500 / 15), 5)
draw(15, objectArray, pathToTarget, 1)
for node in fields:
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))
kb_listen(objectArray, 15)
pygame.display.update()
if __name__ == '__main__':
pygame.init() # inicjalizacja modułów, na razie niepotrzebna
pygame.init()
width = 600
height = 530
screen = pygame.display.set_mode(
(width, height)) # ustalanie rozmiarów okna
(width, height))
# OBLICZANIE RANDOMOWYCH POL START I END
[sX, sY, eX, eY] = generateStartEndPos()
print(sX)
print(sY)
print(sX)
print(eY)
# GENEROWANIE WSZYSTKICH POL - FIELDS
fields = []
for y in range(15):
for x in range(15):
@ -191,14 +186,11 @@ if __name__ == '__main__':
newField = Field(x, y, True)
else:
newField = Field(x, y, False)
# newField.printXandY()
fields.append(newField)
agent = Object("AGENT", sX, sY)
target = Object("TARGET", eX, eY)
# if fields[agent.getX - 1 *
objectArray.append(agent)
objectArray.append(target)
@ -226,25 +218,37 @@ if __name__ == '__main__':
for node in pathToTarget:
node.printXandY()
while 1:
# tymczasowy kolor tła - do usunięcia, jak już będzie zdjęcie
c = (255, 255, 255)
screen.fill(c)
print("PRINTING ACTIONS TO TAKE TO GET TO THE TARGET ============================")
for node in pathToTarget:
currentNode = node
parentNode = node.parent
direction = None
index = pathToTarget.index(node)
if parentNode == None:
continue
if currentNode.xPos != parentNode.xPos:
if currentNode.xPos > parentNode.xPos:
direction = "E"
objectArray[0].xPos = objectArray[0].xPos + 1
else:
direction = "W"
objectArray[0].xPos = objectArray[0].xPos - 1
else:
if currentNode.yPos > parentNode.yPos:
direction = "S"
objectArray[0].yPos = objectArray[0].yPos + 1
else:
direction = "N"
objectArray[0].yPos = objectArray[0].yPos - 1
time.sleep(1)
print("NEXT DIRECTION: " + direction)
draw(15, objectArray, pathToTarget, 1)
pygame.display.update()
print(index)
drawFull(index)
quit()
# TUTAJ RYSUJEMY SCIEZKE
for node in pathToTarget:
if node == pathToTarget[0] or node == pathToTarget[-1]:
continue
green = (0, 255, 0)
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))
kb_listen(objectArray, 15)
pygame.display.update() # by krata pojawiła się w okienku - update powierzchni