SNAKE GAME
Idea behind this Game:
1] To confine the snake draw a border in such a way that if it crosses the border then player should loose.
2] Create a small snake with some filled ovals of same radius (r) side by side this can be achieved by placing circles with a spacing of 2r .
3] Movement of snake can be set by assigning the arrow keys to it.
4] Food can be created at random places by using Random class.
5] Each time the snake catches the food not only score but also length of the snake has to be increased.
6] Speed of the snake can be varied by using Thread.Sleep() function.
7] Concept of double buffering is used to stop blinking in snake movement.
........HAPPY CODING
///////////////////////////////////////////////////////
import java.applet.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
public class snake extends Applet implements KeyListener,Runnable{
private Image i1;
private Graphics doubleG;
int dx=25;int dy=25;char k,k1;int v=0;int b=3;int tx,ty;
int y = 251; int x =251;int x1;int z=0;
public boolean keyLeft,keyUp,keyRight,keyDown;
@SuppressWarnings({ "unused", "serial" })
int xx[]=new int[20];int yy[]=new int[20];int dxx[]=new int[20];
int dyy[]=new int[20];
Random t1 = new Random(); Random t2 = new Random();int t=1;
public void paint (Graphics g) {
if(z==0&&b<19)
{
//String s = String.valueOf(x);g.drawString(s, 990,34); //for knowing movement of snake in x axis
//String s1 = String.valueOf(y);g.drawString(s1, 1100,34);
//for knowing movement of snake in y axis
//String b2 = String.valueOf(b);g.drawString(b2, 990,134);
//for knowing how much food snake had ate
String score = String.valueOf(v*50);//printing score
String time = String.valueOf(t*200);//printing time
Font font = new Font("Arial",Font.BOLD,30);
g.setFont(font);
g.setColor(Color.darkGray);
g.drawString(score, 990,234);g.drawString(time, 990,294);
Font f = new Font("MV Boli",Font.ITALIC,30);
g.setFont(f);
g.setColor(Color.blue);
g.drawString("Score :", 875,230);
g.drawString("Time(ms) :", 805,290);
if(v==0)
{g.setColor(Color.green);
g.fillOval(dxx[0],dyy[0],30,30);//initial position
}
if(dxx[v]==x&&dyy[v]==y)
{
v++;b++;//creating new food once it complete old
}g.setColor(Color.green);
g.fillOval(dxx[v],dyy[v],30,30);
g.setColor(Color.red);
g.drawRect(47, 47, 756, 606);
g.drawRect(48, 48, 754, 604);
g.drawRect(49, 49, 752, 602);//4 lines for borders
g.drawRect(50, 50, 750, 600);
for(int m=0;m<b;m++){xx[b-m]=xx[b-m-1];yy[b-m]=yy[b-m-1];}
xx[0]=x;yy[0]=y;
for(int i=0;i<b;i++)
{
g.setColor(Color.green);
g.fillOval(xx[i],yy[i],30,30);
if(i==0){g.setColor(Color.black);g.fillOval(xx[i]+10,yy[i]+10,10,10); }
}
}
//below shown loop is executed once player lost the game else if(z==1&&b<20)
{
Font font = new Font("Arial",Font.BOLD,70); g.setFont(font);
g.drawString("GAME OVER", 480,250);
}
//below shown loop is executed once player completes the game
else if(b>=19)
{
Font font = new Font("Arial",Font.BOLD,70);
g.setFont(font);
int R = (int)(Math.random()*256);
int G = (int)(Math.random()*256);
int B= (int)(Math.random()*256);
Color c = new Color(R, G, B);g.setColor(c);
g.drawString("U WIN", 480,250);
}
}
public void update(Graphics g)//doubling buffering
{if(i1==null)
{i1=createImage(this.getSize().width,this.getSize().height);
doubleG=i1.getGraphics();
}
doubleG.setColor(getBackground());
doubleG.fillRect(0,0,this.getSize().width,this.getSize().height);
doubleG.setColor(getForeground());
paint(doubleG);
g.drawImage(i1,0,0,this);}
public void init()
{
setSize(1200,1200);//Here random places are assigned
for(int i2=0;i2<18;i2++)
{
tx=t1.nextInt(20); ty=t2.nextInt(20);
dxx[i2]=126+(tx*25);dyy[i2]=126+(ty*25);
}
Thread t=new Thread(this);
t.start();
addKeyListener(this);
setFocusable(true);
}
//In run function both movement and confinement of snake is done
public void run() {
while(b<20)
{
if(x>776){z=1; }if(x<51){z=1;}if(y>626){z=1; }if(y<51){z=1; }
if(y>=51&&k== 'u'){y=y-dy;}else if(x<=776&&k=='r'){x=x+dx;}else if(x>=51&&k== 'l'){x=x-dx;}else if(y<=626&&k=='d'){y=y+dy;}
repaint();try {Thread.sleep(180);t++;}
catch (InterruptedException e) {e.printStackTrace();}
}
}
//in below function arrow keys are lisened
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_UP&&k1!='d'){ k='u';}
if(key == KeyEvent.VK_RIGHT&&k1!='l'){ k='r';}
if(key == KeyEvent.VK_LEFT&&k1!='r'){ k='l';}
if(key == KeyEvent.VK_DOWN&&k1!='u'){ k='d';}
k1=k;
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}
No comments:
Post a Comment