Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Reaping Fire Python (help) [SOLVED]

CodeCombat - Coding games to learn Python and JavaScript? My griffin riders aren’t doing anything, but everything else seems to be working fine. Code:



def chooseStrategy():
    enemy = hero.findNearestEnemy()
    # If you can summon a griffin-rider, return "griffin-rider"
    if hero.gold >= hero.costOf("griffin-rider"):
        hero.summon("griffin-rider")
        return "griffin-rider"
    # If there is a fangrider on your side of the mines, return "fight-back"
    if enemy and enemy.type is "fangrider" and enemy.pos.x < 37:
        return "fight-back"
        # Otherwise, return "collect-coins"
    else:
        return "collect-coins"

def commandAttack():
    # Command your griffin riders to attack ogres.
    friends = hero.findFriends()
    enemy = hero.findNearestEnemy()
    for friend in friends:
        if enemy and friend:
            hero.command(friend, "attack", enemy)
def pickUpCoin():
    # Collect coins
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
def heroAttack():
    fang = hero.findByType("fangrider")
    enemy = hero.findNearest(fang)
    # Your hero should attack fang riders that cross the minefield.
    eX = enemy.pos.x
    if enemy.type == "fangrider" and eX < 25:
        hero.attack(enemy)
while True:
    strategy = chooseStrategy()
    # Call a function, depending on what the current strategy is.
    if strategy == "griffin-rider":
        commandAttack()
    elif strategy == "fight-back":
        heroAttack()
    elif strategy == "collect-coins":
        pickUpCoin()

This is the problem

it should be like this

def heroAttack():
    fang = hero.findByType("fangrider")
    enemy = hero.findNearest(fang)
    # Your hero should attack fang riders that cross the minefield.
    eX = enemy.pos.x
    
    if enemy.type == "fangrider" and eX < 25:
        hero.attack(enemy)

Still doesn’t work. (20)

Get rid of the enemy.pos.x < 37.

ok here’s my new code:



def chooseStrategy():
    enemy = hero.findNearestEnemy()
    # If you can summon a griffin-rider, return "griffin-rider"
    if hero.gold >= hero.costOf("griffin-rider"):
        hero.summon("griffin-rider")
        return "griffin-rider"
    # If there is a fangrider on your side of the mines, return "fight-back"
    if enemy and enemy.type is "fangrider":
        return "fight-back"
        # Otherwise, return "collect-coins"
    else:
        return "collect-coins"

def commandAttack():
    # Command your griffin riders to attack ogres.
    friends = hero.findFriends()
    enemy = hero.findNearestEnemy()
    for friend in friends:
        if enemy and friend:
            hero.command(friend, "attack", enemy)
def pickUpCoin():
    # Collect coins
    item = hero.findNearestItem()
    if item:
        hero.move(item.pos)
def heroAttack():
    fang = hero.findByType("fangrider")
    enemy = hero.findNearest(fang)
    # Your hero should attack fang riders that cross the minefield.
    eX = enemy.pos.x
    if enemy.type == "fangrider" and eX < 25:
        hero.attack(enemy)
while True:
    strategy = chooseStrategy()
    # Call a function, depending on what the current strategy is.
    if strategy == "griffin-rider":
        commandAttack()
    elif strategy == "fight-back":
        heroAttack()
    elif strategy == "collect-coins":
        pickUpCoin()

but it still doesn’t work, the problem I think is something with the commanding though

Don’t need this.

replace it with

if friend.type == "griffin-rider":
    hero.command(friend, "attack", enemy)
    pass

Now my griffin riders are attacking once, then just standing still. Also, the hero isn’t doing anything to attack the fang riders anymore.

nvm figured it out :smile:

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.