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

2017/4/24

Timer and Powerup Question

icantstudy995 icantstudy995

2017/4/24

#
I am working on an rocket and asteroids game. I would like to implement a timer that tracks how long it took for the player to clear all the asteroids, and show whatever time that was recorded on the winning screen. Similarly, I would like the tracked time to show up on the game over screen as well (to tell players how long they lived for). I've read through many discussions, but I don't seem to really get anything (working and understanding wise). I also have in mind of some powerups that will spawn at random time intervals (I don't know how to do this) and random places (I know how to do this). When the rocket ship intersects with the powerup, the speed of the asteroids should decrease for an amount of time. I have tried using setSpeed(), which slows everything down, but it seems to break my game over screen. If anyone can give me some hints/ideas on where to get started - please let me know! I am very new to java and Greenfoot and would really like to learn more.
danpost danpost

2017/4/24

#
icantstudy995 wrote...
I am working on an rocket and asteroids game. I would like to implement a timer that tracks how long it took for the player to clear all the asteroids, and show whatever time that was recorded on the winning screen. Similarly, I would like the tracked time to show up on the game over screen as well (to tell players how long they lived for). I've read through many discussions, but I don't seem to really get anything (working and understanding wise).
My Value Display Tutorial may be able to help on this part.
I also have in mind of some powerups that will spawn at random time intervals (I don't know how to do this) and random places (I know how to do this). When the rocket ship intersects with the powerup, the speed of the asteroids should decrease for an amount of time. I have tried using setSpeed(), which slows everything down, but it seems to break my game over screen.
I do not see why changing the scenario speed would break your game over screen. At any rate, if your asteroids are normally moving at a decent pace, slowing them down should just be a matter of making their speed(s) variable (hint word). You will also need a timer field to track how long (in act cycles) the slow-down is active. A timer field is also going to be needed for the spawning of powerups. You would set it to the sum of a minimum time (in act cycles) before spawning plus a random value for the range (maximum time minus minimum time in act cycles).
icantstudy995 icantstudy995

2017/4/24

#
danpost wrote...
slowing them down should just be a matter of making their speed(s) variable (hint word)
Does this mean that I should do something like, if rocket touches powerup, (speed variable) = x, else (speed variable) = y? If so, how would I go with formatting it? Also, I am having issues accessing the tutorial. I get a message saying "Your browser is ignoring the <APPLET> tag.". Can you help with this? Thanks for the reply.
icantstudy995 icantstudy995

2017/4/24

#
I guess what I don't understand is, how do I call out the asteroid speed when I am detecting collision of powerup from the rocket ship?
danpost danpost

2017/4/24

#
Since all asteroids will be affected by the half-speed effect, you can use a static field in the rocket class:
1
public static int slowDownTimer;
The asteroid can then move with:
1
2
3
int speed = 2;
if (Rocket.slowDownTimer > 0) speed = 1;
move(speed);
You will need the following in the rocket class act method to run the timer:
1
if (slowDownTimer > 0) slowDownTimer--;
icantstudy995 icantstudy995

2017/4/24

#
1
2
3
4
5
6
7
Actor powerup;
powerup = getOneIntersectingObject(Powerup.class);
 
if (powerup != null) {
    currentWorld.removeObject( powerup );
 
}
I have this plus the code you gave me. It doesn't seem to work. Should I add slowDownTimer = 180 in line 6? Also, when I add this piece of code, which is in the rocket class, my game over screen stops showing up when my rocket ship hits an asteroid. This is what I have in World. I used the switch statement with different gamestates.
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
public void act() {
        switch (gameState) {
            case 0: // introduction to the game; 
    
            case 1: if (Greenfoot.isKeyDown("n")) {
                        gameState=2; // for different difficulties
                        removeObjects(getObjects(TextCreator.class));
                        removeObjects(getObjects(Title.class));
                        removeObjects(getObjects(StillAsteroid.class));
                        removeObjects(getObjects(StillPowerup.class));
                        Greenfoot.setSpeed(52);
                    }
                    if (Greenfoot.isKeyDown("c")) {
                        removeObjects(getObjects(TextCreator.class));
                        removeObjects(getObjects(Title.class));
                        removeObjects(getObjects(StillAsteroid.class));
                        removeObjects(getObjects(StillPowerup.class));
                        gameState=3;
                        Greenfoot.setSpeed(58);
                    }
                    if (Greenfoot.isKeyDown("i")) {
                        removeObjects(getObjects(TextCreator.class));
                        removeObjects(getObjects(Title.class));
                        removeObjects(getObjects(StillAsteroid.class));
                        removeObjects(getObjects(StillPowerup.class));
                        gameState=4;
                        Greenfoot.setSpeed(62);
                    }
                    gameTime += 1;
                    break;
                     
            case 2: addObject( new Rocket(), 396, 317);
                    addObject( new Asteroid(), 750, 55);
                    addObject( new Asteroid(), 750, 545);
                    addObject( new Asteroid(), 61, 548);
                    int x = Greenfoot.getRandomNumber(getWidth());
                    int y = Greenfoot.getRandomNumber(getHeight());
                    addObject( new Powerup(), x, y);
                    gameTime += 1;
                    gameState = 5;
                    break;
                     
            case 3: addObject( new Rocket(), 396, 317);
                    addObject( new Asteroid(), 750, 55);
                    addObject( new Asteroid(), 750, 545);
                    addObject( new Asteroid(), 61, 548);
                    addObject( new Asteroid(), 65, 51);
                    addObject( new Asteroid(), 266, 164);
                    gameTime += 1;
                    gameState = 5;
                    break;
                     
            case 4: addObject( new Rocket(), 396, 317);
                    addObject( new Asteroid(), 750, 55);
                    addObject( new Asteroid(), 750, 545);
                    addObject( new Asteroid(), 61, 548);
                    addObject( new Asteroid(), 65, 51);
                    addObject( new Asteroid(), 266, 164);
                    addObject( new Asteroid(), 170, 416);
                    addObject( new Asteroid(), 613, 463);
                    gameTime += 1;
                    gameState = 5;
                    break;
                     
            case 5:if (getObjects(Asteroid.class).isEmpty()) { // game over/ game winning
                       Greenfoot.setWorld(new WinningScreen());
                   }
                   else if (getObjects(Rocket.class).isEmpty()) {
                       Greenfoot.setWorld(new GameOverScreen());
                   }
                    
                    gameTime += 1; //gameTime = gameTime+1
                    break;
                     
                     
            default: gameState=0;
        }
danpost danpost

2017/4/24

#
icantstudy995 wrote...
Should I add slowDownTimer = 180 in line 6?
Yes, I forgot to mention how to start the timer.
when I add this piece of code, which is in the rocket class, my game over screen stops showing up when my rocket ship hits an asteroid.
Need to see the Rocket class codes -- not the world's.
You need to login to post a reply.