Advertisement
Guest User

turtle

a guest
Nov 7th, 2012
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. import se.lth.cs.ptdc.window.SimpleWindow;
  2.  
  3. public class Turtle {
  4. private int x;
  5. private int y;
  6. private boolean pen;
  7. protected SimpleWindow w;
  8. private double a;
  9.  
  10. public Turtle(SimpleWindow w, int x, int y) {
  11.     this.x = x;
  12.     this.y = y;
  13.     this.w = w;
  14.     a = Math.PI / 2;
  15. }
  16.  
  17. public void penDown() {
  18.     pen = true;
  19. }
  20.  
  21. public void penUp() {
  22.     pen = false;
  23. }
  24.  
  25. public void forward(int n) {
  26.     w.moveTo(x, y);
  27.     x = x + (int) (Math.round(Math.cos(a) * n));
  28.     y = y - (int) (Math.round(Math.sin(a) * n));
  29.     if (pen) {
  30.         w.lineTo(x, y);
  31.     } else {
  32.         w.moveTo(x, y);
  33.     }
  34. }
  35.  
  36. public void left(int beta) {
  37.     a = a + Math.toRadians(beta);
  38. }
  39.  
  40. public void jumpTo(int newX, int newY) {
  41.     this.x = newX;
  42.     this.y = newY;
  43.     w.moveTo(newX, newY);
  44. }
  45.  
  46. public void turnNorth() {
  47.     a = Math.PI / 2;
  48. }
  49.  
  50. public int getX() {
  51.     return x;
  52. }
  53.  
  54. public int getY() {
  55.     return y;
  56. }
  57.  
  58. public int getDirection() {
  59.     return (int) Math.toDegrees(a);
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement