NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

float cellSize = 40; // Size of each cell in the grid
float gridSize = 20; // Number of cells in each row and column
float gridOffsetX = 20; // Offset of the grid from the left side
float gridOffsetY = 20; // Offset of the grid from the top side
float gridBottomOffsetY = 20; // Offset of the grid from the bottom side

float snakeX; // X-coordinate of the snake's head
float snakeY; // Y-coordinate of the snake's head
float snakeSpeed = cellSize; // Speed of the snake's movement
float snakeDirectionX = 1; // Initial movement direction of the snake along the x-axis
float snakeDirectionY = 0; // Initial movement direction of the snake along the y-axis

int score = 0; // Player's score
boolean gameOver = false; // Flag to track if the game is over
boolean gameStarted = false; // Flag to track if the game has started
boolean gameWon = false; // Flag to track if the game has been won

ArrayList<PVector> snakeBody; // List to store the coordinates of the snake's body
int appleX; // X-coordinate of the apple
int appleY; // Y-coordinate of the apple

void setup() {
size(800, 800); // Set canvas size to 800x800 pixels
frameRate(5); // Set the frame rate to 5 frames per second
snakeX = gridOffsetX + cellSize * floor(gridSize / 2); // Set the initial x-coordinate of the snake's head
snakeY = gridOffsetY + cellSize * floor(gridSize / 2); // Set the initial y-coordinate of the snake's head

snakeBody = new ArrayList<PVector>(); // Initialize the snake's body
snakeBody.add(new PVector(snakeX, snakeY)); // Add the initial position of the snake's head
score = 0; // Initialize the player's score
}

void draw() {
background(255); // Set background color to white

if (gameStarted) {
drawGrid(); // Call the function to draw the chequered grid

if (!gameOver && !gameWon) {
moveSnake(); // Call the function to move the snake
checkCollisions(); // Check for collisions with boundaries, apples, and self
}

drawSnake(); // Call the function to draw the snake
drawApple(); // Call the function to draw the apple

if (gameOver) {
displayGameOver(); // Display the game over screen
} else if (gameWon) {
displayGameWon(); // Display the game won screen
}

displayScore(); // Display the player's score
} else {
displayWelcomeScreen(); // Display the welcome screen
}
}


void keyPressed() {
if (!gameStarted) {
if (key == CODED && keyCode == SHIFT) { // Start the game if SHIFT key is pressed
startGame();
}
} else {
if (keyCode == UP && snakeDirectionY != 1) { // Avoid the snake reversing its direction instantly
snakeDirectionX = 0;
snakeDirectionY = -1;
} else if (keyCode == DOWN && snakeDirectionY != -1) {
snakeDirectionX = 0;
snakeDirectionY = 1;
} else if (keyCode == LEFT && snakeDirectionX != 1) {
snakeDirectionX = -1;
snakeDirectionY = 0;
} else if (keyCode == RIGHT && snakeDirectionX != -1) {
snakeDirectionX = 1;
snakeDirectionY = 0;
} else if (keyCode == SHIFT && (gameOver || gameWon)) { // Restart the game if SHIFT key is pressed after game over or game won
resetGame();
}
}
}

void moveSnake() {
float newX = snakeBody.get(0).x + snakeDirectionX * snakeSpeed; // Calculate the new x-coordinate of the snake's head
float newY = snakeBody.get(0).y + snakeDirectionY * snakeSpeed; // Calculate the new y-coordinate of the snake's head

// Check if the new position is within the specified boundaries
if (newX >= 20 && newX < 780 && newY >= 60 && newY < 780) {
// Create a new head position
PVector newHead = new PVector(newX, newY);

// Add the new head position to the beginning of the snake's body
snakeBody.add(0, newHead);

// Remove the tail of the snake if it hasn't eaten an apple
if (!hasEatenApple()) {
snakeBody.remove(snakeBody.size() - 1);
}
} else {
gameOver = true; // Set the game over flag to true if the snake moves outside the boundaries
}
}


void drawGrid() {
// Draw the chequered grid
for (int i = 0; i < gridSize; i++) { // Rows
for (int j = 0; j < gridSize; j++) { // Columns
float x = gridOffsetX + j * cellSize; // Calculate x-coordinate of the cell
float y = gridOffsetY + i * cellSize; // Calculate y-coordinate of the cell

if ((i + j) % 2 == 0) {
if (x >= 20 && x < 780 && y >= 60 && y < 780) { // Check if the cell is within the specified boundaries
drawCell(x, y, true); // Even cells have a different color
}
} else {
if (x >= 20 && x < 780 && y >= 60 && y < 780) { // Check if the cell is within the specified boundaries
drawCell(x, y, false); // Odd cells have a different color
}
}
}
}

// Draw the boundaries
stroke(0);
strokeWeight(2);
line(20, 60, 780, 60); // Top boundary
line(780, 60, 780, 780); // Right boundary
line(780, 780, 20, 780); // Bottom boundary
line(20, 780, 20, 60); // Left boundary
}

void drawCell(float x, float y, boolean colour) {
// Draw a single cell
if (colour) {
fill(200); // Set color to gray for even cells
} else {
fill(100); // Set color to dark gray for odd cells
}
stroke(0); // Set border color to black
rect(x, y, cellSize, cellSize); // Draw the cell as a square
}

void drawSnake() {
for (int i = 0; i < snakeBody.size(); i++) {
float x = snakeBody.get(i).x;
float y = snakeBody.get(i).y;

if (i == 0) {
fill(255, 0, 0); // Set color of the snake's head (red)
} else {
fill(0); // Set color of the snake's body (black)
}

rect(x, y, cellSize, cellSize); // Draw each segment of the snake's body
}
}


void placeApple() {
int maxGridIndex = int(gridSize) - 1;

// Generate random coordinates for the apple within the specified boundaries
appleX = int(random(1, maxGridIndex)) * int(cellSize) + int(gridOffsetX);
appleY = int(random(1, maxGridIndex)) * int(cellSize) + int(gridOffsetY);
}

void drawApple() {
fill(255, 0, 0); // Set color of the apple to red
ellipse(appleX + cellSize / 2, appleY + cellSize / 2, cellSize, cellSize); // Draw the apple as a circle
}

boolean hasEatenApple() {
// Check if the snake's head is overlapping with the apple
return snakeBody.get(0).x == appleX && snakeBody.get(0).y == appleY;
}

void checkCollisions() {
// Check if the snake has collided with the boundaries or itself
if (snakeBody.get(0).x < 20 || snakeBody.get(0).x >= 780 ||
snakeBody.get(0).y < 60 || snakeBody.get(0).y >= 780 ||
isCollidingWithSelf()) {
gameOver = true; // Set the game over flag to true
}

if (hasEatenApple()) {
score++; // Increase the player's score
placeApple(); // Place a new apple
if (score == 10) {
gameWon = true; // Set the game won flag to true
}
}
}

boolean isCollidingWithSelf() {
// Check if the snake's head is colliding with any part of its body
for (int i = 1; i < snakeBody.size(); i++) {
if (snakeBody.get(0).x == snakeBody.get(i).x && snakeBody.get(0).y == snakeBody.get(i).y) {
return true;
}
}
return false;
}

void displayWelcomeScreen() {
background(0); // Set background color to black
textAlign(CENTER, CENTER);
fill(255); // Set text color to white
textSize(32);
text("Welcome to Snake Game!", width / 2, height / 2 - 50);
textSize(18);
text("Use arrow keys to move the snake", width / 2, height / 2);
text("Press SHIFT to start the game", width / 2, height / 2 + 50);
}

void displayGameOver() {
fill(0, 200); // Set the background color to transparent black
rect(0, 0, width, height); // Draw a rectangle covering the entire screen

textAlign(CENTER, CENTER);
fill(255); // Set text color to white
textSize(32);
text("Game Over!", width / 2, height / 2 - 50);
textSize(18);
text("Press SHIFT to restart", width / 2, height / 2);
}

void displayGameWon() {
fill(0, 200); // Set the background color to transparent black
rect(0, 0, width, height); // Draw a rectangle covering the entire screen

textAlign(CENTER, CENTER);
fill(255); // Set text color to white
textSize(32);
text("Congratulations!", width / 2, height / 2 - 50);
textSize(18);
text("You won the game!", width / 2, height / 2);
text("Press SHIFT to restart", width / 2, height / 2 + 50);
}

void displayScore() {
textAlign(LEFT, TOP);
fill(0); // Set text color to black
textSize(18);
text("Score: " + score, 20, 20); // Display the score at the top left corner
}

void startGame() {
gameStarted = true; // Set the game started flag to true
score = 0; // Reset the player's score
snakeBody.clear(); // Clear the snake's body
snakeBody.add(new PVector(snakeX, snakeY)); // Add the initial position of the snake's head
placeApple(); // Place the apple
snakeDirectionX = 1; // Reset the movement direction of the snake
snakeDirectionY = 0;
gameOver = false; // Reset the game over flag
gameWon = false; // Reset the game won flag
}

void resetGame() {
gameStarted = false; // Set the game started flag to false
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.