vendredi 15 mai 2020

Python - Using same randint value in different functions within class object

I'm quite new in programming and as an exercise, I wanted to try writing a very basic game.

I will not choke you in details. Let me explain where I'm having trouble. There are 2 players shooting each other. Each turn they spend random amount of bullets between (1,10) and damage the other, and whoever reaches 0 health loses the game.

Right now, I can call random bullet number to shoot the other player. But I want the damage to be related with the amount of bullet used. For example if player1 used 5 bullets that turn, player2 will be damaged 5*10, or if 2 then damaged 2*10 and etc.

And here is my code. I appreciate any kind of help. Thanks!

import random

bullet_spent = random.randrange(1,10)
damage = bullet_spent * 10

# Enemy object
class Enemy():

    def __init__(self,name,hp,ammo):
        self.name = name
        self.hp = hp
        self.ammo = ammo

# Defined actions    
    def attack(self):
        bullet_spent = random.randrange(1,10)
        print(self.name + " Shoots. " + str(bullet_spent) + " bullets spent.")
        self.ammo -= bullet_spent  

    def be_damaged(self):
        damage = bullet_spent * 10
        self.hp -= damage
        print(self.name + " lost " + str(damage) + " HP.")
        print("Remaining HP: " + str(self.hp))

# Enemy list
player1 = Enemy("Player1",100,50)
player2 = Enemy("Player2",100,50)



Aucun commentaire:

Enregistrer un commentaire