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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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)
 *
 */
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.