Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. package com.javarush.games.snake;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.javarush.engine.cell.*;
  5. import java.util.*;
  6.  
  7.  
  8. public class Snake {
  9.    
  10.     private List<GameObject> snakeParts =  new ArrayList<>();
  11.     private static final String HEAD_SIGN = "\uD83D\uDC7E";
  12.     private static final String BODY_SIGN = "\u26AB";
  13.     public boolean isAlive = true;
  14.    
  15.    
  16.    
  17.    
  18.     public Snake(int x, int y){
  19.         GameObject g1 = new GameObject(x,y);
  20.         GameObject g2 = new GameObject(x + 1,y);
  21.         GameObject g3 = new GameObject(x + 2,y);
  22.        
  23.         snakeParts.add(g1);
  24.         snakeParts.add(g2);
  25.         snakeParts.add(g3);
  26.     }
  27.    
  28.     public void draw(Game game) {
  29.  
  30.         for (int i = 0; i < snakeParts.size(); i++) {
  31.             if (i == 0)
  32.                 game.setCellValueEx(
  33.                         snakeParts.get(i).x,
  34.                         snakeParts.get(i).y,
  35.                         Color.NONE,
  36.                         HEAD_SIGN,
  37.                         (isAlive == true) ? Color.BLACK : Color.RED,
  38.                         75);
  39.             else
  40.                 game.setCellValueEx(
  41.                         snakeParts.get(i).x,
  42.                         snakeParts.get(i).y,
  43.                         Color.NONE,
  44.                         BODY_SIGN,
  45.                         (isAlive == true) ? Color.BLACK : Color.RED,
  46.                         75);
  47.         }
  48.     }
  49.  
  50.     private Direction direction = Direction.LEFT;
  51.    
  52.     public void setDirection(Direction direction) {
  53.         switch (this.direction) {
  54.             case LEFT:
  55.             case RIGHT:
  56.                 if (snakeParts.get(0).x == snakeParts.get(1).x) return;
  57.                 break;
  58.             case UP:
  59.             case DOWN:
  60.                 if (snakeParts.get(0).y == snakeParts.get(1).y) return;
  61.                 break;
  62.         }
  63.         this.direction = direction;
  64.     }
  65.        
  66.    
  67.     boolean flag = false;
  68.    
  69.     public void move(Apple apple) {
  70.         GameObject head = createNewHead();
  71.         if (checkCollision(head) == true) {
  72.             isAlive = false;
  73.             return;
  74.         }
  75.        
  76.         if(head.x > SnakeGame.WIDTH -1 || head.y > SnakeGame.HEIGHT -1
  77.            || head.x < 0 || head.y < 0){
  78.                  isAlive = false;
  79.             }
  80.         else if(head.x == apple.x && head.y == apple.y){
  81.             apple.isAlive = false;
  82.             snakeParts.add(0, head);
  83.         }
  84.         else {
  85.              snakeParts.add(0, head);
  86.              removeTail();
  87.         }
  88.        
  89.  
  90.     }
  91.  
  92.     public GameObject createNewHead() {
  93.     GameObject objectnew = null;
  94.  
  95.         if (direction == Direction.UP) {
  96.              objectnew = new GameObject(snakeParts.get(0).x, snakeParts.get(0).y - 1);
  97.              
  98.         } else if (direction == Direction.DOWN) {
  99.              objectnew = new GameObject(snakeParts.get(0).x, snakeParts.get(0).y + 1);
  100.              
  101.         } else if (direction == Direction.RIGHT) {
  102.              objectnew = new GameObject(snakeParts.get(0).x + 1, snakeParts.get(0).y);
  103.              
  104.         } else if (direction == Direction.LEFT) {
  105.              objectnew = new GameObject(snakeParts.get(0).x - 1, snakeParts.get(0).y);
  106.              
  107.         }
  108.          return objectnew;
  109.     }
  110.     public void removeTail(){
  111.         snakeParts.remove(snakeParts.size() - 1);
  112.     }
  113.     public boolean checkCollision(GameObject object){
  114.         for(int i = 0; i < snakeParts.size(); i++){
  115.             if(snakeParts.get(i).x == object.x && snakeParts.get(i).y == object.y){
  116.                 return true;
  117.             }
  118.         }
  119.         return false;
  120.     }
  121.    
  122.     public int getLength(){
  123.         return snakeParts.size();
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement