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();
}
}

Recommended Answers

All 3 Replies

with Anonymous Inner Class Runnable

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

class Snake extends Frame implements KeyListener {//ActionListener

    Thread t = new Thread();
    String s = "####";//my snake,
    String 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
    {///
    }
     */
    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");
        (new Runnable()     {

            public void run() {
                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++;
                }
                l.setLocation(x, y);
                repaint();                 //need repaint 
            }
        }).run();
    }

    public static void main(String a[]) {
        new Snake();
    }
}

i didn't understand why used repaint(), i only know that its used with applets..never used it outside it so please explain that
and the snake is not moving, i wanted to know how to animate that using the thread, using sleep(), but instead now it only moves when i press the arrow key.. still an advance from my previous bug laden code

this.requestFocusInWindow();

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

class Snake extends Frame implements KeyListener, ActionListener, Runnable {

    Thread t;
    String s = "####";//my snake,
    String sc = "";//for storing score
    int score = 0;
    int dir = 1;
    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);
        l.setBackground(Color.yellow);
        sc = String.valueOf("direction: " + dir);
        ls.setText(sc);
        add(ls, BorderLayout.SOUTH);
        add(play, BorderLayout.EAST);
        play.addActionListener(this);
        addKeyListener(this);
        setLocation(250, 250);
        setSize(700, 400);
        setVisible(true);
        t = new Thread(this);
        System.out.print("got here constructor\n");
        this.requestFocusInWindow();
    }

    public void actionPerformed(ActionEvent ae) {
        t.start();
        System.out.print("thread started\n");
        this.requestFocusInWindow();
    }

    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() {
        System.out.print("RUN\n");
        while (true) {
            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++;
            }
            System.out.print("got here while \n");
            l.setLocation(x, y);
            try {
                t.sleep(80);
            } catch (InterruptedException e) {
                System.out.print("got here\n" + e);
            }
        }
    }

    public static void main(String a[]) {
        new Snake();
    }
}

http://download.oracle.com/javase/tutorial/uiswing/events/keylistener.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.