SI-projekt-smieciarka/board

257 lines
8.3 KiB
Plaintext
Raw Normal View History

2022-03-08 11:25:50 +01:00
import sys
2022-04-26 18:08:56 +02:00
from queue import PriorityQueue
2022-03-08 11:25:50 +01:00
import pygame
screen = []
objectArray = []
2022-04-26 18:08:56 +02:00
collisionsMap = []
class Position:
def __init__(self, x, y):
self.x = x
self.y = y
2022-03-08 11:25:50 +01:00
2022-04-26 18:08:56 +02:00
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):
2022-03-08 11:25:50 +01:00
self.name = name
2022-04-26 18:08:56 +02:00
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, event, gridLength):
if event.key == pygame.K_LEFT:
newPos = self.pos.get_moved(-1, 0)
self.move_if_possible(newPos, gridLength)
if event.key == pygame.K_RIGHT:
newPos = self.pos.get_moved(1, 0)
self.move_if_possible(newPos, gridLength)
if event.key == pygame.K_UP:
newPos = self.pos.get_moved(0, -1)
self.move_if_possible(newPos, gridLength)
if event.key == pygame.K_DOWN:
newPos = self.pos.get_moved(0, 1)
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))
2022-03-08 11:25:50 +01:00
def draw(square_num, objectArr):
2022-04-26 18:08:56 +02:00
# 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
2022-03-08 11:25:50 +01:00
2022-04-26 18:08:56 +02:00
grid_size = 510 # rozmiar kraty
square = grid_size // square_num # rozmiar pojedyńczego kwadracika
2022-03-08 11:25:50 +01:00
a = 50
2022-04-26 18:08:56 +02:00
b = 10 # odległości kraty od krawędzi okna
for o in objectArr:
o.draw(square)
2022-03-08 11:25:50 +01:00
for i in range(square_num):
2022-04-26 18:08:56 +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-26 18:08:56 +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,
2022-04-26 18:08:56 +02:00
b + grid_size), 2)
2022-03-08 11:25:50 +01:00
pygame.draw.line(screen, grid_color, (a + grid_size, b),
(a + grid_size, b + grid_size), 2)
def kb_listen(objectArray, gridLength):
2022-04-26 18:08:56 +02:00
agent = objectArray[0]
2022-03-08 11:25:50 +01:00
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
2022-04-26 18:08:56 +02:00
agent.move(event, gridLength)
2022-03-08 11:25:50 +01:00
if event.type == pygame.QUIT:
sys.exit()
2022-04-26 18:08:56 +02:00
#moje
2022-04-27 21:52:09 +02:00
def manhattan(node1, node2):
x1, y1 = node1.state[0], node1.state[1]
x2, y2 = node2.state[0], node2.state[1]
distance = abs(x1 - x2) + abs(y1 - y2)
2022-04-26 18:08:56 +02:00
return distance
class Node: #prawie jak Field w bfs
def __init__(self, state, parent, action):
self.state = state #position - (x, y, direction)
self.parent = parent
self.action = action
2022-04-28 14:47:35 +02:00
def succ1(state):
successors = []#-90 obrót w lewo, +90 obrót w prawo
print(state[0])#operujemy na 0, 90, 180, 270
print(state[1])
right = state[2] + 90
successors.append((("turn", "right"), (state[0], state[1], right)))
left = state[2] - 90
successors.append((("turn", "left"), (state[0], state[1], left)))
if state[2] == 360:
state[2] = 0
if state[2] == -90:
state[2] = 270
if (state[0], state[1]) not in black_list:#działa
if state[2] == 0 and state[0] < 14:
new_x = state[0]+1
successors.append((("move forward"), (new_x, state[1], state[2])))
elif state[2] == 90 and state[1] > 0:
new_y = state[1]-1
successors.append((("move forward"), (state[0], new_y, state[2])))
elif state[2] == 180 and state[0] > 0:
new_x = state[0]-1
successors.append((("move forward"), (new_x, state[1], state[2])))
elif state[2] == 270 and state[1] < 14:
new_y = state[1]+1
successors.append((("move forward"), (state[0], new_y, state[2])))
return successors
2022-04-26 18:08:56 +02:00
def algorithm():
opened = PriorityQueue()#może być też lista
closed = []#już odwiedzone, odrzucone wierzchołki
first_state = (0, 0, "Right")#x, y, kierunek
final_state = (14, 14, "Right")
starting_point = Node(first_state, False, False)
ending_point = Node(final_state, False, False)
pos1 = (starting_point.state[0], starting_point.state[1])
pos2 = (ending_point.state[0], ending_point.state[1])
2022-03-08 11:25:50 +01:00
2022-04-26 18:08:56 +02:00
opened.put((1, starting_point))
a = final_state[0]
b = final_state[1]
2022-04-28 14:47:35 +02:00
#hole = Hole("astar", Position(a, b))#narysowana dziura w miejscu mety(celu), by sprawdzić, czy działa
#objectArray.append(hole)
2022-04-26 18:08:56 +02:00
if __name__ == '__main__':
pygame.init() # inicjalizacja modułów, na razie niepotrzebna
gridSize = 15
astar()
# 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)
]])]
2022-03-08 11:25:50 +01:00
objectArray.append(agent)
2022-04-26 18:08:56 +02:00
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
2022-03-08 11:25:50 +01:00
black_list = [(10, 10), (7, 4), (3, 10), (8, 10), (4, 5), (1, 2), (10, 4), (13, 14), (6, 9), (4, 9), (5, 11), (11, 7), (13, 8)]
#lista obiektów potrzebna do succ1 - na te pole śmieciarka nie wchodzi, więc nie ma ich brać pod uwagę
2022-04-26 18:08:56 +02:00
width = 610
2022-03-08 11:25:50 +01:00
height = 530
2022-04-26 18:08:56 +02:00
screen = pygame.display.set_mode((width, height)) # ustalanie rozmiarów okna
2022-03-08 11:25:50 +01:00
while 1:
2022-04-26 18:08:56 +02:00
c = (255, 255, 255) # tymczasowy kolor tła - do usunięcia, jak już będzie zdjęcie
screen.fill(c)
2022-04-26 18:08:56 +02:00
draw(gridSize, objectArray)
kb_listen(objectArray, gridSize)
pygame.display.update() # by krata pojawiła się w okienku - update powierzc