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

2012/5/5

Help with some coding please.

Swedishsam Swedishsam

2012/5/5

#
OK, so I've finished making the basics of my snake game, i'm not going to make my snake grown and instead just keep it as a simple grey blob. I want to create a leveling system of sorts so when the snake eats a certain amount of berries the speed of the game gets faster. I also want to stop the snake from being able to go straight down when its going upwards. You can see how the game works and the code here. Snake Game
danpost danpost

2012/5/5

#
To stop the snake from reversing upon itself, make a check on the rotation before setting each new one. For example:
if (getRotation() != 0) setRotation(180);
You will need to do similar to that for all four directions. I added the following method to the Snake class (you can add it, if you want).
//This adds eyes to the snake
public Snake()
{
    GreenfootImage img = getImage();
    img.fillOval(27, 5, 3, 3);
    img.fillOval(27, 9, 3, 3);
    setImage(img);
}
danpost danpost

2012/5/5

#
For the speeding up thing, you have a counter, but need an integer variable 'runSpeed'. You can initially set the the value of 'runSpeed' (and initially Greenfoot.setSpeed(runSpeed)), and use it in future setSpeed commands after increasing its value when the counter reaches one of its limits. Note: if the limits are evenly spaced (meaning like after every 10 berries are eaten, the level increases) you can use the modulus (remainder) to determine whether it is time to increase level or not
counter.bumpCount(1);
if (counter.getValue() % 10 == 0)
{
    runSpeed += 5;
    Greenfoot.setSpeed(runSpeed);
}
in the eatAberry() method
Swedishsam Swedishsam

2012/5/5

#
I'm not quite sure how to apply the first bit of code, i applied it in several different ways but that made it so when i pressed up it went left or otherwise it wouldn't move at all. If you could explain how i would make this similar for all the directions that would be very much appreciated.
danpost danpost

2012/5/5

#
This is the new 'moveAndTurn()' method (for now)
//This is what determines the movement of the snake in a left,right,up and down manner.
public void moveAndTurn()
{
    move(2);
    if (Greenfoot.isKeyDown("left"))
    {
        if (getRotation() != 0) setRotation(180);
    }
    if (Greenfoot.isKeyDown("right"))
    {
        if (getRotation() != 180) setRotation(0);
    }
    if (Greenfoot.isKeyDown("down"))
    {
        if (getRotation() != 270) setRotation(90);
    }
    if (Greenfoot.isKeyDown("up"))
    {
        if (getRotation() != 90) setRotation(270);
    }
}
I have noticed that this code will always favour the 'up' key (meaning if the up key is pressed with any combination of other arrow keys, the resultant direction will be up).
Swedishsam Swedishsam

2012/5/5

#
Ok, seems im not really cut out for doing this type of stuff, i added the speed thing to the counter and now im getting a cannot find symbol method - getValue
danpost danpost

2012/5/5

#
I assumed, since you had a bumpCount(int) method in the counter class, that you would also have a method in it to return the value of the counter (normally, it would be called getValue()). Check the counter class code and see if there is a (public) method that returns the value of the counter, and if you find it, use that method name instead of 'getValue()'. If you do not find anything, post the counter class code for help.
Swedishsam Swedishsam

2012/5/5

#
I cant seem to able to tell what method name im supposed to use, Here is all the code that is used for the counter. In the Snake Class
    //This is the counter that adds a point everytime a berry is eaten in the game.
    private void eatAberry()
    {
        SnakeWorld snakeWorld = (SnakeWorld) getWorld();
        Counter counter = snakeWorld.getCounter();
       counter.bumpCount(1);  
       if (() % 10 == 0)  
       {  
        runSpeed += 5;  
        Greenfoot.setSpeed(runSpeed);  
       }  
        
    }
    
In SnakeWorld
    private berry  berry1;
    private Counter theCounter;

    
    //This shows what runs as the game starts.
    public SnakeWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        setBackground("Black.jpg");
        prepare();
        placeberry(1);
        theCounter = new Counter();
        addObject(theCounter, 40, 340);
    }
    
    
    //This is a method so that the snake can retrieve the value of the counter.
        public Counter getCounter()
    {
        return theCounter;
    }
In the counter class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter  extends Actor
{
    private int totalCount = 0;
   
    //This is what sets the display of the counter.
    public Counter()
    {
        setImage(new GreenfootImage("0", 20, Color.WHITE, Color.BLACK));
    }


    //This increases the number displayed on teh counter by a certain amount, in this case 1.
    public void bumpCount(int amount)
    {
        totalCount += amount;
        setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK));
    }
}
danpost danpost

2012/5/6

#
It appears you do not have a way to get the value of the counter out of its class, SO... INSERT, BETWEEN lines 26 and 27 of the Counter class, the following method:
//This returns the value of the counter
public int getValue()
{
    return totalCount;
}
Then change line 7 of the eatAberry() method of the Snake class to
if (counter.getValue() % 10 == 0)
Swedishsam Swedishsam

2012/5/6

#
//
You need to login to post a reply.