+ 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
7 Réponses
+ 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
+ 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.
+ 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
+ 6
Depends on what random is
+ 4
@jay That was what I am thinking but how do we spawn them?
+ 4
I may try that, lemme try
+ 4
ty Ahri