CodeCombat代码全记录(Python学习利器)--安息之云山峰(第四章)代码2

动物园管理员

# 保护笼子。
# 放一个士兵在每一个 X 的位置
points = []
points[0] = {"x": 33, "y": 42}
points[1] = {"x": 47, "y": 42}
points[2] = {"x": 33, "y": 26}
points[3] = {"x": 47, "y": 26}

# 1.收集80金币。
while hero.gold < 80:
    coins = hero.findItems()
    coin = hero.findNearest(coins)
    if coin:
        hero.move(coin.pos)
# 2.建造4个士兵。
for i in range(4):
    hero.summon("soldier")
    
# 3.派你的士兵到特定的位置上。
while True:
    friends = hero.findFriends()
    for j in range(len(friends)):
        point = points[j]
        friend = friends[j]
        enemy = friend.findNearestEnemy()
        if enemy and enemy.team == "ogres" and friend.distanceTo(enemy) < 5:
            # 命令友方攻击。
            hero.command(friend, "attack", enemy)
        else:
            # 命令的朋友移动到特定点上。
            hero.command(friend, "move", point)

木材的叛徒

我使用了法师的角色,购买了个可以召唤树榴巨人的法杖,通过召唤牛逼的生物过的关。。

while True:
    # 收集金子
    coin = hero.findNearest(hero.findItems())
    hero.move(coin.pos)
    # 如果你有足够的金币,那就招募一个士兵。
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
        #判断召唤树怪技能是否准备OK,如果准备好,就召唤。
        if hero.canCast("summon-burl"):
            hero.cast("summon-burl")
            # 使用 for 循环来命令每个士兵。
            #friends = hero.findFriends()
        for friend in hero.findFriends():
            if friend.type == "soldier" or friend.type == "summon-burl":
                hero.command(friend, "move", {"x": 73, "y": 46})
                enemy = friend.findNearestEnemy()
                # 如果这里有一个敌人,那就命令她攻击。
                # 小心!如果你的战士被打败了,术士就会出现!
                # 否则的话,将她移动到地图的右边。
                if enemy:
                    hero.command(friend, "attack", enemy)
                if friend.health < friend.maxHealth / 2:
                    hero.command(friend, "move", hero.pos)
            

高贵的牺牲

# 收集80金。
while hero.gold <= 80:
    coin = hero.findNearest(hero.findItems())
    hero.move(coin.pos)
# 建造4个士兵来做诱饵
while hero.gold > hero.costOf("soldier"):
    hero.summon("soldier")
# 派你的士兵到指定位置。
points = []
points[0] = { "x": 13, "y": 73 }
points[1] = { "x": 51, "y": 73 }
points[2] = { "x": 51, "y": 53 }
points[3] = { "x": 90, "y": 52 }
friends = hero.findFriends()
for j in range(len(friends)):
# 使用range来得到可供遍历的数组。
# 让各盟友士兵和各地点位置匹配,以命令他们移动
    point = points[j]
    friend = friends[j]
    hero.command(friend, "move", point)

狩猎派对

注意下提示,有可能是不对的,如果不看提示,可以按照你自己的思路做编写代码。

# You can use findNearestEnemy() on your soldiers to get their nearest enemy instead of yours.
while True:
    friends = hero.findFriends()
    # Use for-loop and for each friend:
    for friend in friends:
        # Command to move east by small steps.
        hero.command(friend, "move", {"x":friend.pos.x + 0.1,"y": friend.pos.y})
        # If they see an enemy then command to attack.
        enemy = friend.findNearestEnemy()
        if enemy:
            hero.command(friend, "attack", enemy)
        

保护和服务

# 保护工人和动物!

# 捍卫这两个职业:
defend = []
defend[0] = { "x": 98, "y": 28 }
defend[1] = { "x": 84, "y": 7 }

soldiers = []

friends = hero.findFriends()
for index in range(len(friends)):
    friend = friends[index]
    if friend.type == "soldier":
        soldiers.append(friend)
    else:
        # 保卫工人:
        defend.append(friend)

while True:
    # 使用for循环将每个士兵分配给对应的防守[]目标
    # 使用命令(士兵,"defend",胜)或命令(士兵,"defend",位置)
    for j in range(len(soldiers)):
        friend = friends[j]
        soldier = soldiers[j]
        #defend = defend[j]
        hero.command(soldier, "defend", defend[j])
    pass

CodeCombat 是一个通过玩游戏来学习编程的网站,也是GitHub上最大的开源CoffeeScript(一种脚本语言,类似JavaScript)项目,构筑在几十个开源项目之上的,有上千程序员和玩家为其编写程序、测试游戏。到目前为止,已经翻译成17种国外语言。       它是一款多人编码游戏,该款游戏的任务就是教会大家如何编程,并且通过游戏来提升开发者的技能水平。因为开源,我们可以为孩子们定制我们希望的样式。人们不需要任何编程知识即可了解程序的运行逻辑,并编出实用的代码。      游戏总共超过9千关,每个步骤都会有语音操作提示(非中文),无论你是新手还是编程精英都可以加入到这款游戏中。最重要的是,你是写代码执行游戏。你要扮演得是一名非常有力量的魔法师,要通过你写代码魔法,让你的人去消灭怪兽(其实是怪物)。看起来像是塔防游戏——《王城保卫战》,但却是一款即时战略游戏。每关都会有对话讲解如何操作(遗憾没有中文)左侧是游戏界面,右侧是代码界面,通过在右侧输入关键语句代码,控制左侧角色的移动和攻击等动作。前几关非常简单,几行“上下左右”和“攻击”的代码即可完成通关。每关结束后都有个回顾,告诉你在上一关学到了什么。当输入错误,比如大小写错误,在代码下方会提示具体的出错信息,玩家可以据此Debug。第二关中,你要先去右边吃蘑菇变强,再去击杀怪物。(点击图片查看具体代码,其实向右移动一步即可迟到蘑菇,代码中是两步)第三关,在击杀第一个怪物后,角色的血也不多了,所以要先去下方喝药瓶。下去喝药瓶,输入代码按回车,角色就会照做。第四关前N关都是编程中最基本的顺序语句,随着关数的提升,像“if…else,then”等判断、循环语句也会逐渐加入,可玩性越来越高。玩家在不知不觉也就具备了编程思维。 标签:编程游戏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值