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;
- int growAmount;
- 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();
- }
- /*** setOtherSnake ************************************
- * Purpose: set a variable for other snakengs *
- * Parameters: s - other snake *
- * Returns: none *
- ******************************************************/
- void setOtherSnake(Snake s)
- {
- otherSnake = s;
- }
- /*** initSnake *********************************
- * Purpose: begin the movement of the snakes *
- * Parameters: none *
- * Returns: none *
- ******************************************************/
- 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 (p.ghost() == false)
- {
- //collides with all
- 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
- gameOver = true;
- p.m = -1;
- return true;
- }
- }
- else
- {
- //only collides with walls
- if(
- head.x + _x >=40 ||
- head.x + _x <=0 ||
- head.y + _y >=40 ||
- head.y + _y <=0
- )
- {
- //Collision
- gameOver = true;
- p.m = -1;
- return true;
- }
- }
- if (apple.getApple(head.x + _x, head.y + _y)) growCounter = growAmount;
- if (p.getPowerup(head.x + _x, head.y + _y))
- {
- p.movePowerup();
- }
- this.speed = p.haste();
- this.growAmount = p.doubleLength();
- 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;
- }
- /*** moveTail *********************************
- * Purpose: eliminate any trail left by snake *
- * Parameters: none *
- * Returns: none *
- ******************************************************/
- 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];
- }
- /*** drawSnake **********************************
- * Purpose: determines and moves snake in direction *
- * Parameters: none *
- * Returns: true,false *
- ******************************************************/
- 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()
- {
- if(p.dizzy() == false)
- {
- if (gameOver == false)
- {
- //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";
- }
- }
- }
- //INVERT CONTROLS
- else
- {
- if (gameOver == false)
- {
- //UP
- if (
- (keyCode == UP) && controls == "arrows" ||
- (key == 'w' || key == 'W') && controls == "wasd"
- )
- {
- if (snakeDirection == "up" || snakeDirection == "down") return;
- snakeDirection = "down";
- }
- //DOWN
- else if (
- (keyCode == DOWN) && controls == "arrows" ||
- (key == 's' || key == 'S') && controls == "wasd"
- )
- {
- if (snakeDirection == "up" || snakeDirection == "down") return;
- snakeDirection = "up";
- }
- //RIGHT
- else if (
- (keyCode == RIGHT) && controls == "arrows" ||
- (key == 'd' || key == 'D') && controls == "wasd"
- )
- {
- if (snakeDirection == "right" || snakeDirection == "left") return;
- snakeDirection = "left";
- }
- //LEFT
- else if (
- (keyCode == LEFT) && controls == "arrows" ||
- (key == 'a' || key == 'A') && controls == "wasd"
- )
- {
- if (snakeDirection == "right" || snakeDirection == "left") return;
- snakeDirection = "right";
- }
- }
- }
- }
- }
- //new code
- class computerSnake
- {
- String wishDir;
- int wishX;
- int wishY;
- int currentX;
- int currentY;
- int appleX;
- int appleY;
- boolean satisfiedVertical;
- boolean satisfiedHorizontal;
- void getAppleLocation()
- {
- wishX = apple.x;
- wishY = apple.y;
- }
- void decideVerticalDirection()
- {
- if (satisfiedVertical == false)
- {
- if (currentY > wishY)
- {
- wishDir = "up";
- }
- if (currentY < wishY)
- {
- wishDir = "down";
- }
- }
- }
- void decideHorizontalDirection()
- {
- if(satisfiedHorizontal == false)
- {
- if (currentX > wishX)
- {
- wishDir = "left";
- }
- if (currentX < wishX)
- {
- wishDir = "right";
- }
- }
- }
- void decideDirection()
- {
- if (currentY == wishY)
- {
- satisfiedVertical = true;
- }
- if (currentX == wishX)
- {
- satisfiedHorizontal = true;
- }
- }
- }
- //end new code
- 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;
- file3.play();
- moveApple();
- return true;
- }
- return false;
- }
- boolean isApple(int _x, int _y)
- {
- if ( x == _x && y == _y)
- {
- return true;
- }
- return false;
- }
- }
- class Renderer
- {
- /*** drawBoardSingle *********************************
- * Purpose: create gameboard for singleplayer *
- * Parameters: none *
- * Returns: none *
- ******************************************************/
- 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 if (p.isPowerup(i,j)) colour = p.getColour();
- else colour = backgroundColour();
- fill(colour);
- stroke(255,255,255,80);
- rect(i*20, j*20, 20, 20);
- }
- }
- if(frameCount % 60 == 0) p.m--;
- if(frameCount % 60 == 0) p.countDown--;
- }
- /*** drawBoardMulti *********************************
- * Purpose: create gameboard for multiplayer *
- * Parameters: none *
- * Returns: none *
- ******************************************************/
- 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 if (p.isPowerup(i,j)) colour = p.getColour();
- else colour = backgroundColour();
- fill(colour);
- stroke(255,255,255,80);
- rect(i*20, j*20, 20, 20);
- }
- }
- if(frameCount % 60 == 0) p.m--;
- if(frameCount % 60 == 0) p.countDown--;
- }
- /*** drawBoardCollision ******************************
- * Purpose: draw board when snakes die *
- * Parameters: none *
- * Returns: none *
- ******************************************************/
- void drawCollision()
- {
- for(int i = 0; i < 41; i++)
- {
- color colour = #ff0000;
- noFill();
- noStroke();
- 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);
- }
- }
- }
- class Powerup
- {
- int x;
- int y;
- int m = -1;
- long countDown = 0;
- color powerupColour;
- boolean isAvailable = true;
- String type = "none";
- int choose;
- int haste()
- {
- if (choose == 1)
- {
- if (m<0)
- return 6;
- else
- return 4;
- }
- else if (choose == 4)
- {
- if (m<0)
- return 6;
- else
- return 12;
- }
- return 6;
- }
- int doubleLength()
- {
- if (choose == 2)
- {
- if (m<0)
- return 3;
- else
- return 6;
- }
- return 3;
- }
- boolean ghost()
- {
- if (choose == 3)
- {
- if (m<0)
- return false;
- else
- return true;
- }
- else return false;
- }
- boolean dizzy()
- {
- if (choose == 4)
- {
- if (m<0)
- return false;
- else
- return true;
- }
- else return false;
- }
- void choosePowerup()
- {
- choose = (int)random(1,5);
- if (choose == 1) type = "haste";
- if (choose == 2) type = "doubleLength";
- if (choose == 3) type = "ghost";
- if (choose == 4) type = "dizzy";
- }
- Powerup()
- {
- movePowerup();
- }
- color getColour()
- {
- if (isAvailable) return #00AA00;
- else return backgroundColour();
- }
- void movePowerup()
- {
- choosePowerup();
- x = (int)random(1,40);
- y = (int)random(1,40);
- }
- boolean getPowerup(int _x, int _y)
- {
- countDown--;
- if (countDown <= 0)
- {
- isAvailable = true;
- if ( x == _x && y == _y)
- {
- movePowerup();
- countDown = 130;
- isAvailable = false;
- m = 5;
- return true;
- }
- }
- return false;
- }
- boolean isPowerup(int _x, int _y)
- {
- if ( x == _x && y == _y)
- {
- return true;
- }
- return false;
- }
- }
- //make a new snake
- Apple apple = new Apple();
- Renderer renderer = new Renderer();
- boolean gameOver;
- boolean restart;
- Snake s1;
- Snake s2;
- Powerup p = new Powerup();
- int mode;
- /*** chooseGameMode *********************************
- * Purpose: set the gamemode to be single/multiplayer *
- * Parameters: select - selects gamemode *
- * Returns: none *
- ******************************************************/
- 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