Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. package snake;
  2.  
  3. import java.applet.Applet;
  4. import java.util.ArrayList;
  5. import java.util.Random;
  6. import java.util.concurrent.ExecutorService;
  7. import java.util.concurrent.Future;
  8.  
  9. /**
  10.  *
  11.  * @author laudeleo
  12.  */
  13. public class Snake {
  14.  
  15.     int x, y, bredd, hojd;
  16.     int speed;
  17.     char direction;
  18.     Applet a;
  19.     Future trad;
  20.     int respawn;
  21.     ArrayList<Link> linkl = new ArrayList<Link>();
  22.     //ArrayList<Snake> enemysnakes;
  23.     ArrayList<Food> food;
  24.     Snake mysnake;
  25.     Random r = new Random();
  26.     boolean dead, alive;
  27.  
  28.     Snake(int x, int y, int bredd, int hojd, Applet a, ArrayList<Food> food, ExecutorService tg, Snake mysnake) {
  29.  
  30.         linkl.add(new Link(x, y, bredd, hojd));
  31.         //Link link = new Link(x, y);
  32.         //linkl.add(link);
  33.         this.x = x;
  34.         this.y = y;
  35.         this.bredd = bredd;
  36.         this.hojd = hojd;
  37.         this.a = a;
  38.         this.food = food;
  39.         this.mysnake = mysnake;
  40.  
  41.     }
  42.  
  43.     boolean snakeCrash() {
  44.  
  45.         if (linkl.get(0).x < 10 | linkl.get(0).x > 564 | linkl.get(0).y < 10 | linkl.get(0).y > 564) {
  46.             linkl.clear();
  47.             return true;
  48.         } else {
  49.             return false;
  50.         }
  51.     }
  52.  
  53.     public void snakeRespawn() {
  54.  
  55.         if (linkl.isEmpty()) {
  56.             linkl.add(new Link(r.nextInt(50) * 10, r.nextInt(50) * 10, 10, 10));
  57.         }
  58.     }
  59.  
  60.     public void snakeMovements() {
  61.  
  62.         this.x = linkl.get(0).x;
  63.         this.y = linkl.get(0).y;
  64.  
  65.         if (direction == 'r') {
  66.             linkl.get(0).x += 10;
  67.         } else if (direction == 'l') {
  68.             linkl.get(0).x -= 10;
  69.         } else if (direction == 'u') {
  70.             linkl.get(0).y -= 10;
  71.         } else if (direction == 'd') {
  72.             linkl.get(0).y += 10;
  73.         }
  74.         if (linkl.size() > 1) {
  75.             for (int i = linkl.size() - 1; i > 1; i--) {
  76.                 linkl.get(i).x = linkl.get(i - 1).x;
  77.                 linkl.get(i).y = linkl.get(i - 1).y;
  78.  
  79.             }
  80.             linkl.get(1).x = this.x;
  81.             linkl.get(1).y = this.y;
  82.         }
  83.     }
  84.  
  85.     public void snakeEat() {
  86.         for (int i = 0; i < food.size(); i++) {
  87.             if (food.get(i).x == linkl.get(0).x && food.get(i).y == linkl.get(0).y) {
  88.                 food.remove(i);
  89.                 linkl.add(new Link(linkl.get(linkl.size() - 1).x, linkl.get(linkl.size() - 1).y, linkl.get(linkl.size() - 1).bredd, linkl.get(linkl.size() - 1).hojd));
  90.                 break;
  91.             }
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement