Computer Science Canada

Snake game help

Author:  Shingetsu [ Wed Dec 20, 2006 7:42 pm ]
Post subject:  Snake game help

i am in major need of help. my snake won't turn and it's not reseting properly. also for some reason my R.I.P message go away and i've tried printing over it.

code:

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

public class SnakeGame extends Applet implements KeyListener, Runnable
{
    // Place instance variables here

    Snake s;
    Thread t;
    Graphics g;
    double speed;

    public void keyTyped (KeyEvent e)
    {
        char k = e.getKeyChar ();
        if (k == 's')
            reset ();
        else if (k == 'z')
            speed = speed + 100000;
        else if (k == 'x')
            speed = speed - 100000;
    }


    public void keyPressed (KeyEvent e)
    {
        char k = e.getKeyChar ();
        if (k == e.VK_DOWN)
            s.direction = 'd';
        else if (k == e.VK_UP)
            s.direction = 'u';
        else if (k == e.VK_LEFT)
            s.direction = 'l';
        else if (k == e.VK_RIGHT)
            s.direction = 'r';
    }


    public void keyReleased (KeyEvent e)
    {
    }


    public void init ()
    {
        s = new Snake ();
        g = getGraphics ();
        t = null;
        addKeyListener (this);
        speed = 10000000;
    }


    public void paint (Graphics g)
    {
        // Place the body of the drawing method here
    } // paint method


    public void reset ()
    {
        SnakePoint boarder = new SnakePoint (0, 0);
        g.clearRect (9, 9, 290, 290);
        g.drawString ("         ", 400, 70);
        for (int i = 0 ; i < 30 ; i++)
        {
            boarder.plot (g, Color.green);
            boarder = boarder.right ();
        }
        for (int i = 0 ; i < 30 ; i++)
        {
            boarder.plot (g, Color.green);
            boarder = boarder.down ();
        }
        for (int i = 0 ; i < 30 ; i++)
        {
            boarder.plot (g, Color.green);
            boarder = boarder.left ();
        }
        for (int i = 0 ; i < 30 ; i++)
        {
            boarder.plot (g, Color.green);
            boarder = boarder.up ();
        }
        if (t != null)
            t.stop ();
        t = new Thread (this);
        t.run();
    }


    public void run ()
    {
        int food = 0;
        SnakePoint foodPoint=null;
        boolean eating = false;

        while (s.alive ())
        {
            if (food == 0)
            {
                g.setColor (Color.black);
                food = (int) (Math.random () * 9);
                foodPoint = new SnakePoint ((int) (Math.random () * 280) + 10,(int) (Math.random () * 280) + 15);
                g.drawString (Integer.toString (food), foodPoint.getX(), foodPoint.getY());
            }
            g.clearRect (9, 9, 290, 290);
            if (eating)
            {
                s.grow ();
                food--;
            }
            else
            {
                g.setColor (Color.black);
                s.move ();
                g.drawString (Integer.toString (food), foodPoint.getX(), foodPoint.getY());
            }

            for (int i = 0 ; i < 5 ; i++)
                s.draw (g);
            if (food == 0)
                eating = false;
            if (s.head.p.x == foodPoint.getX() && s.head.p.y == foodPoint.getY())
                eating = true;
            for (int i = 0 ; i < 5 ; i++)
                s.draw (g);
            g.setColor (Color.black);
            g.drawString ("Points: " + Integer.toString (s.length - 5), 400, 60);

            for (int delay = 0 ; delay < speed ; delay++)
            {
            }
            System.gc ();
        }
        g.drawString ("R.I.P", 250, 70);

    }
} // SnakeGame class


class SnakePoint
{
    public int x, y;

    public SnakePoint (int a, int b)
    {
        x = a;
        y = b;
    }


    public int getX ()
    {
        return x;
    }


    public int getY ()
    {
        return y;
    }


    public void plot (Graphics g, Color c)
    {
        g.setColor (c);
        g.fillRect (x * 10, y * 10, 9, 9);
    }


    public SnakePoint up ()
    {
        return new SnakePoint (x, y - 1);
    }


    public SnakePoint down ()
    {
        return new SnakePoint (x, y + 1);
    }


    public SnakePoint right ()
    {
        return new SnakePoint (x + 1, y);
    }


    public SnakePoint left ()
    {
        return new SnakePoint (x - 1, y);
    }


    public boolean equals (SnakePoint s)
    {
        return s.x == x && s.y == y;
    }
}


class SnakeNode
{
    public SnakePoint p;
    public SnakeNode next;

    public SnakeNode (int x, int y)
    {
        p = new SnakePoint (x, y);
    }
}


class Snake
{
    public SnakeNode tail;
    public SnakeNode head;
    public char direction;
    public int length;

    public Snake ()
    {
        tail = null;
        head = null;
        add (new SnakePoint (15, 15));
        add (new SnakePoint (15, 14));
        add (new SnakePoint (15, 13));
        add (new SnakePoint (15, 12));
        add (new SnakePoint (15, 11));
        direction = 'u';
    }


    public int length ()
    {
        return length;
    }


    public SnakeNode head ()
    {
        return head;
    }


    public boolean empty ()
    {
        return length == 0;
    }


    public void add (SnakePoint sp)
    {
        if (empty ())
        {
            head = new SnakeNode (sp.x, sp.y);
            tail = head;
        }
        else
        {
            head.next = new SnakeNode (sp.x, sp.y);
            head = head.next;
        }
        length++;
    }


    public SnakePoint remove ()
    {
        SnakeNode ans;
        if (empty ())
            return null;
        else
        {
            ans = tail;
            tail = tail.next;
            length--;
            return ans.p;
        }
    }


    public void draw (Graphics g)
    {
        SnakeNode current = tail;
        while (current != null)
        {
            if (current == head)
                current.p.plot (g, Color.red);
            else
                current.p.plot (g, Color.black);
            current = current.next;
        }
    }


    public void changeDirection (char d)
    {
        direction = d;
    }


    public void grow ()
    {
        if (direction == 'u')
            add (head.p.up ());
        else if (direction == 'r')
            add (head.p.right ());
        else if (direction == 'l')
            add (head.p.left ());
        else if (direction == 'd')
            add (head.p.down ());
    }


    public void move ()
    {
        grow ();
        remove ();
    }


    public boolean onBody (SnakePoint q)
    {
        SnakeNode x = tail;
        while (x != head)
        {
            if (x.p.equals (q))
                return true;
            x = x.next;
        }
        return false;
    }



    public boolean onSnake (SnakePoint p)
    {
        SnakeNode current = head;
        while (current != null && p.equals (current.p) && !p.equals (head.p))
            current = current.next;
        if (current == null)
            return false;
        else
            return true;
    }


    public boolean alive ()
    {
        return (!onBody (head.p) && head.p.getX () > 0 && head.p.getX () < 30 && head.p.getY () > 0 && head.p.getX () < 30);

    }
}

Author:  Shingetsu [ Wed Dec 20, 2006 7:45 pm ]
Post subject: 

forgot to tell you guys i use Ready to Program

Author:  ericfourfour [ Wed Dec 20, 2006 10:10 pm ]
Post subject: 

First of all, indent your code. RTP has an automatic one (that pisses me off) but it will still look better than this indenting.

Secondly, all of your drawing should be done in the paint method. It is there for a reason. Simply type repaint (); in your while loop to keep painting.

Thirdly:
code:
System.gc ();

You do not need this in your code. Java will automatically call the garbage collector if you run out of memory.

The reason the message will not go away has something to do with threads. I'm not very educated on this topic so someone else will have to take over.

Author:  Shingetsu [ Wed Dec 20, 2006 10:31 pm ]
Post subject: 

i finally fixed the damn thing but thx anyways


: