Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- import robocode.*;
- import robocode.Robot;
- import robocode.ScannedRobotEvent;
- import java.awt.*;
- import java.awt.geom.Rectangle2D;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseWheelEvent;
- import static robocode.util.Utils.normalAbsoluteAngle;
- import static robocode.util.Utils.normalRelativeAngle;
- /**
- *
- * @author student
- */
- public class zbrzezniak extends AdvancedRobot{
- private double enemyX;
- private double enemyY;
- private boolean movingForward;
- double myX, myY;
- private Rectangle2D fieldRect;
- int mouseX, mouseY;
- boolean pressed = false;
- public void run() {
- fieldRect = new Rectangle2D.Double(18, 18, getBattleFieldWidth()-36,
- getBattleFieldHeight()-36);
- while (true) {
- // movingForward = true;
- // setTurnRight(90);
- // waitFor(new TurnCompleteCondition(this));
- // setTurnLeft(180);
- // waitFor(new TurnCompleteCondition(this));
- // setTurnRight(180);
- // waitFor(new TurnCompleteCondition(this));
- // setAhead(5);
- if (pressed){
- double dist = distanceMouse();
- if (dist < 10) {
- setAhead(0);
- pressed = false;
- } else
- setAhead(1000);
- setTurnRightRadians(angleToTurnInRadians());
- }
- execute();
- }
- }
- public double distanceMouse(){
- return Math.sqrt(Math.pow(mouseY - getY(), 2) + Math.pow(mouseX - getX(), 2));
- }
- public double angleToTurnInRadians(){
- double angle = normalAbsoluteAngle(Math.atan2(mouseX - getX(), mouseY - getY()));
- return normalRelativeAngle(angle - getHeadingRadians());
- }
- @Override
- public void onHitWall(HitWallEvent e) {
- // Bounce off!
- reverseDirection();
- }
- public void reverseDirection() {
- if (movingForward) {
- setBack(40000);
- movingForward = false;
- } else {
- setAhead(40000);
- movingForward = true;
- }
- }
- @Override
- public void onStatus( StatusEvent e) {
- // you can also put all setAdjustXX(..) methods here
- setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
- }
- // Called when the mouse has been moved
- @Override
- public void onMousePressed(MouseEvent e) {
- pressed = true;
- mouseX = e.getX();
- mouseY = e.getY();
- }
- @Override
- public void onScannedRobot(ScannedRobotEvent e) {
- // setTurnRadarRight(2.0 * Utils.normalRelativeAngleDegrees(getHeading() + e.getBearing() - getRadarHeading()));
- double absoluteBearing = getHeadingRadians() + e.getBearingRadians();
- enemyX = getX() + e.getDistance() * Math.sin(absoluteBearing);
- enemyY = getY() + e.getDistance() * Math.cos(absoluteBearing);
- // wyniki z radaru
- }
- @Override
- public void onPaint(Graphics2D g) {
- // Set the paint color to a red half transparent color
- myX = getX();
- myY = getY();
- String posS =
- String.format("Zbrzeźniak = %f X, %f Y\n Kaczka = %f X, %f Y",
- myX, myY, enemyX, enemyY);
- g.setColor(new Color(0xff, 0xff, 0xff, 0x90));
- g.drawString(posS, 10, 20);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement