0% found this document useful (0 votes)
111 views

Create A Snake Game With Java in Hindi

The document provides instructions for creating a snake game in Java using Processing IDE. It discusses setting up the window, creating the snake as an ArrayList, moving and controlling the snake, creating and consuming food, increasing speed, and game over conditions.

Uploaded by

THE SUPREME
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views

Create A Snake Game With Java in Hindi

The document provides instructions for creating a snake game in Java using Processing IDE. It discusses setting up the window, creating the snake as an ArrayList, moving and controlling the snake, creating and consuming food, increasing speed, and game over conditions.

Uploaded by

THE SUPREME
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Create a Snake Game

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

int []x_dir={0,0,1,-1}; // up down left right


int []y_dir={1,-1,0,0};

if(frameCount%8==0){ // only one element is present


x_pos.add(0,x_pos.get(0)+x_dir[dir]);
y_pos.add(0,y_pos.get(0)+y_dir[dir]);

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:

if(x_pos.get(0)==f_x_pos && y_pos.get(0)==f_y_pos){


f_x_pos=(int)random(0,width);
f_y_pos=(int)random(0,height);
}

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Displaying Score

Positioning our scoreboard:

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;

Just after our snake eats food:

if(x_pos.size()%2==0 && speed>=2 )


speed=speed-1;

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Dying Conditions
1. When our snake touches the border:

if(x_pos.get(0)<0 || y_pos.get(0)<0 || x_pos.get(0)>wdt|| y_pos.get(0)>hgt)


{
gamestatus=true;
}
2. When our touches itself:

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

You might also like