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

2018/12/14

I am Making a snake game and cant get the code to work in my snakehaed, snakebody or snake world classes.

zach0531 zach0531

2018/12/14

#
PLEASE tell me whats wrong!! Snake Body Code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;

/**
 * Write a description of class SnakeBody here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SnakeBody  extends Actor
{
    private int x_speed=1;
    private int y_speed=0;
    private int bodyPos;
    private Point snakeCoords;
    
    /**
     * SnakeBody constructor
     * sets the image for the snakeBody
     * @param int bodyPosition
     */
    public SnakeBody(int bodyPosition)
    {
        GreenfootImage image = new GreenfootImage(10, 10);
        image.setColor(Color.GRAY);
        image.fillRect(0, 0, 10, 10);
        setImage(image);
        bodyPos = bodyPosition;
    }

    /**
     * Act - do whatever the SnakeBody wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveBody();
    }    

    /**
     * moveBody
     * moves the snake body 
     */
    public void moveBody()
    {
      SnakeWorld world = (SnakeWorld)getWorld();
      snakeCoords = world.getBodyPosition(bodyPos);
      setLocation(snakeCoords.getX(), snakeCoords.getY());
    }
}
Snake World code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;

/**
 * Write a description of class SnakeWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SnakeWorld extends World
{
    public static final int DOT_SIZE = 10;
    public static final int MAX_DOTS = (600*400)/(DOT_SIZE*DOT_SIZE);
    private int snakeSize=4;
    private Point snakeCoords[] = new Point[MAX_DOTS];
    //private int x[] = new int[MAX_DOTS];
    //private int y[] = new int[MAX_DOTS];
    int dX=1, dY=0;

    //Direction variables
    private final int SPEED = 1;
    private int directionX = SPEED;
    private int directionY;

    private Score score = new Score();

    private int speed = 30;
    
    /**
     * Constructor for objects of class SnakeWorld.
     * 
     */
    public SnakeWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(61, 41, 10);
        setUpCoords();
        drawSnake();
        placeFood(1);
        addObject(new WorldOutline(), 30, 20);
        addObject(score, 6, 2);
        Greenfoot.setSpeed(speed);
    }

    /**
     * upDateCoords
     * Updates the coordinates of the snake to its new spot as it moves on the screen
     */
    public void updateCoords()
    {

        for(int i = snakeSize - 1; i > 0;i--)
        {

            snakeCoords[i].setX(snakeCoords[i-1].getX());
            snakeCoords[i].setY(snakeCoords[i-1].getY());

        }
        snakeCoords[0].setX(snakeCoords[0].getX() + directionX);
        snakeCoords[0].setY(snakeCoords[0].getY() + directionY);
    }

    /**
     * drawSnake
     * Draws the snake at its starting position
     */
    public void drawSnake()
    {
        addObject(new SnakeHead(0), snakeCoords[0].getX(), snakeCoords[0].getY());
        for(int i = snakeSize - 1; i > 0; i--)
        {
            addObject(new SnakeBody(i), snakeCoords[i].getX(), snakeCoords[i].getY());
        }
    }

    /**
     * placeFood
     * Place a set amount of food in the game window
     * @param int amountOfFood
     */
    public void placeFood(int amountOfFood)
    {
        for (int i = 0; i < amountOfFood; i++)
        {
            addObject(new Food(), Greenfoot.getRandomNumber(59)+1,  Greenfoot.getRandomNumber(39)+1);
        }
    }

    /**
     * setUpCoords
     * sets up the snake coordinates at the start of the game
     */
    public void setUpCoords()
    {
        snakeCoords[0] = new Point(getWidth() / 2, getHeight() / 2);
        for(int i = 1; i < snakeSize; i++)
        {
            snakeCoords[i]=new Point(snakeCoords[i-1].getX() - 1, snakeCoords[i-1].getY());
        }
    }

    /**
     * addTail
     * adds another tail to the end of the snake
     */
    public void addTail()
    {
        snakeCoords[snakeSize] = new Point(snakeCoords[snakeSize-1].getX(), snakeCoords[snakeSize-1].getY());
        addObject(new SnakeBody(snakeSize), snakeCoords[snakeSize].getX(), snakeCoords[snakeSize].getY());
        snakeSize++;
        score.updateScore();
        speed++;
        Greenfoot.setSpeed(speed);
    }

    /**
     * keyPress
     * changes the direction of the snake based upon key presses
     */
    public void keyPress()
    {
        if( Greenfoot.isKeyDown("up") && directionY != SPEED)
        {
            directionY = -SPEED; 
            directionX = 0;
        }
        if( Greenfoot.isKeyDown("down") && directionY != -SPEED)
        {
            directionY = SPEED;
            directionX = 0;
        }
        if( Greenfoot.isKeyDown("left") && directionX != SPEED)
        {
            directionX = -SPEED;
            directionY = 0;
        }
        if( Greenfoot.isKeyDown("right") && directionX != -SPEED)
        {
            directionX = SPEED;
            directionY = 0;
        }
    }

    /**
     * getBodyPosition
     * returns the coordinates of the snake at the passed in body position
     * @param int bodyPosition
     */ 
    public Point getBodyPosition(int bodyPosition)
    {
        return snakeCoords[bodyPosition];
    }

    /**
     * die - ends the game
     */
    public void die()
    {
        addObject(new GameOver(), getWidth() / 2, getHeight() / 2);
        Greenfoot.stop();
    }

    /**
     * hitEdge - game over if you hit the side of the world
     */
    public void hitEdge()
    {
        if(snakeCoords[0].getX() < 1 || snakeCoords[0].getX() > getWidth()-2 || snakeCoords[0].getY() < 1 || snakeCoords[0].getY() > getHeight()-2)
        {
            die();
        }
    }

    public void act()
    {
        keyPress();
        updateCoords();
        
        hitEdge();
    }

}
Snake Head Code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;

/**
 * Write a description of class SnakeHead here.
//  * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SnakeHead  extends Actor
{
    private int bodyPos;
    private Point snakeCoords;
    private int temp = 0;

    /**
     * SnakeHead Constructor
     * Sets the image for snakeHead
     * @param int bodyPosition
     */
    public SnakeHead(int bodyPosition )
    {
        GreenfootImage image = new GreenfootImage(10, 10);
        image.setColor(Color.GREEN);
        image.fillRect(0, 0, 10, 10);
        setImage(image);
    }

    /**
     * Act - do whatever the SnakeHead wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveHead();
        lookForFood();
        hitBody();
    }   

    /**
     * moveHead
     * moves the head to the next position on the screen
     */
    public void moveHead()
    {
        SnakeWorld world = (SnakeWorld)getWorld();
        snakeCoords = world.getBodyPosition(0);
        setLocation(snakeCoords.getX(), snakeCoords.getY());

    }

    /**
     * growTail
     * adds a tail segment to the snake and adds new food to the world
     */
    public void growTail()
    {
        SnakeWorld world = (SnakeWorld)getWorld();
        world.addTail();
        world.placeFood(1);
    }

    /**
     * lookForFood
     * Looks for food.
     * If the snake finds food it eats the food 
     */
    public void lookForFood()
    {
        if (canSee(Food.class))
        {
            eat(Food.class); 
            growTail();
        }
    }

    /**
     * hitBody - checks whether the snake tries to eat it's own body
     */
    public void hitBody()
    {
        SnakeWorld world = (SnakeWorld)getWorld();
        if(canSee(SnakeBody.class))
        {
            world.die();
        }
    }

    /**
     * canSee
     * looks for other objects the intersect with the snake
     * @param Class clss
     */
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }

    /**
     * eat
     * eats the object and removes it from the screen
     * @param Class clss
     */
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null)
        {
            getWorld().removeObject(actor);
        }
    }
}
CoolSharkCody CoolSharkCody

2018/12/14

#
Which part of the code doesn’t work? Be more specific.
danpost danpost

2018/12/14

#
Try removing
import java.awt.*;
from all your classes and add a Point class to the project:

public class Point 
{
    private int x, y;
    
    public Point(int xCoord, int yCoord)
    {
        x = xCoord;
        y = yCoord;
    }
    
    public void setX(int value)
    {
        x = value;
    }
    
    public void setY(int value)
    {
        y = value;
    }
    
    public int getX()
    {
        return x;
    }
    
    public int getY()
    {
        return y;
    }
}
You need to login to post a reply.