This site requires JavaScript, please enable it in your browser!
Greenfoot back
ORSODO
ORSODO wrote ...

2018/2/5

Snake Game

ORSODO ORSODO

2018/2/5

#
I don't know what's wrong with this program. Every time I start the game the Terminal Window appears. (I want to make a snake game using arrays.) Please help!
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Snake extends Actor
{
    /**
     * Act - do whatever the Snake wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int foodEaten=0;
    Tail a[];
	private Snake behind;
    public void act() 
    {
       design();
       eat();
       movee();
    }
    public void design ()
    {
        GreenfootImage snake= new GreenfootImage (20,20);
        snake.setColor(new Color(0,138,0)); //green
        snake.fill();
        setImage(snake);
    }
    public void eat()
    {
        {
            if (isTouching(Yellow.class))
            {
                removeTouching (Yellow.class);
                foodEaten++;
            }
        }
    }
    public void movee()
    {   
        for (int i=0; i<foodEaten+1; i++)
        {
            if (i==0){
            if( a[i].getRotation()==0) a[0].setLocation(getX()+1,getY());
            if( a[i].getRotation()==90) a[0].setLocation(getX(),getY()+1);
            if( a[i].getRotation()==180) a[0].setLocation(getX()-1,getY());
            if( a[i].getRotation()==270) a[0].setLocation(getX(),getY()-1); }
            else 
            {
                {
                    int x1=a[i].getX();
                    int y1=a[i].getY();
                    if( a[i].getRotation()==0) a[i].setLocation(x1+1,y1);
                    if( a[i].getRotation()==90) a[i].setLocation(x1,y1+1);
                    if( a[i].getRotation()==180) a[i].setLocation(x1-1,y1);
                    if( a[i].getRotation()==270) a[i].setLocation(x1,y1-1); 
                }
            }
        }
    }
}
danpost danpost

2018/2/5

#
First off, you are not initializing your array (you never actually create one). You declared a field to reference one; but, it is never assigned one. Secondly, once you do create one and assign it to the field, its length is fixed. You would have to continually replace the array and copy over the elements from the old array to the larger one to accommodate each added tail. Are you required to use an array, or is this just something you believe is needed? -- it is not! The rotation of each part of the tail can be determined by that of the one ahead of it. You would only need to set the any new rotation of the head before calling the 'move(Actor, boolean)' method and add a line in that method to set its new rotation.
danpost danpost

2018/2/5

#
The 'design' method should only be executed once when the actor is created. Remove the call from the 'act' method and add a constructor block to the class:
public Snake()
{
    design();
}
You could replace the call to 'design' in the constructor block with the code within the 'design' method and then delete the 'design' method from the class.
You need to login to post a reply.