Advertisement
Ramdan51-062

Snakes

Dec 14th, 2017
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. package Snake;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Point;
  6. import java.util.ArrayList;
  7.  
  8. public class Snakes {
  9.     public static final int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3, SCALE = 10;
  10.  
  11.     public Snake snake;
  12.     public int direction = DOWN, score = 0, tailLength = 5;
  13.     public ArrayList<Point> snakeParts = new ArrayList<Point>();
  14.     public int playerNumber, x, y;
  15.     public Point head;
  16.     public Cherry cherry;
  17.     public boolean over = false;
  18.  
  19.     public Snakes(Snake snake, int playerNumber, int x, int y) {
  20.         this.head = new Point(x, y);
  21.         this.x = x;
  22.         this.y = y;
  23.         this.playerNumber = playerNumber;
  24.         for (int i = 0; i < tailLength; i++)
  25.             snakeParts.add(new Point(head.x, head.y));
  26.     }
  27.  
  28.     public void render(Graphics g) {
  29.         String string = "Score: " + score + ", Length: " + tailLength;
  30.         if (playerNumber == 1)
  31.             g.setColor(Color.RED);
  32.         else
  33.             g.setColor(Color.YELLOW);
  34.         Move(direction);
  35.         for (Point point : this.snakeParts) {
  36.             g.fillRect(point.x * Snake.SCALE, point.y * Snake.SCALE, Snake.SCALE, Snake.SCALE);
  37.         }
  38.         g.fillRect(this.head.x * Snake.SCALE, this.head.y * Snake.SCALE, Snake.SCALE, Snake.SCALE);
  39.         g.setFont(new Font("Arial", 1, 15));
  40.         if(playerNumber == 1) {
  41.             g.drawString(string, 0, 10);
  42.         }
  43.         else
  44.             g.drawString(string, 660, 10);
  45.     }
  46.  
  47.     public void Move(int moveDirection) {
  48.         // this.direction = moveDirection;
  49.         snakeParts.add(new Point(head.x, head.y));
  50.         if (snakeParts.size() > tailLength)
  51.             snakeParts.remove(0);
  52.         switch (moveDirection) {
  53.         case (UP):
  54.             if (this.direction != DOWN) {
  55.                 if (head.y - 1 >= 0 && NoTailAt(head.x, head.y - 1)) {
  56.                     head = new Point(head.x, head.y - 1);
  57.                     this.direction = moveDirection;
  58.                 } else
  59.                     over = true;
  60.             }
  61.             break;
  62.         case (DOWN):
  63.             if (this.direction != UP) {
  64.                 if (head.y + 1 < 67 && NoTailAt(head.x, head.y + 1)) {
  65.                     head = new Point(head.x, head.y + 1);
  66.                     this.direction = moveDirection;
  67.                 } else
  68.                     over = true;
  69.             }
  70.             break;
  71.         case (LEFT):
  72.             if (this.direction != RIGHT) {
  73.                 if (head.x - 1 >= 0 && NoTailAt(head.x - 1, head.y)) {
  74.                     head = new Point(head.x - 1, head.y);
  75.                     this.direction = moveDirection;
  76.                 } else
  77.                     over = true;
  78.             }
  79.             break;
  80.         case (RIGHT):
  81.             if (this.direction != LEFT) {
  82.                 if (head.x + 1 < 80 && NoTailAt(head.x + 1, head.y)) {
  83.                     head = new Point(head.x + 1, head.y);
  84.                     this.direction = moveDirection;
  85.                 } else
  86.                     over = true;
  87.             }
  88.             break;
  89.         }
  90.     }
  91.  
  92.     public boolean NoTailAt(int x, int y) {
  93.         for (Point point : snakeParts) {
  94.             if (point.equals(new Point(x, y)))
  95.                 return false;
  96.         }
  97.         return true;
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement