Please enable JavaScript.
Coggle requires JavaScript to display documents.
Make a snake game (Algorithm (void draw(){ (if (frameCount%5==0) { //…
Make a snake game
Algorithm
ArrayList<Integer> x = new ArrayList<Integer>();
ArrayList<Integer> y = new ArrayList<Integer>();
int w = 30; //witdh
int h = 30; // height
int bs = 20; // block size
int dir = 2; // direction
int[] dx = {0,0,1,-1}, dy = {1,-1,0,0}; //easier way to show short arrays //dx shows left and right, while dy shows up and down on the rect.
int applex = 12, appley = 10; // coordinates of apple(x,y)
int applecolorx = (int)random(255);
int applecolory = (int)random(255);
int applecolorz = (int)random(255);
boolean gameover = false;
void setup(){
size(600,600);
// size(w
bs, h
bs);
x.add(5); // starting pos of arraylist x;
y.add(5); // starting pos of arraylist y;
}
void draw(){
background(255);
for(int i = 0; i < w; i++) line(i
bs, 0, i
bs, height);
for(int i = 0; i < h; i++) line(0, i
bs, width, i
bs);
for(int i = 0; i < x.size(); i++){ // x.size is reffering to the arraylist of name x;
fill(0, 255, 0); //makes the rectangle(s) green;
rect(x.get(i)
bs,y.get(i)
bs,bs,bs); // .get is reffering to i value of the arraylist;
}
if(!gameover){ // run all this if the game is not over
fill(applecolorx,applecolory,applecolorz); // makes the apple(s) a random color
rect(applex
bs, appley
bs, bs, bs); // creates the apple using the apple coordinates and the block size
if (frameCount%5==0) { // every 5 frames
x.add(0, x.get(0) + dx[dir]); //adds an additional tile in the direction you are going
y.add(0, y.get(0) + dy[dir]);
if( x.get(0) < 0 || y.get(0) < 0 || x.get(0) >= w || y.get(0) >= h){ // if you pass the border (edge of screen)
gameover = true; // then the game is over
}
for(int i =1; i < x.size(); i++){
if(x.get(0)==x.get(i) && y.get(0) == y.get(i)){ // if you hit one of the rectangles that belongs to your snake(in the arraylist) the game ends
gameover = true;
}
}
if(x.get(0) == applex && y.get(0) == appley){ // if the snake touches the apple
applex = (int)random(0,w); // makes random new coordinates for new apple
appley = (int)random(0,h);
applecolorx = (int)random(255);
applecolory = (int)random(255); // changes the color of the apple every time a new one spawns
applecolorz = (int)random(255);
}else{// removes the last tile in your snake so it looks as if the snake was moving and doesnt if you ate an apple therefore adding an extra tile to the snake
x.remove(x.size() -1);
y.remove(y.size() -1);
}
}
}else{ // if the game is over
fill(0);
textSize(30);
textAlign(CENTER); // puts text in the middle of the screen
text ("GAME OVER, PRESS SPACE TO RESTART", width/2, height/2); // text large on screen saying game over and space to restart
if(keyPressed && key==' '){ // if spacebar is presses
x.clear(); // clear all arraylists
y.clear();
x.add(5); // add default snake back in
y.add(5);
gameover = false; // make the game not over anymore
}
}
}
void keyPressed(){
int newdir = key=='s' ? 0 : (key=='w' ? 1 : (key=='d' ? 2 : (key=='a' ? 3 : -1)) ); // ? and : work like if and else statements
if(newdir != -1 && (x.size() <= 1 || !(x.get(1) ==x.get(0) + dx[newdir] && y.get (1) == y.get(0) + dy[newdir]) )) dir = newdir; // as long as its not -1 (which means any other key other than wasd) the new direction will decome the normal direction
// as long as its not -1 (which means any other key other than wasd) the new direction will decome the normal direction
// and the long part is saying that you cannot go into a direction where your most current block will hit your 2nd most current block
}
Decomposition
Make a grid on the screen using for loops and the line property
Create a system that adds and deletes blocks every 5 frames
Use arraylists to store every block the snake occupies
for each block added to the snake, delete 1 block
When you get an apple, the remove function does not go off once, adding an extra block to the snake
Create a system that generates apples in random location that changes every time the snake eats one ( and color because why not)
make a system(boolean) that makes the game end
Make something happen when the game ends
Put text saying you lost
Put something that makes it so when the player presses a specific key, the game restarts (the boolean becomes false)
Make it so that when you restart, the arraylists restart
Make it so when you restart, the default snake reloads
Fill the screen so that just the grid and text appears
Add things that trigger the boolean of the game ending
make it so when you hit the border the boolean becomes trur
make it so that when you hit one of the values in the arraylist (one of the blocks of the snakes body) the boolean becomes true
Patterns
The function for adding a block to the arraylist
x.add(0, x.get(0) + dx[dir]);
y.add(0, y.get(0) + dy[dir]);
The function for seeing if your snake has passed the border
if( x.get(0) < 0 || y.get(0) < 0 || x.get(0) >= w || y.get(0) >= h){
The function for seeing if your snake (arraylist) hit a previous part of its body (arraylist value)
if(x.get(0)==x.get(i) && y.get(0) == y.get(i)){
Abstraction
The color of the apple after every respawn
int applecolorx = (int)random(255);
int applecolory = (int)random(255);
int applecolorz = (int)random(255);
The amount of tiles in the snake, and the location of the snake
The addition of tiles
x.add(0, x.get(0) + dx[dir]);
y.add(0, y.get(0) + dy[dir]);
The removal of tiles
x.remove(x.size() -1);
y.remove(y.size() -1);
The boolean that determines if the game is over or not