CodeCombat代码全记录(Python学习利器)--边地森林(第二章)代码11

Chameleons(伪装者)

在这里插入图片描述

# Ogres are disguised as coins or gems!

while True:
    enemy = hero.findNearestEnemy()
    # If you see an enemy - attack it:
    if enemy:
        hero.attack(enemy)
    item = hero.findNearestItem()
    # If you see a coin or a gem - move to it's X and Y position:
    if item:
        hero.moveXY(item.pos.x, item.pos.y)

Arrowproof Wolf(无箭狼)

# Collect mushrooms.

# First, come to the wolf pet and wake up it (say).
hero.moveXY(12, 34)
hero.say("Weak Up wolf!!")
# Next collect mushrooms just usual items.
while True:
    item = hero.findNearestItem()
    if item:
        hero.moveXY(item.pos.x, item.pos.y)

攻擊和凍住你的敵人

# 你掉进陷阱里了!别动!你会受伤的!

# 这个函数检查敌人是否再攻击范围。
def inAttackRange(enemy):
    distance = hero.distanceTo(enemy)
    # Almost all swords have attack range of 3.
    if distance <= 3:
        return True
    else:
        return False

# Attack ogres only when they're within reach.
while True:
    # 找到最近的敌人,并将其储存在一个变量中。
    enemy = hero.findNearestEnemy()
    # 调用 inAttackRange(enemy),将 enemy 作为参数
    # 把结果保存于 “canAttack” 变量中
    canAttack = inAttackRange(enemy)
    # If the result stored in canAttack is True, 然后下手!
    if canAttack is True:
        hero.attack(enemy)
    pass

多人宝藏森林(挑战关卡)

我使用了和平共处的五项原则,没有对电脑下手,挑战关卡,大家可以进行自己的代码编写。这里我就简单编写了。
关于旗子的使用,后面会有很多的练习关卡,让我们多加练习如何使用旗子。

# 当第一个收集100个金币的人!
# 如果你死了,重生的时候只有原来67%的金币

while True:
    # 找到金币并攻击敌人
    # 使用旗子和特殊的移动策略来赢得比赛!
    coin = hero.findNearestItem()
    enemy = hero.findNearestItem()
    #flag = hero.findFlag()
    
    if coin:
        hero.moveXY(coin.pos.x, coin.pos.y)

围攻Stonehold(挑战开放关卡)

之后此类关卡先不编写内容,大家自行编写,后续我们会统一用开放式的思想进行此类关卡的内容编写。
并且此类关卡涉及到自身血量的问题,后续的关卡我们也会练习到。如果你不会定义血量等内容,那先使用你的砖石购买些好的装备,来称下血量。

为援兵坚持住

旗子练习关卡!!注意,旗子关卡你需要提交后才可以使用旗子控制英雄!!

# 食人魔正在爬悬崖
# 为集结民兵组织保护足够长时间的农民。
while True:
    flag = hero.findFlag()
    enemy = hero.findNearestEnemy()
    if flag:
        # 捡旗子
        hero.pickUpFlag(flag)
    
    elif enemy:
        # 否则,攻击!
        # 使用旗子移动到指定位置,如果 “cleave” 技能冷却完毕,就使用 “cleave” 技能。
        if hero.isReady("cleave"):
            hero.cleave(enemy)
        else:
            hero.attack(enemy)
        

赚钱行家

# To make the training more interesting Senick poisoned you.
# While you aren't moving the poison is harmless.

# This function should check if a coin is closer than 20m.
def isCoinClose(coin):
    # Find the distance to the coin.
    distanceToCoin = hero.distanceTo(coin)
    # If the distance is less than 20: 
    if distanceToCoin < 20:
        # Return True
        return True
    # Else:
    else:
        # Return False
        return False
    pass

while True:
    item = hero.findNearestItem()
    if item:
        # If isCoinClose(item) returns true:
        if isCoinClose(item):
            hero.moveXY(item.pos.x, item.pos.y)

盐碱地

# Ogres are attacking a nearby settlement!
# 小心,兽人在地上放了毒药。
# Gather coins and defeat the ogres, but avoid the burls and poison!

while True:
    enemy = hero.findNearestEnemy()
    if enemy.type == "munchkin" or enemy.type == "thrower":
        hero.attack(enemy)
    item = hero.findNearestItem()
    # 检查物品类型,确保英雄不会捡起毒药!
    # Look for types: 'gem' or 'coin'
    if item.type == "gem" or item.type == 'coin':
        hero.moveXY(item.pos.x, item.pos.y)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值