merge
This commit is contained in:
commit
8b609f0259
56
astar.py
Normal file
56
astar.py
Normal 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
32
game.py
@ -4,6 +4,7 @@ import numpy as np
|
||||
import random
|
||||
import os
|
||||
import shutil
|
||||
import astar
|
||||
|
||||
pygame.init()
|
||||
|
||||
@ -37,6 +38,7 @@ def game():
|
||||
# Petla az uzytkownik zamknie program
|
||||
done = False
|
||||
clock = pygame.time.Clock()
|
||||
temp = True
|
||||
|
||||
start = False
|
||||
|
||||
@ -56,17 +58,25 @@ def game():
|
||||
wiersz = pozycja_myszki[1] // (HEIGHT + MARGIN)
|
||||
print("Click ", pozycja_myszki,
|
||||
"Grid coordinates: ", wiersz, kolumna)
|
||||
obiekty["plansza"][kolumna,wiersz].setKolor(BLACK)
|
||||
rysowaniePlanszy(obiekty)
|
||||
pressed = pygame.key.get_pressed()
|
||||
if pressed[pygame.K_q]:
|
||||
start = False
|
||||
if pressed[pygame.K_s]:
|
||||
start = True
|
||||
|
||||
if start:
|
||||
obiekty["smieciarka"].rand_move()
|
||||
elif event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_LEFT:
|
||||
obiekty["smieciarka"].w_lewo()
|
||||
if event.key == pygame.K_RIGHT:
|
||||
obiekty["smieciarka"].w_prawo()
|
||||
if event.key == pygame.K_UP:
|
||||
obiekty["smieciarka"].w_gore()
|
||||
if event.key == pygame.K_DOWN:
|
||||
obiekty["smieciarka"].w_dol()
|
||||
# obiekty["smieciarka"].rand_move()
|
||||
while(temp):
|
||||
obiekty["smieciarka"].astar_move(obiekty)
|
||||
temp = False
|
||||
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()
|
||||
|
||||
|
||||
@ -98,7 +108,7 @@ def utworzObiekty():
|
||||
plansza[14, 14].setObiekt(smieciarka)
|
||||
|
||||
# punkt docelowy
|
||||
plansza[0,0].setKolor(MAGENTA)
|
||||
plansza[0, 0].setKolor(MAGENTA)
|
||||
|
||||
# tworzenie wyswietlanego okna
|
||||
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (0, 30)
|
||||
|
87
modele.py
87
modele.py
@ -1,6 +1,12 @@
|
||||
import astar
|
||||
import shutil
|
||||
import os
|
||||
import pygame
|
||||
import game
|
||||
import random
|
||||
<< << << < HEAD
|
||||
== == == =
|
||||
>>>>>> > 7cb3aa904cd3d316c84df69942bbfdf66561a2e9
|
||||
|
||||
# wysokosc i szerokosc kazdej kratki
|
||||
WIDTH = 60
|
||||
@ -22,6 +28,7 @@ class Smieciarka(pygame.sprite.Sprite):
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.pozycja = (self.x, self.y)
|
||||
self.image = pygame.image.load('resources/plansza/smieciarka.png')
|
||||
self.obraz = None
|
||||
self.ruch = 1
|
||||
@ -45,6 +52,21 @@ class Smieciarka(pygame.sprite.Sprite):
|
||||
elif rand_int == 3:
|
||||
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):
|
||||
if self.x > 0:
|
||||
if self.ruch == 2:
|
||||
@ -83,12 +105,37 @@ class Smieciarka(pygame.sprite.Sprite):
|
||||
|
||||
def w_dol(self):
|
||||
if self.y < 14:
|
||||
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
|
||||
if self.plansza[self.x, self.y + 1].jestPrzeszkoda is not True:
|
||||
if self.plansza[self.x, self.y + 1].jestDomem is True:
|
||||
if [self.x, self.y + 1] not in self.wspolrzedne_odwiedzonych_domow:
|
||||
self.wspolrzedne_odwiedzonych_domow.append(
|
||||
[self.x, 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):
|
||||
self.plansza = plansza
|
||||
@ -101,17 +148,45 @@ class Kratka(pygame.sprite.Sprite):
|
||||
def __init__(self, poz_x, poz_y):
|
||||
self.pozX = poz_x
|
||||
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.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)
|
||||
self.image = pygame.image.__class__
|
||||
self.rect = pygame.Rect(self.pozX * WIDTH + MARGIN * self.pozX + MARGIN,
|
||||
self.pozY * HEIGHT + MARGIN * self.pozY + MARGIN,
|
||||
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):
|
||||
self.image = image
|
||||
|
||||
def setRodzic(self, rodzic):
|
||||
self.rodzic = rodzic
|
||||
|
||||
def setObiekt(self, obiekt):
|
||||
self.obiekt = obiekt
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user