hi, i'm writing a snake game and so far i've gotten nowhere, not even to moving the hashes i'm calling snake, and that's what i'm gonna discuss
now i know i don't clearly have an understanding of threading for animation but before this i've done lotsa stuff like bouncing frames etc but this time i just don't get it, where am i going wrong with the threads and all?! the damn label i call snake is just not moving!
and btw, i'vent written any logic for the directional movement, just basic x,y axis movement,which means the snake won't ever go into a T shape so..
here's the basic code i've written so far
the sop("got here") statements are what i use to see control flow,cause i don't use an ide,and yeah its primitive i know to use the notepad and cmd.. but i still like it
import java.awt.*;
import java.awt.event.*;
class snake extends Frame implements Runnable,KeyListener//ActionListener
{
Thread t=new Thread();
String s="####"//my snake,sc="";//for storing score
int score=0;int dir=0;
int kt;
int x,y;//position variables for snake label
Label l=new Label(s);
Label ls=new Label("second label");
//Button play = new Button("Play");
snake()
{super("Snake by A.K");
setLayout(new BorderLayout());
add(l,BorderLayout.NORTH);
ls.setText(sc);
add(ls,BorderLayout.SOUTH);
//add(play,BorderLayout.SOUTH);
//play.addActionListener(this);
addKeyListener(this);
setLocation(250,250);
setSize(700,400);
setVisible(true);
System.out.print("got here constructor\n");
}
/*public void actionPerformed(ActionEvent ae)//useless since i'm gonna use only //keystrokes
{t.start();
}
*/
public void keyTyped(KeyEvent k){}
public void keyReleased(KeyEvent k){}
public void keyPressed(KeyEvent k)
{ kt = k.getKeyCode();
if(kt==KeyEvent.VK_UP)
{
dir=0;//l.setLocation(l.getX(),l.getY()+5);
}
else if(kt==KeyEvent.VK_DOWN)
{
dir=1;//l.setLocation(l.getX(),l.getY()-5);
}
else if(kt==KeyEvent.VK_LEFT)
{
dir=2;//l.setLocation(l.getX()-5,l.getY());
}
else if(kt==KeyEvent.VK_RIGHT)
{
dir=3;//l.setLocation(l.getX()+5,l.getY());
}
sc=String.valueOf("direction: "+dir);
ls.setText(sc);
System.out.print("got here keyPressed\n");
}
public void run()
{try{
System.out.print("got here run\n");
x=l.getX();
y=l.getY();
if(dir==0)
y--;
if(dir==1)
y++;
if(dir==2)
x--;
if(dir==3)
x++;
while(true)
{System.out.print("got here while \n");
l.setLocation(x,y);
t.sleep(100);
}
}
catch(Exception e){System.out.print("got here\n"+e);}
}
public static void main(String a[])
{
new snake();
}
}