This commit is contained in:
Bowske 2020-04-26 13:32:20 +02:00
commit 8b609f0259
3 changed files with 158 additions and 17 deletions

56
astar.py Normal file
View File

@ -0,0 +1,56 @@
import heapq
def heurystyka(obiekty, a, b):
heur = abs((b[0] - a[0])) + abs((b[1] - a[1]))
if obiekty["plansza"][b[0], b[1]].jestDomem is True:
heur += 2
if obiekty["plansza"][b[0], b[1]].jestPrzeszkoda is True:
heur += 100
return heur
def astar(obiekty, start, cel):
sasiedzi = [(0, 1), (0, -1), (1, 0), (-1, 0)]
close_set = set()
came_from = {}
hscore = {start: heurystyka(obiekty, start, cel)}
gscore = {start: 0}
fscore = {start: heurystyka(obiekty, start, cel)}
oheap = []
dodatkowy_koszt = 0
heapq.heappush(oheap, (fscore[start], start))
while oheap:
current = heapq.heappop(oheap)[1]
if current == cel:
obiekty["plansza"][current[0], current[1]].setKolor((255, 0, 0))
data = []
while current in came_from:
data.append(current)
current = came_from[current]
return data[::-1]
close_set.add(current)
obiekty["plansza"][current[0], current[1]].setKolor((255, 0, 0))
for i, j in sasiedzi:
sasiad = current[0] + i, current[1] + j
if 14 < sasiad[0] or sasiad[0] < 0:
continue
elif 14 < sasiad[1] or sasiad[1] < 0:
continue
elif 6 <= sasiad[0] <= 7 and 10 <= sasiad[1] <= 11:
continue
tentative_h_score = heurystyka(obiekty, sasiad, cel) + heurystyka(obiekty, current, sasiad)
if sasiad in [i[1] for i in oheap] and tentative_h_score > hscore.get(current, 0):
continue
if sasiad not in close_set and sasiad not in [i[1] for i in oheap]:
came_from[sasiad] = current
hscore[sasiad] = tentative_h_score
fscore[sasiad] = tentative_h_score + gscore[current]
gscore[sasiad] = gscore[current] + heurystyka(obiekty, current, sasiad)
heapq.heappush(oheap, (fscore[sasiad], sasiad))
obiekty["plansza"][sasiad[0], sasiad[1]].setKolor((0, 255, 0))
return False

32
game.py
View File

@ -4,6 +4,7 @@ import numpy as np
import random import random
import os import os
import shutil import shutil
import astar
pygame.init() pygame.init()
@ -37,6 +38,7 @@ def game():
# Petla az uzytkownik zamknie program # Petla az uzytkownik zamknie program
done = False done = False
clock = pygame.time.Clock() clock = pygame.time.Clock()
temp = True
start = False start = False
@ -56,17 +58,25 @@ def game():
wiersz = pozycja_myszki[1] // (HEIGHT + MARGIN) wiersz = pozycja_myszki[1] // (HEIGHT + MARGIN)
print("Click ", pozycja_myszki, print("Click ", pozycja_myszki,
"Grid coordinates: ", wiersz, kolumna) "Grid coordinates: ", wiersz, kolumna)
obiekty["plansza"][kolumna,wiersz].setKolor(BLACK) elif event.type == pygame.KEYDOWN:
rysowaniePlanszy(obiekty) if event.key == pygame.K_LEFT:
pressed = pygame.key.get_pressed() obiekty["smieciarka"].w_lewo()
if pressed[pygame.K_q]: if event.key == pygame.K_RIGHT:
start = False obiekty["smieciarka"].w_prawo()
if pressed[pygame.K_s]: if event.key == pygame.K_UP:
start = True obiekty["smieciarka"].w_gore()
if event.key == pygame.K_DOWN:
if start: obiekty["smieciarka"].w_dol()
obiekty["smieciarka"].rand_move() # obiekty["smieciarka"].rand_move()
while(temp):
obiekty["smieciarka"].astar_move(obiekty)
temp = False
clock.tick(7) clock.tick(7)
#start = obiekty["plansza"][0, 14]
#koniec = obiekty["plansza"][14, 0]
#print(astar.astar(obiekty, (0, 14), (14, 0)))
#print(len(astar.astar(start, koniec)))
pygame.quit() pygame.quit()
@ -98,7 +108,7 @@ def utworzObiekty():
plansza[14, 14].setObiekt(smieciarka) plansza[14, 14].setObiekt(smieciarka)
# punkt docelowy # punkt docelowy
plansza[0,0].setKolor(MAGENTA) plansza[0, 0].setKolor(MAGENTA)
# tworzenie wyswietlanego okna # tworzenie wyswietlanego okna
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (0, 30) os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (0, 30)

View File

@ -1,6 +1,12 @@
import astar
import shutil
import os
import pygame import pygame
import game import game
import random import random
<< << << < HEAD
== == == =
>>>>>> > 7cb3aa904cd3d316c84df69942bbfdf66561a2e9
# wysokosc i szerokosc kazdej kratki # wysokosc i szerokosc kazdej kratki
WIDTH = 60 WIDTH = 60
@ -22,6 +28,7 @@ class Smieciarka(pygame.sprite.Sprite):
def __init__(self, x, y): def __init__(self, x, y):
self.x = x self.x = x
self.y = y self.y = y
self.pozycja = (self.x, self.y)
self.image = pygame.image.load('resources/plansza/smieciarka.png') self.image = pygame.image.load('resources/plansza/smieciarka.png')
self.obraz = None self.obraz = None
self.ruch = 1 self.ruch = 1
@ -45,6 +52,21 @@ class Smieciarka(pygame.sprite.Sprite):
elif rand_int == 3: elif rand_int == 3:
self.w_dol() self.w_dol()
def astar_move(self, obiekty):
sciezka = astar.astar(obiekty, self.pozycja,
(random.randrange(15), random.randrange(15)))
print(sciezka)
for koord in sciezka:
if koord[0] == self.x - 1 and koord[1] == self.y:
self.w_lewo()
elif koord[0] == self.x + 1 and koord[1] == self.y:
self.w_prawo()
elif koord[0] == self.x and koord[1] + 1 == self.y:
self.w_gore()
elif koord[0] == self.x and koord[1] - 1 == self.y:
self.w_dol()
print("skonczylem")
def w_lewo(self): def w_lewo(self):
if self.x > 0: if self.x > 0:
if self.ruch == 2: if self.ruch == 2:
@ -83,12 +105,37 @@ class Smieciarka(pygame.sprite.Sprite):
def w_dol(self): def w_dol(self):
if self.y < 14: if self.y < 14:
self.plansza[self.x, self.y + 1].setKolor(BLUE) if self.plansza[self.x, self.y + 1].jestPrzeszkoda is not True:
for i in range((WIDTH + MARGIN) // 5): if self.plansza[self.x, self.y + 1].jestDomem is True:
self.rect.y += 5 if [self.x, self.y + 1] not in self.wspolrzedne_odwiedzonych_domow:
self.obraz.blit(self.image, (self.rect.x, self.rect.y)) self.wspolrzedne_odwiedzonych_domow.append(
game.rysowaniePlanszy(self.obiekty) [self.x, self.y + 1])
self.y += 1 self.zwiekszIloscOdwiedzonychDomow()
self.plansza[self.x, self.y + 1].setKolor(BLUE)
for i in range((WIDTH + MARGIN) // 5):
self.rect.y += 5
self.obraz.blit(self.image, (self.rect.x, self.rect.y))
game.rysowaniePlanszy(self.obiekty)
self.y += 1
def dodajPlastik(self, smiec):
self.plastik.append(smiec)
def dodajSzklo(self, smiec):
self.szklo.append(smiec)
def dodajPapier(self, smiec):
self.papier.append(smiec)
def dodajMetal(self, smiec):
self.metal.append(smiec)
def zwiekszIloscOdwiedzonychDomow(self):
self.odwiedzone_domy += 1
def getOdwiedzoneDomy(self):
return self.odwiedzone_domy
def setPlansza(self, plansza): def setPlansza(self, plansza):
self.plansza = plansza self.plansza = plansza
@ -101,17 +148,45 @@ class Kratka(pygame.sprite.Sprite):
def __init__(self, poz_x, poz_y): def __init__(self, poz_x, poz_y):
self.pozX = poz_x self.pozX = poz_x
self.pozY = poz_y self.pozY = poz_y
<< << << < HEAD
== == == =
self.pozycja = (self.pozX, self.pozY)
self.rodzic = None
self.jestDomem = False
self.jestKontenerem = False
self.jestWysypiskiem = False
self.jestPrzeszkoda = False
>>>>>> > 7cb3aa904cd3d316c84df69942bbfdf66561a2e9
self.kolor = GREY self.kolor = GREY
self.obiekt = None self.obiekt = None
self.g = 0 # Distance to start node
self.h = 0 # Distance to goal node
self.f = 0 # Total cost
pygame.sprite.Sprite.__init__(self) pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.__class__ self.image = pygame.image.__class__
self.rect = pygame.Rect(self.pozX * WIDTH + MARGIN * self.pozX + MARGIN, self.rect = pygame.Rect(self.pozX * WIDTH + MARGIN * self.pozX + MARGIN,
self.pozY * HEIGHT + MARGIN * self.pozY + MARGIN, self.pozY * HEIGHT + MARGIN * self.pozY + MARGIN,
WIDTH, HEIGHT) WIDTH, HEIGHT)
# Sort nodes
def __lt__(self, other):
return self.f < other.f
def __repr__(self):
return ('{0}'.format(self.pozycja))
# Compare nodes
def __eq__(self, other):
return True if self.pozycja == other.pozycja else False
def setImage(self, image): def setImage(self, image):
self.image = image self.image = image
def setRodzic(self, rodzic):
self.rodzic = rodzic
def setObiekt(self, obiekt): def setObiekt(self, obiekt):
self.obiekt = obiekt self.obiekt = obiekt