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

2020/7/7

Stuck on "The world is being constructed" after restarting my computer.

fieryraidenx fieryraidenx

2020/7/7

#
Doing a bit of research, it seems like the problem may be my MyWorld class, so I'm going to link that here.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.io.*;
import java.time.LocalDateTime;  
import java.time.format.DateTimeFormatter; 

/**
 * The world will be a square with aliens coming from the top and the spaceship
 * residing at the bottom. The world keeps track of lives(as soon as it reaches
 * 0, the trial ends and all values reset)
 * 
 * Credits: https://www.w3schools.com/java/java_files_create.asp(writing to files), https://www.greenfoot.org/topics/58693/0(user input),
 * https://stackabuse.com/how-to-get-current-date-and-time-in-java/(keeping track of date)
 */
public class MyWorld extends World
{
    private int numTrials = SimulationParameters.numTrials;
    private int timeBetweenSpawn = 50;
    private int currentTimer = 0;
    private int numPeriods = 0;
    
    private final String filename= String.format("/simulation-results/%s", getFileTag());
    //I'm not sure how to check for file exceptions as I cannot use a try/catch block when declaring a member variable
    private File currentSimulationFile = new File(filename);
    
    private String getFileTag(){
        /*Get filetag in format of ddMMyyhhmmss*/
        LocalDateTime datetime = LocalDateTime.now(); 
        DateTimeFormatter formatterDateTime = DateTimeFormatter.ofPattern("ddMMyyyyHHmmss");
        return datetime.format(formatterDateTime);
    }
    
    private void buffAliens(){
        /*buffs the aliens by reducing spawn time and increasing speed by a
        certain percentage*/
        timeBetweenSpawn *= .8;
        Alien.alienSpeed *= 1.2;
    }
    
    private void startInvasion(){
        /*Peridoically summons aliens. After a certain amount of periods, the
        summoning time of aliens will decrease and their speed will increase*/
        currentTimer -= 1;
        if(currentTimer <= 0){
            numPeriods += 1;
            if(numPeriods > 6){
                numPeriods = 0;
                buffAliens();
            }
            Alien alien = new Alien();
            addObject(alien, 50 + Greenfoot.getRandomNumber(getWidth() - 100),0);
            currentTimer = timeBetweenSpawn;
        }
    }
    
    private void placeSpaceship(){
        /*Places the spaceship at a random position at the bottom of the 
         screen*/
        Spaceship spaceship = new Spaceship();
        addObject(spaceship, 50 + Greenfoot.getRandomNumber(getWidth() - 100),450);
    }
    
    private void displayInfo(){
        /*Display # of lives and current score*/
        showText("Score: " + String.valueOf(Alien.currentScore), 50, 50);
        showText("Lives: " + String.valueOf(Alien.lives), 50, 70);
    }
    
    private void resetAllStats(){
        /*Remove all aliens and reset all parameters*/
        Alien.lives = SimulationParameters.lives;
        for(Alien a: getObjects(Alien.class)){
            this.removeObject(a);
        }
        numPeriods = 0;
        currentTimer = 0;
        timeBetweenSpawn = SimulationParameters.timeBetweenSpawn;
        Alien.alienSpeed = SimulationParameters.alienSpeed;
    }
    
    private void writeScore(){
        /*Will write the current score to the file*/
        try{
            FileWriter writer = new FileWriter(filename);
            int trialNum = SimulationParameters.numTrials - numTrials;
            writer.write(String.format("Trial %s: %s",String.valueOf(trialNum),String.valueOf(Alien.currentScore)));
        }
        catch(IOException e){
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
        Alien.currentScore = 0;
    }
    
    private void stopSimulation(){
        /*Stops the program*/
        Greenfoot.stop();
    }
    
    private void checkForDeath(){
        /*If the ship dies and we aren't finished with the simulation, then reset the 
         board and varstates for the next invasion. Write the current score to a textfile.
         If this is the last trial, close the file*/
         if(Alien.lives == 0){
             numTrials--;
             writeScore();
             if(numTrials > 0){
                 resetAllStats();
             }
             else{
                 stopSimulation();
             }
         }
    }
    
    public void act(){
        startInvasion();
        displayInfo();
        checkForDeath();
    }
    
    public MyWorld()    {    
        /*Create world dimensions,place spaceship, get Simulation Parameters, and start alien invasion.*/
        super(500, 500, 1);
        SimulationParameters.numTrials = Integer.parseInt(Greenfoot.ask("Enter # of trials: "));
        Alien.lives = Integer.parseInt(Greenfoot.ask("Enter # of lives(1-50 is recommended): "));
        Spaceship.bulletReload = Integer.parseInt(Greenfoot.ask("Enter bullet reload time(2-20 is recommended): "));
        SimulationParameters.timeBetweenSpawn = Integer.parseInt(Greenfoot.ask("Enter initial alien spawning time(30-150 is recommended): "));
        Alien.alienSpeed = Integer.parseInt(Greenfoot.ask("Enter intial alien speed(1-10 is recommended): "));
        Alien.worldHeight = getHeight();
        placeSpaceship();
    }
}
I also have a sneaking suspicion that it may be because of either me trying to create a new file within the project or me asking the user to enter in parameters before starting the world.
danpost danpost

2020/7/7

#
fieryraidenx wrote...
I also have a sneaking suspicion that it may be because of either me trying to create a new file within the project or me asking the user to enter in parameters before starting the world.
Creating a file locally should not be a problem. Asking for user input while the scenario is in a stopped state just might be. Try using the started method to ask for that input and complete your world build.
You need to login to post a reply.