Ben McAlindin
Ben McAlindin

Reputation: 542

Python - Compare two lists and find matching values and their positions in the list

I am trying to make a turn based RPG and I am currently working on the turn system.

To keep it simple for now I am trying to order each character/enemy by their speed and then highest speed goes first. Below is the start of my code for the turn system.

An example of what I want to do is read Order[0] (which will be the fasted character/enemy) and find out which character/enemy that relates to. Speed[0] to Speed[3] gives the speed of my 4 characters and enemySpeed[0] to enemySpeed[4] gives the speed of the 5 enemies.

def Turn():
    Turn = [int(Speed[0]), int(Speed[1]), int(Speed[2]), int(Speed[3]), int(enemySpeed[0]), int(enemySpeed[1]),int(enemySpeed[2]), int(enemySpeed[3]), int(enemySpeed[4])]
    Order = sorted(Turn, key = int, reverse = True)

Edit: Here is some information regarding player stats. In main():

    name = ['Ben','Ellis','Curt','Jen']
    HP = [100,100,100,100]
    exp = [0,0,0,0]
    lvl = [1,1,1,1]
    player1 = [name[0],HP[0],exp[0],lvl[0]]
    player2 = [name[1],HP[1],exp[1],lvl[1]]
    player3 = [name[2],HP[2],exp[2],lvl[2]]
    player4 = [name[3],HP[3],exp[3],lvl[3]]
    PLAYERS = [player1, player2, player3, player4]
    REGION = 'start'
    POS = '[x, y]'
    ITEMS = None
    SIGEVENTS = False
    gameData = [PLAYERS, REGION, POS, ITEMS, SIGEVENTS]

Out of main():

def playerStats():
    global Attack, Defense, Speed, MaxHP
    Attack      = [0,0,0,0]
    Defense     = [0,0,0,0]
    Speed       = [0,0,0,0]
    MaxHP   = [0,0,0,0]
    for i in range(0,4):
        Attack[i]       =   lvl[i] * 1
        Defense[i]      =   lvl[i] * 2
        Speed[i]        =   lvl[i] * 3
        MaxHP[i]        =   lvl[i] * 4

    return Attack, Defense, Speed, MaxHP

Upvotes: 0

Views: 337

Answers (2)

Hyperboreus
Hyperboreus

Reputation: 32429

Martijn Pieters already pointed out the most important issues. Try to use objects. Try to remodel the objects of your game and don't manage their attributes seperately.

I will elaborate a bit on this and maybe I can give you so some ideas.

A Character, be it a NPC or a PC, is one object of a class that could look something like this:

class Character:
    def __init__ (self, name, xp = 0, level = 1, hp = 4, speed = 3, atk = 1, def_ = 2):
        self.name = name
        self.xp = xp
        self.level = level
        self.hp = self.curHp = hp
        self.speed = speed
        self.atk = atk
        self.def_ = def_
        self.inventory = []

Now using this class you can instantiate your PCs and NPCs:

npcs = [Character (name) for name in ['Baldur', 'Lord Never', 'Ashyra'] ]

Adding methods to your class lets you interact with it, for example:

class Character:
    [....]
    def takeDamage (self, dmg):
        self.curHp -= min (0, dmg - self.def_)

    def gainXP (self, xp):
        self.xp += xp
        #check for level up

    def takeItem (self, item):
        self.inventory.append (item)

    def useItem (self, item):
        item.activate ()
        self.inventory.remove (item)

Or in order to get all your characters ordered by their speed, you can use:

charactersBySpeed = sorted (characters, key = -character.speed)

And so further and so on. The idea is really to use object oriented programming for modelling the reality (even the made-up reality of your game) in your code.

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121446

Instead of looking at just the speeds, look at your players and enemies, and devise a key to determine what speed goes with what player.

You didn't share any details on how you define players and speeds; I'll use a number here for now:

players_and_enemies = range(8)

fastest = max(players_and_enemies, key=lambda p: int(Speed[i] if i < 4 else enemySpeed[i - 4]))

It may well be that you can simplify this based on your data structures; if you have objects per player and enemy that have a speed attribute, for example, you can access that attribute in the key function instead.

max() returns the highest value by key; no need to sort all speeds if all you need is the fastest driver.

Upvotes: 2

Related Questions