diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/SI-projekt-smieciarka2.iml b/.idea/SI-projekt-smieciarka2.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/SI-projekt-smieciarka2.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d56657a --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..085e946 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/board b/board deleted file mode 100644 index 326e375..0000000 --- a/board +++ /dev/null @@ -1,78 +0,0 @@ -import sys -import pygame - -screen = [] -objectArray = [] - -class Agent: - def __init__(self, name, xPos, yPos): - self.name = name - self.xPos = xPos - self.yPos = yPos - -def draw(square_num, objectArr): - #następne dwie linijki do odkomentowania, jak będzie wgrane zdjęcie do tła - #background = pygame.image.load("ścieżka do pliku").convert() #tu ścieżka do zdjęcia w tle - #screen.blit(background, (0, 0)) - grid_color = (0, 0, 0) #kolor czarny - - grid_size = 500 #rozmiar kraty - square = grid_size/square_num #rozmiar pojedyńczego kwadracika - - a = 50 - b = 10 #odległości kraty od krawędzi okna - - for i in range(square_num): - pygame.draw.line(screen, grid_color, (a + i*square, b), (a + i*square, b + grid_size), 2) - - pygame.draw.line(screen, grid_color, (a, b + i*square), (a + grid_size, b + i*square), 2) - - 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) - - ## tutaj rysujemy agenta i inne obiekty juz na gotowej mapie - - ## RYSUJEMY AGENTA - #agent_color = (255, 0, 0) - circleX = objectArr[0].xPos * square + square + square/ 2 #dodane jedno +square, by śmieciara nie wychodziła poza kratę - circleY = objectArr[0].yPos * square - square / 2 - #radius = 10 - #pygame.draw.circle(screen, agent_color, (a + circleX, b + circleY), radius) - truck = pygame.image.load("car.png").convert_alpha() #tu ścieżka do zdjęcia w tle - truck = pygame.transform.scale(truck, (square, square)) - screen.blit(truck, (circleX, circleY)) - -def kb_listen(objectArray, gridLength): - for event in pygame.event.get(): - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_LEFT and objectArray[0].xPos > 0: - objectArray[0].xPos = objectArray[0].xPos - 1 - if event.key == pygame.K_RIGHT and objectArray[0].xPos < gridLength - 1: - objectArray[0].xPos = objectArray[0].xPos + 1 - if event.key == pygame.K_UP and objectArray[0].yPos > 1: - objectArray[0].yPos = objectArray[0].yPos - 1 - if event.key == pygame.K_DOWN and objectArray[0].yPos < gridLength: - objectArray[0].yPos = objectArray[0].yPos + 1 - if event.type == pygame.QUIT: - sys.exit() - - -if __name__ == '__main__': - pygame.init() #inicjalizacja modułów, na razie niepotrzebna - - #Tworzymy nowego playera, czy tam agenta - agent = Agent("smieciarka", 5, 7) - objectArray.append(agent) - - width = 600 - height = 530 - screen = pygame.display.set_mode((width, height)) #ustalanie rozmiarów okna - - while 1: - c = (255, 255, 255) #tymczasowy kolor tła - do usunięcia, jak już będzie zdjęcie - screen.fill(c) - draw(15, objectArray) - kb_listen(objectArray, 15) - pygame.display.update() #by krata pojawiła się w okienku - update powierzchni diff --git a/hole.png b/hole.png new file mode 100644 index 0000000..461d79e Binary files /dev/null and b/hole.png differ diff --git a/house.png b/house.png new file mode 100644 index 0000000..9d5f021 Binary files /dev/null and b/house.png differ diff --git a/junkyard.png b/junkyard.png new file mode 100644 index 0000000..785706d Binary files /dev/null and b/junkyard.png differ diff --git a/src/astar.py b/src/astar.py new file mode 100644 index 0000000..b50b2e2 --- /dev/null +++ b/src/astar.py @@ -0,0 +1,51 @@ +from queue import PriorityQueue + +def heuristic(xy1, xy2): + return abs(xy1[0] - xy2[0]) + abs(xy1[1] - xy2[1]) + +def neighbours(point): + x, y = point + list=((x+1,y), (x,y+1), (x,y-1), (x-1,y)) + return list + +#determining the cost of a specific field in the grid +def checkCost(grid, xy): + x, y = xy + cost = grid[x][y] + return cost + +def aStar(grid, start, goal): + openlist = PriorityQueue() + openlist.put(start, 0) + + fScore = {} + origin = {start: None} + fScore[start] = 0 + closedlist=[] + + while openlist!= {} : + current = openlist.get() + + if current == goal: + path = [] + #following from the succesors to the root our starting point + while current != start: + path.append(current) + current = origin[current] + path.reverse() + break + + # successor function + for succ in neighbours(current): + #checking if didn't go out of the maze + if(succ[0] < 0 or succ[1] < 0 or succ[0] > 14 or succ[1] > 14): + continue + + gScore = fScore[current[0],current[1]] + checkCost(grid, current) + if succ not in closedlist or gScore < fScore[succ[0],succ[1]]: + closedlist.append(succ) + origin[succ[0],succ[1]] = current + fScore[succ[0],succ[1]] = gScore + priority = gScore + heuristic(goal, succ) + openlist.put(succ, priority) + return path \ No newline at end of file diff --git a/src/board.py b/src/board.py new file mode 100644 index 0000000..14746a4 --- /dev/null +++ b/src/board.py @@ -0,0 +1,195 @@ +import astar +import pygame + +screen = [] +objectArray = [] +collisionsMap = [] + +weightsMap = ([1, 2, 1, 4, 5, 2, 7, 8, 5, 4, 15, 3, 4, 5, 8], + [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 1], + [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 3], + [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 3, 3, 8, 5, 4], + [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 9, 5, 2], + [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 12, 4, 5, 6], + [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 7, 3, 4, 5, 7], + [5, 2, 1, 4, 5, 2, 7, 8, 1, 4, 17, 14, 4, 5, 1], + [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 14, 3], + [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 14, 2], + [5, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 14, 6], + [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 13, 14, 15, 7], + [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 14, 4, 14, 1], + [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 15, 2], + [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 15, 2]) + +class Position: + def __init__(self, x, y): + self.x = x + self.y = y + + def get_moved(self, delta_x, delta_y): + return Position(self.x + delta_x, self.y + delta_y) + + +class Object: + def __init__(self, name, pos): + self.name = name + self.pos = pos + + def draw(self, square): + leftTopX, leftTopY = 50 + self.pos.x * square, 10 + self.pos.y * square + pygame.draw.rect(screen, (0, 0, 0), (leftTopX, leftTopY, square, square)) + + +def detect_collision(newPos): + return collisionsMap[newPos.x][newPos.y] + + +def position_in_grid(pos, gridLength): + return 0 <= pos.x < gridLength and 0 <= pos.y < gridLength + + +def movement_allowed(newPos, gridLength): + return position_in_grid(newPos, gridLength) and not detect_collision(newPos) + + +class Agent(Object): + def __init__(self, name, pos): + super().__init__(name, pos) + + def draw(self, square): + ## RYSUJEMY AGENTA + circleX = 52 + self.pos.x * square + circleY = 12 + self.pos.y * square + + truck = pygame.image.load("car.png").convert_alpha() # tu ścieżka do zdjęcia w tle + truck = pygame.transform.scale(truck, (square, square)) + screen.blit(truck, (circleX, circleY)) + + def move(self, gridLength, path): + for (x, y) in path: + newPos = self.pos.get_moved(x, y) + self.move_if_possible(newPos, gridLength) + + def move_if_possible(self, newPos, gridLength): + if movement_allowed(newPos, gridLength): + self.pos = newPos + + +class House(Object): + def __init__(self, name, pos): + super().__init__(name, pos) + self.trash_cans = { + "paper": False, + "glass": False, + "plastic": False, + "bio": False + } + + def draw(self, square): + x = 52 + self.pos.x * square + y = 12 + self.pos.y * square + + house = pygame.image.load("house.png").convert_alpha() # tu ścieżka do zdjęcia w tle + house = pygame.transform.scale(house, (square, square)) + screen.blit(house, (x, y)) + + +class Junkyard(Object): + def __init__(self, name, pos): + super().__init__(name, pos) + self.heaps = { + "paper": True, + "glass": True, + "plastic": True, + "bio": True + } + + def draw(self, square): + x = 52 + self.pos.x * square + y = 12 + self.pos.y * square + + junkyard = pygame.image.load("junkyard.png").convert_alpha() # tu ścieżka do zdjęcia w tle + junkyard = pygame.transform.scale(junkyard, (square, square)) + screen.blit(junkyard, (x, y)) + + +class Hole(Object): + def __init__(self, name, pos): + super().__init__(name, pos) + + def draw(self, square): + x = 52 + self.pos.x * square + y = 12 + self.pos.y * square + + hole = pygame.image.load("hole.png").convert_alpha() # tu ścieżka do zdjęcia w tle + hole = pygame.transform.scale(hole, (square, square)) + screen.blit(hole, (x, y)) + + +def draw(square_num, objectArr): + # następne dwie linijki do odkomentowania, jak będzie wgrane zdjęcie do tła + # background = pygame.image.load("ścieżka do pliku").convert() #tu ścieżka do zdjęcia w tle + # screen.blit(background, (0, 0)) + grid_color = (0, 0, 0) # kolor czarny + + grid_size = 510 # rozmiar kraty + square = grid_size // square_num # rozmiar pojedyńczego kwadracika + + a = 50 + b = 10 # odległości kraty od krawędzi okna + + for o in objectArr: + o.draw(square) + + for i in range(square_num): + pygame.draw.line(screen, grid_color, (a + i * square, b), (a + i * square, b + grid_size), 2) + + pygame.draw.line(screen, grid_color, (a, b + i * square), (a + grid_size, b + i * square), 2) + + 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) + + +def kb_listen(objectArray, gridLength, path): + agent = objectArray[0] + agent.move(gridLength, path) + +if __name__ == '__main__': + pygame.init() # inicjalizacja modułów, na razie niepotrzebna + gridSize = 15 + + # Tworzymy nowego playera, czy tam agenta + agent = Agent("smieciarka", Position(0, 0)) + junkyard = Junkyard("wysypisko", Position(10, 10)) + houses = [House(f'dom-{i}', pos) for i, pos in enumerate([Position(x, y) for x, y in [ + (7, 4), (3, 10), (8, 10), (4, 5), (1, 2), (10, 4), (13, 14), (6, 9) + ]])] + holes = [Hole(f'dziura-{i}', pos) for i, pos in enumerate([Position(x, y) for x, y in [ + (4, 9), (5, 11), (11, 7), (13, 8) + ]])] + objectArray.append(agent) + objectArray.append(junkyard) + objectArray += houses + objectArray += holes + + collisionsMap = [[False] * gridSize for _ in range(gridSize)] + for object in objectArray[1:]: + collisionsMap[object.pos.x][object.pos.y] = True + + width = 610 + height = 530 + screen = pygame.display.set_mode((width, height)) # ustalanie rozmiarów okna + + startPos = (0, 0) + finalPos = (14, 14) + astarPath = astar.aStar(weightsMap, startPos, finalPos) + print(astarPath) + + while True: + c = (255, 255, 255) # tymczasowy kolor tła - do usunięcia, jak już będzie zdjęcie + screen.fill(c) + draw(gridSize, objectArray) + kb_listen(objectArray, gridSize, astarPath) + pygame.display.update() # by krata pojawiła się w okienku - update powierzc \ No newline at end of file