Presentation is loading. Please wait.

Presentation is loading. Please wait.

My Second Robot. Anatomy of a Robot A robot consists of 3 independently moving parts: Vehicle - determines the direction in which the robot will move.

Similar presentations


Presentation on theme: "My Second Robot. Anatomy of a Robot A robot consists of 3 independently moving parts: Vehicle - determines the direction in which the robot will move."— Presentation transcript:

1 My Second Robot

2 Anatomy of a Robot A robot consists of 3 independently moving parts: Vehicle - determines the direction in which the robot will move Gun - determines the direction in which the robot will fire Radar - determines what the robot can see (if you turn on Visible scan arcs in options -> Preferences, this will give you a feel for how much a robot can see)

3 Turning By default, moving a component will result in all components ON TOP of it moving accordingly (e.g. if the gun turns 90 degrees left, the radar will turn 90 degrees left). However, you can set components to adjust themselves automatically so that they are independent of the movement of other components, using these methods: setAdjustGunForRobotTurn(boolean flag): If the flag is set to true, the gun will remain in the same direction while the vehicle turns. setAdjustRadarForRobotTurn(boolean flag): If the flag is set to true, the radar will remain in the same direction while the vehicle (and the gun) turns. setAdjustRadarForGunTurn(boolean flag): If the flag is set to true, the radar will remain in the same direction while the gun turns. It will also act as if setAdjustRadarForRobotTurn(true) has been called.

4 Basic Commands Below are the basic commands for moving the robot around, you can find more detailed definitions in the Robocode API.Robocode API turnRight(double degree) and turnLeft(double degree) turn the robot by a specified degree. ahead(double distance) and back(double distance) move the robot by the specified distance; these two methods return early if the robot hits a wall or another robot. turnGunRight(double degree) and turnGunLeft(double degree) turn the gun, independent of the vehicle's direction. turnRadarRight(double degree) and turnRadarLeft(double degree) turn the radar on top of the gun, independent of the gun's direction (and the vehicle's direction).

5 How to build a robot public class GenericRobot extends Robot { public void run() { while(true) { }

6 Useful Event-handlers onScannedRobot() - this method is called when the radar detects a robot. onHitByBullet() - this method is called when the robot is hit by a bullet. onHitRobot() - this method is called when your robot hits another robot. onHitWall() method; this method is called when your robot hits a wall.

7 Problem What does this robot do? import robocode.*; public class MySecondRobot extends Robot { public void run() { turnLeft(getHeading()); while(true) { ahead(1000); turnRight(90); } public void onScannedRobot(ScannedRobotEvent e) { fire(1); } public void onHitByBullet(HitByBulletEvent e) { turnLeft(180); }

8 Solution The robot begins by driving straight up (turnLeft(getHeading())). It turns right 90 degrees whenever it hits something, so it moves orthogonally in a large rectangle. If hit by a bullet it changes direction (turnLeft(180)).


Download ppt "My Second Robot. Anatomy of a Robot A robot consists of 3 independently moving parts: Vehicle - determines the direction in which the robot will move."

Similar presentations


Ads by Google