0

I'm creating a game (very simple, just to practice what I'm learning) and I have a problem with objects referencing other object

In my game, I have two classes, player and enemy. It is supposed that when you enter a room, a random enemy MAY appear. (The may is important, it means the obj of class "enemy" doesn't exist unless you run the program and play the game, something like catching a pokemon, the Pokemon you capture isn't exactly as it is in the program, so you can capture multiple Pokemons of the same class) The player and the enemies has both an attack function, and a hp parameter The problem is, how do I reference the hp of an enemy that doesn't already exist? I want to have multiple enemies, and I just cannot put a function to reduce the hp of every enemy... So I cannot program in the player class "Reducesnakehp()" and take the snake.hp value, and then "reduceogrehp(), and then take the ogre.hp value and continue like that. Again with Pokemon, if you try to do that you should code thousand of different cases, and that's absurd. I hope it is understandable, I tried my best to explain it... How could I fix that? :(

5th May 2019, 1:42 AM
Eduardo Garcia
Eduardo Garcia - avatar
2 Answers
+ 6
Not sure I understand the question entirely. But do you mean something like this? https://code.sololearn.com/cI26Uqr56JXQ/?ref=app
5th May 2019, 2:22 AM
jay
jay - avatar
0
Oh yes! Actually I needed something like that. Thank you a lot!
5th May 2019, 2:26 PM
Eduardo Garcia
Eduardo Garcia - avatar