Login    New User    Forgot Password    Help      

Snake Game (Need Improvement )

Java Tutorials

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class snake extends Applet implements Runnable,KeyListener
{
Thread t;
//cox and coy are co-ordinates of x-axis and y-axis of engine
int cox;
int coy;
//cx and cy are co-ordinates of x-axis and y-axis of tail
int cx[]=new int[400];
int cy[]=new int[400];

// int len_snake is used to calculate the lenth of sanke
int len_snake=3;

//ci is used for loop control of codinates of x and y axis loop
int ci=0;
int tempx;
int tempy;

 

// keycontrol to check the movemovent of arrow keys
int keycontrol=0;

// blink to use for flash the game over
boolean blink;

// game over to check the game is over or not
boolean gameover;

Thread t1;
int speed;
int score;
// zx zy are codinated distance of two blockes
int zx=5;
int zy=5;
Font f= new Font("TimesRoman", Font.BOLD,42);
Font f1= new Font("TimesRoman", Font.BOLD,42);

//////////////////////////////////////////////////////////////////////////
public void init()
{
speed=100;
score=0;
gameover=false;
blink=false;
setBackground(Color.white);
setForeground(Color.red);
addKeyListener(this);
requestFocus();

cox=11;
coy=80;
for(ci=0;ci<=len_snake;ci++)
{
cx[ci]=cox-zx;
cy[ci]=coy ;
zx=zx-5;
}
zx=5;
}
////////////////////////////////////////////////////////////////
public void keyPressed(KeyEvent ke)
{
int key=ke.getKeyCode();
switch(key)
{
case KeyEvent.VK_DOWN:
if(keycontrol!=3)
keycontrol=1;

break;

case KeyEvent.VK_UP:
if(keycontrol!=1)
keycontrol=3;

break;

case KeyEvent.VK_RIGHT:
if(keycontrol!=2)
keycontrol=0;

break;

case KeyEvent.VK_LEFT:
if(keycontrol!=0)
keycontrol=2;

break;

case KeyEvent.VK_PAGE_UP:
// temp code for increase of snake
len_snake++;

}
}

public void keyReleased(KeyEvent ke)
{}
public void keyTyped(KeyEvent ke)
{}
public void start()
{
if(t==null)
{
t=new Thread(this);
t.start();
}
}

///////////////////////////////////////////////////////////////////////////
public void stop()
{
if(t!=null)
{
t.stop();
t=null;
}
}

public void run()
{
while(true)
{

if(gameover!=true)
{

switch(keycontrol)
{
case 0:
cox=cox+5;
break;

 

case 1:
coy=coy+5;
break;

case 2:
cox=cox-5;
break;

case 3:
coy=coy-5;
break;
}
}

cx[0]=cox;
cy[0]=coy;
for(ci=1;ci<=len_snake;ci++)
{
tempx=cx[ci];
cx[ci]=cx[ci-1];
cx[ci-1]=tempx;
tempy=cy[ci];
cy[ci]=cy[ci-1];
cy[ci-1]=tempy;

 

}
///////////////////////////////////////////////////////////////////////

repaint();

try
{
Thread.sleep(speed);
}
catch(InterruptedException e)
{}
}
}

public void destroy()
{

}

public void paint(Graphics g)
{

g.setColor(Color.red);
g.drawRect(10,10,400,400);



if(cox<410 && coy<410 && cox>10 && coy>10 )
{


// code for snake head
g.setColor(Color.black);
g.fillRect(cox,coy,7,7);
g.setColor(Color.green);

// printing of tail
for(ci=0;ci<=len_snake;ci++)
{
g.fillRect(cx[ci],cy[ci],5,5);
}

// end of snake tail
}
else
{
showStatus("Go and Sleep cant do noting in life . your score is only "+ score );
if(blink==true)
{
blink=false;
}
else
{
blink=true;
g.setFont(f);
g.drawString("GAME OVER", 100,200);
g.setColor(Color.green);
g.setFont(f1);
g.drawString("GAME OVER", 102,202);
speed=200;
gameover=true;
}


}
}

}

Output

Output in pannel

Java Tutorials