Breadth-First Search
Strategie przeszukiwania 1b
This commit is contained in:
parent
3569f7e739
commit
f5fa862365
45
bfs.py
Normal file
45
bfs.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
class BFS:
|
||||||
|
# Finds a suitable path from point A to point B using Breadth-First-Search Algorithm
|
||||||
|
def __init__(self, graph, start, goal):
|
||||||
|
self.graph = graph
|
||||||
|
self.start = start
|
||||||
|
self.goal = goal
|
||||||
|
|
||||||
|
def solve(self):
|
||||||
|
print('Start\n\n')
|
||||||
|
print(self.graph)
|
||||||
|
print('\n\n')
|
||||||
|
# keep track of explored nodes
|
||||||
|
explored = []
|
||||||
|
|
||||||
|
# keep track of all paths to be checked
|
||||||
|
queue = [[self.start]]
|
||||||
|
|
||||||
|
# return path if start is goal
|
||||||
|
if self.start == self.goal:
|
||||||
|
return 'That was easy. Start == Goal'
|
||||||
|
|
||||||
|
# keep looping until all possible paths are explored
|
||||||
|
while queue:
|
||||||
|
# pop the first path from the queue
|
||||||
|
path = queue.pop(0)
|
||||||
|
# get the last node from the path
|
||||||
|
node = path[-1]
|
||||||
|
|
||||||
|
if node not in explored:
|
||||||
|
neighbors = self.graph[node]
|
||||||
|
# go through all neighbor nodes
|
||||||
|
# push it into the queue
|
||||||
|
for neighbor in neighbors:
|
||||||
|
new_path = list(path)
|
||||||
|
new_path.append(neighbor)
|
||||||
|
queue.append(new_path)
|
||||||
|
|
||||||
|
if neighbor == self.goal:
|
||||||
|
return new_path
|
||||||
|
|
||||||
|
# mark node as explored
|
||||||
|
explored.append(node)
|
||||||
|
|
||||||
|
# in case there is no path
|
||||||
|
return "path not accessible"
|
71
board.py
Normal file
71
board.py
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
import pygame
|
||||||
|
from screen import SCREEN
|
||||||
|
global BLACK
|
||||||
|
|
||||||
|
# global SCREEN
|
||||||
|
global BLACK
|
||||||
|
global gridObjects
|
||||||
|
global imgTree
|
||||||
|
global imgTree
|
||||||
|
imgTree = pygame.image.load('img/tree.png')
|
||||||
|
|
||||||
|
gridObjects = {} # Store grid-box objects from Grid Class
|
||||||
|
|
||||||
|
class Grid(object):
|
||||||
|
# ta klasa rysuje kratę na ekranie
|
||||||
|
def __init__(self, x, y, sx, sy):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.sx = sx
|
||||||
|
self.sy = sy
|
||||||
|
self.width = 1
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
# global SCREEN
|
||||||
|
global BLACK
|
||||||
|
# SCREEN = pygame.display.set_mode([600,650])
|
||||||
|
BLACK = (0, 0, 0)
|
||||||
|
pygame.draw.rect(SCREEN, BLACK, (self.x, self.y, self.sx, self.sy), self.width)
|
||||||
|
|
||||||
|
class Box(object):
|
||||||
|
# global SCREEN
|
||||||
|
|
||||||
|
def __init__(self, x, y, sx, sy, color):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.sx = sx
|
||||||
|
self.sy = sy
|
||||||
|
self.color = color
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
# global SCREEN
|
||||||
|
# SCREEN = pygame.display.set_mode([600,650])
|
||||||
|
# global BLACK
|
||||||
|
pygame.draw.rect(SCREEN, self.color, pygame.Rect(self.x, self.y, self.sx, self.sy))
|
||||||
|
|
||||||
|
class Obstacle(object):
|
||||||
|
def __init__(self, mouseObj):
|
||||||
|
self.mseX = mouseObj[0]
|
||||||
|
self.mseY = mouseObj[1]
|
||||||
|
|
||||||
|
for grid in gridObjects:
|
||||||
|
g = getGridBoxes(grid)
|
||||||
|
self.x = g.x
|
||||||
|
self.y = g.y
|
||||||
|
self.sx = g.sx
|
||||||
|
self.sy = g.sy
|
||||||
|
if self.mseX > 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))
|
||||||
|
global imgTree
|
||||||
|
SCREEN.blit(imgTree, (self.posX, self.posY))
|
||||||
|
# pygame.display.update()
|
||||||
|
|
||||||
|
def getGridBoxes(grid_box):
|
||||||
|
global gridObjects
|
||||||
|
return gridObjects[grid_box]
|
24
classes.py
Normal file
24
classes.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
class Field:
|
||||||
|
def __init__(self, fieldType, plantType, isWet, wetTime, isFertilized, fertilizedTime):
|
||||||
|
self.fieldType =fieldType # good/bad
|
||||||
|
self.plantType =plantType # wheat/carrot/cabbage
|
||||||
|
self.isWet =isWet # yes/no
|
||||||
|
self.wetTime =wetTime # number
|
||||||
|
self.isFertilized =isFertilized # yes/no
|
||||||
|
self.fertilizedTime =fertilizedTime # number
|
||||||
|
|
||||||
|
class Plant:
|
||||||
|
def __init__(self, plantType, growthState):
|
||||||
|
self.plantType = plantType # wheat/carrot/cabbage
|
||||||
|
self.growthState = growthState # growing/grown
|
||||||
|
|
||||||
|
|
||||||
|
class Fertilizer:
|
||||||
|
def __init__(self, fertilizerType):
|
||||||
|
self.fertilizerType = fertilizerType # wheat/carrot/cabbage
|
||||||
|
|
||||||
|
|
||||||
|
class Player:
|
||||||
|
x = 0
|
||||||
|
y = 0
|
||||||
|
rotation = 0
|
383
main.py
383
main.py
@ -1,135 +1,49 @@
|
|||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
pygame.init()
|
from classes import Field, Plant, Fertilizer, Player
|
||||||
|
from bfs import BFS
|
||||||
|
from board import Grid, Box, Obstacle, getGridBoxes, gridObjects
|
||||||
|
from screen import SCREEN
|
||||||
|
|
||||||
|
# pygame.init()
|
||||||
|
|
||||||
# Game Constants
|
# Game Constants
|
||||||
Ucelu = False
|
Ucelu = False
|
||||||
|
|
||||||
SCREENX = 500
|
SCREENX = 500
|
||||||
SCREENY = 500
|
SCREENY = 500
|
||||||
|
|
||||||
# SCREEN = pygame.display.set_mode([600, 600])
|
pygame.display.set_caption('Inteligentny Traktor')
|
||||||
# screen = pygame.display.set_mode((SCREENX, SCREENY))
|
|
||||||
SCREEN = pygame.display.set_mode([600,650])
|
|
||||||
pygame.display.set_caption('Inteligenty Traktor')
|
|
||||||
|
|
||||||
# COLORS
|
# COLORS
|
||||||
WHITE = (255, 255, 255)
|
WHITE = (255, 255, 255)
|
||||||
BLACK = (0, 0, 0)
|
BLACK = (0, 0, 0)
|
||||||
RED = (255, 0, 0)
|
RED = (255, 0, 0)
|
||||||
GREEN = (0, 255, 0, 0)
|
GREEN = (0, 255, 0, 0)
|
||||||
BLUE = (0, 0, 255)
|
BLUE = (0, 0, 255, 0)
|
||||||
GREY = (128, 128, 128)
|
GREY = (128, 128, 128)
|
||||||
|
|
||||||
CLOCK = pygame.time.Clock()
|
CLOCK = pygame.time.Clock()
|
||||||
FPS = 300
|
FPS = 30
|
||||||
DELAY = 100
|
DELAY = 300
|
||||||
|
|
||||||
|
# np. 10 pól x 10 pól = 100 pól
|
||||||
GRIDX = 10
|
GRIDX = 10
|
||||||
GRIDY = 10
|
GRIDY = 10
|
||||||
|
|
||||||
|
|
||||||
obstacleObjects = {} # Store the obstacle objects (Blocks on the path) from Obstacle class
|
obstacleObjects = {} # Store the obstacle objects (Blocks on the path) from Obstacle class
|
||||||
gridObjects = {} # Store grid-box objects from Grid Class
|
# global gridObjects
|
||||||
|
# gridObjects = {} # Store grid-box objects from Grid Class
|
||||||
gridObstacle = {} # Store the grid:obstacle pair stuck together
|
gridObstacle = {} # Store the grid:obstacle pair stuck together
|
||||||
boxObjects = {}
|
boxObjects = {}
|
||||||
boxes = 1
|
boxes = 1
|
||||||
obstacles = 1
|
obstacles = 1
|
||||||
|
|
||||||
|
|
||||||
# BFS Variables
|
# BFS Variables
|
||||||
startNode = 0
|
startNode = 0
|
||||||
goalNode = 0
|
goalNode = 0
|
||||||
graph = dict()
|
graph = dict()
|
||||||
pathFound = [] # Store the path in a list box index to draw on later
|
pathFound = [] # Store the path in a list box index to draw on later
|
||||||
|
|
||||||
class BFS:
|
|
||||||
# Finds a suitable path from point A to point B using Breadth-First-Search Algorithm
|
|
||||||
def __init__(self, graph, start, goal):
|
|
||||||
self.graph = graph
|
|
||||||
self.start = start
|
|
||||||
self.goal = goal
|
|
||||||
|
|
||||||
def solve(self):
|
|
||||||
print('Start\n\n')
|
|
||||||
print(self.graph)
|
|
||||||
print('\n\n')
|
|
||||||
# keep track of explored nodes
|
|
||||||
explored = []
|
|
||||||
|
|
||||||
# keep track of all paths to be checked
|
|
||||||
queue = [[self.start]]
|
|
||||||
|
|
||||||
# return path if start is goal
|
|
||||||
if self.start == self.goal:
|
|
||||||
return 'That was easy. Start == Goal'
|
|
||||||
|
|
||||||
# keep looping until all possible paths are explored
|
|
||||||
while queue:
|
|
||||||
# pop the first path from the queue
|
|
||||||
path = queue.pop(0)
|
|
||||||
# get the last node from the path
|
|
||||||
node = path[-1]
|
|
||||||
|
|
||||||
if node not in explored:
|
|
||||||
neighbors = self.graph[node]
|
|
||||||
# go through all neighbor nodes
|
|
||||||
# push it into the queue
|
|
||||||
for neighbor in neighbors:
|
|
||||||
new_path = list(path)
|
|
||||||
new_path.append(neighbor)
|
|
||||||
queue.append(new_path)
|
|
||||||
|
|
||||||
if neighbor == self.goal:
|
|
||||||
return new_path
|
|
||||||
|
|
||||||
# mark node as explored
|
|
||||||
explored.append(node)
|
|
||||||
|
|
||||||
# in case there is no path
|
|
||||||
return "path not accessible"
|
|
||||||
class Grid(object):
|
|
||||||
def __init__(self, x, y, sx, sy):
|
|
||||||
self.x = x
|
|
||||||
self.y = y
|
|
||||||
self.sx = sx
|
|
||||||
self.sy = sy
|
|
||||||
self.width = 1
|
|
||||||
def draw(self):
|
|
||||||
pygame.draw.rect(SCREEN, BLACK, (self.x, self.y, self.sx, self.sy), self.width)
|
|
||||||
|
|
||||||
class Box(object):
|
|
||||||
def __init__(self, x, y, sx, sy, color):
|
|
||||||
self.x = x
|
|
||||||
self.y = y
|
|
||||||
self.sx = sx
|
|
||||||
self.sy = sy
|
|
||||||
self.color = color
|
|
||||||
|
|
||||||
def draw(self):
|
|
||||||
pygame.draw.rect(SCREEN, self.color, pygame.Rect(self.x, self.y, self.sx, self.sy))
|
|
||||||
|
|
||||||
class Obstacle(object):
|
|
||||||
def __init__(self, mouseObj):
|
|
||||||
self.mseX = mouseObj[0]
|
|
||||||
self.mseY = mouseObj[1]
|
|
||||||
|
|
||||||
for grid in gridObjects:
|
|
||||||
g = getGridBoxes(grid)
|
|
||||||
self.x = g.x
|
|
||||||
self.y = g.y
|
|
||||||
self.sx = g.sx
|
|
||||||
self.sy = g.sy
|
|
||||||
if self.mseX > 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):
|
def drawGrid(sizex,sizey):
|
||||||
spaceX = SCREENX // sizex
|
spaceX = SCREENX // sizex
|
||||||
spaceY = SCREENY // sizey
|
spaceY = SCREENY // sizey
|
||||||
@ -156,18 +70,18 @@ def generateGraph(row,col):
|
|||||||
miniG = {}
|
miniG = {}
|
||||||
for grid in range(len(gridObjects)):
|
for grid in range(len(gridObjects)):
|
||||||
grid += 1 # Synchronize index
|
grid += 1 # Synchronize index
|
||||||
mod = grid % col # Used to check the Top and Bottom Grid Boxes!
|
mod = grid % col # Used to check the Top and Bottom Grid Boxes
|
||||||
gN = grid - 1
|
gN = grid - 1
|
||||||
gS = grid + 1
|
gS = grid + 1
|
||||||
gE = grid + col
|
gE = grid + col
|
||||||
gW = grid - col
|
gW = grid - col
|
||||||
|
|
||||||
|
|
||||||
# CHECK THE NEIGHBORS TO THE GRID-BOXES, ACCOUNTING FOR THE EXTREME GRID-BOXES(BORDERS)
|
# 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 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: # 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
|
if grid > (col*row)-col: # You are on the Right Border of the screen - You can't go East
|
||||||
miniG[grid] = [gN, gW]
|
miniG[grid] = [gN, gW]
|
||||||
|
miniG[grid] = [gN, gW]
|
||||||
else: # Away from the Right Border of the Screen - You can go East
|
else: # Away from the Right Border of the Screen - You can go East
|
||||||
miniG[grid] = [gN, gE, gW]
|
miniG[grid] = [gN, gE, gW]
|
||||||
else: # You are on the Left Edge of the screen - You can't go West
|
else: # You are on the Left Edge of the screen - You can't go West
|
||||||
@ -191,7 +105,6 @@ def generateGraph(row,col):
|
|||||||
else: # You are on the Left Edge of the screen - You can't go West
|
else: # You are on the Left Edge of the screen - You can't go West
|
||||||
miniG[grid] = [gN, gS, gE]
|
miniG[grid] = [gN, gS, gE]
|
||||||
|
|
||||||
|
|
||||||
# FILTER OUT OBSTACLES FROM THE GRAPH
|
# FILTER OUT OBSTACLES FROM THE GRAPH
|
||||||
miniG2 = {}
|
miniG2 = {}
|
||||||
for grid in range(len(gridObjects)):
|
for grid in range(len(gridObjects)):
|
||||||
@ -205,7 +118,6 @@ def generateGraph(row,col):
|
|||||||
if neigbor in gridObstacle:
|
if neigbor in gridObstacle:
|
||||||
miniG2[grid].remove(neigbor)
|
miniG2[grid].remove(neigbor)
|
||||||
|
|
||||||
|
|
||||||
# Filtering again as the first Filter block didn't clear out everything
|
# Filtering again as the first Filter block didn't clear out everything
|
||||||
# Filtering through the neighbors
|
# Filtering through the neighbors
|
||||||
for grid in miniG2:
|
for grid in miniG2:
|
||||||
@ -213,25 +125,11 @@ def generateGraph(row,col):
|
|||||||
if item in gridObstacle:
|
if item in gridObstacle:
|
||||||
miniG2[grid].remove(item)
|
miniG2[grid].remove(item)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return miniG2
|
return miniG2
|
||||||
|
|
||||||
|
def refreshScreen():
|
||||||
def drawGraph(pathF, Ucelu):
|
#pygame.display.update()
|
||||||
#Draws the path given the path-list
|
#SCREEN.blit(tmpImg, (55 + 50 * player.x, 55 + 50 * player.y))
|
||||||
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
|
i = 0
|
||||||
@ -264,6 +162,7 @@ def drawGraph(pathF, Ucelu):
|
|||||||
for obs in obstacleObjects:
|
for obs in obstacleObjects:
|
||||||
obstacleObjects[obs].draw()
|
obstacleObjects[obs].draw()
|
||||||
|
|
||||||
|
|
||||||
for bx in boxObjects:
|
for bx in boxObjects:
|
||||||
boxObjects[bx].draw()
|
boxObjects[bx].draw()
|
||||||
|
|
||||||
@ -294,12 +193,59 @@ def drawGraph(pathF, Ucelu):
|
|||||||
|
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
pygame.time.wait(300)
|
pygame.time.wait(300)
|
||||||
#SCREEN.fill((WHITE))
|
SCREEN.fill((WHITE))
|
||||||
|
|
||||||
|
def drawGraph(pathF):
|
||||||
|
#Draws the path given the path-list
|
||||||
|
global Ucelu
|
||||||
|
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
|
||||||
|
a = 0
|
||||||
|
# pygame.draw.rect(SCREEN, GREEN, pygame.Rect(x, y, sx, sy))
|
||||||
|
|
||||||
|
if player.x < (x/50 - 1):
|
||||||
|
a = 1
|
||||||
|
if player.x > (x/50 - 1):
|
||||||
|
a =2
|
||||||
|
if player.y < (y/50 - 1):
|
||||||
|
a =3
|
||||||
|
if player.y > (y/50 - 1):
|
||||||
|
a =4
|
||||||
|
|
||||||
|
if a==1:
|
||||||
|
# player.x = x/50 - 1
|
||||||
|
player.rotation = 0
|
||||||
|
if a==2:
|
||||||
|
# player.x = x/50 - 1
|
||||||
|
player.rotation = 180
|
||||||
|
if a==3:
|
||||||
|
# player.y = y/50 - 1
|
||||||
|
player.rotation = 270
|
||||||
|
if a==4:
|
||||||
|
# player.y = y/50 - 1
|
||||||
|
player.rotation = 90
|
||||||
|
|
||||||
|
refreshScreen()
|
||||||
|
|
||||||
|
#pygame.time.wait(2000)
|
||||||
|
|
||||||
|
player.y = y/50 - 1
|
||||||
|
player.x = x/50 - 1
|
||||||
|
|
||||||
|
refreshScreen()
|
||||||
# pygame.time.wait(50)
|
# pygame.time.wait(50)
|
||||||
# pygame.draw.rect(SCREEN, WHITE, pygame.Rect(x, y, sx, sy))
|
# pygame.draw.rect(SCREEN, WHITE, pygame.Rect(x, y, sx, sy))
|
||||||
Ucelu = True
|
Ucelu = True
|
||||||
def UIHandler(mouseObj, Ucelu):
|
def UIHandler(mouseObj):
|
||||||
# drawGrid(GRIDX, GRIDY)
|
# drawGrid(GRIDX, GRIDY)
|
||||||
|
global Ucelu
|
||||||
drawGrid(10,10)
|
drawGrid(10,10)
|
||||||
|
|
||||||
for grid in gridObjects:
|
for grid in gridObjects:
|
||||||
@ -312,16 +258,62 @@ def UIHandler(mouseObj, Ucelu):
|
|||||||
obstacleObjects[obs].draw()
|
obstacleObjects[obs].draw()
|
||||||
|
|
||||||
if pathFound:
|
if pathFound:
|
||||||
if Ucelu == False:
|
drawGraph(pathFound)
|
||||||
drawGraph(pathFound, Ucelu)
|
# Ucelu = False
|
||||||
Ucelu = True
|
|
||||||
|
|
||||||
def eventHandler(kbdObj,mouseObj, Ucelu):
|
def eventHandler(kbdObj,mouseObj):
|
||||||
global boxes
|
global boxes
|
||||||
global obstacles
|
global obstacles
|
||||||
global startNode
|
global startNode
|
||||||
global goalNode
|
global goalNode
|
||||||
global pathFound
|
global pathFound
|
||||||
|
global Ucelu
|
||||||
|
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
exit("Thank you for new plants <3")
|
||||||
|
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
pygame.time.wait(DELAY)
|
||||||
|
|
||||||
|
if event.key == pygame.K_LEFT:
|
||||||
|
if player.x > 0:
|
||||||
|
player.x = player.x - 1
|
||||||
|
player.rotation = 180
|
||||||
|
|
||||||
|
if event.key == pygame.K_UP:
|
||||||
|
if player.y > 0:
|
||||||
|
player.y = player.y - 1
|
||||||
|
player.rotation = 90
|
||||||
|
|
||||||
|
if event.key == pygame.K_RIGHT:
|
||||||
|
if player.x < 9:
|
||||||
|
player.x = player.x + 1
|
||||||
|
player.rotation = 0
|
||||||
|
|
||||||
|
if event.key == pygame.K_DOWN:
|
||||||
|
if player.y < 9:
|
||||||
|
player.y = player.y + 1
|
||||||
|
player.rotation = 270
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# If Key_f is pressed, set goal node
|
# If Key_f is pressed, set goal node
|
||||||
@ -361,10 +353,12 @@ def eventHandler(kbdObj,mouseObj, Ucelu):
|
|||||||
# goalNode = GRIDX*GRIDX
|
# goalNode = GRIDX*GRIDX
|
||||||
# goalNode = (10 * (x + 1) + (y + 1) - 10)
|
# goalNode = (10 * (x + 1) + (y + 1) - 10)
|
||||||
goalNode = (10 * (posX/50 ) + (posY/50) - 10)
|
goalNode = (10 * (posX/50 ) + (posY/50) - 10)
|
||||||
|
# pygame.display.update()
|
||||||
|
|
||||||
# goalNode = (x/sx) * (y/sy)
|
# goalNode = (x/sx) * (y/sy)
|
||||||
# Delay to avoid multiple spawning of objects
|
# Delay to avoid multiple spawning of objects
|
||||||
pygame.time.wait(DELAY)
|
pygame.time.wait(DELAY)
|
||||||
|
|
||||||
# If Key_x is pressed, spawn tree
|
# If Key_x is pressed, spawn tree
|
||||||
if kbdObj[pygame.K_x]:
|
if kbdObj[pygame.K_x]:
|
||||||
obs = Obstacle(mouseObj)
|
obs = Obstacle(mouseObj)
|
||||||
@ -374,11 +368,13 @@ def eventHandler(kbdObj,mouseObj, Ucelu):
|
|||||||
# print(obstacleObjects)
|
# print(obstacleObjects)
|
||||||
gridObstacle[obs.gridBox] = obstacles
|
gridObstacle[obs.gridBox] = obstacles
|
||||||
# Delay to avoid multiple spawning of objects
|
# Delay to avoid multiple spawning of objects
|
||||||
|
pygame.display.update()
|
||||||
pygame.time.wait(DELAY)
|
pygame.time.wait(DELAY)
|
||||||
|
|
||||||
|
|
||||||
# if Key_SPACE is pressed, start the magic
|
# if Key_SPACE is pressed, start the magic
|
||||||
if kbdObj[pygame.K_SPACE]:
|
if kbdObj[pygame.K_SPACE]:
|
||||||
|
Ucelu = False
|
||||||
gBox = getGridBoxes(1)
|
gBox = getGridBoxes(1)
|
||||||
|
|
||||||
x = gBox.x
|
x = gBox.x
|
||||||
@ -411,40 +407,22 @@ def eventHandler(kbdObj,mouseObj, Ucelu):
|
|||||||
#pygame.time.wait(DELAY)
|
#pygame.time.wait(DELAY)
|
||||||
|
|
||||||
graph = generateGraph(GRIDY,GRIDX)
|
graph = generateGraph(GRIDY,GRIDX)
|
||||||
|
|
||||||
|
if startNode != goalNode:
|
||||||
bfs = BFS(graph, startNode, goalNode)
|
bfs = BFS(graph, startNode, goalNode)
|
||||||
# print(bfs.solve())
|
# print(bfs.solve())
|
||||||
pathFound = bfs.solve()
|
pathFound = bfs.solve()
|
||||||
|
# else:
|
||||||
|
# startNode = (10 * (player.x + 1) + (player.y + 1) - 10)
|
||||||
|
# Ucelu = True
|
||||||
|
|
||||||
# Delay to avoid multiple spawning of objects
|
# Delay to avoid multiple spawning of objects
|
||||||
pygame.time.wait(DELAY)
|
pygame.time.wait(DELAY)
|
||||||
|
# startNode = goalNode
|
||||||
|
|
||||||
#With it it keeps going, if without it turns off
|
#With it it keeps going, if without it turns off
|
||||||
Ucelu = False
|
|
||||||
|
|
||||||
class Field:
|
# Ucelu = False
|
||||||
def __init__(self, fieldType, plantType, isWet, wetTime, isFertilized, fertilizedTime):
|
|
||||||
self.fieldType = fieldType # good/bad
|
|
||||||
self.plantType = plantType # wheat/carrot/cabbage
|
|
||||||
self.isWet = isWet # yes/no
|
|
||||||
self.wetTime = wetTime # number
|
|
||||||
self.isFertilized = isFertilized # yes/no
|
|
||||||
self.fertilizedTime = fertilizedTime # number
|
|
||||||
|
|
||||||
|
|
||||||
class Plant:
|
|
||||||
def __init__(self, plantType, growthState):
|
|
||||||
self.plantType = plantType # wheat/carrot/cabbage
|
|
||||||
self.growthState = growthState # growing/grown
|
|
||||||
|
|
||||||
|
|
||||||
class Fertilizer:
|
|
||||||
def __init__(self, fertilizerType):
|
|
||||||
self.fertilizerType = fertilizerType # wheat/carrot/cabbage
|
|
||||||
|
|
||||||
|
|
||||||
class Player:
|
|
||||||
x = 0
|
|
||||||
y = 0
|
|
||||||
rotation = 0
|
|
||||||
|
|
||||||
|
|
||||||
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(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)],
|
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(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)],
|
||||||
@ -458,84 +436,39 @@ 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)],
|
||||||
[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()
|
|
||||||
|
|
||||||
|
# =========================================================================================
|
||||||
|
# main
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
player = Player()
|
player = Player()
|
||||||
|
|
||||||
# player.x = 2
|
|
||||||
# player.y = 2
|
|
||||||
|
|
||||||
#screen = pygame.display.set_mode([600, 600])
|
|
||||||
|
|
||||||
running = True
|
running = True
|
||||||
# clock = pygame.time.Clock()
|
# clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
SCREEN.fill(WHITE)
|
||||||
|
|
||||||
SCREEN.fill((WHITE))
|
|
||||||
while running:
|
while running:
|
||||||
|
|
||||||
for event in pygame.event.get():
|
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:
|
|
||||||
player.x = player.x - 1
|
|
||||||
if event.key == pygame.K_UP:
|
|
||||||
if player.y > 0:
|
|
||||||
player.y = player.y - 1
|
|
||||||
if event.key == pygame.K_RIGHT:
|
|
||||||
if player.x < 9:
|
|
||||||
player.x = player.x + 1
|
|
||||||
if event.key == pygame.K_DOWN:
|
|
||||||
if player.y < 9:
|
|
||||||
player.y = player.y + 1
|
|
||||||
|
|
||||||
# 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 = pygame.key.get_pressed()
|
||||||
# kbd = event.key()
|
|
||||||
mse = pygame.mouse.get_pos()
|
mse = pygame.mouse.get_pos()
|
||||||
|
UIHandler(mse)
|
||||||
# SCREEN.fill((WHITE))
|
eventHandler(kbd, mse)
|
||||||
Ucelu = False
|
|
||||||
UIHandler(mse, Ucelu)
|
|
||||||
eventHandler(kbd, mse, Ucelu)
|
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
# CLOCK.tick(FPS)
|
# CLOCK.tick(FPS)
|
||||||
|
|
||||||
#screen.fill((175, 255, 50, 0))
|
#screen.fill((175, 255, 50, 0))
|
||||||
#screen.fill((WHITE))
|
|
||||||
|
# SCREEN.fill((WHITE))
|
||||||
imgWheat = pygame.image.load('img/wheat.png')
|
imgWheat = pygame.image.load('img/wheat.png')
|
||||||
imgCarrot = pygame.image.load('img/carrot.png')
|
imgCarrot = pygame.image.load('img/carrot.png')
|
||||||
imgCabbage = pygame.image.load('img/cabbage.png')
|
imgCabbage = pygame.image.load('img/cabbage.png')
|
||||||
imgPlayer = pygame.image.load('img/player.png')
|
imgPlayer = pygame.image.load('img/player.png')
|
||||||
|
global imgTree
|
||||||
imgTree = pygame.image.load('img/tree.png')
|
imgTree = pygame.image.load('img/tree.png')
|
||||||
|
# pygame.display.update()
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
while i < len(T):
|
while i < len(T):
|
||||||
@ -569,6 +502,14 @@ while running:
|
|||||||
pygame.draw.line(SCREEN, (0, 0, 0), (50, 50 + i * 50), (50 + len(T) * 50, 50 + i * 50), 1)
|
pygame.draw.line(SCREEN, (0, 0, 0), (50, 50 + i * 50), (50 + len(T) * 50, 50 + i * 50), 1)
|
||||||
i = i + 1
|
i = i + 1
|
||||||
|
|
||||||
|
for obs in obstacleObjects:
|
||||||
|
obstacleObjects[obs].draw()
|
||||||
|
|
||||||
|
if startNode != goalNode:
|
||||||
|
for bx in boxObjects:
|
||||||
|
boxObjects[bx].draw()
|
||||||
|
|
||||||
|
|
||||||
tmpImg = pygame.transform.rotate(imgPlayer, player.rotation)
|
tmpImg = pygame.transform.rotate(imgPlayer, player.rotation)
|
||||||
if player.rotation == 180:
|
if player.rotation == 180:
|
||||||
tmpImg = pygame.transform.flip(tmpImg, True, True)
|
tmpImg = pygame.transform.flip(tmpImg, True, True)
|
||||||
@ -577,18 +518,12 @@ while running:
|
|||||||
#player seen at the beginning
|
#player seen at the beginning
|
||||||
SCREEN.blit(tmpImg, (55 + 50 * player.x, 55 + 50 * player.y))
|
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
|
# if Ucelu == False:
|
||||||
# y = gBox.y
|
# for bx in boxObjects:
|
||||||
# sx = gBox.sx
|
# boxObjects[bx].draw()
|
||||||
# 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)
|
font = pygame.font.SysFont('comicsans', 18)
|
||||||
label = font.render('f- punkt końcowy, x- drzewa, spacja- uruchomienie', 1, (0, 0, 0))
|
label = font.render('f- punkt końcowy, x- drzewa, spacja- uruchomienie', 1, (0, 0, 0))
|
||||||
|
Loading…
Reference in New Issue
Block a user