Snake+Game+using+Java
Snake+Game+using+Java
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Prerequisites
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
How can we create our Window
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
How can we create our Snake
ArrayList<Integer> x_pos = new ArrayList<Integer>();
ArrayList<Integer> y_pos = new ArrayList<Integer>();
void setup(){
x_pos.add(4); // initial position
y_pos.add(15);
}
void draw(){
fill(255);
for(int i=0;i<x_pos.size();i++)
rect(x_pos.get(i)*block, y_pos.get(i)*block , block,block);
}
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
How can we move our Snake
int dir=2; //0 -down , 1-up, 2-left, 3-right
x_pos.remove(x_pos.size()- 1);
y_pos.remove(y_pos.size()- 1);
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
How can we control our Snake
int dir=2; //0 -down , 1-up, 2-right, 3-left
int []x_dir={0,0,1,-1}; // up down left right
int []y_dir={1,-1,0,0};
void keyPressed(){
int new_dir = keyCode;
if(keyCode==DOWN)
new_dir=0;
else if(keyCode==UP)
new_dir=1;
else if(keyCode==LEFT)
new_dir=3;
else if(keyCode==RIGHT)
new_dir=2;
else
new_dir =-1;
if(new_dir!=-1)
dir=new_dir;
}
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
How does our Snake Eat Food
int f_x_pos=15;
int f_y_pos=15;
boolean gamestatus =false;
if (!gamestatus)
{
fill(255);
rect(f_x_pos*block,f_y_pos*block,block,block);
}
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
How does our Snake Eat Food
2. Now making our snake eat food and creating random positions for next
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
How to display Score
textAlign(LEFT); //alignment
textSize(25); // position
fill(255);
text("Score:" + x_pos.size(),0,20);
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
How can we increase our Snake’s Speed
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
When does our Snake die
1. When our snake touches the border:
for(int i=1;i<x_pos.size();i++)
{
if(x_pos.get(0)==x_pos.get(i) && y_pos.get(0)==y_pos.get(i))
gamestatus=true;
}
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Display the score
fill(222, 9, 12);
textAlign(CENTER);
textSize(30);
text("Game Over \n Score: " + x_pos.size() + "\n Press Enter", 500/2,500/2);
if(keyCode ==ENTER)
{
x_pos.clear();
y_pos.clear();
x_pos.add(4);
y_pos.add(15);
dir =2;
speed=10;
gamestatus = false;
}
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Thank You
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.