Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help about a function
#1
Hi to everyone,

I have a question. I have a game map and 5 monsters. I want all my monsters born on the "dirt" not on the rock or water and I have this code;

while True:
    mnstrh=random.randint(0,mapheight-1)
    mnstrw=random.randint(0,mapwidth-1)
    if floormap[mnstrh][mnstrw]=="dirt":
        monsterpos[0]=mnstrw
        monsterpos[1]=mnstrh
        break
    else:
        True

while True:
    mnstrh1=random.randint(0,mapheight-1)
    mnstrw1=random.randint(0,mapwidth-1)
    if floormap[mnstrh1][mnstrw1]=="dirt":
        monster1pos[0]=mnstrw1
        monster1pos[1]=mnstrh1
        break
    else:
        True
Monster variable names are monster,monster1... and their position names are monsterpos, monster1pos, monster2pos..

How can I shorten this code by using a function? I know that there is a short way by using a function but I can't do it :) Thanks in advance.
Reply
#2
Here's how a function could be useful:

get_position(floormap):
    while True:
        row = random.randint(0, mapheight - 1)
        column = random.randint(0, mapwidth - 1)
        if floormap[row][column]=="dirt":
            return [row, column]

monsterpos = get_position()
monsterpos1 = get_position()
...
That is a step forward, but not what I would recommend. I would have a list of monsters. Each monster would be a dictionary. Say you have a list of names, you could do it like this:

monster_names = ['demi-gorgon', 'bunny rabbit', 'medusa']
monsters = []
for name in monster_names:
    monsters.append({'name': name, 'position': get_position()})
One thing to note is that it is currently possible to have two monsters in the same position. If that is not what you want, the get_position function is not the best idea. Better would be to loop through all the positions, and make a list of the ones that are dirt. Then use random.shuffle to mix up the list of positions, and do:

monsters = []
for name, position in zip(monster_names, dirt_positions):
    monsters.append({'name': name, 'position': position})
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Jan-15-2019, 07:20 PM)ichabod801 Wrote: Here's how a function could be useful:

get_position(floormap):
    while True:
        row = random.randint(0, mapheight - 1)
        column = random.randint(0, mapwidth - 1)
        if floormap[row][column]=="dirt":
            return [row, column]

monsterpos = get_position()
monsterpos1 = get_position()
...
That is a step forward, but not what I would recommend. I would have a list of monsters. Each monster would be a dictionary. Say you have a list of names, you could do it like this:

monster_names = ['demi-gorgon', 'bunny rabbit', 'medusa']
monsters = []
for name in monster_names:
    monsters.append({'name': name, 'position': get_position()})
One thing to note is that it is currently possible to have two monsters in the same position. If that is not what you want, the get_position function is not the best idea. Better would be to loop through all the positions, and make a list of the ones that are dirt. Then use random.shuffle to mix up the list of positions, and do:

monsters = []
for name, position in zip(monster_names, dirt_positions):
    monsters.append({'name': name, 'position': position})

Thank you so much for your reply and your time. I have learnt so many things from this coding logic :)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020