From 1acb7d42d7e5e41077bb946ecc91beb4b554e602 Mon Sep 17 00:00:00 2001 From: tubks Date: Tue, 30 Mar 2021 14:17:54 +0200 Subject: [PATCH] working model with agent collecting gold, having some stats, weapons and armors, fighting with enemies + a random enemy picture --- agent2.py | 96 ++++++++++++++++++++++++++----------------- armory.py | 52 +++++++++++------------ model.py | 21 ++++++++-- server.py | 2 + sprites/creature.png | Bin 0 -> 3877 bytes 5 files changed, 105 insertions(+), 66 deletions(-) create mode 100644 sprites/creature.png diff --git a/agent2.py b/agent2.py index f35472f..cb92acf 100644 --- a/agent2.py +++ b/agent2.py @@ -1,4 +1,4 @@ -from mesa import Agent, Model +from mesa import Agent import random def dice(number): @@ -8,16 +8,23 @@ class Wall(Agent): def __init__(self, unique_id, model): super().__init__(unique_id, model) -class Chest(Agent): - def __init__(self, unique_id, model, loot): + def step(self): + pass + +class Box(Agent): + def __init__(self, unique_id, model): super().__init__(unique_id, model) - self.gold = 3*dice(6) - self.loot = loot + self.gold=3*dice(6) + self.isBox=True + self.isCreature=False + + def step(self): + pass class Weapon(): - def __init__(self,name,typ,damage): + def __init__(self,name,type,damage): self.name = name - self.typ = typ + self.type = type self.damage = damage class Armor(): @@ -38,6 +45,9 @@ class Creature(Agent): self.gold = g self.weapon1 = weap self.armor = arm + self.isBox = False + self.isCreature=True + def meleeAttack(self,opponent): attackValue = self.strength + dice(6) @@ -58,19 +68,18 @@ class Creature(Agent): defenseValue = opponent.wisdom damage = attackValue - defenseValue if (damage > 0) and (damage + self.weapon1.damage - opponent.armor.mag_protection > 0): - opponent.health = opponent.health - (damage + self.weapon1.damage - opponent.armor.mag_protection) - + opponent.health = opponent.health - (damage + self.weapon1.damage - opponent.armor.mag_protection) def defaultAttack(self,opponent): - if self.weapon1.typ == "Meele": + if self.weapon1.type == "Meele": self.meleeAttack(opponent) - elif self.weapon1.typ == "Range": + elif self.weapon1.type == "Range": self.rangeAttack(opponent) else: self.magicAttack(opponent) class Player(Creature): def __init__(self, unique_id, model, n, s, a, w, maxhp, hp, weap, arm, g, w2, w3): - super().__init__(self, unique_id, model, n, s, a, w, maxhp, hp, weap, arm, g) + super().__init__(unique_id, model, n, s, a, w, maxhp, hp, weap, arm, g) self.name = n self.strength = s self.agility = a @@ -82,6 +91,8 @@ class Player(Creature): self.weapon2 = w2 self.weapon3 = w3 self.armor = arm + self.isBox = False + self.isCreature = False def move(self): possible_steps = self.model.grid.get_neighborhood( @@ -116,83 +127,94 @@ class Player(Creature): combat = True while combat: choice = dice(4) + print("dice rolled:", choice) if choice == 1: running_speed = self.agility + dice(6) opponent_speed = opponent.agility + dice(6) if running_speed > opponent_speed: combat = False + print("Player ran away") self.step() else: opponent.defaultAttack(self) if self.health <= 0: combat = False + print("Player died :/") elif choice == 2: self.meleeAttack(opponent) if opponent.health > 0: opponent.defaultAttack(self) if self.health <= 0: combat = False + print("Player died :/") else: combat = False self.gold = self.gold + opponent.gold opponent.gold = 0 opponent.model.grid.remove_agent(opponent) + print("Fight won") elif choice == 3: self.rangeAttack(opponent) if opponent.health > 0: opponent.defaultAttack(self) if self.health <= 0: combat = False + print("Player died :/") else: combat = False self.gold = self.gold + opponent.gold opponent.gold = 0 opponent.model.grid.remove_agent(opponent) + print("Fight won") else: - self.magicAttackAttack(opponent) + self.magicAttack(opponent) if opponent.health > 0: opponent.defaultAttack(self) if self.health <= 0: combat = False + print("Player died :/") else: combat = False self.gold = self.gold + opponent.gold opponent.gold = 0 opponent.model.grid.remove_agent(opponent) + print("Fight won") def openChest(self,chest): self.gold = self.gold + chest.gold + print("Chest opened. Gold inside:", chest.gold) chest.gold = 0 - if isinstance(chest.loot,Armor): - buffer = self.armor - self.armor = chest.loot - chest.loot = buffer - if isinstance(chest.loot,Weapon): - if chest.loot.typ == "Melee": - buffer = self.weapon1 - self.weapon1 = chest.loot - chest.loot = buffer - elif chest.loot.typ == "Range": - buffer = self.weapon2 - self.weapon2 = chest.loot - chest.loot = buffer - elif chest.loot.typ == "Magic": - buffer = self.weapon3 - self.weapon3 = chest.loot - chest.loot = buffer + # if isinstance(chest.loot,Armor): + # buffer = self.armor + # self.armor = chest.loot + # chest.loot = buffer + # if isinstance(chest.loot,Weapon): + # if chest.loot.type == "Melee": + # buffer = self.weapon1 + # self.weapon1 = chest.loot + # chest.loot = buffer + # elif chest.loot.type == "Range": + # buffer = self.weapon2 + # self.weapon2 = chest.loot + # chest.loot = buffer + # elif chest.loot.type == "Magic": + # buffer = self.weapon3 + # self.weapon3 = chest.loot + # chest.loot = buffer def step(self): if self.health > 0: self.move() cellmates = self.model.grid.get_cell_list_contents([self.pos]) if len(cellmates) > 1: - if isinstance(cellmates[1],Chest): - openChest(cellmates[1]) + if isinstance(cellmates[0],Box): + self.openChest(cellmates[0]) else: - opponent = cellmates[1] - fightOrFlight(opponent) - print("HP: "+self.health+" / "+self.maxHealth) - print("Gold: "+self.gold) + opponent = cellmates[0] + print("Fighting") + self.fightOrFlight(opponent) + print("HP: "+str(self.health)+" / "+str(self.maxHealth)) + print("Gold: "+str(self.gold)) else: - print("HP: 0 / "+self.maxHealth) + print("HP: 0 / "+str(self.maxHealth)) \ No newline at end of file diff --git a/armory.py b/armory.py index fc2e31f..1a6db27 100644 --- a/armory.py +++ b/armory.py @@ -1,6 +1,6 @@ from mesa import Agent, Model import random -import agent2 +from agent2 import Weapon, Armor, Box, Creature, Player, dice WM1 = Weapon("Log","Melee",1) WM2 = Weapon("Log","Melee",1) @@ -50,29 +50,29 @@ A14 = Armor("Robe",1,2) A15 = Armor("Robe",1,2) A16 = Armor("Magical Plate Armor",3,2) -C1 = Chest(WM6) -C2 = Chest(WR3) -C3 = Chest(WR6) -C4 = Chest(WR7) -C5 = Chest(WR9) -C6 = Chest(WM9) -C7 = Chest(WM10) -C8 = Chest(S4) -C9 = Chest(S5) -C10 = Chest(A8) -C11 = Chest(A12) -C12 = Chest(A14) +# C1 = Box(WM6) +# C2 = Box(WR3) +# C3 = Box(WR6) +# C4 = Box(WR7) +# C5 = Box(WR9) +# C6 = Box(WM9) +# C7 = Box(WM10) +# C8 = Box(S4) +# C9 = Box(S5) +# C10 = Box(A8) +# C11 = Box(A12) +# C12 = Box(A14) -Gracz = Player("Janusz",3,3,3,20,20,WM1,A1,0,WR1,S1) - -M1 = Creature("Goblin",2,2,1,10,10,WM2,A2,dice(6)) -M2 = Creature("Goblin",2,2,1,10,10,WM3,A3,dice(6)) -M3 = Creature("Goblin",2,2,1,10,10,WR2,A4,dice(6)) -M4 = Creature("Skeleton Archer",2,3,4,15,15,WR4,A5,dice(6)) -M5 = Creature("Skeleton Archer",2,3,4,15,15,WR5,A6,dice(6)) -M6 = Creature("Skeleton",3,2,4,15,15,WM7,A7,dice(6)) -M7 = Creature("Skeleton",3,2,4,15,15,WM8,A7,dice(6)) -M8 = Creature("Bandit",3,3,2,15,15,WM4,A9,2*dice(6)) -M9 = Creature("Bandit",3,3,2,15,15,WM5,A10,2*dice(6)) -M10 = Creature("Chieftain",4,4,4,20,20,WR8,A11,3*dice(6)) -M11 = Creature("Sorcerer",3,3,6,25,25,S3,A15,3*dice(6)) +#Gracz = Player(1000, self, "Janusz",3,3,3,20,20,WM1,A1,0,WR1,S1) +# def __init__(self, unique_id, model, n, s, a, w, maxhp, hp, weap, arm, g): +# M1 = Creature("Goblin",2,2,1,10,10,WM2,A2,dice(6)) +# M2 = Creature("Goblin",2,2,1,10,10,WM3,A3,dice(6)) +# M3 = Creature("Goblin",2,2,1,10,10,WR2,A4,dice(6)) +# M4 = Creature("Skeleton Archer",2,3,4,15,15,WR4,A5,dice(6)) +# M5 = Creature("Skeleton Archer",2,3,4,15,15,WR5,A6,dice(6)) +# M6 = Creature("Skeleton",3,2,4,15,15,WM7,A7,dice(6)) +# M7 = Creature("Skeleton",3,2,4,15,15,WM8,A7,dice(6)) +# M8 = Creature("Bandit",3,3,2,15,15,WM4,A9,2*dice(6)) +# M9 = Creature("Bandit",3,3,2,15,15,WM5,A10,2*dice(6)) +# M10 = Creature("Chieftain",4,4,4,20,20,WR8,A11,3*dice(6)) +# M11 = Creature("Sorcerer",3,3,6,25,25,S3,A15,3*dice(6)) diff --git a/model.py b/model.py index 303f156..f4d2903 100644 --- a/model.py +++ b/model.py @@ -1,5 +1,6 @@ from mesa import Model, Agent -from agent import Player, Box +from agent2 import Player, Creature, Box, Wall, dice +from armory import WM1, A1, WR1, S1, WM2, A2 from mesa.time import RandomActivation from mesa.space import MultiGrid #from mesa.datacollection import DataCollector @@ -7,9 +8,9 @@ import random x = 10 y = 10 -possible_contents = ['gold', 'sword', 'shield', 'potion', 'empty'] step_counter = 0 boxes_number = 5 +creatures_number = 5 class GameMap(Model): @@ -18,12 +19,15 @@ class GameMap(Model): self.schedule = RandomActivation(self) # agenci losowo po kolei wykonują swoje akcje # to jest potrzebne przy założeniu, że potwory chodzą? self.boxes_number = boxes_number + self.creatures_number = creatures_number self.running=True - player = Player(1000, self) + #player = Player(1000, self) + player = Player(1000, self, "Janusz", 3, 3, 3, 20, 20, WM1,A1,0,WR1,S1) self.schedule.add(player) x = self.random.randrange(self.grid.width) y = self.random.randrange(self.grid.height) self.grid.place_agent(player, (x, y)) + for i in range(self.boxes_number): box = Box(i, self) self.schedule.add(box) @@ -34,6 +38,17 @@ class GameMap(Model): except: pass + for i in range(self.boxes_number, self.boxes_number+self.creatures_number): #taki range, żeby każdy agent miał poprawne unique_id + #creature = Creature(i, self) + creature = Creature(i, self, "Goblin", 0, 0, 0, 1, 1, WM2,A2, dice(6)) + self.schedule.add(creature) + x = self.random.randrange(self.grid.width) + y = self.random.randrange(self.grid.height) + try: + self.grid.place_agent(creature, (x, y)) + except: + pass + # self.datacollector=DataCollector #informacje o stanie planszy, pozycja agenta diff --git a/server.py b/server.py index 814ec14..dc07617 100644 --- a/server.py +++ b/server.py @@ -9,6 +9,8 @@ def player_representation(agent): if agent.isBox: portrayal["Shape"] = "sprites/box.png" portrayal["Layer"] = 0 + elif agent.isCreature: + portrayal["Shape"]='sprites/creature.png' return portrayal grid = CanvasGrid(player_representation, 10, 10, 500, 500) diff --git a/sprites/creature.png b/sprites/creature.png new file mode 100644 index 0000000000000000000000000000000000000000..8dcf4ac88a5e0a81bd4ad11114f6906539fd7b68 GIT binary patch literal 3877 zcmV+=58CjFP)rT6#u%F4?7`}^VH;i96V{QUgh-rnx+?o3Qf)6>(px3{gWt?20JmzS4va&oY+ zuxo2;^78UIIXQ@kh=GBDo12?KK|#R4zz`4+Q&UrUd3i4{FGoj5US3`v9v&AL7gUG#n?mbi&^s?ij zl)4+HFY<dX)YIpwDXuLeFIq#(FKWy@HY}P5KqxSg1-^Z*1C-c>Fe#K9cu!F!R|=kk5;dApDK`-AT=)L1X7bcqMZpa3S{lQYHv+*=TX-9~blSvbh}37svkL+w|;fsnC%I*Km8feXy_r zTqfA#tLDj$EJ8mgu0}>@@+r0Ts%vV1e}A09Ke;+FgEvQ%t<`3FlJ{_^VnjW3){?p2 zS0zXrji!D2037_z>h5)MJD;V+v zZRKoK36FKf;k^2A^b%>=iwxGH6MX)E2mWVNFDk_MXrD78Q3*$!@ph_8+^t#B4xmFH z(?9&IV3>;us}GbeKmSby2{69Rlt|!$<@y+rislAMO90RL`Xr&Ppm1@ad~~GXg-CNGB@Coc~gvg940&1hJM(S z^q0qYOqhePq8w2}TXM^8mduo-FmTbh#nSmeY0{7;5xpq~*QQy}ENaB9`gIin$Q~@LcOc{fSJf4Gp1(9JyqF@*yl)bG3P8P91NXTa9*6f>x z#AH{*5lV{R=-|R}E8eO`oPs zF(_3@E%%BFD?+-xdZ5=Px#3LDSH`G-#PB|ajIMql)!C(|jX!Y{JwBi>k#kb@A`)E& zI+jIBk!i-Ux2|L|qN%p*rCl!6VQhTKyY@%Va;fSdR+3}bLWSSzm4jQ~5jU;eBMMi720svHUp3@nCm zzQOALxL4tM0`{n{iUASe-QP!7TmbsQwPe-|2u&Jh5ay1r9U&okRh*&guF9j8fXHf^%f8Ryg+Pn@`Zy~(5$*u9Cj3KSZau9Y_ zuS0YuL`n!mX@Pq$)rE6s(e8DqMGhfMAzx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