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

2023/4/14

Can sombody tell me why the game does not work in the second space world?

stijn.b stijn.b

2023/4/14

#
I need to make a game for my school project but i just cant fix this https://www.greenfoot.org/scenarios/31131
danpost danpost

2023/4/14

#
stijn.b wrote...
I need to make a game for my school project but i just cant fix this https://www.greenfoot.org/scenarios/31131
We will need access to your codes for this project to help.
stijn.b stijn.b

2023/4/14

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Eindscherm here. * * @author (your name) * @version (a version number or a date) */ public class Eindscherm extends World { /** * Constructor for objects of class Eindscherm. * */ public Eindscherm() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); } }
stijn.b stijn.b

2023/4/14

#
public class Space extends World { //1: hier een veld maken die de Objectreferentie naar de Counter kan opslaan private Counter counter; /** * Constructor for objects of class Space. */ public Space() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); addObject(new Rocket(), 300, 340); //2: hier de Objectreferentie van 1: gebruiken om een nieuwe counter te maken counter = new Counter(); addObject(counter, 20,20); } //3: hier maak je een methode die de counter ophaalt. // Noem de methode maar: public Counter getCounter() public void getCounter() { counter.bumpCount(1); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { } public void act() { spawnAsteroid(); } public void spawnAsteroid() { if (Greenfoot.getRandomNumber(1000) < 3) { addObject(new Asteroid(), 0, 20); } } public class Counter extends Actor { private int totalCount = 0; public Counter() { setImage(new GreenfootImage("0", 20, Color.WHITE, Color.BLACK)); } /** * Increase the total amount displayed on the counter, by a given amount. */ public void bumpCount(int amount) { totalCount += amount; setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK)); if (totalCount >= 10) { Greenfoot.setWorld(new Tussenscherm()); } } } }
stijn.b stijn.b

2023/4/14

#
public class Space extends World { //1: hier een veld maken die de Objectreferentie naar de Counter kan opslaan private Counter counter; /** * Constructor for objects of class Space. */ public Space() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); addObject(new Rocket(), 300, 340); //2: hier de Objectreferentie van 1: gebruiken om een nieuwe counter te maken counter = new Counter(); addObject(counter, 20,20); } //3: hier maak je een methode die de counter ophaalt. // Noem de methode maar: public Counter getCounter() public void getCounter() { counter.bumpCount(1); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { } public void act() { spawnAsteroid(); } public void spawnAsteroid() { if (Greenfoot.getRandomNumber(1000) < 3) { addObject(new Asteroid(), 0, 20); } } public class Counter extends Actor { private int totalCount = 0; public Counter() { setImage(new GreenfootImage("0", 20, Color.WHITE, Color.BLACK)); } /** * Increase the total amount displayed on the counter, by a given amount. */ public void bumpCount(int amount) { totalCount += amount; setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK)); if (totalCount >= 10) { Greenfoot.setWorld(new Tussenscherm()); } } } }
stijn.b stijn.b

2023/4/14

#
public class Space extends World { //1: hier een veld maken die de Objectreferentie naar de Counter kan opslaan private Counter counter; /** * Constructor for objects of class Space. */ public Space() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); addObject(new Rocket(), 300, 340); //2: hier de Objectreferentie van 1: gebruiken om een nieuwe counter te maken counter = new Counter(); addObject(counter, 20,20); } //3: hier maak je een methode die de counter ophaalt. // Noem de methode maar: public Counter getCounter() public void getCounter() { counter.bumpCount(1); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { } public void act() { spawnAsteroid(); } public void spawnAsteroid() { if (Greenfoot.getRandomNumber(1000) < 3) { addObject(new Asteroid(), 0, 20); } } public class Counter extends Actor { private int totalCount = 0; public Counter() { setImage(new GreenfootImage("0", 20, Color.WHITE, Color.BLACK)); } /** * Increase the total amount displayed on the counter, by a given amount. */ public void bumpCount(int amount) { totalCount += amount; setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK)); if (totalCount >= 10) { Greenfoot.setWorld(new Tussenscherm()); } } } }
stijn.b stijn.b

2023/4/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Tussenscherm here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tussenscherm extends World
{

    /**
     * Constructor for objects of class Tussenscherm.
     * 
     */
    public Tussenscherm()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 700, 1); 
        addObject(new Timer(), 1290, 30);
    }
}
stijn.b stijn.b

2023/4/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Tussenscherm here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tussenscherm extends World
{

    /**
     * Constructor for objects of class Tussenscherm.
     * 
     */
    public Tussenscherm()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 700, 1); 
        addObject(new Timer(), 1290, 30);
    }
}
stijn.b stijn.b

2023/4/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


/**
 * 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;

    public Counter()
    {
        setImage(new GreenfootImage("0", 20, Color.WHITE, Color.BLACK));
    }

    public void bumpCount(int amount)
    {
        totalCount += amount;
        setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK));
    }
}
stijn.b stijn.b

2023/4/14

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * A Space ship which shoots at asteroids. * * @author Davin McCall */ public class Rocket extends Actor { private int shotTimer = 0; /** * Act - do whatever the Ship wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { shootYourShot(); } public void shootYourShot() { if (shotTimer > 0) { shotTimer = shotTimer - 1; } else if (Greenfoot.isKeyDown("space")) { getWorld().addObject(new Shot(this), getX(), getY()); Greenfoot.playSound("schietgeluid.mp3"); shotTimer = 200; // delay next shot } } }
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * A Space ship which shoots at asteroids.
 * 
 * @author Davin McCall
 */
public class Rocket  extends Actor
{
    private int shotTimer = 0;

    /**
     * Act - do whatever the Ship wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        shootYourShot();
    }
    
    public void shootYourShot() {
        if (shotTimer > 0) {
            shotTimer = shotTimer - 1;
        }
        else if (Greenfoot.isKeyDown("space")) {
            getWorld().addObject(new Shot(this), getX(), getY());
            Greenfoot.playSound("schietgeluid.mp3");
            shotTimer = 200; // delay next shot
        }
    }

}
stijn.b stijn.b

2023/4/14

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * A Space ship which shoots at asteroids. * * @author Davin McCall */ public class Rocket extends Actor { private int shotTimer = 0; /** * Act - do whatever the Ship wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { shootYourShot(); } public void shootYourShot() { if (shotTimer > 0) { shotTimer = shotTimer - 1; } else if (Greenfoot.isKeyDown("space")) { getWorld().addObject(new Shot(this), getX(), getY()); Greenfoot.playSound("schietgeluid.mp3"); shotTimer = 200; // delay next shot } } }
stijn.b stijn.b

2023/4/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class StartKnop here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class StartKnop extends Actor
{
    /**
     * Act - do whatever the StartKnop wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
      if (Greenfoot.mouseClicked(this))
      { 
          Greenfoot.setWorld(new Space());// Add your action code here.
        }
    }
}
stijn.b stijn.b

2023/4/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Tijdslimiet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Timer extends Actor
{
    //(hoelang duurt de timer? 30 seconden)
    private int timer = 55*5;
    public void act()
    {
        // hoe tijd werkt 
        timer--;
        if (timer % 55 == 0) updateImage();
        if (timer <=0) Greenfoot.setWorld(new Space2());
    }
     
    private void updateImage()
    {
        // kleur tijd 
        setImage(new GreenfootImage("Tijd over:" + timer/55, 20, Color.BLACK, Color.WHITE));
    }
}
stijn.b stijn.b

2023/4/14

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * An Asteroid that flies across the screen.
 */
public class Asteroid  extends Actor
{
    /**
     * Act - do whatever the Asteroid wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkEdgeRight();
    }    

    public void checkEdgeRight() {
        int xpos = getX() + 1;
        if (xpos >= getWorld().getWidth()) {
            getWorld().removeObject(this);
        }
        else {
            setLocation(xpos, getY());
        }
    }
}
danpost danpost

2023/4/15

#
stijn.b wrote...
<< Codes Omitted >>
What about the Space2 class code?
You need to login to post a reply.