From bc433f23a93471ed6d5f92bd2ee66c0b60773979 Mon Sep 17 00:00:00 2001 From: micwuj Date: Sat, 22 Apr 2023 21:31:19 +0200 Subject: [PATCH] bfs template added & movement tractor changes --- __pycache__/settings.cpython-310.pyc | Bin 492 -> 553 bytes main.py | 33 ++++-- settings.py | 4 +- src/Plant.py | 3 +- src/Tractor.py | 127 ++++++++++++++++-------- src/__pycache__/Plant.cpython-310.pyc | Bin 1103 -> 1343 bytes src/__pycache__/Tractor.cpython-310.pyc | Bin 1086 -> 3312 bytes src/__pycache__/bfs.cpython-310.pyc | Bin 0 -> 2288 bytes src/__pycache__/map.cpython-310.pyc | Bin 1436 -> 1424 bytes src/bfs.py | 19 ++++ 10 files changed, 133 insertions(+), 53 deletions(-) create mode 100644 src/__pycache__/bfs.cpython-310.pyc create mode 100644 src/bfs.py diff --git a/__pycache__/settings.cpython-310.pyc b/__pycache__/settings.cpython-310.pyc index 4d2d62ec63cf3dde28f9c847e9e78970b06cb8d9..f1197560e16e1f9da5b8efb621eac42c4ffb2cb8 100644 GIT binary patch delta 284 zcmaFEyplybpO=@50SKCYIHycwWMFs<;vfT7AjbiSi=!rL`FyD7i^au1-0v2PkE>gVCQA{}6$~$K1oeW@=7Ke0gR{NyaT62qz;oGd-i^78it9oLQB6ixu2QWrs|g#rxs0Kz-YiIHTfc=Bnu0p5EB3nGg31E delta 224 zcmZ3<@`hPEpO=@50SH1BjQ-u}nOx!CAx!Wcq3HMordc42z25 zPRmTqNr^AdOex8T;(>57QZv&tN}{+RyyDEN)F@ssyEr~EH@`HmWFSU)APC^IpoJin-_GOsv0aq?|O14fC-JWP^I9E<>e<~vvb diff --git a/main.py b/main.py index 26bbac1..deec3b8 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,11 @@ import pygame import sys 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.Tractor import Tractor from src.Plant import Plant +from src.bfs import BFS # pygame initialization pygame.init() @@ -21,20 +22,29 @@ background.fill((90,50,20)) background = drawRoads(background) 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), (line * 36, 0), (line * 36, SIZE[1])) + pygame.draw.line(background, (0, 0, 0), (0, line * block_size), (screen_width, line * block_size)) + pygame.draw.line(background, (0, 0, 0), (line * block_size, 0), (line * block_size, screen_height)) #TRACTOR -tractor = Tractor('oil','manual', 'fuel', 'fertilizer1') +tractor = Tractor('oil','manual', 'fuel', 'fertilizer1', 20) tractor_group = pygame.sprite.Group() -tractor.rect.x = 0 -tractor.rect.y = 0 tractor_group.add(tractor) #PLANTS plant_group = pygame.sprite.Group() 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__": running = True @@ -49,11 +59,16 @@ if __name__ == "__main__": tractor.collect(plant_group) if event.key == pygame.K_ESCAPE: running = False - - Tractor.movement(tractor) + if event.type == tractor_move: + if len(moves) != 0: + step = moves.pop() + tractor.movement(step[0]) + + + Tractor.movement_using_keys(tractor) screen.blit(background,(0,0)) plant_group.draw(screen) - tractor_group.draw(screen) + tractor_group.draw((screen)) tractor_group.update() pygame.display.flip() clock.tick(60) \ No newline at end of file diff --git a/settings.py b/settings.py index f807018..916f954 100644 --- a/settings.py +++ b/settings.py @@ -12,4 +12,6 @@ road_coords = [0, 5, 10, 15, 20, 25] field_width = 4 field_height = 4 field_size = field_width*field_height -fields_amount = 25 +fields_amount = 26 + +directions = {0: 'UP', 90: 'RIGHT', 180: 'DOWN', 270: 'LEFT'} \ No newline at end of file diff --git a/src/Plant.py b/src/Plant.py index 4702979..11e8cc4 100644 --- a/src/Plant.py +++ b/src/Plant.py @@ -1,4 +1,5 @@ import pygame +from settings import block_size class Plant(pygame.sprite.Sprite): 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.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.center = [pos_x,pos_y] \ No newline at end of file diff --git a/src/Tractor.py b/src/Tractor.py index 3cfc78e..aff7f1d 100644 --- a/src/Tractor.py +++ b/src/Tractor.py @@ -1,60 +1,103 @@ import pygame - - +from settings import block_size, tile class Tractor(pygame.sprite.Sprite): - def __init__(self,engine,transmission,fuel,fertilizer): + def __init__(self, engine, transmission, fuel, fertilizer, capacity): super().__init__() - self.image=pygame.image.load("assets/tractor/tractor.png") - self.image=pygame.transform.scale(self.image,(36,36)) - 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.image = pygame.image.load("assets/tractor/tractor.png") + self.image = pygame.transform.scale(self.image, tile) + self.rect = self.image.get_rect() + + 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.x = 0 + self.rect.y = 0 + self.direction = 'F' + self.rotation = 90 - self.engine=engine - self.transmission=transmission - self.fuel=fuel - self.fertilizer=fertilizer + self.collected = 0 + self.capacity = capacity + self.engine = engine + self.transmission = transmission + self.fuel = fuel + self.fertilizer = fertilizer - def movement(self): + def movement_using_keys(self): keys = pygame.key.get_pressed() - if keys[pygame.K_LEFT] and self.rect.x>0: - self.image = self.LEFT - self.rect.x -= 36 - if keys[pygame.K_RIGHT] and self.rect.x<900: - self.image = self.RIGHT - self.rect.x += 36 - 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): - self.plant_group=plant_group - print("collected plant") - pygame.sprite.spritecollide(self,self.plant_group,True) - # collected=collected+1 - # print("plants in trunk "+collected) + if keys[pygame.K_LEFT]: + self.movement('L') + if keys[pygame.K_RIGHT]: + self.movement('R') + if keys[pygame.K_UP]: + self.movement('F') - def water_plant(self,plant_group): - self.plant_group=plant_group + #waits between moves to avoid moving to fast + pygame.time.wait(100) + + def move_forward(self): + 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") - # def update(self): - # self.rect.center=pygame.mouse.get_pos() def fertilize(self, plant_group): - self.plant_group=plant_group + self.plant_group = plant_group print("fertilize") def plant(self, plant_group): - self.plant_group=plant_group + self.plant_group = plant_group print("new plant") - def find_nearest_plant(self,plant_group): - self.plant_group=plant_group + def find_nearest_plant(self, plant_group): + self.plant_group = plant_group \ No newline at end of file diff --git a/src/__pycache__/Plant.cpython-310.pyc b/src/__pycache__/Plant.cpython-310.pyc index f5e775c8df75df5656dbd1d4678100a776a19c05..9fa6092586ae0180ab460f98df79ad1589cc3086 100644 GIT binary patch delta 787 zcmaiyPm9w)7{+HN$uvouZnxY0dkM8~wwrF6lq7;J zg&y=Ql-zauAv}BV193j=f}Ngre+cFp*Rzqy+*GF|Tr~yjVZH7jx5GFN2i`GCpT&EUhoz}r+@dn}wZGt9k zffn5W?X)_mU7_K?UU29dbp2`tx|TN5#<^IihS(OWRu>F$bE-}CKC*+RyGHvAXSIEt z%U(B*qqHELOmU>JtIz>*J&oe?>eiY`;u>0Yfw35gV+;lwhvajNi5exWa zzr}5N6G6O_Y1-@4ZG`F;lUIPvC59;!m&5$8mfEsb%?+`UebzcNWt=;%+rx(>Jue7c zH$Ft}FAh|sjwV*WkB&D{Fj6a|OS)lG-$Qiq_uMU1H}NnIq+L05`d*Tum+a^5}noOj;GyR|T!k!@Q9zn2Ra9<|hI?5raL0yHE&azcOs ze!e=LHQ5j(_|~(nXUt(rtG`0@7K^_p`!y#IV9$wpd3ZzD1xmNM=2$xl%+B;0&`|1B*@Pxaua!BhHC;7Ds!3B z#4XTb&j}JI;fZ#G?ZEL%K=ANY@k48(&W^+-I}opIUwpBa&6Znui>QRqE(^+!#WnZD z8{g2YvR@M){LHIhDK8u!B3M+%VK9j}|6A#NrIuM$3^i{m12Wt9`{7j__XlC<`%w#f zE7+9PwgiTtURCa9kUCYJwida_Gd&(hx06Apch#)4KQSs$#$<*!9Kl04=ul*_MsykG FvR?$+VwwN| diff --git a/src/__pycache__/Tractor.cpython-310.pyc b/src/__pycache__/Tractor.cpython-310.pyc index 3b365b4e383c464da79f0c1effef20b23eea1819..bb7eed465d962b7c6b22b1b6c55a51e4761a08bf 100644 GIT binary patch literal 3312 zcma)8TW=dh6rS0e*Vi~snp?TFDpayS4fnQCN>Oj1R#s^P5*BFXdN$5F_S(#@39-ym z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070..c8e5bdd882509a90da756ffc6a28a89903d8d4e7 100644 GIT binary patch delta 178 zcmbQkJ%O7$pO=@50SH>QDa2k|D1KQmP0d^g#s33M4{das{gj03B~J0RR91 delta 209 zcmbQhJ%^h+pO=@50SLkrVf#wuR1Bockywvh|uwt;`5JGnHY8FdI&B;$$ YGUSy(O65R=CWrvJ0Ev*DoWrUD06@SxlmGw# diff --git a/src/bfs.py b/src/bfs.py new file mode 100644 index 0000000..f47144e --- /dev/null +++ b/src/bfs.py @@ -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