From bc433f23a93471ed6d5f92bd2ee66c0b60773979 Mon Sep 17 00:00:00 2001 From: micwuj Date: Sat, 22 Apr 2023 21:31:19 +0200 Subject: [PATCH 1/5] 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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2fe95070..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 -- 2.20.1 From 560cceafc929766ac72cc23f608978ba4417c751 Mon Sep 17 00:00:00 2001 From: dardwo Date: Sat, 22 Apr 2023 21:43:28 +0200 Subject: [PATCH 2/5] added successor --- src/__pycache__/plant.cpython-310.pyc | Bin 1315 -> 1343 bytes src/__pycache__/tractor.cpython-310.pyc | Bin 2085 -> 3312 bytes src/bfs.py | 18 +++++++++++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/__pycache__/plant.cpython-310.pyc b/src/__pycache__/plant.cpython-310.pyc index 21fa799ffc9851f2178fd5d8b8665b9097dbc304..9fa6092586ae0180ab460f98df79ad1589cc3086 100644 GIT binary patch delta 528 zcmYjNO^XvT7|zGc&`vwDA7znM7u3BJ7V0XyAe23c9)yA@9*PiVC+Rdb)08C3Zc9Ds z(My>>;NoBKtOx%^PF{-#kK)ZYJ#FB9@;>j!lQ-|z^hcWMEK3obpHH4~xytT6KLWxL z#|jO@O%$%kkYMxzamu4lh(`iV=p_0`hcU;mQ8!+P{y`V}^jzuUJ+pElR&U>=F@!0Y zJy?DFH*i~SYecTe60!CYP1;|!uMzk=OS+6oh)#CcE=QoZKk!MB8>8KtL-+b|fP(;i z03SJRM!x=wpWgDVnau5?Sj?>xzCI(TO`k}WZ`cQN()P_#$tl!?^mlTIJM|y(;H7Vs zMqj!ybFvaXxfHS-JD*i@%EohUawqi^Xnu8rHm#P5x%o`^bR>+E3VvyP{f<704S~Vx z&-DKG9dOLuFlIc{mR-TCbgy;P8|zB+F03&1Z~8Fahl04?jRttHK9BmZ0{0nX1&nSB zp%i20I4EtP7rd|p6P6i)*#HgH!J9}oF^9l?!wTo*w6uoQzoO1@sJ&vk5>0wefB8A9(7Jv;Y7A delta 508 zcmYjN%Ss$U6s?yNr=1QuJ`lyk7lDC**(4Ycw^;~oOg4p3G*jhFO?tYUstRKd16d?L zFx1kG#+6^;ALIiXf54?1!K`wtlZ6F!?mhR`Ik&2Aqw6Tn<2XXFeywcE{b{`Mb`yw1 z5~qlxPe>9;eK{=K91a;Vb&>T;T&k=)!!nSME-%oGk+7+1~(VCz^>vH}I zLB24d6V`+3U_rDc0`$24*l~AUTBo=_B_Eo+q0^*h7vz15 zH+!kh;7n-Wk=>Zw@4`?s571fkm~J2Oxt_^Kt}mP(s;V(ky4QC+9_mc=OKB3P^9*Qm z{){$lu2bR4Lhu@p5SOVZPMQ+9Uzfj)bswqSl?q`L#L4Wr5 zzF1}Ko=l8RaI#tAo<&Qzu5_4L0bVt1VYGv1p474KJ+rC3f=xAhFwEt*R2^bqmZ3&4TP1%1*N=uU diff --git a/src/__pycache__/tractor.cpython-310.pyc b/src/__pycache__/tractor.cpython-310.pyc index 7bb4d92f3c7514461389cb95abab89e4cdd660b1..bb7eed465d962b7c6b22b1b6c55a51e4761a08bf 100644 GIT binary patch literal 3312 zcma)8TW=dh6rS0e*Vi~snp?TFDpayS4fnQCN>Oj1R#s^P5*BFXdN$5F_S(#@39-ym zBlT}M5<<#5zknaX!#pAJl;^%7r0|_tZ{jqGU{`bI?3pt&=YF;e!Rp3yw$23?#*SMzlRN5`u!Xb3>bA%T z7qcU>B8NF6@}hv*6-80PoE0;oj5#M}MFn$S%!zr-1#wO+U@nS9v4pwwoLQ_s18J94 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eMTro35O+o$w^ zn^rLC2Bh-+P9u!Hmh27sNdXF=W+E^X3`?R!dgUCyntRq zUC8=pICJQ;q>Y}?E_K!miW;pEi{Y|oX{#`5!{QcNxAesYEh{7;A(Rahi{hI6SnQygcCByV9`)u^fLQA__Eq|@${cd_t2E#3h*sv?Iw|MQk% zHuSd=73EpCKxuMUvMk%Czn7fEZchN1nIoHOU!|S@1Yn3g{SIs0* v@8SnF#$Q2{G&_^dxdEV@CpsN8^VDrR1?V>A-bl2($?+9YMRZx2FB$&=-SWr_ literal 2085 zcmahKO>Y}TbY^#Uz5a@wv<+?PAqT!zKnzp@R3U^!p+G8wXeuOMM$69F+GM?JX4ZsQ z=9EbN4;)Z@LCT%q!4H@d5~rNGaUi@m>rI`u5o7I}_ujmDZ{DZ#cDq4f{d!|d{8=O9 zFCfoO|0*y~RSBZCH5?3miF zisB+yvA1FoaeO)&*1rHK$PqcA3qncZ?71gQ&;^|n?gHhV_;a%73TNS5aB7z0T25eP zN)EG}`U=OhoW=^rx11(wIB6{i=xoo?*4iBPcjhj@?wkQ!7X)0W^=G#ut!1HmMd?bl zZ%?vO=$yR*?^o!Fq5#f*M=$`8{C0kR9&Q*%%loE!?a9?>N>vo27*llF9A5Erp|esg1elb zl7H^?9&4#|Pedw-#6hlRhne1wdiSN?FY-xmcdEY6A-_PVo>p-WaLo1gud=M!f^ex`E@xO8~y-L$$9Z>>l-m%5u) zcN?VRr8Mf6fUjMawz`{GQq>o-zAYJBJjpS58g^6wZW?!A9-1a5c%mfKo-qCn$KE9?W_0D1K$ zf@k90wBp?Q_7-Z!Y3S1yon5OQvm$ZjgVft_Ep+;({2i@4 Date: Sat, 22 Apr 2023 21:47:00 +0200 Subject: [PATCH 3/5] change successor & added map validation --- src/bfs.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/bfs.py b/src/bfs.py index 629f19c..fd02b29 100644 --- a/src/bfs.py +++ b/src/bfs.py @@ -29,7 +29,17 @@ class BFS: states = [(pos_x - block_size, pos_y, directions[270]), (pos_x, pos_y, directions[0]), (pos_x, pos_y, directions[180])] actions = ['F', 'L', 'R'] - return states, actions + for s, a in zip(states, actions): + if self.valid_state(s): + options.append((a, s)) + + return options + + def valid_state(self, state): + pos_x, pos_y, rotation = state + if pos_x < 0 or pos_x >= screen_width or pos_y < 0 or pos_y >= screen_width: + return False + return True def search(self): pass -- 2.20.1 From eb408cd25de21df9c5b9b4cd5ff713c4c972f54c Mon Sep 17 00:00:00 2001 From: micwuj Date: Sat, 22 Apr 2023 21:51:55 +0200 Subject: [PATCH 4/5] added search function --- src/bfs.py | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/src/bfs.py b/src/bfs.py index fd02b29..fc396e2 100644 --- a/src/bfs.py +++ b/src/bfs.py @@ -1,4 +1,5 @@ from settings import block_size, screen_width, directions +import copy class Node: @@ -41,5 +42,45 @@ class BFS: return False return True - def search(self): - pass + def tempfunc(self, elem): + path = [] + + while elem.parent: + path.append([elem.action, elem.state[0], elem.state[1]]) + elem = elem.parent + + path = path[::-1] + return path + + def search(self, istate, goaltest): + node = Node([istate[0], istate[1], istate[2]]) + fringe = [] + fringe.append(node) + fringe_states = [fringe[0].state] + + #fringe_state = [fringe[0].state] + #visited = [] + explored_states = [] + + while True: + if not fringe: + return False + + elem = fringe.pop(0) + temp = copy.copy(elem) + fringe_states.pop(0) + + # DESTINATION + if elem.state[0] == goaltest[0] and elem.state[1] == goaltest[1]: + return self.tempfunc(elem) + + explored_states.append(elem.state) #elem.state(?) + + for (action, state) in self.successor(temp): + print + if (state not in fringe_states) and (state not in explored_states): + x = Node([state[0][0], state[1], state[2]]) + x.parent = elem + x.action = action + fringe.append(x) + fringe_states.append(x.state) -- 2.20.1 From 29c4878825aed7bee8ba8cd4eacb272bf80c6659 Mon Sep 17 00:00:00 2001 From: dardwo Date: Sat, 22 Apr 2023 21:54:08 +0200 Subject: [PATCH 5/5] changed bfs $ function goal_path --- src/bfs.py | 41 +++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/src/bfs.py b/src/bfs.py index fc396e2..e5bacef 100644 --- a/src/bfs.py +++ b/src/bfs.py @@ -42,7 +42,7 @@ class BFS: return False return True - def tempfunc(self, elem): + def goal_path(self, elem): path = [] while elem.parent: @@ -53,34 +53,23 @@ class BFS: return path def search(self, istate, goaltest): - node = Node([istate[0], istate[1], istate[2]]) - fringe = [] - fringe.append(node) - fringe_states = [fringe[0].state] - - #fringe_state = [fringe[0].state] - #visited = [] - explored_states = [] - + x, y, rotation = istate + start_node = Node((x, y, rotation)) + + self.fringe.append(start_node) + while True: - if not fringe: + if len(self.fringe) == 0: return False - elem = fringe.pop(0) - temp = copy.copy(elem) - fringe_states.pop(0) - - # DESTINATION + elem = self.fringe.pop(0) + if elem.state[0] == goaltest[0] and elem.state[1] == goaltest[1]: - return self.tempfunc(elem) + return self.goal_path(elem) - explored_states.append(elem.state) #elem.state(?) + self.explored.append(elem.state) - for (action, state) in self.successor(temp): - print - if (state not in fringe_states) and (state not in explored_states): - x = Node([state[0][0], state[1], state[2]]) - x.parent = elem - x.action = action - fringe.append(x) - fringe_states.append(x.state) + for (action, state) in self.successor(elem.state): + if state not in self.explored: + x = Node(state, elem, action) + self.fringe.append(x) \ No newline at end of file -- 2.20.1