+ 5

How to create random objects in Python?

I'm making a text-based game and I want random enemies to spawn in the map(yes i made a map in a text based game lol) but I cannot think how can I make. I hope you can help me :p

30th Jun 2017, 8:54 AM
Ali Emir Kızıl
Ali Emir Kızıl - avatar
7 Respostas
+ 6
not sure how this would be done in python. in c++ i would have an array to store the enemies and use the new keyword to create an enemy then push it into the array
30th Jun 2017, 9:02 AM
jay
jay - avatar
+ 9
import random map=[] for y in range(0,50): map.append([]) for y in range(0,len(map)): for x in range(0,40): map[y].append("blank") enemies=["fox hater","java user","politician"] for y in range(0,len(map)): for x in range(0,len(map[y])): if random.randint(0,400) == 0: map[y][x]=random.choice(enemies) for y in range(0,len(map)): print("".join(map[y])) #something like that, although obviously edited to apply to your code. I use stuff like this alot for my 2d text based junk.
30th Jun 2017, 9:37 AM
Ahri Fox
Ahri Fox - avatar
+ 6
In c++ I would do this as follows Assign each type of enemy a number ( 1 to max enemy type) Generate a random number between 1 and max enemy types. Switch case on result of random number and spawn that type
30th Jun 2017, 8:58 AM
jay
jay - avatar
+ 6
Depends on what random is
20th Jul 2017, 2:49 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 4
@jay That was what I am thinking but how do we spawn them?
30th Jun 2017, 8:59 AM
Ali Emir Kızıl
Ali Emir Kızıl - avatar
+ 4
I may try that, lemme try
30th Jun 2017, 9:03 AM
Ali Emir Kızıl
Ali Emir Kızıl - avatar
+ 4
ty Ahri
30th Jun 2017, 4:25 PM
Ali Emir Kızıl
Ali Emir Kızıl - avatar