Advertisement
qtinio

dddd

May 13th, 2019
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. import robocode.*;
  8. import robocode.Robot;
  9.  
  10. import robocode.ScannedRobotEvent;
  11. import java.awt.*;
  12. import java.awt.geom.Rectangle2D;
  13. import java.awt.event.MouseEvent;
  14. import java.awt.event.MouseWheelEvent;
  15. import static robocode.util.Utils.normalAbsoluteAngle;
  16. import static robocode.util.Utils.normalRelativeAngle;
  17. /**
  18. *
  19. * @author student
  20. */
  21. public class zbrzezniak extends AdvancedRobot{
  22. private double enemyX;
  23. private double enemyY;
  24. private boolean movingForward;
  25. double myX, myY;
  26. private Rectangle2D fieldRect;
  27. int mouseX, mouseY;
  28. boolean pressed = false;
  29.  
  30.  
  31. public void run() {
  32. fieldRect = new Rectangle2D.Double(18, 18, getBattleFieldWidth()-36,
  33. getBattleFieldHeight()-36);
  34. while (true) {
  35.  
  36. // movingForward = true;
  37. // setTurnRight(90);
  38. // waitFor(new TurnCompleteCondition(this));
  39. // setTurnLeft(180);
  40. // waitFor(new TurnCompleteCondition(this));
  41. // setTurnRight(180);
  42. // waitFor(new TurnCompleteCondition(this));
  43. // setAhead(5);
  44. if (pressed){
  45. double dist = distanceMouse();
  46. if (dist < 10) {
  47. setAhead(0);
  48. pressed = false;
  49. } else
  50. setAhead(1000);
  51.  
  52. setTurnRightRadians(angleToTurnInRadians());
  53. }
  54. execute();
  55. }
  56. }
  57.  
  58.  
  59. public double distanceMouse(){
  60. return Math.sqrt(Math.pow(mouseY - getY(), 2) + Math.pow(mouseX - getX(), 2));
  61. }
  62.  
  63.  
  64. public double angleToTurnInRadians(){
  65. double angle = normalAbsoluteAngle(Math.atan2(mouseX - getX(), mouseY - getY()));
  66. return normalRelativeAngle(angle - getHeadingRadians());
  67. }
  68.  
  69. @Override
  70. public void onHitWall(HitWallEvent e) {
  71. // Bounce off!
  72. reverseDirection();
  73. }
  74.  
  75. public void reverseDirection() {
  76. if (movingForward) {
  77. setBack(40000);
  78. movingForward = false;
  79. } else {
  80. setAhead(40000);
  81. movingForward = true;
  82. }
  83. }
  84.  
  85. @Override
  86. public void onStatus( StatusEvent e) {
  87. // you can also put all setAdjustXX(..) methods here
  88. setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
  89. }
  90.  
  91. // Called when the mouse has been moved
  92. @Override
  93. public void onMousePressed(MouseEvent e) {
  94. pressed = true;
  95. mouseX = e.getX();
  96. mouseY = e.getY();
  97. }
  98.  
  99. @Override
  100. public void onScannedRobot(ScannedRobotEvent e) {
  101. // setTurnRadarRight(2.0 * Utils.normalRelativeAngleDegrees(getHeading() + e.getBearing() - getRadarHeading()));
  102. double absoluteBearing = getHeadingRadians() + e.getBearingRadians();
  103. enemyX = getX() + e.getDistance() * Math.sin(absoluteBearing);
  104. enemyY = getY() + e.getDistance() * Math.cos(absoluteBearing);
  105. // wyniki z radaru
  106.  
  107. }
  108.  
  109. @Override
  110. public void onPaint(Graphics2D g) {
  111. // Set the paint color to a red half transparent color
  112. myX = getX();
  113. myY = getY();
  114.  
  115. String posS =
  116. String.format("Zbrzeźniak = %f X, %f Y\n Kaczka = %f X, %f Y",
  117. myX, myY, enemyX, enemyY);
  118.  
  119. g.setColor(new Color(0xff, 0xff, 0xff, 0x90));
  120. g.drawString(posS, 10, 20);
  121.  
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement