java坦克大战_混用Java/Kotlin/Scala玩Robocode坦克大战

fd8b2391da2df9614da1c7924e0795a5.png

混用Java/Kotlin/Scala玩Robocode坦克大战

创建robots项目

直接使用robocode自带的SourceEditor来创建:Robot -> Source Editor -> File -> New -> Robot,然后按照提示输入包结构和Robot名称,最后一定记得需要Ctrl+S保存一下项目才算真正将创建的项目保存下来,紧接着就可以在robocode安装目录下的robots中找到创建项目

自带的Source Editor太挫,这里选择使用IDEA打开,稍微调整一下结构后得到如下:

6737ba80c37052906579901c5969b20c.png
  • src目录下放的是自定义robot源码,上面的例子中共有三种版本:java、kotlin和scala
  • cn/chph/robots/xxx放的是编译后的robot字节码文件

要注意的是:robot字节码文件所在的目录结构要和robot的package结构相同,例如上面三个Robot源码都是在cn.chph.robots包下,所以生成的字节码也必须放到对应的目录结构cn/chph/robots下,否则在启动Battle时会抛出类似下面的异常:

java Got an error with out.production.robots.cn.chph.robots.KotlinRobot: java.lang.ClassNotFoundException: cn/chph/robots/KotlinRobot (wrong name: out/production/robots/cn/chph/robots/KotlinRobot) Got an error with out.production.robots.cn.chph.robots.JavaRobot: java.lang.ClassNotFoundException: cn/chph/robots/JavaRobot (wrong name: out/production/robots/cn/chph/robots/JavaRobot) Got an error with out.production.robots.cn.chph.robots.ScalaRobot: java.lang.ClassNotFoundException: cn/chph/robots/ScalaRobot (wrong name: out/production/robots/cn/chph/robots/ScalaRobot)

添加依赖

因为官方的robocode只支持java,因此为了使得kotlin/scala编写的robot能够顺利进入战斗,我们需要添加kotlin/scala对应的语言标jar准包,直接修改启动脚本robocode.sh:

b9d84fdf185a03d5aa5077065dcc395b.png

启动

紧接着按照正常的步骤进入游戏,robocode就能够正确识别并允许我们编译后的不同语言版本的robot

2da1d2f550ee6f3e9deeb25d5f5c055a.png

449365762706749c366c54d65120f9a4.png
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package squidM; import robocode.*; import java.awt.*; import robocode.util.Utils; // API help : http://robocode.sourceforge.net/docs/robocode/robocode/Robot.html /** * Squidman 2.4 - a robot by (Blubex)-- LATEST VERSION */ public class Squidman extends AdvancedRobot { /** * run: Squidman's yodeling fun time */ double previousEnergy = 100; int dir = 1; // ----------------- public void run() { setAdjustRadarForGunTurn(true); setAdjustRadarForRobotTurn(true); setAdjustGunForRobotTurn(true); setColors(Color.red,Color.pink,Color.red,Color.red,Color.pink); // body,gun,radar,bullets,scan arc setTurnRadarRight(Double.POSITIVE_INFINITY); // Radar Lock } public void onScannedRobot(ScannedRobotEvent e) { // Stay at right angles to the opponent setTurnRight(e.getBearing()+90- 30*dir); double changeInEnergy = previousEnergy-e.getEnergy(); if (changeInEnergy>0 && changeInEnergy<=3) { // run away dir = -dir; setAhead((e.getDistance()/4+25)); } //radar lock cont. setTurnRadarLeft(getRadarTurnRemaining()); //linear target double power = 2; double bVel = 20 - (power * 3); double absoluteBearing = getHeadingRadians() + e.getBearingRadians(); setTurnGunRightRadians(Utils.normalRelativeAngle (absoluteBearing - getGunHeadingRadians() + (e.getVelocity() * Math.sin(e.getHeadingRadians() - absoluteBearing) / bVel))); fire(power); // Track the energy level previousEnergy = e.getEnergy(); } public void onHitWall(HitWallEvent e) { double move; move = e.getBearing(); if (move > 0) { setTurnLeft(180); } else { setTurnRight(180); } ahead(200); } public void onBulletHit(BulletHitEvent event) { setColors(Color.orange,Color.red,Color.black,Color.red,Color.pink); } public void onBulletMissed(BulletMissedEvent event) { setColors(Color.red,Color.pink,Color.red,Color.red,Color.pink); } public void onWin (WinEvent e) { //WIN WIN WIN WIN WIN setColors(Color.orange,Color.red,Color.black,Color.red,Color.orange); setTurnLeft(360); ahead(30); back(30); setTurnRight(360); } //end }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值