added rocks as obstacles and spawning obstacles on the board

This commit is contained in:
MarRac 2024-04-14 23:19:06 +02:00
parent f10a60063d
commit 1f49f5275d
13 changed files with 65 additions and 21 deletions

View File

@ -1,8 +1,10 @@
# create a field here : 1: add tiles, 2: place them
import pygame
import random
from area.constants import WIDTH,HEIGHT,FIELD_WIDTH,FIELD_HEIGHT,TILE_SIZE,GREY,ROWS,COLS
from tile import Tile
from ground import Dirt
tiles = []
@ -22,6 +24,9 @@ def createTiles():
for y in range(0, COLS):
for x in range(0, ROWS):
tile = Tile(x*TILE_SIZE, y*TILE_SIZE)
dirt = Dirt(random.randint(1, 100), random.randint(1, 100))
dirt.pests_and_weeds()
tile.ground = dirt
tile.randomizeContent()
tiles.append(tile)
positionFieldElements()

View File

@ -1,6 +1,6 @@
from crop_protection_product import CropProtectionProduct
from area.constants import TILE_SIZE, DIRECTION_EAST, DIRECTION_SOUTH, DIRECTION_WEST, DIRECTION_NORTH, FIELD_WIDTH, FIELD_HEIGHT, WIDTH, HEIGHT
from area.field import fieldX, fieldY
from area.field import fieldX, fieldY, tiles
import pygame
import time
@ -18,7 +18,7 @@ class Tractor:
metazachlor = CropProtectionProduct("weeds", "vegetable")
# etc
def __init__(self, x, y, direction):
def __init__(self, x, y, direction, tractor_start, tractor_end):
self.x = x
self.y = y
@ -27,6 +27,9 @@ class Tractor:
self.direction = direction
self.image = pygame.image.load('resources/images/tractor_right.png').convert_alpha()
self.tractor_start = tractor_start #important for bfs - prevents from spawning obstacles on these positions
self.tractor_end = tractor_end #
if (self.direction==1):
self.image = pygame.image.load('resources/images/tractor_up.png').convert_alpha()
elif (self.direction==3):
@ -107,7 +110,7 @@ class Tractor:
self.direction -= 1
self.image = pygame.transform.rotate(self.image, 90)
#sprawdza czy mozna sie dalej poruszyc, czy nie wyjdziemy poza mape - do funkcji succ w bfs
#checks if we can move further and if we won't get out of boundaries - for bfs:
def can_it_move_node(node):
if node.get_direction() == DIRECTION_EAST and node.get_x() + TILE_SIZE < 830: #last tile on the west has y:797
return "move east"
@ -120,13 +123,24 @@ class Tractor:
else:
return False
#checks if there's an obstacle on the given coordinates (important for bfs):
def is_obstacle(self, x, y):
for tile in tiles:
if tile.x == x and tile.y == y:
ground = tile.ground
if ground.obstacle and self.tractor_start != (x, y) and self.tractor_end != (x,y):
return True
return False
def draw_tractor(self, win):
imageTractor = pygame.transform.scale(self.image, (TILE_SIZE, TILE_SIZE))
win.blit(imageTractor, (self.rect.x, self.rect.y))
pygame.display.flip()
#translates move_list generated by bfs into the actual movement:
def do_actions(tractor, WIN, move_list):
trail = pygame.image.load("resources/images/background.jpg").convert_alpha()
trail = pygame.transform.scale(trail, (TILE_SIZE, TILE_SIZE))
@ -143,3 +157,5 @@ def do_actions(tractor, WIN, move_list):
tractor.draw_tractor(WIN)
pygame.display.update()
time.sleep(1)

View File

@ -94,7 +94,7 @@ def goal_test(elem, goalstate):
#actions(string): move, rotate_to_left, rotate_to_right
#main search function:
def graphsearch(istate, succ, goaltest):
def graphsearch(istate, succ, goaltest, tractor):
fringe = []
explored = []
@ -113,7 +113,7 @@ def graphsearch(istate, succ, goaltest):
explored.append(elem)
for (action, state) in succ(temp): #dla wszystkich mozliwych stanow i akcjach otrzymanych dla danego wierzcholka
for (action, state) in succ(temp, tractor): #dla wszystkich mozliwych stanow i akcjach otrzymanych dla danego wierzcholka
fringe_tuple = []
explored_tuple = []
@ -129,7 +129,7 @@ def graphsearch(istate, succ, goaltest):
#funkcja nastepnika - jakie akcje sa mozlwie na danym polu i jaki bedzie stan po wykonaniu tych akcji
def succ(elem):
def succ(elem, tractor):
actions_states = []
temp = copy.copy(elem.get_direction())
@ -151,14 +151,18 @@ def succ(elem):
temp_move_north = elem.get_y() - TILE_SIZE
temp_move_south = elem.get_y() + TILE_SIZE
if Tractor.can_it_move_node(elem) == "move east":
if Tractor.can_it_move_node(elem) == "move east" and not Tractor.is_obstacle(tractor, temp_move_east, elem.get_y()):
actions_states.append(("move", (temp_move_east, elem.get_y(), elem.get_direction())))
elif Tractor.can_it_move_node(elem) == "move west":
elif Tractor.can_it_move_node(elem) == "move west" and not Tractor.is_obstacle(tractor, temp_move_west, elem.get_y()):
actions_states.append(("move", (temp_move_west, elem.get_y(), elem.get_direction())))
elif Tractor.can_it_move_node(elem) == "move north":
elif Tractor.can_it_move_node(elem) == "move north" and not Tractor.is_obstacle(tractor,elem.get_x(), temp_move_north):
actions_states.append(("move", (elem.get_x(), temp_move_north, elem.get_direction())))
elif Tractor.can_it_move_node(elem) == "move south":
elif Tractor.can_it_move_node(elem) == "move south" and not Tractor.is_obstacle(tractor, elem.get_x(), temp_move_south):
actions_states.append(("move", (elem.get_x(), temp_move_south, elem.get_direction())))
return actions_states

View File

@ -20,7 +20,7 @@ class Dirt:
elif i == 4:
self.weed = True
self.pest = True
elif i == 5 or i == 6 or i == 7:
elif i == 5:
self.obstacle = True
# add init, getters,setters

View File

@ -19,21 +19,38 @@ def main():
window = drawWindow(WIN)
pygame.display.update()
#getting coordinates of a certain tile - test:
tile_index=399
#getting coordinates of our "goal tile":
tile_index=245
tile_x, tile_y = get_tile_coordinates(tile_index)
if tile_x is not None and tile_y is not None:
print(f"Coordinates of tile {tile_index} are: ({tile_x}, {tile_y})")
else: print("Such tile does not exist")
#mark the goal tile:
tiles[tile_index].image = "resources/images/sampling_goal.png"
image = pygame.image.load(tiles[tile_index].image).convert()
image = pygame.transform.scale(image, (TILE_SIZE, TILE_SIZE))
WIN.blit(image, (tiles[tile_index].x, tiles[tile_index].y))
pygame.display.flip()
#graphsearch activation:
istate = Istate(170, 100, 2)
goaltest = []
goaltest.append(tile_x)
goaltest.append(tile_y)
moves = (graphsearch(istate, succ, goaltest))
tractor = Tractor(0*TILE_SIZE, 0*TILE_SIZE, 2, None, None)
tractor.rect.x += fieldX
tractor.rect.y += fieldY
tractor.tractor_start = ((istate.get_x(), istate.get_y()))
tractor.tractor_end = ((goaltest[0], goaltest[1]))
moves = (graphsearch(istate, succ, goaltest, tractor))
print(moves)
#main loop:
while run:
for event in pygame.event.get():
@ -49,16 +66,14 @@ def main():
tile1.ground=d1
#movement based on route-planning test:
tractor = Tractor(0*TILE_SIZE, 0*TILE_SIZE, 2)
tractor.rect.x += fieldX
tractor.rect.y += fieldY
tractor.draw_tractor(WIN)
time.sleep(1)
if moves != False:
do_actions(tractor, WIN, moves)
tractor.work_on_field(tile1, d1, p1)
time.sleep(2)
time.sleep(15)
print("\n")

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

View File

@ -43,5 +43,9 @@ class Tile:
else:
self.image = "resources/images/dirt.png"
self.photo = "resources/images/background.jpg"
ground = self.ground
if ground.obstacle:
self.image = "resources/images/rock_dirt.png"
# DISCLAMER check column and choose plant type ("potato","wheat" etc.)