bfs template added & movement tractor changes

This commit is contained in:
micwuj 2023-04-22 21:31:19 +02:00
parent dde8b5e72c
commit bc433f23a9
10 changed files with 133 additions and 53 deletions

Binary file not shown.

31
main.py
View File

@ -1,10 +1,11 @@
import pygame import pygame
import sys import sys
import random import random
from settings import screen_height, screen_width, SIZE, SPECIES, block_size, tile, road_coords from settings import screen_height, screen_width, SIZE, SPECIES, block_size, tile, road_coords, directions
from src.map import drawRoads, seedForFirstTime from src.map import drawRoads, seedForFirstTime
from src.Tractor import Tractor from src.Tractor import Tractor
from src.Plant import Plant from src.Plant import Plant
from src.bfs import BFS
# pygame initialization # pygame initialization
pygame.init() pygame.init()
@ -21,20 +22,29 @@ background.fill((90,50,20))
background = drawRoads(background) background = drawRoads(background)
for line in range(26): for line in range(26):
pygame.draw.line(background, (0, 0, 0), (0, line * 36), (SIZE[0], line * 36)) pygame.draw.line(background, (0, 0, 0), (0, line * block_size), (screen_width, line * block_size))
pygame.draw.line(background, (0, 0, 0), (line * 36, 0), (line * 36, SIZE[1])) pygame.draw.line(background, (0, 0, 0), (line * block_size, 0), (line * block_size, screen_height))
#TRACTOR #TRACTOR
tractor = Tractor('oil','manual', 'fuel', 'fertilizer1') tractor = Tractor('oil','manual', 'fuel', 'fertilizer1', 20)
tractor_group = pygame.sprite.Group() tractor_group = pygame.sprite.Group()
tractor.rect.x = 0
tractor.rect.y = 0
tractor_group.add(tractor) tractor_group.add(tractor)
#PLANTS #PLANTS
plant_group = pygame.sprite.Group() plant_group = pygame.sprite.Group()
plant_group = seedForFirstTime() plant_group = seedForFirstTime()
#
tractor_move = pygame.USEREVENT + 1
pygame.time.set_timer(tractor_move, 800)
moves = []
goal_bfs = BFS()
destination = (random.randrange(0, 936, 36), random.randrange(0, 900, 36))
print("Destination: ", destination)
moves = goal_bfs.search(
[tractor.rect.x, tractor.rect.y, directions[tractor.rotation]], destination)
if __name__ == "__main__": if __name__ == "__main__":
running = True running = True
@ -49,11 +59,16 @@ if __name__ == "__main__":
tractor.collect(plant_group) tractor.collect(plant_group)
if event.key == pygame.K_ESCAPE: if event.key == pygame.K_ESCAPE:
running = False running = False
if event.type == tractor_move:
if len(moves) != 0:
step = moves.pop()
tractor.movement(step[0])
Tractor.movement(tractor)
Tractor.movement_using_keys(tractor)
screen.blit(background,(0,0)) screen.blit(background,(0,0))
plant_group.draw(screen) plant_group.draw(screen)
tractor_group.draw(screen) tractor_group.draw((screen))
tractor_group.update() tractor_group.update()
pygame.display.flip() pygame.display.flip()
clock.tick(60) clock.tick(60)

View File

@ -12,4 +12,6 @@ road_coords = [0, 5, 10, 15, 20, 25]
field_width = 4 field_width = 4
field_height = 4 field_height = 4
field_size = field_width*field_height field_size = field_width*field_height
fields_amount = 25 fields_amount = 26
directions = {0: 'UP', 90: 'RIGHT', 180: 'DOWN', 270: 'LEFT'}

View File

@ -1,4 +1,5 @@
import pygame import pygame
from settings import block_size
class Plant(pygame.sprite.Sprite): class Plant(pygame.sprite.Sprite):
def __init__(self,species,is_ill,pos_x,pos_y): def __init__(self,species,is_ill,pos_x,pos_y):
@ -43,7 +44,7 @@ class Plant(pygame.sprite.Sprite):
self.pic_path="assets/Wheat.png" self.pic_path="assets/Wheat.png"
self.image = pygame.image.load(self.pic_path) #zmienic self.image = pygame.image.load(self.pic_path) #zmienic
self.image = pygame.transform.scale(self.image,(36,36)) self.image = pygame.transform.scale(self.image,(block_size, block_size))
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
self.rect.center = [pos_x,pos_y] self.rect.center = [pos_x,pos_y]

View File

@ -1,60 +1,103 @@
import pygame import pygame
from settings import block_size, tile
class Tractor(pygame.sprite.Sprite): class Tractor(pygame.sprite.Sprite):
def __init__(self,engine,transmission,fuel,fertilizer): def __init__(self, engine, transmission, fuel, fertilizer, capacity):
super().__init__() super().__init__()
self.image=pygame.image.load("assets/tractor/tractor.png") self.image = pygame.image.load("assets/tractor/tractor.png")
self.image=pygame.transform.scale(self.image,(36,36)) self.image = pygame.transform.scale(self.image, tile)
self.UP = pygame.transform.rotate(self.image, 0)
self.DOWN = pygame.transform.rotate(self.image, 180)
self.LEFT = pygame.transform.rotate(self.image, 90)
self.RIGHT = pygame.transform.rotate(self.image, -90)
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
self.engine=engine self.up = pygame.transform.rotate(self.image, 0)
self.transmission=transmission self.down = pygame.transform.rotate(self.image, 180)
self.fuel=fuel self.left = pygame.transform.rotate(self.image, 90)
self.fertilizer=fertilizer self.right = pygame.transform.rotate(self.image, -90)
def movement(self): self.rect.x = 0
self.rect.y = 0
self.direction = 'F'
self.rotation = 90
self.collected = 0
self.capacity = capacity
self.engine = engine
self.transmission = transmission
self.fuel = fuel
self.fertilizer = fertilizer
def movement_using_keys(self):
keys = pygame.key.get_pressed() keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and self.rect.x>0: if keys[pygame.K_LEFT]:
self.image = self.LEFT self.movement('L')
self.rect.x -= 36 if keys[pygame.K_RIGHT]:
if keys[pygame.K_RIGHT] and self.rect.x<900: self.movement('R')
self.image = self.RIGHT if keys[pygame.K_UP]:
self.rect.x += 36 self.movement('F')
if keys[pygame.K_UP] and self.rect.y>0:
self.image = self.UP
self.rect.y -= 36
if keys[pygame.K_DOWN] and self.rect.y<900:
self.image = self.DOWN
self.rect.y += 36
def collect(self,plant_group): #waits between moves to avoid moving to fast
self.plant_group=plant_group pygame.time.wait(100)
print("collected plant")
pygame.sprite.spritecollide(self,self.plant_group,True)
# collected=collected+1
# print("plants in trunk "+collected)
def water_plant(self,plant_group): def move_forward(self):
self.plant_group=plant_group if self.rect.y > 0 and self.rotation == 0:
self.rect.y -= block_size
if self.rect.x < 900 and self.rotation == 90:
self.rect.x += block_size
if self.rect.y < 900 and self.rotation == 180:
self.rect.y += block_size
if self.rect.x > 0 and self.rotation == 270:
self.rect.x -= block_size
def move_left(self):
self.rotation -= 90
if self.rotation < 0:
self.rotation = 270
def move_right(self):
self.rotation += 90
if self.rotation >= 360:
self.rotation = 0
def check_rotation(self):
if self.rotation == 0:
self.image = self.up
elif self.rotation == 90:
self.image = self.right
elif self.rotation == 180:
self.image = self.down
elif self.rotation == 270:
self.image = self.left
def movement(self, direction):
if direction == 'F':
self.move_forward()
elif direction == 'L':
self.move_left()
elif direction == 'R':
self.move_right()
self.check_rotation()
def collect(self, plant_group):
if self.collected <= self.capacity:
self.plant_group=plant_group
# print("collected plant")
pygame.sprite.spritecollide(self, self.plant_group, True)
self.collected += 1
# print("plants in trunk "+collected)
def water_plant(self, plant_group):
self.plant_group = plant_group
print("watered plant") print("watered plant")
# def update(self):
# self.rect.center=pygame.mouse.get_pos()
def fertilize(self, plant_group): def fertilize(self, plant_group):
self.plant_group=plant_group self.plant_group = plant_group
print("fertilize") print("fertilize")
def plant(self, plant_group): def plant(self, plant_group):
self.plant_group=plant_group self.plant_group = plant_group
print("new plant") print("new plant")
def find_nearest_plant(self,plant_group): def find_nearest_plant(self, plant_group):
self.plant_group=plant_group self.plant_group = plant_group

Binary file not shown.

Binary file not shown.

19
src/bfs.py Normal file
View File

@ -0,0 +1,19 @@
from settings import block_size, screen_width, directions
class Node:
def __init__(self, state, parent=None, action=None):
self.state = state
self.parent = parent
self.action = action
class BFS:
def __init__(self):
self.fringe = []
self.explored = []
def successor(self, state):
pass
def search(self):
pass