UPDATED SNAKE GAME
Description
This game provides all of the challenge expected from an "impossible" game while also making it's code very simple.what's New
Update 1.01Bug fixes and tweaks.
FLAW'S AND RECTIFICATION
There are 2 major Flaws in before game:-
1] speed of snake does not increases as score increases.
2] Even though Snake's Head touches it's Tail, game is allowing us to play further instead of showing GAME OVER CARD.
In this Update I fixed those 2 Flaws:-
=>First one is fixed by decreasing the TIME DELAY as Score increases.
=>Second is resolved by logical analysis that is if first circle touches any other circle then it comes out of loop.
.............HAPPY CODING,
VISWADEEP.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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;int sl=180;
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(b>3)
{for(int i=0;i<b;i++) //THIS BLOCK IS TO MAKE PLAYER TO EXIT FROM GAME IF // HEAD COLLIDE WITH TAIL
{
if(xx[0]==xx[b-i]&&yy[0]==yy[b-i])
{z=1;}
}
}
if(z==0&&b<19)
{
//String s = String.valueOf(x);g.drawString(s, 990,34);
//String s1 = String.valueOf(y);g.drawString(s1, 1100,34);
//String b2 = String.valueOf(b);g.drawString(b2, 990,134);
String score = String.valueOf(v*50);
String time = String.valueOf(t*200);
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);
}
if(dxx[v]==x&&dyy[v]==y)
{
v++;b++;
sl=sl-10;//SPEED OF SNAKE INCREASES WITH SCORE
}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);
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); }
}
}
else if(z==1&&b<20)
{
Font font = new Font("Arial",Font.BOLD,70); g.setFont(font);
g.drawString("GAME OVER", 480,250);
}
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)
{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);
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);
}
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(sl);t++;} catch (InterruptedException e) {e.printStackTrace();}
}
}
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) {}
}