Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package snake;
- import java.applet.Applet;
- import java.util.ArrayList;
- import java.util.Random;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Future;
- /**
- *
- * @author laudeleo
- */
- public class Snake {
- int x, y, bredd, hojd;
- int speed;
- char direction;
- Applet a;
- Future trad;
- int respawn;
- ArrayList<Link> linkl = new ArrayList<Link>();
- //ArrayList<Snake> enemysnakes;
- ArrayList<Food> food;
- Snake mysnake;
- Random r = new Random();
- boolean dead, alive;
- Snake(int x, int y, int bredd, int hojd, Applet a, ArrayList<Food> food, ExecutorService tg, Snake mysnake) {
- linkl.add(new Link(x, y, bredd, hojd));
- //Link link = new Link(x, y);
- //linkl.add(link);
- this.x = x;
- this.y = y;
- this.bredd = bredd;
- this.hojd = hojd;
- this.a = a;
- this.food = food;
- this.mysnake = mysnake;
- }
- boolean snakeCrash() {
- if (linkl.get(0).x < 10 | linkl.get(0).x > 564 | linkl.get(0).y < 10 | linkl.get(0).y > 564) {
- linkl.clear();
- return true;
- } else {
- return false;
- }
- }
- public void snakeRespawn() {
- if (linkl.isEmpty()) {
- linkl.add(new Link(r.nextInt(50) * 10, r.nextInt(50) * 10, 10, 10));
- }
- }
- public void snakeMovements() {
- this.x = linkl.get(0).x;
- this.y = linkl.get(0).y;
- if (direction == 'r') {
- linkl.get(0).x += 10;
- } else if (direction == 'l') {
- linkl.get(0).x -= 10;
- } else if (direction == 'u') {
- linkl.get(0).y -= 10;
- } else if (direction == 'd') {
- linkl.get(0).y += 10;
- }
- if (linkl.size() > 1) {
- for (int i = linkl.size() - 1; i > 1; i--) {
- linkl.get(i).x = linkl.get(i - 1).x;
- linkl.get(i).y = linkl.get(i - 1).y;
- }
- linkl.get(1).x = this.x;
- linkl.get(1).y = this.y;
- }
- }
- public void snakeEat() {
- for (int i = 0; i < food.size(); i++) {
- if (food.get(i).x == linkl.get(0).x && food.get(i).y == linkl.get(0).y) {
- food.remove(i);
- 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));
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement