//Hungry Square //By Alexander Massart ///////////////////////// //P.S everything is called snake because i was going to use arrayLists to make a snake, changed to a square later ///Define any global variables int score; String snakeDir; int sideLen; float snakeX, snakeY, snakeRed, snakeGreen, snakeBlue; float foodX, foodY, foodRed, foodGreen, foodBlue; int timer; int fade; float foodAmount; //Only define at top to allow for reset button DONT SET void setup() { //Setup all the variables values and the size of the screen //GIVE VARIABLES VALUES size(400, 400); frameRate(14); rectMode(CENTER); textAlign(CENTER, CENTER); score = 0; sideLen = 20; snakeSetup(); foodReset(); //7 real seconds for the timer not 7 frames timer = int(frameRate * 7); fade = 255; foodAmount = 100; print("WASD to move, Run into the Circles, Dont let your hunger bar empty\n"); } void draw() { rectMode(CENTER); background(255-foodRed, 255-foodGreen, 255-foodBlue); noStroke(); //draw persistent things drawGrid(); drawScenery(); drawFoodBar(); drawScore(); //setup if statment to manage game over if (foodAmount > 0) { //Calls the drawing functions that wont draw after game over rectMode(CENTER); foodDisplay(); snakeDisplay(); //Call movement snakeMove(); //Check collision checkHit(); //Timer to manage drawing the title if (timer > 0) { drawIntro(); } } else { //Gameover gameOver(); } } void keyPressed() { //using the char for both the capital and lowercase letters i can check if the user pressed W A S D //inside the if statements set the direction of the movement if (key == 'w' || key == 'W') { snakeDir = "up"; } if (key == 's' || key == 'S') { snakeDir = "down"; } if (key == 'a' || key == 'A') { snakeDir = "left"; } if (key == 'd' || key == 'D') { snakeDir = "right"; } if (foodAmount <= 0 && (key == 'r' || key == 'R')) { //run setup to reset the playstate setup(); } } void drawGrid() { //Draw grid pattern for (int i = 0; i < 19; i ++) { //loop twice to move horizantal and vertical for (int k = 0; k < 19; k ++) { //Use foodRed, Green, Blue so that it can change fill(foodRed, foodGreen, foodBlue); //Offset the position based on i and k and 50 rect(i * 25, k * 50 + (i%2) * 25, 25, 25); } } } void drawScore() { rectMode(CENTER); fill(255); textSize(25); //draw score text, add an int to a string makes it into a string, YAY text("Score = " + score, width - 90, height - 35); rectMode(CORNER); } void gameOver() { //What draws when there is a game over fill(0, 0, 0); textSize(41); text( "Game Over", width/2, 30); fill(255, 255, 255); textSize(40); text( "Game Over", width/2, 30); } void drawIntro() { //drawing the title and setting up the fade effect fill(0, 0, 0, fade); textSize(41); text( "Hungry Square", width/2, 30); fill(0, 0, 0, fade); textSize(20.5); text( "By: Alexander Massart", width/2, 60); fill(255, 255, 255, fade); textSize(40); text( "Hungry Square", width/2, 30); fill(255, 255, 255, fade); textSize(20); text( "By: Alexander Massart", width/2, 60); timer --; fade -= 10; } void drawScenery() { //draw the carpets rectMode(CORNER); fill(124, 2, 152); rect(0, 0, 75, height); rect(width-75, 0, 75, height); rect(0, 0, width, 75); rect(0, height - 75, width, 75); fill(142, 0, 175); rect(13, 20, 50, height - 75); rect(width-63, 20, 50, height-75); rect(13, 13, width - 26, 50); rect(13, height - 63, width - 26, 50); } void drawFoodBar() { //draw the food bar that changes based on foodAmount fill(255); textSize(20); text("Hunger Meter", 70, height - 40); fill(203, 165, 66); rectMode(CORNER); rect(0, height - 15, foodAmount, 15); foodAmount --; } void snakeSetup() { //sets the variables for the square/snake snakeDir = "right"; snakeX = width/2; snakeY = height/2; snakeRed = random(256); snakeGreen = random(256); snakeBlue = random(256); } void snakeDisplay() { //draws the square based on position stroke(0); strokeWeight(1); fill(snakeRed, snakeGreen, snakeBlue); rect(snakeX, snakeY, sideLen, sideLen); } void snakeMove() { //The actual movement of the square based upon the directions set in keyPressed if (snakeDir == "left") { snakeX -= sideLen; } if (snakeDir == "right") { snakeX += sideLen; } if (snakeDir == "up") { snakeY -= sideLen; } if (snakeDir == "down") { snakeY += sideLen; } //Looping code //By adding the width/height and getting the remainder from moding the value by width/height you guarentee //that the square is in the screen snakeX = (snakeX + width) % width; snakeY = (snakeY + height) % height; } void checkHit() { //check if snake hits food //Distance between food and snake is less than the side length //only works for perfect circles and squares if (dist(foodX, foodY, snakeX, snakeY) < sideLen) { //add to score, food amount and reset the food position foodReset(); foodAmount += 10; score += 1; if (dist(foodX, foodY, snakeX, snakeY) < sideLen + 100) { //reset the position again if the position of the food is too close foodReset(); } } } void foodReset() { //Changes the color and position of the food foodX = random(50, width - 100); foodY = random(50, height - 100); foodRed = random(256); foodGreen = random(256); foodBlue = random(256); } void foodDisplay() { //draws the food stroke(0); strokeWeight(2); fill(foodRed, foodGreen, foodBlue); ellipse(foodX, foodY, sideLen, sideLen); }