Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By superhaNds

Thread: Help needed to add food to my Snake game.

  1. #1
    liveurlife007 is offline Member
    Join Date
    Feb 2014
    Location
    Manchester
    Posts
    4
    Rep Power
    0

    Default Help needed to add food to my Snake game.

    Hello guys ..I'm doing my lab work but I'm having trouble while adding food to my game. I have finished part A and B.
    I have this method to help me but don't know how to use it. I have to use setFood() method..
    x = (int) (Math.random() * n);
    y = (int) (Math.random() * n);

    Here is my code

    public class Game
    {

    // ----------------------------------------------------------------------
    // Part a: the score message

    private String scoreName;

    public String getScoreMessage()
    {
    return "A message";
    } // getScoreMessage


    public void setScoreMessage(String message)
    {
    scoreName = message;
    } // setScoreMessage


    public String getAuthor()
    {
    return "Mr John";
    } // getAuthor


    // ----------------------------------------------------------------------
    // Part b: constructor and grid accessors
    private final int gridSize;
    private final Cell [] [] gridArray;


    public Game(int requiredGridSize)
    {
    gridSize = requiredGridSize;gridArray = new Cell [gridSize] [gridSize];
    for (int i = 0; i < gridSize ; i ++ )
    for (int b = 0; b < gridSize; b ++ )
    gridArray [i] [b] = new Cell();

    } // Game


    public int getGridSize()
    {
    return gridSize;
    } // getGridSize


    public Cell getGridCell(int x, int y)
    {
    return gridArray [x] [y];
    } // getGridCell


    // ----------------------------------------------------------------------
    // Part c: initial game state

    // Part c-1: setInitialGameState method

    public void setInitialGameState(int requiredTailX, int requiredTailY,
    int requiredLength, int requiredDirection)
    {
    for (int i = 0; i < gridSize; i++)
    for (int b = 0; b < gridSize; b++)
    gridArray[i][b].setClear();

    } // setInitialGameState


    // ----------------------------------------------------------------------
    // Part c-2 place food

  2. #2
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    162
    Rep Power
    2

    Default Re: Help needed to add food to my Snake game.

    Can you post your code in CODE tags so it's readable?

  3. #3
    liveurlife007 is offline Member
    Join Date
    Feb 2014
    Location
    Manchester
    Posts
    4
    Rep Power
    0

    Default Re: Help needed to add food to my Snake game.

    Heyy I'm sorry but what do you mean ?

  4. #4
    liveurlife007 is offline Member
    Join Date
    Feb 2014
    Location
    Manchester
    Posts
    4
    Rep Power
    0

    Default Re: Help needed to add food to my Snake game.

    Here is full code
    public class Game
    {

    // ----------------------------------------------------------------------
    // Part a: the score message

    private String scoreName;

    public String getScoreMessage()
    {
    return "A message";
    } // getScoreMessage


    public void setScoreMessage(String message)
    {
    scoreName = message;
    } // setScoreMessage


    public String getAuthor()
    {
    return "Mr John";
    } // getAuthor


    // ----------------------------------------------------------------------
    // Part b: constructor and grid accessors
    private final int gridSize;
    private final Cell [] [] gridArray;


    public Game(int requiredGridSize)
    {
    gridSize = requiredGridSize;
    gridArray = new Cell [gridSize] [gridSize];
    for (int i = 0; i < gridSize ; i ++ )
    for (int b = 0; b < gridSize; b ++ )
    gridArray [i] [b] = new Cell();

    } // Game


    public int getGridSize()
    {
    return gridSize;
    } // getGridSize


    public Cell getGridCell(int x, int y)
    {
    return gridArray [x] [y];
    } // getGridCell


    // ----------------------------------------------------------------------
    // Part c: initial game state

    // Part c-1: setInitialGameState method

    public void setInitialGameState(int requiredTailX, int requiredTailY,
    int requiredLength, int requiredDirection)
    {
    for (int i = 0; i < gridSize; i++)
    for (int b = 0; b < gridSize; b++)
    gridArray[i][b].setClear();

    } // setInitialGameState


    // ----------------------------------------------------------------------
    // Part c-2 place food







    // ----------------------------------------------------------------------
    // Part c-3: place snake


    // ----------------------------------------------------------------------
    // Part d: set snake direction


    public void setSnakeDirection(int requiredDirection)
    {
    } // setSnakeDirection


    // ----------------------------------------------------------------------
    // Part e: snake movement

    // Part e-1: move method


    public void move(int moveValue)
    {
    } // move


    // ----------------------------------------------------------------------
    // Part e-2: move the snake head


    // ----------------------------------------------------------------------
    // Part e-3: move the snake tail


    // ----------------------------------------------------------------------
    // Part e-4: check for and deal with crashes


    // ----------------------------------------------------------------------
    // Part e-5: eat the food


    public int getScore()
    {
    return 99999999;
    } // getScore


    // ----------------------------------------------------------------------
    // Part f: cheat


    public void cheat()
    {
    } // cheat


    // ----------------------------------------------------------------------
    // Part g: trees


    public void toggleTrees()
    {
    } // toggleTrees


    // ----------------------------------------------------------------------
    // Part h: crash countdown


    // ----------------------------------------------------------------------
    // Part i: optional extras


    public String optionalExtras()
    {
    return " No optional extras defined\n";
    } // optionalExtras


    public void optionalExtraInterface(char c)
    {
    if (c > ' ' && c <= '~')
    setScoreMessage("Key " + new Character(c).toString()
    + " is unrecognised (try h)");
    } // optionalExtraInterface

    } // class Game

  5. #5
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    262
    Rep Power
    2

    Default Re: Help needed to add food to my Snake game.

    Place your code in [CODE] [ /CODE ] tags
    liveurlife007 likes this.

Similar Threads

  1. Snake Game
    By FrankElliot9 in forum New To Java
    Replies: 5
    Last Post: 12-03-2012, 05:01 AM
  2. Help needed with simple Java Snake game
    By AndySun94 in forum New To Java
    Replies: 1
    Last Post: 04-01-2012, 06:59 AM
  3. Snake Game
    By LuluMM in forum New To Java
    Replies: 0
    Last Post: 03-08-2012, 06:48 AM
  4. Snake Game
    By mustachMan in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 10:35 PM
  5. Stand O' Food Game Proyect
    By piachens in forum New To Java
    Replies: 1
    Last Post: 03-24-2008, 05:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •