mardi 13 août 2019

How can I make a number randomly appear a certain percent of the time - repeatedly?

I am making a game where I want fluctuating damage. There are two enemies. For enemy one, they lose a constant amount of health (5 points). For enemy two, there is a chance that the player will inflict a small amount of damage 80% of the time (3 points) and a large amount of damage 20% of the time (10 points).

I tried using choice from a group of numbers, but I found that it will choose one of these random numbers (3 for example) and will stick with it until the game restarts. I want the damage number to change each time the player attacks so that it looks more like "-3, -3, -3, -10, -3" for example.

In the main loop I have:

 '''hits = pg.sprite.groupcollide(self.enemy1s, self.shards, False, True)
    for hit in hits:
        hit.health -= SHARD_DAMAGE1
        hit.vel = vec(0, 0)
    hits = pg.sprite.groupcollide(self.enemy2s, self.shards, False, True)
    for hit in hits:
        hit.health -= SHARD_DAMAGE1
        hit.vel = vec(0, 0)'''

The SHARD_DAMAGE is defined separately in another file as:

 '''CONSTANT = [5, 5, 5, 5, 5]
    RAND = [3, 3, 3, 3, 10]
    SHARD_DAMAGE1 = choice(CONSTANT)
    SHARD_DAMAGE2 = choice(RAND)
    at the very top of both files I have: from random import uniform, choice'''

Like I said, the game will lock onto 3 or 10 and make that the damage until the entire thing is restarted. Even if I were to have 5 random numbers, it only picks one until I restart.




Aucun commentaire:

Enregistrer un commentaire