Python Text-Based Rpg Enemy Attack System Help!

+1 Vincent Gizzarelli · September 17, 2015
Alright so I'm working on a text-based space-themed rpg in Python where you control your own fleet of different variations of ships and you have to go out to conquer the universe. I need to create a system where the enemy will be able to attack the player but the way I have player health set up starts to make that pretty complex.

First for every ship in your fleet it has it's own unique ship number and individual health. 

code example:

for ship in range(ExploreIG.Bships):
            global Bshipnum
            Bshipnum = ExploreIG.Bshipnum
            ExploreIG.Bshipnum += 1

What I need is a system where the enemy will have a random range of attack like 50-100 for example and say the enemy will
end up dealing 70 damage. I need that distributed to a random amount of ships. So split up the 70 to a random amount of ships.
so in a fleet of 100 ships it might affect 10 ships and deal 5 damage to one of them, 7 damage to another, etc. (Dividing the damage among the ships [NOT EVENLY, BUT RANDOMLY]) 

So basically to sum up what I need is something that get's random keys from dictionaries and sets the values of those keys subtracted by a certain amount.  

Please any help would be greatly appreciated.
Also I feel like I might have sounded pretty confusion so if you need to me to clear up any confusion please do 
say so. Thanks in advance!

Post a Reply


Oldest  Newest  Rating
+1 Vincent Gizzarelli · September 21, 2015
Well i finally figured it out! I created a system where for each ship it takes a random amount of damage from the whole amount dealt by the enemy using this algorithm:

            EAttack = random.randrange(1, ExploreIG.planetstr)
            EBShipRange = random.randint(int(ExploreIG.Bships / 3), int(ExploreIG.Bships / 2))
            EAttackBship = random.sample(ExploreIG.Bshiph, EBShipRange)
            EAttackBship1 = {}
            self.BShip = list(ExploreIG.Bshiph.items())
            for Bships in EAttackBship:
                key, value = random.choice(self.BShip)
            self.EAttackDO = random.randint(int(ExploreIG.planetstr * 0.7), ExploreIG.planetstr)
            for (key, value) in EAttackBship1.items():
                EAttackD = random.randint(int(self.EAttackDO / 500), int(self.EAttackDO / 70))
                EAttackBship1[key] -= EAttackD
                ExploreIG.Bshiph[key] -= EAttackD
                if EAttackBship1[key] >= 50 and EAttackBship1[key] < 100:
                    self.damagedbship += 1
                elif EAttackBship1[key] < 50 and EAttackBship1[key] > 0:
                    self.cdamagedbship += 1
                elif EAttackBship1[key] <= 0:
                    self.destroyedbship += 1
                    ExploreIG.Bships -= 1
                    del ExploreIG.Bshiph[key]

* planetstr is the planets base damage which I have set to 7000

If you have any questions about this code feel free to ask
0 sfolje 0 · September 20, 2015
Yeah, that is exactly what I had in mind.
And i cant think of better one.
I also had another idea of separating number into partitions, but realized it's useless.
for example : We can write number 5 on 7 different ways as a sum :
5= 5 ,
5= 4+1,
5= 3+2,
5= 3+1+1,
5= 2+2+1,
5= 2+1+1+1,
5= 1+1+1+1+1.
If we pick sums that has 3 summands (3+1+1 and 2+2+1), we succsessfully distributed 5 damage to 3 ships. We could then chose random partition in order to randomly distribute damage to ships.
But I figured out it is not working out good, because most partitions have one big number and alot of small ones.
partition of 70 as sum of 10 numbers, would in allmost all cases be like 38+21+4+1+1+1+1+1+1+1, which is pretty useless.
0 James Kon · September 18, 2015
I'm stuck on this one.

If it was divided evenly, it would've been a lot easier. Maybe if once divided evenly you made a formula that would make it seem random for each number?
0 sfolje 0 · September 18, 2015
Ignoring code example, not fully understanding the goal and focusing on frequentlly used word random, I suggest a look at random module at python's documentation. (Btw I recommend reading as little as you possible, skip to examples, try them yourself.)
Module random can chose between numbers, list’s elements, not sure what you really need.
Thats how I understand you:
Wanna make blackbox function like this:
Blackbox( 70 damage, 10 ships)

Result: [10, 7, 4, 7, 6, 9, 5, 4, 6, 10]

Am i wrong? Can you make one example more? Feel free if help needed
0 Vincent Gizzarelli · September 19, 2015
Alright so I figured out some of the problems stated previously. The only thing I'm having a problem with is random damage distribution. There is one thing I found though, and I was thinking of doing something like this.

EAttackDO = planet.strenth
for ship in BShipRange:
    EAttack = random.randrange(int(EAttackDO / 10), int(EAttackDO / 3))
   BShiph -= EAttack  ('Here is where the ship would take damage.)
    EAttackDO -= Ettack

That is the system I was thinking of having for random damage distribution
If you have any other questions or suggestions please do tell me. 
  • 1



This section is all about snakes! Just kidding.

Bucky Roberts Administrator