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

2012/1/16

Greenfoot not displaying anything

1
2
danpost danpost

2012/1/19

#
If you are just trying to create a text image use 'GreenfootImage img = new GreenfootImage("This is my text.",fontSize, foreColor, backColor);' where fontSize can be a literal integer or a assigned variable integer, and foreColor and backColor are literal Colors or assigned variable Colors. If you are trying to apply text to an already created image use 'alreadyCreatedImageName.drawString("This is also my text", xLocation, yLocation);' where alreadyCreatedImageName is the name you gave the image for which you want to apply text to, and xLocation and yLocation are integer literals or assigned variable integers. Also, the text can be a literal, as I show here, or an assigned variable String.
Mete0r1t3 Mete0r1t3

2012/1/19

#
ok now i have another problem i cant get the snake parts to show. SnakeWorld:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class SnakeWorld extends World
{
    private int i = 1;
    private double score;
    public String scoreS = "Score: ", scoreS2;
    public SnakeWorld(){
        super(500,500,1);
        score = 0.0;
        addObject(h, 250, 250);
        setPaintOrder(SnakeHead.class, SnakePart.class, Food.class); 
        addObject(f, randInt(400) + 50, randInt(400) + 50);
        addObject(scoreHUD, 50,10);
        Greenfoot.setSpeed(25);
        firstCycle = true;
    }
    public SnakeHead h = new SnakeHead();
    public Food f = new Food();
    public SnakePart[] p = new SnakePart[100];
    private Score scoreHUD = new Score();
    private Music music = new Music(); 
    public final int NORTH = 359;
    public final int SOUTH = 89;
    public final int EAST = 0;
    public final int WEST = 179;
    private boolean firstCycle;
    public int randInt(int lim){
        return Greenfoot.getRandomNumber(lim);
    }

    public void act(){
        scoreS2 = scoreS.valueOf(score);
        scoreS.concat(scoreS2);
        if(firstCycle){
            music.start();
        }
        
        makeFood();

        if(h.isDead()){
            music.playMusic = false;
            youDied();
        }
        score += 0.1;
        firstCycle = false;
        scoreS = "Score: ";
    }
    private int fx, fy;
    public void makeFood(){
        if(!f.exist()){
            removeObject(f);
            fx = randInt(400) + 50;
            fy = randInt(400) + 50;
            if(h.getX() != fx || (h.getX() != fx + 7.5) || (h.getX() + 7.5 != fx) || (h.getX() != fx - 7.5) || (h.getX() - 7.5!= fx)){                
                if(h.getY() != fy || (h.getY() != fy + 7.5) || (h.getY() + 7.5 != fy) || (h.getY() != fy - 7.5) || (h.getY() - 7.5!= fy)){
                    addObject(f, fx, fy);
                }
            }
        }
    }
    private int px, py;
    public void newPartX(int x){
        px = x;
    }

    public void newPartY(int y){
        py = y;
    }

    public void incI(){
        i++;
    }

    public int i(){
        return i;
    }

    private void youDied(){
        //delay() 

        System.out.println("GAME OVER \n YOU SCORED: \n " + score);
        Greenfoot.stop();
    }

    public double score(){
        return score;
    }

    public void scoreIncF(){
        score += 100;
    }

}
SnakeHead:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class SnakeHead extends Actor
{
    public SnakeHead(){
        dead = false;
        setLocation(250,250);
    }
    private int trailCT = 0, trailMax = 0;
    /**
     * 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() 
    {

        // Add your action code here.
        if(Greenfoot.isKeyDown("up")){
            setRotation(269);
        }

        if(Greenfoot.isKeyDown("down")){
            setRotation(89);
        }            

        if(Greenfoot.isKeyDown("left")){
            setRotation(179);
        }

        if(Greenfoot.isKeyDown("right")){
            setRotation(0);
        }

        this.move(15);
        eatFood();
        oOB();
    }

    public boolean foundFood(){
        SnakeWorld w = ((SnakeWorld) getWorld());
        if(intersects(w.f)){
            w.f.existM(false);
            return true;
        }else {
            w.f.existM(true);
            return false;
        }
    }

    private void oOB(){
        if((getX() < 0 || getX() > 500) || (getX() - 7.5 < 0 || getX() + 7.5 > 500)){
            die();
        }
        if((getY() < 0 || getY() > 500) || (getY() - 7.5 < 0 || getY() + 7.5 > 500)){
            die();
        }
    }

    public void eatFood(){
        SnakeWorld w = ((SnakeWorld) getWorld());
        if(foundFood()){
            w.f.existM(false);
            
            w.scoreIncF();

            trailMax += 1;
            growSnake();
        }
    }
    private boolean dead;

    public void growSnake(){
        SnakeWorld w = ((SnakeWorld) getWorld());
        if(trailCT < trailMax){
            for(int y = trailCT; y < trailMax; y++){
                if(trailCT == 0){
                    if(getRotation() == w.NORTH){
                        w.addObject(w.p[w.i()],getX(),getY()+15);                        
                        w.p[w.i()].construct(w.i(), getX(), getY()+15);
                    }else if(getRotation() == w.SOUTH){
                        w.addObject(w.p[w.i()],getX(),getY()-15);
                        w.p[w.i()].construct(w.i(), getX(), getY()-15);
                    }else if(getRotation() == w.EAST){
                        w.addObject(w.p[w.i()],getX()+15,getY());
                        w.p[w.i()].construct(w.i(), getX()+15, getY());
                    }else if(getRotation() == w.WEST){
                        w.addObject(w.p[w.i()],getX()-15,getY());
                        w.p[w.i()].construct(w.i(), getX()-15, getY());
                    }
                    w.incI();
                }
            }
        }
    }

    public boolean isDead(){
        return dead;
    }

    public void die(){
        dead = true;
    }
}
SnakePart:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class SnakePart extends Actor
{
    public SnakePart(){
        exist = true;
    }
    public boolean exist;
    private int i;
    public void construct(int trailNum, int x, int y){
        i = trailNum;
        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() 
    {
        // Add your action code here.
        SnakeWorld w = ((SnakeWorld) getWorld());
        if(intersects(w.h)){
            w.h.die();
        }
    }
    
    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(){
        SnakeWorld w = ((SnakeWorld) getWorld());
        if(this == w.p[1]){
            turn2face(w.h);
            move(15);
        }else {
            turn2face(w.p[i-1]);
            move(15);
        }
    }
}
Score:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;

public class Score extends Actor
{
    public Score(){
        setLocation(50,10);
    }
    private SnakeWorld w = ((SnakeWorld)getWorld());
    public void scorePut(){
        Color red = new Color(255,0,0);
        
        Color transparent = new Color(0,0,0,0);
        GreenfootImage img = new GreenfootImage("Score: ",16, red, transparent);
        GreenfootImage scoreBoard = new GreenfootImage(500,30);       
        scoreBoard.drawString("Score: ", 50, 5);
    }

    /**
     * Act - do whatever the Score wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.        
        scorePut();
    }
}
Food:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Food extends Actor
{
    public Food(){
        exist = true;
    }
    private boolean exist;
    /**
     * Act - do whatever the Food wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }

    public boolean exist(){
        return exist;
    }

    public void existM(boolean b){
        exist = b;
    }
}
Music:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Music extends Thread
{
    private int song;
    public boolean playMusic;
    public Music(){
        playMusic = true;
    }
    /**
     * Act - do whatever the Music wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void run() 
    {        
        // Add your action code here.
        GreenfootSound a = new GreenfootSound("Goof.mp3");
        GreenfootSound b = new GreenfootSound("Sandjorda.mp3");
        GreenfootSound c = new GreenfootSound("Widibf.mp3");
        GreenfootSound d = new GreenfootSound("Fuayfsilfm.mp3");
        GreenfootSound e = new GreenfootSound("Still Alive.mp3");

        a.setVolume(50);
        b.setVolume(50);
        c.setVolume(50);
        d.setVolume(50);
        e.setVolume(50);
        while(playMusic){
            song = randInt(4);
            if(!(a.isPlaying() || b.isPlaying() || c.isPlaying() || d.isPlaying() || e.isPlaying())){
                if(song == 0){
                    a.play();
                }else if(song == 1){
                    b.play();
                }else if(song == 2){
                    c.play();
                }else if(song == 3){
                    d.play();
                }else if(song == 4){
                    e.play();
                }
            }
        }
    }
    
    public int randInt(int lim){
        return Greenfoot.getRandomNumber(lim);
    }
}
danpost danpost

2012/1/19

#
From within SnakeHead, remove line 7: for one, it is not in the world yet; and two, the addObject command in world has where it needs to be located. From within Score, remove line 7: for the same reasons as above, except what is the name of this class (in the world class, you are adding an object called ScoreHUD, but in the score class, it is called Score? Also, from within Score, remove line 9: firstly, you are not using that reference in this class; and secondly, it will always be null as when the object is created there is not yet a world for it, so getWorld() returns null. Line 16 in Score ( scoreBoard.drawString("Score: ", 50, 5); ) : the y-location ( 5 ) needs to be more like ( 22 ) as it is the location of the base-line of the text. From within SnakeWorld, lines 23 through 26: the values should not be decremented as you show them; use 0, 90, 180, and 270 (270 for NORTH, you had shown 359, which would be 0 (- 1) ). Finally, line 20 in SnakeWorld initiates a new array of 100 SnakeParts, but each element is still null; so, you need to set each equal to a new SnakePart() as the snake grows. This is why no SnakeParts were displaying. Was this even compiling, as you had shown it here (I cannot see how it could have)?
kiarocks kiarocks

2012/1/19

#
scoreHud is defined in his code as Score scoreHUD = new Score();
Mete0r1t3 Mete0r1t3

2012/1/19

#
yes it was compiling but it would occasionally give me some errors regarding the background music
Mete0r1t3 Mete0r1t3

2012/1/19

#
anyway, it still does not work (snakeparts are not showing up), and it due tomoro so...(well today now) but ill keep on working on it and eventually upload it here :D and...

THANK YOU SO MUCH

for your help

You need to login to post a reply.
1
2