druga bramka dla zwiekszenia mozliwosci, niezalezny koszt obrotu

This commit is contained in:
LuminoX 2024-04-28 22:40:13 +02:00
parent 7f74fcf2fa
commit 088956ccb8
3 changed files with 52 additions and 35 deletions

View File

@ -1,14 +1,16 @@
import pygame
class Enclosure:
def __init__(self, x1, y1, x2, y2, gate, type, imageH, imageV, imageGate):
def __init__(self, x1, y1, x2, y2, gate1, gate2, type, imageH, imageV, imageGate):
self.x1 = x1 - 1
self.y1 = y1 - 1
#(x1,y1) - wierzchołek przekątnej
# (x1,y1) - wierzchołek przekątnej
self.x2 = x2 - 1
self.y2 = y2 - 1
#(x2,y2) - 2 wierzchołek przekątnej
self.gate = gate
# (x2,y2) - 2 wierzchołek przekątnej
self.gate1 = gate1
self.gate2 = gate2
self.type = type
self.imageH = imageH
self.imageV = imageV
@ -16,41 +18,50 @@ class Enclosure:
def gatebuild(self, screen, grid_size):
self.imageGate = pygame.transform.scale(self.imageGate, (grid_size, grid_size))
gate_x, gate_y = self.gate
gate_x -= 1
gate_y -= 1
rect = pygame.Rect(gate_x * grid_size, gate_y * grid_size, grid_size, grid_size)
screen.blit(self.imageGate, (gate_x * grid_size, gate_y * grid_size))
gate_x1, gate_y1 = self.gate1
gate_x2, gate_y2 = self.gate2
gate_x1 -= 1
gate_y1 -= 1
gate_x2 -= 1
gate_y2 -= 1
rect1 = pygame.Rect(gate_x1 * grid_size, gate_y1 * grid_size, grid_size, grid_size)
rect2 = pygame.Rect(gate_x2 * grid_size, gate_y2 * grid_size, grid_size, grid_size)
screen.blit(self.imageGate, (gate_x1 * grid_size, gate_y1 * grid_size))
screen.blit(self.imageGate, (gate_x2 * grid_size, gate_y2 * grid_size))
def draw(self, screen, grid_size):
self.imageH = pygame.transform.scale(self.imageH, (grid_size, grid_size))
self.imageV = pygame.transform.scale(self.imageV, (grid_size, grid_size))
gate_x, gate_y = self.gate
gate_x -= 1
gate_y -= 1
gate_x1, gate_y1 = self.gate1
gate_x2, gate_y2 = self.gate2
gate_x1 -= 1
gate_y1 -= 1
gate_x2 -= 1
gate_y2 -= 1
if self.x1 < self.x2:
for i in range(self.x1, self.x2+1):
if (i, self.y1) != (gate_x, gate_y):
for i in range(self.x1, self.x2 + 1):
if (i, self.y1) != (gate_x1, gate_y1) and (i, self.y1) != (gate_x2, gate_y2):
screen.blit(self.imageH, (i * grid_size, self.y1 * grid_size))
if (i, self.y2) != (gate_x, gate_y):
if (i, self.y2) != (gate_x1, gate_y1) and (i, self.y2) != (gate_x2, gate_y2):
screen.blit(self.imageH, (i * grid_size, self.y2 * grid_size))
for j in range(self.y1, self.y2+1):
if (self.x1, j) != (gate_x, gate_y):
for j in range(self.y1, self.y2 + 1):
if (self.x1, j) != (gate_x1, gate_y1) and (self.x1, j) != (gate_x2, gate_y2):
screen.blit(self.imageV, (self.x1 * grid_size, j * grid_size))
if (self.x2, j) != (gate_x, gate_y):
if (self.x2, j) != (gate_x1, gate_y1) and (self.x2, j) != (gate_x2, gate_y2):
screen.blit(self.imageV, (self.x2 * grid_size, j * grid_size))
def create_enclosures():
fenceH = pygame.image.load('images/fenceHor.png')
fenceV = pygame.image.load('images/fenceVer.png')
gate = pygame.image.load('images/gate.png')
en1 = Enclosure(0,5, 9,11, (9,6),"hot", fenceH, fenceV, gate) # Lewa klatka
en2 = Enclosure(13,0, 29,3, (16,3), 'medium', fenceH, fenceV, gate) # Górna klatka
en3 = Enclosure(11,5, 16,11, (12,5),'cold', fenceH, fenceV, gate) # Środkowa klatka
en4 = Enclosure(19,5, 31,11, (25,5),'hot', fenceH, fenceV, gate) # Prawa klatka
en5 = Enclosure(4,13, 28,16, (16,13),'cold', fenceH, fenceV, gate) # Dolna klatka
en1 = Enclosure(0, 5, 9, 11, (9, 6), (4, 11), "hot", fenceH, fenceV, gate) # Lewa klatka
en2 = Enclosure(13, 0, 29, 3, (16, 3), (27, 3), 'medium', fenceH, fenceV, gate) # Górna klatka
en3 = Enclosure(11, 5, 16, 11, (12, 5), (16, 8), 'cold', fenceH, fenceV, gate) # Środkowa klatka
en4 = Enclosure(19, 5, 31, 11, (23, 5), (25, 11), 'hot', fenceH, fenceV, gate) # Prawa klatka
en5 = Enclosure(4, 13, 28, 16, (12, 13), (20, 13), 'cold', fenceH, fenceV, gate) # Dolna klatka
Enclosures = [en1, en2, en3, en4, en5]
return Enclosures
return Enclosures

20
main.py
View File

@ -94,28 +94,32 @@ def generate_obstacles():
for en in Enclosures:
# Pobierz współrzędne bramy
gate_x, gate_y = en.gate
gate_x, gate_y = en.gate1
gate_x -= 1
gate_y -= 1
gate_x2, gate_y2 = en.gate2
gate_x2 -= 1
gate_y2 -= 1
# Dodaj lewy brzeg prostokąta
for y in range(en.y1, en.y2 + 1):
if (en.x1, y) != (gate_x, gate_y):
if (en.x1, y) != (gate_x, gate_y) and (en.x1, y) != (gate_x2, gate_y2):
obstacles.add((en.x1, y))
# Dodaj prawy brzeg prostokąta
for y in range(en.y1, en.y2 + 1):
if (en.x2, y) != (gate_x, gate_y):
if (en.x2, y) != (gate_x, gate_y) and (en.x2, y) != (gate_x2, gate_y2):
obstacles.add((en.x2, y))
# Dodaj górny brzeg prostokąta
for x in range(en.x1+1, en.x2):
if (x, en.y1) != (gate_x, gate_y):
if (x, en.y1) != (gate_x, gate_y) and (x, en.y1) != (gate_x2, gate_y2):
obstacles.add((x, en.y1))
# Dodaj dolny brzeg prostokąta
for x in range(en.x1+1, en.x2):
if (x, en.y2) != (gate_x, gate_y):
if (x, en.y2) != (gate_x, gate_y) and (x, en.y2) != (gate_x2, gate_y2):
obstacles.add((x, en.y2))
return obstacles
@ -251,8 +255,8 @@ animal1 = Elephant(14, 10)
animal2 = Elephant(13, 10)
animal3 = Elephant(12, 10)
animal4 = Elephant(11, 10)
Animals = [animal, animal1, animal2, animal3, animal4]
Terrain_Obstacles = [puddle1, bush1]
#Animals = [animal, animal1, animal2, animal3, animal4] *Uncomment to test A*
#Terrain_Obstacles = [puddle1, bush1] *Uncomment to test A*
empty_rows = [5, 7, 9]
@ -305,7 +309,7 @@ class DebugMode(Enum):
A_STAR_TESTING = 3
if __name__ == "__main__":
debug_mode = DebugMode.A_STAR_TESTING
debug_mode = DebugMode.MAIN
if debug_mode == DebugMode.MAIN:
main()

View File

@ -88,10 +88,12 @@ def current_cost(node, cost_map):
while node[1] is not None: # Dopóki nie dojdziemy do korzenia
_, parent, action = node
# Dodaj koszt pola z mapy kosztów tylko jeśli akcja to "Forward"
# if action == 'Go Forward':
if True:
if action == 'Go Forward':
#if True:
state, _, _ = node
cost += cost_map.get(state[:2], DEFAULT_COST_VALUE) # Pobiera koszt przejścia przez dane pole, a jeśli koszt nie jest zdefiniowany to bierze wartość domyślną
if action == 'Turn Right' or action == 'Turn Left':
cost += 1
node = parent # Przejdź do rodzica
return cost