Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int snakeLength = 2, rectWidth = 20, generatePickupFrequency = 120, snakeSpeed=30,snakeSpeedJump=5;
- int startPosX, startPosY;
- int speedX, speedY;
- int pickupPosX, pickupPosY;
- boolean pickedBlock = false;
- int lastLeftPosX, lastLeftPosY; // Use these to remember which cell you where last on to re-use it to grow
- int []snakeRectPosXArr = new int[2];
- int []snakeRectPosYArr = new int[2];
- void setup()
- {
- size(400, 400);
- background(0);
- startPosX = width/2;
- startPosY = height/2;
- snakeRectPosXArr[0]=startPosX;
- snakeRectPosYArr[0]=startPosY;
- snakeRectPosXArr[1]=startPosX;
- snakeRectPosYArr[1]=startPosY+rectWidth;
- speedY = -1; // start with the snake moving upwards
- speedX = 0;
- generatePickupPos();
- }
- void draw()
- {
- clear();
- background(0);
- // ReDraw pickup every 2 seconds
- if ( frameCount % generatePickupFrequency == 0 ) {
- generatePickupPos();
- }
- drawPickup();
- drawSnake();
- if ( checkIfLost() ) {
- // LOST !!
- noLoop();
- background(255, 0, 0);
- textSize(32);
- fill(255);
- text("LOST! - SCORE: "+ (snakeRectPosYArr.length - 2), 60, height/2);
- }
- // Move snake every x seconds depending on length of snake
- // The longer the snake, the faster moveSnake will be called
- if ( frameCount % floor(snakeSpeed/(floor((snakeRectPosYArr.length - 2)/snakeSpeedJump)+1)) == 0 ) {
- moveSnake();
- }
- }
- void drawSnake() {
- fill(255); // Fill the snake with white
- for (int i = 0; i < snakeRectPosXArr.length; i++) {
- rect(snakeRectPosXArr[i], snakeRectPosYArr[i], rectWidth, rectWidth);
- }
- // Draw a dot in the first element (head) with a slightly different color
- fill(0, 0, 255); // Blue for the head
- ellipse(snakeRectPosXArr[0]+rectWidth/2, snakeRectPosYArr[0]+rectWidth/2, rectWidth/2, rectWidth/2);
- }
- void moveSnake() {
- // Shift the array backwards, so the new position
- // can be added to the array on index position 0
- // Remember which cell you where last on to re-use it to grow in case you eat a pickup
- lastLeftPosX = snakeRectPosXArr[snakeRectPosXArr.length-1];
- lastLeftPosY = snakeRectPosYArr[snakeRectPosYArr.length-1];
- // Shift cells
- for (int i = snakeRectPosXArr.length-1; i > 0; i--) {
- snakeRectPosXArr[i] = snakeRectPosXArr[i-1];
- snakeRectPosYArr[i] = snakeRectPosYArr[i-1];
- }
- // Add new position as per pressed keys
- snakeRectPosXArr[0] = snakeRectPosXArr[1]+(speedX*rectWidth);
- snakeRectPosYArr[0] = snakeRectPosYArr[1]+(speedY*rectWidth);
- // Check if you hit the Pickup
- if (pickupPosX == snakeRectPosXArr[0] && pickupPosY == snakeRectPosYArr[0] ) {
- // You hit the pickup
- addPosition();
- }
- }
- void generatePickupPos() {
- boolean spotTaken = true;
- // Avoid adding a Pickup on a location used by the snake
- while (spotTaken) {
- // generate a random number that is a multiple of rectWidth
- pickupPosX = (int) random(width/rectWidth) * rectWidth ; // random between 0 and 19
- pickupPosY = (int) random(height/rectWidth) * rectWidth ;
- // Check if that point is not already taken by the snake
- for (int i = 0; i < snakeRectPosXArr.length; i++) {
- if (pickupPosX != snakeRectPosXArr[i] && pickupPosY != snakeRectPosYArr[i]) {
- spotTaken = false; // free spot found! We can stop here.
- }
- }
- }
- ;
- }
- void drawPickup() {
- fill(0, 255, 0); // Fill the pickup with green
- rect(pickupPosX, pickupPosY, rectWidth, rectWidth);
- }
- void addPosition() {
- // Use the cell you left last to grow
- // as this spot is free and within the screen borders
- snakeRectPosXArr = append(snakeRectPosXArr, lastLeftPosX);
- snakeRectPosYArr = append(snakeRectPosYArr, lastLeftPosY);
- }
- boolean checkIfLost() {
- // Check if the first element of the snake hits the walls or itself
- // Check for walls
- if ( snakeRectPosXArr[0] >= width ||
- snakeRectPosXArr[0] < 0 ||
- snakeRectPosYArr[0] >= height ||
- snakeRectPosYArr[0] < 0 ) {
- return true;
- }
- // Check for the snake itself by checking
- // if the X Y position of one of the elements matches the first element
- for (int i = 1; i < snakeRectPosXArr.length; i++) {
- if ( snakeRectPosXArr[0] == snakeRectPosXArr[i] && snakeRectPosYArr[0] == snakeRectPosYArr[i]) {
- return true;
- }
- }
- // You are not lost if you got this far
- return false;
- }
- void keyPressed() { // Controls new Snake direction
- if (key==CODED) {
- // Note: Ignore accidental direction changes of 180 degrees or you would be eating yourself immediately
- if (keyCode==RIGHT) {
- if ( speedX == -1 && speedY == 0 ) return ; // ignore if we try to turn 180 degrees
- speedX=1;
- speedY=0;
- }
- if (keyCode==LEFT) {
- if ( speedX == 1 && speedY == 0 ) return ; // ignore if we try to turn 180 degrees
- speedX=-1;
- speedY=0;
- }
- if (keyCode==UP) {
- if ( speedX == 0 && speedY == 1 ) return ; // ignore if we try to turn 180 degrees
- speedX=0;
- speedY=-1;
- }
- if (keyCode==DOWN) {
- if ( speedX == 0 && speedY == -1 ) return ; // ignore if we try to turn 180 degrees
- speedX=0;
- speedY=1;
- }
- moveSnake();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement