working model with agent collecting gold, having some stats, weapons and armors, fighting with enemies + a random enemy picture

This commit is contained in:
tubks 2021-03-30 14:17:54 +02:00
parent 999d85d235
commit 1acb7d42d7
5 changed files with 105 additions and 66 deletions

View File

@ -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)
@ -59,18 +69,17 @@ class Creature(Agent):
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)
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))

View File

@ -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))

View File

@ -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

View File

@ -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)

BIN
sprites/creature.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB