Snake game movement problem...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Snake game movement problem...

  1. #1
    Join Date
    Jan 2004
    Posts
    1

    Snake game movement problem...

    Hi,

    Im trying to make a snake game for a coursework at uni... the problem is that i have allowed for the keybaord to control the head of the snake so far, but i it will only move if the key is repeatedly pressed or held down. as tradition in snake games the snake will carry on moving in the last pressed direction... this is what i cannot fathom.

    i have tried a while statement, but the applet viewer kinda 'jams' and the dot vanishes.

    I am new to java and maybe i was too enthusiatstic and got myself stuck, or im just missing something obvious!

    try compiling the code...and u'll understand what i mean.

    Here is the complete code of the applet so far, i have only just started it so it isnt far/complex:

    ignore some parts like double buffering as they are not complete.

    CODE

    import java.awt.*;

    import java.applet.*;



    public class Worm extends Applet

    {

    Image dot;

    Image Buffer;

    Graphics g;

    boolean pressedLeft, pressedRight, pressedUp, pressedDown;

    int x;

    int y;

    Thread runner;

    public void init()

    {

    //Double Buffering

    Buffer=createImage(size().width,size().height);

    g=Buffer.getGraphics();



    //set no-direction

    pressedUp=false;

    pressedRight=false;

    pressedLeft=false;

    pressedDown=false;



    dot = getImage(getCodeBase(), "dot.gif");

    x=100;

    y=100;

    }



    public boolean keyDown(Event e, int key)

    {

    //set boolean values on each direction.

    if(key==Event.LEFT)

    {pressedLeft=true;

    pressedRight=false;

    pressedUp=false;

    pressedDown=false;}



    if(key==Event.RIGHT)

    {pressedRight=true;

    pressedLeft=false;

    pressedUp=false;

    pressedDown=false;}



    if(key==Event.UP)

    {pressedUp=true;

    pressedRight=false;

    pressedLeft=false;

    pressedDown=false;}



    if(key==Event.DOWN)

    {pressedDown=true;

    pressedRight=false;

    pressedUp=false;

    pressedLeft=false;}





    repaint();



    return true;

    }





    public void drawSnake()

    {

    g.setColor(Color.blue);

    g.fillRect(0,0,size().width,size().height);



    g.setFont(new Font("Helvetica",Font.PLAIN,14));

    g.setColor(Color.orange);

    //if we want to use "" inside a string, we have to mask it with \!





    //left

    if(pressedLeft)

    {g.drawString ("left",20,40);



    x=x-10;



    }

    //right

    if(pressedRight)

    {g.drawString ("right",20,40);

    x=x+10;

    }

    //up

    if(pressedUp)

    {g.drawString ("up",20,40);

    y=y-10;

    }

    //down

    if(pressedDown)

    {g.drawString ("down",20,40);

    y=y+10;

    }

    g.drawImage(dot,x,y,this);







    }



    public void run() {

    while(pressedLeft) {

    x--;

    }









    }

    public void update(Graphics g)

    {

    paint(g);

    }



    public void paint(Graphics g)

    {



    drawSnake();

    g.drawImage (Buffer,0,0, this);

    }

    }


    /CODE

  2. #2
    Join Date
    Jan 2004
    Posts
    22
    I made something like this. I had a direction string that I set to u, d, l, or r based on the key pressed. It ran a thread, looping every .5 or so seconds and checking the value of direction. It modified the variables appropriately.

    Basics of threads: http://java.sun.com/docs/books/tutor...ntial/threads/

    My source code is attached if you are interested in seeing an example. Here's what it does. Not complete.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center