Hi, I am trying to make a simple snake game, but whenever I try to attach a body piece it appears randomly on the screen rather than behind the head. Also, the body doesn't exactly follow the head either. I checked the code over and over for any problems but nothing seems wrong. I know the code is a bit rough but its just a quick draft. Please help.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class snake extends Applet implements Runnable, KeyListener
{
Thread t;
boolean var = true ;
boolean loop = true ;
boolean lose = true ;
boolean win = false ;
boolean turn = true ;
String body ;
int xRandom ;
int yRandom ;
int aFood = 20 ;
int bFood = 20 ;
int a = 20 ;
int b = 20 ;
int size = 20 ;
int adir ;
int bdir ;
int xdir = 10 ;
int ydir = 10 ;
int scoreCounter = -1 ;
int [] xBody = new int [100] ;
int [] yBody = new int [100] ;
int xTurn ;
int yTurn ;
public void init ()
{
setSize(600,600) ;
setBackground(Color.BLACK) ;
addKeyListener(this);
}
public void start()
{
t = new Thread(this);
t.start();
}
public void stop()
{
}
public void keyTyped (KeyEvent e)
{
}
public void keyPressed ( KeyEvent e)
{
int keyValue = e.getKeyCode();
System.out.println(keyValue) ;
//down
if(keyValue==40)
{
bdir = 10 ;
xTurn = a ;
yTurn = b ;
loop = false ;
turn = false ;
body = "down" ;
}
//up
if(keyValue==38)
{
loop = false ;
bdir = -10;
xTurn = a ;
yTurn = b ;
body = "up" ;
turn = false ;
}
//left
if(keyValue==37)
{
loop = true ;
adir = -10 ;
xTurn = a ;
yTurn = b ;
turn = false ;
body = "left" ;
}
//right
if(keyValue==39)
{
loop = true ;
xTurn = a ;
yTurn = b ;
adir = 10;
turn = false ;
body = "right" ;
}
}
public void keyReleased ( KeyEvent e )
{
}
public void run()
{
Thread thisThread = Thread.currentThread();
while (true)
{
//lose
if(a>600 || a<0 || b>600 || b<0)
{
lose = false ;
scoreCounter = 0 ;
}
//new game
if(win==true)
{
a = ((((int)(Math.random()*10))*10)*6) ;
b = ((((int)(Math.random()*10))*10)*6) ;
win = false ;
}
//vertical loop
if(loop==false)
{
b = bdir + b ;
for(int j=0;j<=scoreCounter;j=j+1)
{
yBody[j] = yBody[j]+ydir ;
}
}
//horizontal loop
if(loop==true)
{
a = adir + a ;
for(int k=0;k<=scoreCounter;k=k+1)
{
xBody[k] = xBody[k]+xdir ;
}
}
//body turn
for(int h=0;h<=scoreCounter;h=h+1)
{
if(xBody[h]==xTurn)
{
if(yBody[h]==yTurn)
{
if(turn==false)
{
if(body.equals("up"))
{
ydir = -10 ;
}
if(body.equals("down"))
{
ydir = 10 ;
}
if(body.equals("left"))
{
xdir = -10 ;
}
if(body.equals("right"))
{
xdir = 10 ;
}
turn = true ;
}
}
}
}
xRandom = ((((int)(Math.random()*10))*10)*6) ;
yRandom = ((((int)(Math.random()*10))*10)*6) ;
//eating food
if(a<=aFood+size && a>=aFood)
{
if(b<=bFood+size && b>=bFood)
{
//adds body
if(loop==true)
{
if(adir==-10)
{
xBody[scoreCounter] = a+(scoreCounter*size) ;
yBody[scoreCounter] = b ;
}
if(adir==10)
{
xBody[scoreCounter] = a-(scoreCounter*size) ;
yBody[scoreCounter] = b ;
}
}
if(loop==false)
{
if(bdir==-10)
{
xBody[scoreCounter] = a ;
yBody[scoreCounter] = b+(scoreCounter*size) ;
}
if(bdir==10)
{
xBody[scoreCounter] = a ;
yBody[scoreCounter] = b-(scoreCounter*size) ;
}
}
var = false ;
}
}
//food location & score count
if(var==false)
{
aFood = xRandom ;
scoreCounter = scoreCounter + 1 ;
bFood = yRandom ;
var = true ;
}
try
{
t.sleep(100);
}
catch (InterruptedException e)
{
}
repaint();
}
}
public void paint (Graphics g)
{
g.setColor(Color.WHITE) ;
g.fillOval(a,b,size,size);
g.drawString("Score: ",250,280) ;
g.drawString(" "+scoreCounter,290,280) ;
if(lose==false)
{
g.drawString("YOU LOSE!!!",250,300) ;
lose = true ;
win = true ;
}
g.setColor(Color.GREEN) ;
g.fillRect(aFood,bFood,size,size);
for(int i=0;i<scoreCounter;i=i+1)
{
g.setColor(Color.BLUE) ;
g.fillOval(xBody[i],yBody[i],size,size);
}
}
public void update(Graphics g)
{
Graphics offgc;
Image offscreen = null;
Dimension d = size();
offscreen = createImage(d.width, d.height);
offgc = offscreen.getGraphics();
offgc.setColor(getBackground());
offgc.fillRect(0, 0, d.width, d.height);
offgc.setColor(getForeground());
paint(offgc);
g.drawImage(offscreen, 0, 0, this);
}
}