From 3569f7e739511b34bf0ec403122d03e6c889f1e0 Mon Sep 17 00:00:00 2001 From: Aga Date: Fri, 21 Apr 2023 06:30:54 +0200 Subject: [PATCH] Breadth-First Search Strategie przeszukiwania 1 --- .idea/misc.xml | 2 +- img/tree.png | Bin 0 -> 2079 bytes main.py | 536 +++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 520 insertions(+), 18 deletions(-) create mode 100644 img/tree.png diff --git a/.idea/misc.xml b/.idea/misc.xml index a0f56f8..d37d1d2 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/img/tree.png b/img/tree.png new file mode 100644 index 0000000000000000000000000000000000000000..a56c6b5f67ec25071adc384d376fd0281cf7f587 GIT binary patch literal 2079 zcmV+)2;ldLP)da}F{yyoEBSkwZ*74sIQ)9a%Vg**P6c-(22s zbzt3=YkO-uLXfNq*DSpG)~miFz=#R~PnnL>t>@RbeC+w5%?cG0=9yH-;_|NZ`m@~z z3zv3%;)V!J(0*Btj(uZFSkhHamY;1Zn{O*wi7B?^^7ZBVNT zo;5nLI!#SlFvV0CQ&UJx!J5=)Ym_z0C^QC*Lah|s^X%;`JT))#)uYvZWTP=jbs+|c zUWAoGrIprbG&Uw`txl{eB)X8AVxo(QPOVOivRWB~Mk%dODO9M`5w~ks|MsbQ<+OPY zF7tJ<+Gu4^jLU!`fFJ@kEEH<A11LEJ0y)XrD z>(1YQblnBp_HGtn;z%4@Tg#H`yv9D`7=%S8LZxvL01=pYD#wlBwT<`COFwyL|D%(5 zWA&jg-u=nk*8wE35A5@pd&;`z&B-d9*D1IV1FB} zPvoPIgD=;H_k_x57RW~md8|$1Eu7nPWz*PqA|MPBMWWP3h=?LZ7kuViB^Sqx~6;k{FG|1l~*iSZtYTlaeAP5~fHT z8vQ3B&%biz#>I12l#-??`u(!=K4euw5r9!5NC5G`K^c{#rZA(>JhR;1J$s4IV`Jqq z=dvuX)qG_Di`=F|(S}mlHroO!10Xu}+yRbIsUCrlpSFeX+YUVGTRW7G(EiJ~HATG;tUlU;ffsyfJFj@bu zCnB!yxq5Za)z9sH;-+t3Vu%YBhuPIcvVq^Jek^9Ak_zUh?nE^oSu+0~7p{!~{k(>a7uWRwxv)~=y{oK#zcPx6nyyF_n zhn{-nk*{vLvE%HtG(&e57A(y!{nPp%jIL=P860Y_&Zuufo* zd+y>=+Vr1pdfmEpC+8JRsk>t-5e?K2B$>YTy+7?T-H&g2;NJUQDE0Z9Zd$)`;kyqU z+be?SoU!E2o9|z_=sg{6bFE3Hw&T~W@oe!!}m$p6f&~4Ygt9SQK zX3l(laP)BQvs0Q(gV)gSezxIL7o53vap}6ZxBT))-<)pxw7(xHhR>{7ezdQ*sip1i zM_)-xQ-@5JHg@?XR{+4WORktM>~welFz4)Z0AT)-b5A4pba+Y|05J9hJMazX zWwmht7(CSb7V(A$4g self.x and self.mseX < self.x + self.sx: + if self.mseY > self.y and self.mseY < self.y + self.sy: + self.posX = self.x + self.posY = self.y + self.gridBox = grid + + def draw(self): + # pygame.draw.rect(SCREEN, GREY, pygame.Rect(self.posX, self.posY, self.sx, self.sy)) + SCREEN.blit(imgTree, (self.posX, self.posY)) +def getGridBoxes(grid_box): + return gridObjects[grid_box] +def drawGrid(sizex,sizey): + spaceX = SCREENX // sizex + spaceY = SCREENY // sizey + width = 2 + + counter = 1 + for i in range(sizex): + for j in range(sizey): + # g = Grid(i*spaceX, j*spaceY, spaceX, spaceY) + g = Grid(50 + i*50, 50 + j*50, spaceX, spaceY) + gridObjects[counter] = g + counter += 1 +def generateGraph(row,col): + # This function generates a graph based on the gridObjects instantiated! + sample_graph = {'A':['B','C','E'], + 'B':['A','D','E'], + 'C':['A','F','G'], + 'D':['B'], + 'E':['A','B','D'], + 'F':['C'], + 'G':['C'] + } + + miniG = {} + for grid in range(len(gridObjects)): + grid += 1 # Synchronize index + mod = grid % col # Used to check the Top and Bottom Grid Boxes! + gN = grid - 1 + gS = grid + 1 + gE = grid + col + gW = grid - col + + + # CHECK THE NEIGHBORS TO THE GRID-BOXES, ACCOUNTING FOR THE EXTREME GRID-BOXES(BORDERS) + if mod == 0: # 5,10,15,20,25 - You can't go south from here (Bottom Boxes) + if grid > col: # Away from the Left Border of the Screen + if grid > (col*row)-col: # You are on the Right Border of the screen - You can't go East + miniG[grid] = [gN, gW] + else: # Away from the Right Border of the Screen - You can go East + miniG[grid] = [gN, gE, gW] + else: # You are on the Left Edge of the screen - You can't go West + miniG[grid] = [gN, gE] + + elif mod == 1: # 6,11,16,21 :> You can't go North from here (Top Boxes) + if grid > col: # Away from the Left Border of the Screen + if grid > (col*row)-col: # You are on the Right Border of the screen - You can't go East + miniG[grid] = [gS, gW] + else: # Away from the Right Border of the Screen - You can go east + miniG[grid] = [gS, gE, gW] + else: # You are on the Left Edge of the screen - You can't go West + miniG[grid] = [gS, gE] + + else: # All the rest (Not Top or Bottom Boxes) - You can go North or South + if grid > col: # Away from the Left Border of the Screen + if grid > (col*row)-col: # You are on the Right Border of the screen - You can't go East + miniG[grid] = [gN, gS, gW] + else: # Away from the Right Border of the Screen - You can go East + miniG[grid] = [gN, gS, gE, gW] + else: # You are on the Left Edge of the screen - You can't go West + miniG[grid] = [gN, gS, gE] + + + # FILTER OUT OBSTACLES FROM THE GRAPH + miniG2 = {} + for grid in range(len(gridObjects)): + grid += 1 + if grid not in gridObstacle: + # gridObjects.remove(grid) # Dict object has no attribute : 'remove' + # HACK + miniG2[grid] = miniG[grid] # Created a new dictionary that stored the values required + # IN-DEPTH FILTER - Filter out obstacles from the neighbors-list + for neigbor in miniG2[grid]: + if neigbor in gridObstacle: + miniG2[grid].remove(neigbor) + + + # Filtering again as the first Filter block didn't clear out everything + # Filtering through the neighbors + for grid in miniG2: + for item in miniG2[grid]: + if item in gridObstacle: + miniG2[grid].remove(item) + + + + + return miniG2 + + +def drawGraph(pathF, Ucelu): + #Draws the path given the path-list + print(pathF) + if Ucelu == False: + for grid in pathF: + g = gridObjects[grid] # Get the grid-box object mentioned in the path + x = g.x + y = g.y + sx = g.sx + sy = g.sy + # pygame.draw.rect(SCREEN, GREEN, pygame.Rect(x, y, sx, sy)) + player.x = x/50 - 1 + player.y =y/50 - 1 + + # ----------------------------- + i = 0 + while i < len(T): + j = 0 + while j < len(T[i]): + #color = (255, 255, 255, 0) + if T[i][j].isWet == 0: + # a = 1 + color = (160, 80, 40, 0) + else: + # a = 1 + color = (50, 25, 0, 0) + + #Covers 'player' on the way + pygame.draw.rect(SCREEN, color, pygame.Rect(50 + 50 * i, 50 + 50 * j, 50, 50)) + if T[i][j].plantType == 1: + SCREEN.blit(imgWheat, (50 + 50 * i, 50 + 50 * j)) + if T[i][j].plantType == 2: + SCREEN.blit(imgCarrot, (50 + 50 * i, 50 + 50 * j)) + if T[i][j].plantType == 3: + SCREEN.blit(imgCabbage, (50 + 50 * i, 50 + 50 * j)) + if T[i][j].plantType == 4: + SCREEN.blit(imgTree, (50 + 50 * i, 50 + 50 * j)) + + j = j + 1 + i = i + 1 + + # Render the trees + for obs in obstacleObjects: + obstacleObjects[obs].draw() + + for bx in boxObjects: + boxObjects[bx].draw() + + i = 0 + while i < len(T)+1: + pygame.draw.line(SCREEN, (0, 0, 0), (50 + i * 50, 50), (50 + i * 50, 50 + len(T) * 50), 1) + pygame.draw.line(SCREEN, (0, 0, 0), (50, 50 + i * 50), (50 + len(T) * 50, 50 + i * 50), 1) + i = i + 1 + + tmpImg = pygame.transform.rotate(imgPlayer, player.rotation) + if player.rotation == 180: + tmpImg = pygame.transform.flip(tmpImg, True, True) + tmpImg = pygame.transform.flip(tmpImg, True, False) + + #player is seen on the way + SCREEN.blit(tmpImg, (55 + 50 * player.x, 55 + 50 * player.y)) + + # -------------------------------------- + + # tmpImg = pygame.transform.rotate(imgPlayer, player.rotation) + # # if flip: + # # if flip == True: + # if player.rotation == 180: + # tmpImg = pygame.transform.flip(tmpImg, True, True) + # tmpImg = pygame.transform.flip(tmpImg, True, False) + # + # SCREEN.blit(tmpImg, (55 + 50 * player.x, 55 + 50 * player.y)) + + pygame.display.update() + pygame.time.wait(300) + #SCREEN.fill((WHITE)) + # pygame.time.wait(50) + # pygame.draw.rect(SCREEN, WHITE, pygame.Rect(x, y, sx, sy)) + Ucelu = True +def UIHandler(mouseObj, Ucelu): + # drawGrid(GRIDX, GRIDY) + drawGrid(10,10) + + for grid in gridObjects: + gridObjects[grid].draw() + + for bx in boxObjects: + boxObjects[bx].draw() + + for obs in obstacleObjects: + obstacleObjects[obs].draw() + + if pathFound: + if Ucelu == False: + drawGraph(pathFound, Ucelu) + Ucelu = True + +def eventHandler(kbdObj,mouseObj, Ucelu): + global boxes + global obstacles + global startNode + global goalNode + global pathFound + + + # If Key_f is pressed, set goal node + if kbdObj[pygame.K_f]: + gBox = getGridBoxes(int(len(gridObjects))) + # gBox = getGridBoxes() + + x = mouseObj[0] + y = mouseObj[1] + # x = gBox.x + # y = gBox.y + sx = gBox.sx + sy = gBox.sy + # ---------------------------------------- + mseX = mouseObj[0] + mseY = mouseObj[1] + + for grid in gridObjects: + g = getGridBoxes(grid) + x = g.x + y = g.y + sx = g.sx + sy = g.sy + if mseX > x and mseX < x + sx: + if mseY > y and mseY < y + sy: + posX = x + posY = y + gridBox = grid + + # SCREEN.blit(imgTree, (posX, posY)) + + # --------------------------------------- + bo = Box(posX, posY, sx, sy, BLUE) + boxObjects[boxes] = bo + # boxes += 1 + boxes = 1 + # goalNode = GRIDX*GRIDX + # goalNode = (10 * (x + 1) + (y + 1) - 10) + goalNode = (10 * (posX/50 ) + (posY/50) - 10) + + # goalNode = (x/sx) * (y/sy) + # Delay to avoid multiple spawning of objects + pygame.time.wait(DELAY) + # If Key_x is pressed, spawn tree + if kbdObj[pygame.K_x]: + obs = Obstacle(mouseObj) + obstacleObjects[obstacles] = obs + # print(obs.gridBox) + obstacles += 1 + # print(obstacleObjects) + gridObstacle[obs.gridBox] = obstacles + # Delay to avoid multiple spawning of objects + pygame.time.wait(DELAY) + + + # if Key_SPACE is pressed, start the magic + if kbdObj[pygame.K_SPACE]: + gBox = getGridBoxes(1) + + x = gBox.x + y = gBox.y + sx = gBox.sx + sy = gBox.sy + + x = (player.x +1) * 50 + y = (player.y +1) * 50 + + # tmpImg = pygame.transform.rotate(imgPlayer, player.rotation) + # SCREEN.blit(tmpImg, (50 + 50 * player.x, 50 + 50 * player.y)) + # pygame.display.update() + + #when on it keeps flashing - among others + #bo = Box(x, y, sx, sy, RED) + #boxObjects[boxes] = bo + + # boxes += 1 + boxes = 1 + startNode = (10 * (player.x + 1) + (player.y + 1) - 10) + # startNode = (((player.x + 1)*10 - 9) * (player.y + 1) ) + # startNode = 2 + + # tmpImg = pygame.transform.rotate(imgPlayer, player.rotation) + # SCREEN.blit(tmpImg, (55 + 50 * player.x, 55 + 50 * player.y)) + # pygame.display.update() + + # Delay to avoid multiple spawning of objects + #pygame.time.wait(DELAY) + + graph = generateGraph(GRIDY,GRIDX) + bfs = BFS(graph, startNode, goalNode) + # print(bfs.solve()) + pathFound = bfs.solve() + + # Delay to avoid multiple spawning of objects + pygame.time.wait(DELAY) + #With it it keeps going, if without it turns off + Ucelu = False class Field: def __init__(self, fieldType, plantType, isWet, wetTime, isFertilized, fertilizedTime): @@ -39,18 +458,25 @@ T = [[Field(1,0,0,0,0,0),Field(0,0,1,0,0,0),Field(1,1,1,0,0,0),Field(1,2,0,0,0,0 [Field(1,0,0,0,0,0),Field(0,0,1,0,0,0),Field(1,1,1,0,0,0),Field(1,2,0,0,0,0),Field(0,3,1,0,0,0),Field(0,0,0,0,0,0),Field(0,0,0,0,0,0),Field(1,0,1,0,0,0),Field(1,0,0,0,0,0),Field(1,0,1,0,0,0)], [Field(1,0,0,0,0,0),Field(0,0,1,0,0,0),Field(1,1,1,0,0,0),Field(1,2,0,0,0,0),Field(0,3,1,0,0,0),Field(0,0,0,0,0,0),Field(0,0,0,0,0,0),Field(1,0,1,0,0,0),Field(1,0,0,0,0,0),Field(1,0,1,0,0,0)]] -pygame.init() +#pygame.init() player = Player() -screen = pygame.display.set_mode([600, 600]) +# player.x = 2 +# player.y = 2 + +#screen = pygame.display.set_mode([600, 600]) running = True -clock = pygame.time.Clock() +# clock = pygame.time.Clock() + + +SCREEN.fill((WHITE)) while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False + if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: if player.x > 0: @@ -65,42 +491,118 @@ while running: if player.y < 9: player.y = player.y + 1 - screen.fill((175, 255, 50)) + # Aga start lewo prawo, naprzód + if event.key == pygame.K_a: + player.rotation = (player.rotation + 90) % 360 + + if event.key == pygame.K_d: + player.rotation = (player.rotation - 90) % 360 + + if event.key == pygame.K_w: + if player.rotation == 0: + if player.x < 9: + player.x = player.x + 1 + + if player.rotation == 180: + if player.x > 0: + player.x = player.x - 1 + + if player.rotation == 270: + if player.y < 9: + player.y = player.y + 1 + + if player.rotation == 90: + if player.y > 0: + player.y = player.y - 1 + # left, right, forward + + # if it's not here, it leaves a trail WELL NOT ANYMORE + #SCREEN.fill((WHITE)) + kbd = pygame.key.get_pressed() + # kbd = event.key() + mse = pygame.mouse.get_pos() + + # SCREEN.fill((WHITE)) + Ucelu = False + UIHandler(mse, Ucelu) + eventHandler(kbd, mse, Ucelu) + pygame.display.update() + # CLOCK.tick(FPS) + + #screen.fill((175, 255, 50, 0)) + #screen.fill((WHITE)) imgWheat = pygame.image.load('img/wheat.png') imgCarrot = pygame.image.load('img/carrot.png') imgCabbage = pygame.image.load('img/cabbage.png') imgPlayer = pygame.image.load('img/player.png') + imgTree = pygame.image.load('img/tree.png') i = 0 while i < len(T): j = 0 while j < len(T[i]): - color = (0, 0, 0) + # color = (255, 255, 255, 0) if T[i][j].isWet == 0: - color = (160, 80, 40) + # a = 1 + color = (160, 80, 40, 0) else: - color = (50, 25, 0) - pygame.draw.rect(screen, color, pygame.Rect(50 + 50 * i, 50 + 50 * j, 50, 50)) + # a = 1 + color = (50, 25, 0, 0) + #colour from the beginning + pygame.draw.rect(SCREEN, color, pygame.Rect(50 + 50 * i, 50 + 50 * j, 50, 50)) if T[i][j].plantType == 1: - screen.blit(imgWheat, (50 + 50 * i, 50 + 50 * j)) + SCREEN.blit(imgWheat, (50 + 50 * i, 50 + 50 * j)) if T[i][j].plantType == 2: - screen.blit(imgCarrot, (50 + 50 * i, 50 + 50 * j)) + SCREEN.blit(imgCarrot, (50 + 50 * i, 50 + 50 * j)) if T[i][j].plantType == 3: - screen.blit(imgCabbage, (50 + 50 * i, 50 + 50 * j)) + SCREEN.blit(imgCabbage, (50 + 50 * i, 50 + 50 * j)) + if T[i][j].plantType == 4: + SCREEN.blit(imgTree, (50 + 50 * i, 50 + 50 * j)) + + j = j + 1 i = i + 1 i = 0 while i < len(T)+1: - pygame.draw.line(screen, (0, 0, 0), (50 + i * 50, 50), (50 + i * 50, 50 + len(T) * 50), 5) - pygame.draw.line(screen, (0, 0, 0), (50, 50 + i * 50), (50 + len(T) * 50, 50 + i * 50), 5) + pygame.draw.line(SCREEN, (0, 0, 0), (50 + i * 50, 50), (50 + i * 50, 50 + len(T) * 50), 1) + pygame.draw.line(SCREEN, (0, 0, 0), (50, 50 + i * 50), (50 + len(T) * 50, 50 + i * 50), 1) i = i + 1 tmpImg = pygame.transform.rotate(imgPlayer, player.rotation) - screen.blit(tmpImg, (55 + 50 * player.x, 55 + 50 * player.y)) - pygame.display.flip() -# clock.tick(30) + if player.rotation == 180: + tmpImg = pygame.transform.flip(tmpImg, True, True) + tmpImg = pygame.transform.flip(tmpImg, True, False) + + #player seen at the beginning + SCREEN.blit(tmpImg, (55 + 50 * player.x, 55 + 50 * player.y)) + + # set Start Node where the Player is located + # gBox = getGridBoxes(1) + # x = gBox.x + # y = gBox.y + # sx = gBox.sx + # sy = gBox.sy + # bo = Box(x, y, sx, sy, RED) + # boxObjects[boxes] = bo + # boxes += 1 + # startNode = 1 + # # Delay to avoid multiple spawning of objects + # pygame.time.wait(DELAY) + + font = pygame.font.SysFont('comicsans', 18) + label = font.render('f- punkt końcowy, x- drzewa, spacja- uruchomienie', 1, (0, 0, 0)) + label1 = font.render('strzałki-ręczne poruszanie traktorem,', 1, (0, 0, 0)) + label2 = font.render('a- obrót w lewo, d- w prawo, w-ruch naprzód', 1, (0, 0, 0)) + SCREEN.blit(label, (10, 570)) + SCREEN.blit(label1, (10, 590)) + SCREEN.blit(label2, (10, 610)) + + # pygame.display.flip() + + pygame.display.update() + CLOCK.tick(FPS) # Done! Time to quit. -pygame.quit() +pygame.quit() \ No newline at end of file