I'm a beginner, learning python, and decided to start by making a very simple text based game. I'm not to the game logic yet, I'm just starting with items, weapons, and enemies. I'm trying to make it as logical as possible e.i.:
- The player has armor, the Armor is a subclass of Items
- The player can have a Weapon, the Weapons are a separate class
- The enemies have weapons
Here's my code for the Weapons:
class Weapon:
def __init__(self, name, acc, rang, damg, crit, value):
self.name = name
self.acc = acc
self.rang = rang
self.damg = damg
self.crit = crit
self.value = value
def __str__(self):
return "{}\nAccuracy: {}\nRange: {}\nDamage: {}\nCrit: {}\nValue: {}\n".format(self.name, self.acc, self.rang, self.damg, self.crit, self.value)
weaponsList = []
with open(r'weaponsList.csv', "r") as w:
wep_reader = csv.reader(w)
for row in wep_reader:
weaponsList.append(Weapon(row[0], row[1], row[2], row[3], row[4], row[5]))
The CSV data for Weapons:
Revolver,70,35,50,50,10
Rifle,75,75,40,20,25
Shotgun,90,15,70,80,20
Fists,100,2,45,75,0
And my code for Enemies:
class Enemy:
def __init__(self, name, hp, weapon):
self.name = name
self.hp = hp
self.weapon = weapon
def __str__(self):
return "{}\nHP: {}\nMain Weapon: {}\n".format(self.name, self.hp, self.weapon)
enemiesList = []
with open(r'enemiesList.csv', "r") as e:
enemy_reader = csv.reader(e)
for row in enemy_reader:
enemiesList.append(Enemy(row[0], row[1], row[2]))
Here's the CSV of Enemies
Cranky Miner,50,weapons.weaponsList[3]
Gun Slinger,100,weapons.weaponsList[0]
Hunter,100,weapons.weaponsList[2]
Soldier,100,weapons.weaponsList[1]
I started out by making the objects in code using the classes, then realized it would be cleaner to put the attributes of the Armor, Weapons, Enemies, and Items in a CSV file. I'd then read the CSV to put the data into a list, then add the list to the class. I pretty much copied over what was weapons.revolver
with weapons.weaponsList[0]
thinking it would work, just maybe not look as "English". To test, I just use print() to show the data I want. With the old way, if I wanted to print something like an enemy or the name of an enemies weapon, it would work great:
slinger = Enemy('Gun Slinger', 100, weapons.revolver)
print(enemies.slinger)
Output:
Gun Slinger
HP: 100
Main Weapon: Revolver
And
print(enemies.slinger.weapon.name)
Output:
Revolver
One problem I've run into with the new way using a CSV file is when I call to print() the same enemy
print(enemies.enemiesList[1])
I get this:
Gun Slinger
HP: 100
Main Weapon: weapons.weaponsList[0]
And I can't call the attribute .name
because it does't exist. But I can still print() a weapon:
print(weapons.weaponsList[0])
And get
Revolver
Accuracy: 70
Range: 35
Damage: 50
Crit: 50
Value: 10
My problem appears to be when I put an object from a list in a CSV, it loses it's ability to be seen as an object in a list? Like I said in the beginning, I'm learning.
As a second question, is there a way to use the CSV data to make objects in a way that looks/reads as nice as if I were to make them in code? (enemies.enemiesList[1]
vs enemies.slinger
)
I've uploaded both versions of my code (old and new) to GitHub if anyone wants to take a more in depth look, since I'm sure I didn't make complete sense. (p.s. Sorry for the long and probably messy post, first time asking on here)
Old version: https://github.com/Aturbo97/WesternGamePoC
New Version: https://github.com/Aturbo97/WesternGamePoCv2