Create A Snake Game With Java in Hindi
Create A Snake Game With Java in Hindi
with Java
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Prerequisites
● Basic Of Java
● Processing IDE
● ArrayList & Its Methods
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Creating our Window
void setup() // for defining initial properties
{
size(500,500);
background(0);
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Creating our Snake
ArrayList<Integer> x_pos = new ArrayList<Integer>();
ArrayList<Integer> y_pos = new ArrayList<Integer>();
int hgt=24,wdt=24; // window
int block = 20;
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
Moving our Snake
int dir=2; //0-down , 1-up, 2-right, 3-left
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
Controlling 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) else if(keyCode==UP)
new_dir=1; new_dir=0;
else if(keyCode==LEFT) else if(keyCode==RIGHT)
new_dir=2; new_dir=3;
else
new_dir =-1;
if(new_dir!=-1)
dir=new_dir;
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Creating Food
Creating food for Snake:
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
Making Snake Eat Food
Making our snake eat food and creating random positions:
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Displaying Score
textAlign(LEFT); //alignment
textSize(25); // font
fill(255);
text("Score:" + x_pos.size(),0,20); // (character, x-cord,y-cord)
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Increasing Speed
int speed =10;
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Dying Conditions
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
Displaying Final 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