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

2012/1/16

Greenfoot not displaying anything

1
2
Mete0r1t3 Mete0r1t3

2012/1/16

#
So, i am creating a snake game in greenfoot and i've run into a bit of a problem. My program is not doing anything from what i can see; a while ago it was running ok but half of it wasnt even coded back then. Now it isn't even showing anything. I've tried rewritting the code (different algorithms, etc.) but still nothing ive attached the code for the classes in this post so that you guys can take a look and see what is going on. The world class:
import greenfoot.*;
import java.util.*;
public class Main extends World
{
    /**
     * Constructor for objects of class SnakeWorld.
     */
    public Main()
    {    
        // Create a new world with 500x500 cells with a cell size of 15x15 pixels.
        super(500, 500, 15); 
        addObject(h, 250, 250);
        addObject(f, r.nextInt(500), r.nextInt(500));
        //GreenfootImage baseImage = new GreenfootImage("board.jpg");
        setPaintOrder(SnakeHead.class, SnakePart.class, Food.class); 

    }

    public  int i = 1;
    public SnakeHead h = new SnakeHead();
    public SnakePart p = new SnakePart(0);
    public Food f = new Food();
    public Random r = new Random();

    
    public void act(){
        h.act();
        if(!f.exist()){
            addObject(f, r.nextInt(500), r.nextInt(500));
        }

        if(h.isDead() == true){
            removeObject(h);
            for(int j = 1; j<=i; j++){
                removeObject(p);
            }
            removeObject(f);
        }

        if(i > h.trailMax()){
            for(int j = 1; j <= (h.trailCT - h.trailMax()); j++){
                if(p.i < (h.trailCT - h.trailMax())){
                    removeObject(p);
                }
            }
        }
    }

    public SnakeHead head(){
        return h;
    }

    public int snakePartNum(){
        return i;
    }
}
 
The food class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Food extends Actor
{
    public World w = getWorld();
    /**
     * Act - do whatever the Food wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Food(){
        exist = true;
        setImage("food.png");
    }
    private boolean exist;
    public boolean exist(){
        return exist;
    }
    //SnakeHead h = new SnakeHead();
    public void act() 
    {
        
        Actor snake = getOneObjectAtOffset(0, 0, SnakeHead.class);
        exist = true;
        if(snake != null){
            exist = false;
        }
    }    
}
The SnakeHead class
import java.util.*;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class SnakeHead extends Actor
{
    private World w = getWorld();
    public int trail = 0;
    public int trailCT = 0;

    public SnakeHead(){

        this.setImage("red.png");
        setLocation(250,250);
    }

    /**
     * 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() 
    {
        control();

    }

    private Main m = new Main();
    private Food f = new Food();
    public boolean foundFood(){
        Actor food = getOneObjectAtOffset(0, 0, Food.class);
        if(food != null){
            return true;
        } else {
            return false;
        }
    }

    public void eatFood(){
        Actor food = getOneObjectAtOffset(0, 0, Food.class);
        if(food != null){
            getWorld().removeObject(food);
            trailMax += 3;
            growSnake();
        }
    }
    public  int hr;
    public void collision(){
        for(int i = 0; i <= m.snakePartNum(); i++){
            if(intersects(m.p)){
                die();
            }            
        }
    }

    public void control(){
        while (!isDead()){
            if(Greenfoot.getKey() == "up"){
                setRotation(269);                
            }else if(Greenfoot.getKey() == "down"){
                setRotation(89);                
            }else if(Greenfoot.getKey() == "left"){
                setRotation(179);                
            }else if(Greenfoot.getKey() == "right"){
                setRotation(0);                
            }else{
                this.move(1);
            }
            hr = getRotation();
            this.move(1);
            if(foundFood()){
                eatFood();
            }
        }
    }

    private boolean grow;
    public boolean growable(){
        return grow;
    }
    private int trailMax = 2;

    public int trailMax(){
        return trailMax;
    }

    public void growSnake(){
        grow(true);


        if(trail <= trailMax){
            trail++;
            for(; trail <= trailMax; trailCT++){
                if (trail == 1){
                    getWorld().addObject(new SnakePart(trail), getX() , getY());
                    trail = 0;
                }
            }
        }
    }

    public void grow(boolean b){
        grow = b;
    }
    private boolean dead = false;
    public boolean isDead(){
        return dead;
    }

    public void die(){
        dead = true;
    }
}
The SnakePart class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
public class SnakePart  extends Actor
{
    public Main w = new Main();
    public SnakePart(int trailCT){
        i = trailCT;
        exist = true;
    }
    public int i;
    /**
     * Act - do whatever the SnakePart wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        this.setImage("green.png");
        setLocation(getX(),getY());

        //while(!SnakeHead.isDead()){
        //followPrev();
        //}
        
        
    }
    public boolean exist;
    public SnakeHead sh = new SnakeHead();
    public int r = getRotation();
    //public int prevR = w.p(i-1).getRotation();

    

    public void turn2faceP(SnakePart p){
        setRotation((int)(180*Math.atan2(p.getY()-getY(),p.getX()-getX())/Math.PI)); 
    }
    /*
    public void followPrev(){

        //if(head != null){  
        //   turn2face(head);  
        ///

        if(this == w.p(0)){
            //do nothing
        }else {
            while(!intersects(w.p[i-1])){
                if(prevR = 
                setLocation(
            }
        }
    }*/
}
davmac davmac

2012/1/16

#
When Main is created, it creates a new SnakePart (line 21). When a SnakePart is created, it creates a new Main (line 5) - which creates a new SnakePart, which creates a new Main, which.... ... you see the problem? :)
Mete0r1t3 Mete0r1t3

2012/1/16

#
oh i see....so is there anyway to be able to access the data in main from snakepart without creating a new main? btw i have tried using getWorld() but it seems to be a sort of a hit and miss situation with that where the actors would only be able to access certain things (and yes those inaccessible things are set to public).
danpost danpost

2012/1/16

#
I am not totally sure about it, but I believe that World methods that are inherited can be called from an actor via getWorld(), whereas variables and methods YOU code and write need to be called with ((WorldName) getWorld()), where WorldName is the name of YOUR world.
Mete0r1t3 Mete0r1t3

2012/1/16

#
ohh....that makes sense, one question with the (WorldName)getWorld() is there a dot after the (WorldName) and do i need to drop the brackets around (WorldName)?
danpost danpost

2012/1/16

#
(WorldName) is a casting -- there should not be a dot after it. Your world name, I see, is Main; so let us say you were in SnakeHead and wanted to refer to f in Main class, which is a Food object.
Food food = ((Main) getWorld()).f;
would set 'food' to be a reference in the SnakeHead class to the food object 'f' in the Main class. If you wanted to remove the food
getWorld().removeObject(((Main) getWorld()).f);
should work in SnakeHead.
Mete0r1t3 Mete0r1t3

2012/1/16

#
ok ill try it out thanks a lot guys ps ill give you guys some credit in the game info file :)
davmac davmac

2012/1/16

#
Also, getWorld() returns null, until the actor has been added in to the world. So normally you shouldn't call it from an actor's constructor or initializer. For example line 4 of the Food class is wrong (and pointless).
Mete0r1t3 Mete0r1t3

2012/1/18

#
hey guys i've tried what you guys had suggested but for some reason it still does not work ill attach the code again so you can check it out Main (the world class):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;

public class Main extends World
{
    /**
     * Constructor for objects of class SnakeWorld.
     */
    public Main()
    {    
        // Create a new world with 500x500 cells with a cell size of 15x15 pixels.
        super(500, 500, 15); 
        
        setBackground("board.jpg");
        
        

    }
    public final int NORTH = 359;
    public final int SOUTH = 89;
    public final int EAST = 0;
    public final int WEST = 179;
    
    
    private  int i = 1;
    public SnakeHead h = new SnakeHead();

    
    public Food f = new Food();
    public Random r = new Random();
    public SnakePart[] p = new SnakePart[i];

    public void act(){
        setPaintOrder(SnakeHead.class, SnakePart.class, Food.class); 
        addObject(h, 250, 250);
        Food f = new Food();
        addObject(f, r.nextInt(500), r.nextInt(500));
        h.act();
        for(int x = i; x <= i; x++){
            p[x].construct(x, px, py);
        }
        if(!f.exist()){
            addObject(f, r.nextInt(500), r.nextInt(500));
        }

        if(h.isDead() == true){
            removeObject(h);
            for(int j = 1; j<=i; j++){
                removeObject(p[j]);
            }
            removeObject(f);
        }

        if(i > h.trailMax()){
            for(int j = 1; j <= (h.trailCT - h.trailMax()); j++){
                if(p[j].i < (h.trailCT - h.trailMax())){
                    del = (h.trailMax() - h.trailCT);
                    
                    if((del * -1) > 0){
                        for(int k = 1; k <= (del * -1); k++){
                            removeObject(p[k]);
                        }
                    }
                }
            }
        }
        del += del;
    }
    private int del;
    

    public int snakePartNum(){
        return i;
    }
    private int px, py;
    public void newPartX(int x){
        px = x;
    }
    public void newPartY(int y){
        py = y;
    }
}
The food class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Food extends Actor
{
    
    /**
     * Act - do whatever the Food wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Food(){
        exist = true;
        setImage("food.png");
    }
    private boolean exist;
    public boolean exist(){
        return exist;
    }
    
    public void act() 
    {        
        Actor snake = getOneObjectAtOffset(0, 0, SnakeHead.class);
        exist = true;
        if(snake != null){
            exist = false;
        }
    }    
}
The SnakePart class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;

public class SnakePart  extends Actor
{
    
    public SnakePart(){
        
        exist = true;
    }
    public int i;
    public void construct(int trailCT, int x, int y){
        i = trailCT;
        setLocation(x,y);
    }
    
    /**
     * Act - do whatever the SnakePart wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        this.setImage("green.png");
        setLocation(getX(),getY());

        while(!SnakeHead.isDead()){
            followPrev();
        }
    }
    public boolean exist;
    public int r = getRotation();
    private Main m = ((Main) getWorld());
    public int prevR = m.p[i-1].getRotation();

    public void turn2face(SnakePart p){
        setRotation((int)(180*Math.atan2(p.getY()-getY(),p.getX()-getX())/Math.PI)); 
    }
    
    public void turn2face(SnakeHead h){
        setRotation((int)(180*Math.atan2(h.getY()-getY(),h.getX()-getX())/Math.PI)); 
    }
    
    public void followPrev(){
        
        if(this == m.p[0]){
            turn2face(m.h);
            move(1);            
        }else {
            turn2face(m.p[i-1]);
            move(1);
        }
    }
}
The SnakeHead class:
import java.util.*;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class SnakeHead extends Actor
{
    
    public int trail = 0;
    public int trailCT = 0;

    public SnakeHead(){

        this.setImage("red.png");
        setLocation(250,250);
    }

    /**
     * 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() 
    {
        control();

    }

    public boolean foundFood(){
        Actor food = getOneObjectAtOffset(0, 0, Food.class);
        if(food != null){
            return true;
        } else {
            return false;
        }
    }

    public void eatFood(){
        Actor food = getOneObjectAtOffset(0, 0, Food.class);
        if(food != null){
            getWorld().removeObject(food);
            trailMax += 3;
            growSnake();
        }
    }
    public int hr;
    public void collision(){
        Main m = ((Main) getWorld());
        for(int i = 0; i <= m.snakePartNum(); i++){
            if(intersects(m.p[i])){
                die();
            }            
        }
    }

    public void control(){
        while (!isDead()){
            if(Greenfoot.getKey() == "up"){
                setRotation(269);                
            }else if(Greenfoot.getKey() == "down"){
                setRotation(89);                
            }else if(Greenfoot.getKey() == "left"){
                setRotation(179);                
            }else if(Greenfoot.getKey() == "right"){
                setRotation(0);                
            }else{
                this.move(1);
            }
            hr = getRotation();
            this.move(1);
            if(foundFood()){
                eatFood();
            }
        }
    }

    private boolean grow;
    public boolean growable(){
        return grow;
    }
    private int trailMax = 2;

    public int trailMax(){
        return trailMax;
    }

    public void growSnake(){
        grow(true);
        Main m = ((Main) getWorld());
        if(trail <= trailMax){
            trail++;
            for(; trail <= trailMax; trailCT++){
                if (trail == 1){
                    if (getRotation() == m.NORTH){
                        m.newPartX(getX()); m.newPartY(getY() + 15); 
                    }else if(getRotation() == m.SOUTH){
                        m.newPartX(getX()); m.newPartY(getY() - 15);
                    }else if(getRotation() == m.EAST){
                        m.newPartX(getX() - 15); m.newPartY(getY());
                    }else if(getRotation() == m.WEST){
                        m.newPartX(getX() + 15); m.newPartY(getY());
                    }
                }
                trail = 0;
            }
        }
    }

    public void grow(boolean b){
        grow = b;
    }
    private static boolean dead = false;
    public static boolean isDead(){
        return dead;
    }

    public void die(){
        dead = true;
    }
}
kiarocks kiarocks

2012/1/18

#
Line 33 in SnakePart will not work, instead put
private Main m;
protected void addToWorld(World world)
{
    m = (Main) world;
}
danpost danpost

2012/1/18

#
There is so much that needs to be done, I really do not know where to start! Well, maybe I do -- Back to the Basics. (1) The 'super(int width, int height, int cellsize)' statement: -- the width and height are the width and height of each CELL; so if you have a cellsize of 15 and both width and height set at 500, your world will be HUGE! 7500 x 7500! -- all references to location within your world are based on cells (not pixels); so all distance-related parameters and variables and offsets need to be re-adjusted. (2) Comparing strings: -- getKey, keep in mind, returns a string OR null on the first call AND null on all subsequent calls within the same act cycle -- strings should be compared with the '.equals(String)' method; so you need to set getKey() to a variable (String key = Greenfoot.getKey();) and then check for null; if not null (if (key != null)), then compare the value of the variable to the different keys (if ("up".equals(key))). (3) Main class: -- the first 4 statements in you act() method should be in your Main() constructor -- it appears that the only thing the world needs to 'act' on, is to replenish the food (if (getObjects(Food.class).size() == 0) addObject(f, r.nextInt(500), r.nextInt(500));) and to check for h.isDead(); everything else can be accomplished within the specific actor classes. (4) There are probably other issues, but, for one, I do not have the time to elaborate on them now, and two, these aforementioned issues need be addressed first. Before I sign off, it would be a good idea to properly indent and document your code. It would not only help you with your coding, but others, as well, who are trying to help. Repost your code when you are done updating it. We will take another look.
kiarocks kiarocks

2012/1/18

#
I think width times height is cells per world, not cell width.
davmac davmac

2012/1/18

#
kiarocks, danpost is saying that in the call to the World (superclass) constructor on line 11: super(500, 500, 15); ... the world width and height are specified as 500 x 500 cells, and cell size is specified as 15x15 pixels, making the total world size 7500 x 7500 which, as danpost says, is huge.
danpost danpost

2012/1/18

#
In (3) above, when adding food, I used (r.nextInt(500)). Both of these will need to be adjusted to suit the actual world dimensions. A reasonable size might be (super(40, 40, 15)), which would make the world 600 x 600, and if that is what you make it, you would use (r.nextInt(40)) both times.
Mete0r1t3 Mete0r1t3

2012/1/19

#
thanks guys i followed your suggestions and the game is working as it should be, mostly, now I'm trying to add some new functions to the game EDIT: for some reason when im trying to draw a string onto the screen using:
GreenfootImage.drawString(java.lang.String, int x,int y) 
it keeps telling me that i can't use it there because it is a non static method and that im using it in a static context even though i am not. How could i fix this?
There are more replies on the next page.
1
2