Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GameMap {
- /* Item's ID */
- private final int snakeID = 0,
- mapID = 1,
- borderID = 2,
- appleID = 3;
- /* Game Map */
- private int[][] GameMap;
- /* Snake Object */
- public Snake object;
- /* Apple Pos */
- private int[] applePosition = new int[2];
- public GameMap() {
- }
- /* Map Dimension: 7x7 */
- public void createMap(){
- int[][] map = {
- {2, 2, 2, 2, 2, 2, 2},
- {2, 1, 1, 1, 1, 1, 2},
- {2, 1, 1, 1, 1, 1, 2},
- {2, 1, 1, 1, 1, 1, 2},
- {2, 1, 1, 1, 1, 1, 2},
- {2, 1, 1, 1, 1, 1, 2},
- {2, 2, 2, 2, 2, 2, 2}
- };
- this.GameMap = map;
- }
- public boolean setMap(int[] pos, int[] newPos){
- // Remove a old snake pos.
- this.GameMap[pos[0]][pos[1]] = this.mapID;
- // Set a new snake position.
- if (this.GameMap[newPos[0]][newPos[1]] == this.borderID){ // if occur colision then..
- return true;
- } else if (this.GameMap[newPos[0]][newPos[1]] == this.appleID) { // if eat apple then..
- object.setSize();
- createApple();
- this.GameMap[newPos[0]][newPos[1]] = this.snakeID; // new pos.
- } else {
- this.GameMap[newPos[0]][newPos[1]] = this.snakeID; // new pos.
- }
- return false;
- }
- /* Create Snake on the map */
- public void createSnake(){
- int row = (int) Math.random() * this.GameMap.length,
- column = (int) Math.random() * this.GameMap.length;
- if(this.GameMap[row][column] == 1){
- this.GameMap[row][column] = this.snakeID;
- object = new Snake(row, column);
- } else {
- createSnake();
- }
- }
- /* Create Apple on the map */
- public void createApple(){
- int row = (int) Math.random() * this.GameMap.length;
- int column = (int) Math.random() * this.GameMap.length;
- if(this.GameMap[row][column] != 2 && this.GameMap[row][column] != 0){
- this.GameMap[row][column] = this.appleID;
- this.applePosition[0] = row;
- this.applePosition[1] = column;
- } else {
- createApple();
- }
- }
- public int[] getApplePosition(){
- return this.applePosition;
- }
- public int getMapID(int row, int column){
- return this.GameMap[row][column];
- }
- }
- public class Snake {
- private boolean Live;
- private int size; // implements after.
- private int[] position = new int[2];
- public GameMap pos;
- public Snake(int row, int column){
- this.Live = true;
- this.size = 1;
- this.position[0] = row;
- this.position[1] = column;
- }
- public int[] getPosition() {
- return this.position;
- }
- public void setPosition(int[] newPos){
- pos = new GameMap();
- boolean check = pos.setMap(this.position, newPos);
- if(check == true){
- this.Live = false;
- } else {
- this.position = newPos;
- }
- }
- public void setSize(){
- this.size += 1;
- }
- public boolean isLive() {
- return this.Live;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement