Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Snake
- {
- SnakePoint snakeGrid [][] = new SnakePoint[41][41];
- SnakePoint head;
- SnakePoint tail;
- String snakeDirection;
- //powerups
- int speed;
- int growCounter;
- Snake otherSnake;
- int initialY;
- String controls;
- class SnakePoint
- {
- int x;
- int y;
- String nextPointDirection;
- boolean isSnake;
- SnakePoint(boolean _isSnake, String _nextPointDirection, int _x, int _y)
- {
- x = _x;
- y = _y;
- isSnake = _isSnake;
- nextPointDirection = _nextPointDirection;
- }
- }
- Snake(int _initialY, String _controls)
- {
- growCounter = 0;
- controls = _controls;
- initialY = _initialY;
- otherSnake = null;
- speed = 6;
- growAmount = 3;
- for(int i = 0; i < 41; i++)
- {
- for(int j = 0; j < 41; j++)
- {
- snakeGrid[i][j] = new SnakePoint(false, "", i, j);
- }
- }
- initSnake();
- }
- void setOtherSnake(Snake s)
- {
- otherSnake = s;
- }
- void initSnake()
- {
- snakeGrid[10][initialY].isSnake = true;
- snakeGrid[10][initialY].nextPointDirection = "right";
- snakeGrid[11][initialY].isSnake = true;
- snakeGrid[11][initialY].nextPointDirection = "right";
- snakeGrid[12][initialY].isSnake = true;
- snakeGrid[12][initialY].nextPointDirection = "right";
- head = snakeGrid[12][initialY];
- tail = snakeGrid[10][initialY];
- snakeDirection = "right";
- }
- boolean isSnakeSquare(int x, int y)
- {
- return snakeGrid[x][y].isSnake;
- }
- boolean moveSnake(int _x, int _y)
- {
- boolean _moveTail = true;
- snakeGrid[head.x][head.y].nextPointDirection = snakeDirection;
- if(
- isSnakeSquare(head.x + _x, head.y + _y) ||
- (otherSnake != null && otherSnake.isSnakeSquare(head.x + _x, head.y + _y)) ||
- head.x + _x >=40 ||
- head.x + _x <=0 ||
- head.y + _y >=40 ||
- head.y + _y <=0
- )
- {
- //Collision
- return true;
- }
- if (apple.getApple(head.x + _x, head.y + _y)) growCounter = growAmount;
- snakeGrid[head.x + _x][head.y + _y].isSnake = true;
- head = snakeGrid[head.x + _x][head.y + _y];
- if (growCounter > 0)
- {
- growCounter --;
- }
- else
- {
- moveTail();
- }
- //No Collision
- return false;
- }
- void moveTail()
- {
- tail.isSnake = false;
- if(tail.nextPointDirection == "up") tail = snakeGrid[tail.x][tail.y - 1];
- else if(tail.nextPointDirection == "down") tail = snakeGrid[tail.x][tail.y + 1];
- else if(tail.nextPointDirection == "right") tail = snakeGrid[tail.x + 1][tail.y];
- else if(tail.nextPointDirection == "left") tail = snakeGrid[tail.x - 1][tail.y];
- }
- int countDown;
- boolean drawSnake()
- {
- countDown--;
- if(countDown <= 0)
- {
- keyPressed();
- if(snakeDirection == "up")
- {
- if (moveSnake(0, -1)) return true;
- }
- else if(snakeDirection == "down")
- {
- if (moveSnake(0, 1)) return true;
- }
- else if(snakeDirection == "right")
- {
- if (moveSnake(1, 0)) return true;
- }
- else if(snakeDirection == "left")
- {
- if (moveSnake(-1, 0)) return true;
- }
- countDown = speed;
- }
- return false;
- }
- void keyPressed()
- {
- //UP
- if (
- (keyCode == UP) && controls == "arrows" ||
- (key == 'w' || key == 'W') && controls == "wasd"
- )
- {
- if (snakeDirection == "up" || snakeDirection == "down") return;
- snakeDirection = "up";
- }
- //DOWN
- else if (
- (keyCode == DOWN) && controls == "arrows" ||
- (key == 's' || key == 'S') && controls == "wasd"
- )
- {
- if (snakeDirection == "up" || snakeDirection == "down") return;
- snakeDirection = "down";
- }
- //RIGHT
- else if (
- (keyCode == RIGHT) && controls == "arrows" ||
- (key == 'd' || key == 'D') && controls == "wasd"
- )
- {
- if (snakeDirection == "right" || snakeDirection == "left") return;
- snakeDirection = "right";
- }
- //LEFT
- else if (
- (keyCode == LEFT) && controls == "arrows" ||
- (key == 'a' || key == 'A') && controls == "wasd"
- )
- {
- if (snakeDirection == "right" || snakeDirection == "left") return;
- snakeDirection = "left";
- }
- }
- }
- class Apple
- {
- int x;
- int y;
- Apple()
- {
- moveApple();
- }
- void moveApple()
- {
- x = (int)random(1,40);
- y = (int)random(1,40);
- }
- boolean getApple(int _x, int _y)
- {
- if ( x == _x && y == _y)
- {
- g+=3;
- moveApple();
- return true;
- }
- return false;
- }
- boolean isApple(int _x, int _y)
- {
- if ( x == _x && y == _y)
- {
- return true;
- }
- return false;
- }
- }
- class Renderer
- {
- void drawBoardSingle()
- {
- for(int i = 0; i < 41; i++)
- {
- for(int j = 0; j < 41; j++)
- {
- color colour;
- if (apple.isApple(i,j)) colour = #8B4513;
- else if (s1.isSnakeSquare(i,j)) colour = #00ff00;
- else colour = backgroundColour();
- fill(colour);
- stroke(255,255,255,80);
- rect(i*20, j*20, 20, 20);
- }
- }
- }
- void drawBoardMulti()
- {
- for(int i = 0; i < 41; i++)
- {
- for(int j = 0; j < 41; j++)
- {
- color colour;
- if (apple.isApple(i,j)) colour = #8B4513;
- else if (s1.isSnakeSquare(i,j)) colour = #00ff00;
- else if (s2.isSnakeSquare(i,j)) colour = #ff0000;
- else colour = #5ba4d8;
- fill(colour);
- stroke(255,255,255,80);
- rect(i*20, j*20, 20, 20);
- }
- }
- }
- void drawCollision()
- {
- for(int i = 0; i < 41; i++)
- {
- color colour = #ff0000;
- fill(colour);
- stroke(255,255,255,80);
- rect(i*20, 0*20, 20, 20);
- rect(0*20, i*20, 20, 20);
- rect(i*20, 40*20, 20, 20);
- rect(40*20, i*20, 20, 20);
- }
- }
- }
- //make a new snake
- Apple apple = new Apple();
- Renderer renderer = new Renderer();
- boolean gameOver;
- boolean restart;
- Snake s1;
- Snake s2;
- int mode;
- void chooseGameMode(int select)
- {
- if (select == 1)
- {
- mode = 1;
- s1 = new Snake(10, "arrows");
- println("Single");
- }
- if (select == 2)
- {
- mode = 2;
- s1 = new Snake(10, "arrows");
- s2 = new Snake(20, "wasd");
- s1.setOtherSnake(s2);
- s2.setOtherSnake(s1);
- println("Multi");
- }
- }
- void keyReleased()
- {
- //RESTART
- if (key == 'p' || key == 'P')
- {
- if(restart == false)
- {
- restart = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement