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

2013/6/23

Can some one help me with this?

derp2000 derp2000

2013/6/23

#
These are the code's I got for my game :
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Wereld here.
 * 
 * @author (Harmen Bakker) 
 * @version (a version number or a date)
 */
public class Wereld extends World
{
     // Verklaar class variablen
    private int counter = 0;
    private int spawnRate;
    private int spawnY;
    private int vehicleSpeed;

    private int lives = 3;

    private int level;
    private int autoCount;

    private boolean levelLoaded = false;
    private boolean spawning;

    private boolean actionPaused = false;
    private boolean showScore = false;
    private boolean deathSequenceActive = false;

    // Not implemented yet:
    private int chanceOfDouble;
    private int chanceOfTriple;

    // Verklaar objecten
    private LifeCounter lifeCounter = new LifeCounter ();
    private LevelCounter levelCounter = new LevelCounter ();
    private Leerling myLeerling = new Leerling();
    private Level currentLevel;
    
   public Wereld()
    {    
        // Maak een nieuwe wereld met 600x400 cells met een cell groote van 1x1 pixels.
        super(800, 600, 1); 
        
        // Voeg de LifeCounter toe aan de wereld
        addObject (lifeCounter, 67, 25);
        addObject (levelCounter, 530, 24);
       
        // Zet het level op 1
        level = 1;
        autoCount = 0;
    }

    /**
     * The Act method is responsible for:
     *      loading new levels and death screens
     *      Triggering scoreboard and end the game
     *      Spawning cars
     */
    public void act ()
    {
        // Laad level als nodig
        if (levelLoaded == false)
        {
            // Laad level als speler levens heeft
            if (lives > 0)
            {
                levelLoader();
                levelLoaded = true;
            }
            else
            {
                ScoreBoard s = new ScoreBoard (level, "Game Over", "On Level ");
                addObject (s, 300,200);
                // stop pragamma
                Greenfoot.stop();
            }
        }   
       
       
        
       
        if (deathSequenceActive)
        {
            if (autoCount == 0)
            {
                deathSequenceActive = false;
                levelLoaded = false;
            }
        }
        
        
        if (actionPaused == false && spawning == true)
        {
           spawnCars();
        }
       

    }
 
    
        private void levelLoader ()
    {
        // Voeg een nieuwe leerling toe
        addObject(new Leerling(), 400, 570);

        // Update the info stored in currentLevel
        currentLevel = new Level (level);

        levelCounter.setLevel(level);

        myLeerling.setImage("LeerlingImage.png");
        
        // change appropriate booleans for game flow
        actionPaused = false;
        spawning = true;

        // load instance variables from Level
        spawnRate = currentLevel.getSpawnRate();
        vehicleSpeed = currentLevel.getVehicleSpeed();
        chanceOfDouble = currentLevel.getChanceOfDouble();
        chanceOfTriple = currentLevel.getChanceOfTriple();
        myLeerling.setSpeed(currentLevel.getLeerlingSpeed());

        // Ensure that level is not loaded again until necessary
        levelLoaded = true;
    }

   /**
     * Controlled spawning of new cars
     */
    private void spawnCars()
    {
        // Generate a random value to decide in which lane to spawn
        // a car
        int randVal = Greenfoot.getRandomNumber(3)+1;

        // Spawn a single car every 50 acts
        if (counter % spawnRate == 0)
        {
            if (randVal == 1)
                spawnY = 127;
            else if (randVal == 2)
                spawnY = 223;
            else
                spawnY = 320;
            addObject(new Car1(vehicleSpeed), 20, spawnY);
            autoCount++;
        }
        else if (counter % spawnRate == spawnRate / 2)
        {
            if (randVal == 1)
                spawnY = 78;
            else if (randVal == 2)
                spawnY = 173;
            else
                spawnY = 270;
            addObject(new Car2(vehicleSpeed), 578, spawnY);
            // keep track of how many cars on on the screen, so that
            // you can be sure they all leave the screen before the 
            // Frogger respawns after dying
            autoCount++;
        }
        counter++;
    }



    /**
     * This method gets called when the player dies or finishes a level
     * 
     * True = player completed level
     * False = player died
     */
    public void endLevel(boolean win)
    {
        ScoreBoard s = null;
        actionPaused = true;
        spawning = false;
        if (win == true)
        {
            level++;
            removeObject (myLeerling);
            levelLoaded = false;
            Greenfoot.delay(50);

        }
        else
        {
            // Change the Frogger's image to the bloody frog
          
           
            lives -= 1;
            lifeCounter.subtractLife();
            actionPaused = false;

            deathSequenceActive = true;

            removeObject (myLeerling);
        }
    }

    /**
     * Returns whether the current gamestate is paused (true) or not (false)
     */
    public boolean isActionPaused ()
    {
        return actionPaused;
    }

    /**
     * Allows objects to inform this controller when they are about to 
     * remove themselves from the world.
     */
    public void reduceAutoCount (int byHowMany)
    {
        autoCount -= byHowMany;
    }

    
    
}

That was my world code here are the actor classes:
/**
 * Write a description of class LifeCounter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LifeCounter extends Actor
{

    private int livesLeft;

    private GreenfootImage frog = new GreenfootImage ("frog.png");
    private GreenfootImage splat = new GreenfootImage ("splat.png");
    private GreenfootImage lives = new GreenfootImage (126,40);
    
    public LifeCounter ()
    {
        lives.drawImage (frog, 1, 1);
        lives.drawImage (frog, 43, 1);
        lives.drawImage (frog, 85, 1);
        this.setImage (lives);
        
        livesLeft = 3;
    }

    public void subtractLife()
    {
        livesLeft--;
        lives.drawImage (splat, 1 + (livesLeft) * 42, 1);
    }
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class FinishLine here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class FinishLine extends Actor
{
    /**
     * Act - do whatever the FinishLine 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.
    }    
}
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
import java.awt.Font;
import java.util.Calendar;

/**
 * The ScoreBoard is used to display results on the screen. It can display some
 * text and several numbers. Updated by Jordan Cohen to allow multiple constructors
 * which change the labels.
 *
 * @author M Kolling
 * @version 1.0
 * 
 * @Modified by Jordan Cohen to provide a few alternate constructors and uses.
 */
public class ScoreBoard extends Actor
{
    public static final float FONT_SIZE = 48.0f;
    public static final int WIDTH = 400;
    public static final int HEIGHT = 300;

    /**
     * Create a score board with dummy result for testing.
     */
    public ScoreBoard()
    {
        this(100);
    }

    /**
     * Create a score board for the final result.
     */
    public ScoreBoard(int score)
    {
        makeImage("Game Over", "Score: ", score);
    }
    public ScoreBoard(int score, String mess)
    {
        makeImage (mess, "Score: ", score);
    }
    public ScoreBoard(int score, String mess, String desc)
    {
        makeImage (mess, desc, score);
    }
    

    /**
     * Make the score board image.
     */
    private void makeImage(String title, String prefix, int score)
    {
        GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);

        image.setColor(new Color(0, 0, 0, 160));
        image.fillRect(0, 0, WIDTH, HEIGHT);
        image.setColor(new Color(255, 255, 255, 100));
        image.fillRect(5, 5, WIDTH-10, HEIGHT-10);
        Font font = image.getFont();
        font = font.deriveFont(FONT_SIZE);
        image.setFont(font);
        image.setColor(Color.WHITE);
        image.drawString(title, 60, 100);
        image.drawString(prefix + score, 60, 200);
        setImage(image);
    }
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * De Leerling * * Door Harmen Bakker */ public class Leerling extends Actor { private int speed; private boolean active; public Leerling () { active = true; speed = 2; } /** * Act - do whatever the Frogger wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkKeys(); if (checkWin() ) { endGame(true); } else if(checkDeath() && active == true) { active = false; endGame(false); } } public void addedToWorld (World w) { active = true; } private void checkKeys() { if (active == true) { if (Greenfoot.isKeyDown("up")) { setLocation (getX(), getY() - speed); } if (Greenfoot.isKeyDown("down")) { setLocation (getX(), getY() + speed); } if (Greenfoot.isKeyDown("left")) { setLocation (getX() - speed, getY()); } if (Greenfoot.isKeyDown("right")) { setLocation (getX() + speed, getY()); } } } private boolean checkDeath() { Enemy e = (Enemy)getOneIntersectingObject(Enemy.class); if (e != null) { // if an enemy is detected colliding with Frogger, then death = true :( return true; } return false; } private boolean checkWin() { if (getY() <= 32) { active = false; return true; } else return false; } /** * endGame method will expect true (for success) or false (for failure!) */ private void endGame (boolean win) { // Return a message declaring win or lose to the Desert (World) Wereld d = (Wereld)getWorld(); d.endLevel (win); } public void setSpeed (int speed) { this.speed = speed; } }
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy here.
 * 
 * @author (Harmen) 
 * @version (a version number or a date)
 */
public abstract class Enemy extends Actor
{
    /**
     * Act - do whatever the Enemy 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 int getSpeed ()
    {
        return 0;
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Car here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Car2 extends Enemy
{
    private int speed;
    private Wereld d;

    public Car2 (int speed)
    {
        this.speed = speed;

    }

    public void addedToWorld (World w)
    {
        d = (Wereld)getWorld();
    }

    public void act() 
    {

        if (d.isActionPaused() == false)
        {
            if (!(atWorldEdge()))
            {
                Enemy carDirectlyInFrontOfMe = (Enemy)getOneObjectAtOffset (getImage().getWidth()/2 + speed, 0, Enemy.class);
                if (carDirectlyInFrontOfMe == null)
                    setLocation (getX() + speed, getY());
                else
                    speed = carDirectlyInFrontOfMe.getSpeed();
            }
            else 
            {
                d.reduceAutoCount(1);
                d.removeObject(this);
            }
        }
    }    

    public boolean atWorldEdge()
    {
        if(getX() > getWorld().getWidth() - (getImage().getWidth() / 2) + 20)
            return true;

        else
            return false;
    }
    public int getSpeed ()
    {
        return speed;
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Car here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Car1 extends Enemy
{
    private int speed;
    private Wereld d;

    public Car1 (int speed)
    {
        this.speed = speed;

    }

    public void addedToWorld (World w)
    {
        d = (Wereld)getWorld();
    }

    public void act() 
    {

        if (d.isActionPaused() == false)
        {
            if (!(atWorldEdge()))
            {
                Enemy carDirectlyInFrontOfMe = (Enemy)getOneObjectAtOffset (getImage().getWidth()/2 + speed, 0, Enemy.class);
                if (carDirectlyInFrontOfMe == null)
                    setLocation (getX() + speed, getY());
                else
                    speed = carDirectlyInFrontOfMe.getSpeed();
            }
            else 
            {
                d.reduceAutoCount(1);
                d.removeObject(this);
            }
        }
    }    

    public boolean atWorldEdge()
    {
        if(getX() > getWorld().getWidth() - (getImage().getWidth() / 2) + 20)
            return true;

        else
            return false;
    }
    public int getSpeed ()
    {
        return speed;
    }
}
End then ther is another class named level, The class ain't a part of actor
/**
 * Write a description of class Level here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level  
{
    private int spawnRate;
    private int vehicleSpeed;
    private int chanceOfDouble;
    private int chanceOfTriple;
    private int leerlingSpeed;
    /**
     * Constructor for objects of class Level
     */
    public Level(int levelNum)
    {
        loadLevel (levelNum);
    }

    // Load the data for the current level
    // - crude code, but wanted to avoid using the file system so that
    //   the final product could easily be shared via Greenfoot Gallery
    private void loadLevel (int levelNum)
    {
        if (levelNum == 1)
        {
            spawnRate = 100;
            vehicleSpeed = 3;
            chanceOfDouble = 0;
            chanceOfTriple = 0;
            leerlingSpeed = 2;
        }
        else if (levelNum == 2)
        {
            spawnRate = 96;
            vehicleSpeed = 3;
            chanceOfDouble = 10;
            chanceOfTriple = 5;
            leerlingSpeed = 2;
        }
        else if (levelNum == 3)
        {
            spawnRate = 90;
            vehicleSpeed = 4;
            chanceOfDouble = 15;
            chanceOfTriple = 10;
            leerlingSpeed = 3;
        }
        else if (levelNum == 4)
        {
            spawnRate = 84;
            vehicleSpeed = 4; 
            chanceOfDouble = 25;
            chanceOfTriple = 10;
            leerlingSpeed = 3;
        }
        else if (levelNum == 5)
        {
            spawnRate = 78;
            vehicleSpeed = 4; 
            chanceOfDouble = 30;
            chanceOfTriple = 15;
            leerlingSpeed = 3;
        }
        else if (levelNum == 6)
        {
            spawnRate = 72;
            vehicleSpeed = 4; 
            chanceOfDouble = 30;
            chanceOfTriple = 15;
            leerlingSpeed = 3;
        }            
        else if (levelNum == 7)
        {
            spawnRate = 70;
            vehicleSpeed = 5; 
            chanceOfDouble = 35;
            chanceOfTriple = 25;
            leerlingSpeed = 4;
        }
        else if (levelNum == 8)
        {
            spawnRate = 64;
            vehicleSpeed = 5; 
            chanceOfDouble = 50;
            chanceOfTriple = 25;
            leerlingSpeed = 4;
        }
        else if (levelNum == 9)
        {
            spawnRate = 50;
            vehicleSpeed = 6; 
            chanceOfDouble = 60;
            chanceOfTriple = 30;
            leerlingSpeed = 5;
        }
        else if (levelNum >= 9)
        {
            spawnRate = 48;
            vehicleSpeed = 7; 
            chanceOfDouble = 60;
            chanceOfTriple = 30;
            leerlingSpeed = 6;
        }
        

    }
    public int getSpawnRate ()
    {
        return spawnRate;
    }

    public int getVehicleSpeed ()
    {
        return vehicleSpeed;
    }

    public int getChanceOfDouble ()
    {
        return chanceOfDouble;
    }

    public int getChanceOfTriple ()
    {
        return chanceOfTriple;
    }
    
    public int getLeerlingSpeed ()
    {
        return leerlingSpeed;
    }

}
What's the problem you're having, and where specifically is the error (if there is one).
derp2000 derp2000

2013/6/23

#
Well, there is no error but when you start the game, Car2 is acting weird and if you get past level one the game gets stuck and just continious to count up the levels and you can't move
I know you've already posted a lot of code, but what does your setLevel() method look like in the LevelCounter class? I think the problem might be there. Also, I think it might be something where you keep calling level++ in the Wereld endLevel() method, because something somewhere might be calling it, but you have so much code that I cannot tell for sure where the source of the problem is.
derp2000 derp2000

2013/6/23

#
 public void setLevel (int level)
    {
        levelBoard.setColor (backgroundColor);
        levelBoard.fill();
        levelBoard.setColor (textColor);
        String s = "LEVEL " + level;
        levelBoard.drawString(s, 1, 32);
    }
This is the setlevel
You need to login to post a reply.