So I noticed with my game Ambulance Run! that when running it in the greenfoot program if I let the time run out, and the game execute the game over code. when I hit reset and then play it again im getting my time is up message. I tested it out and stopped it at a set time, reset it, and then played and the time was in fact at the same value as when I stopped.
I have a couple of theories as to why. My time var is declared and initialized at the top of the code to my world class but is not in the constructor, could that be why?
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /**. * Ambulance Run!! * * Drive your ambulance and resuce the injured * Eat some burgers * Don't get hit by a car * * Flashing ambulance lights added * Custom Zombie * Custom payvement background * Ambulance Siren added. * Multiple car colors added * * @author BMR * @version 5.0 * * ###UPDATE 2/23/18 * Added text displaying how to play and controls * ##### * * ##UPDATE 3/19/2018 * Levels have been added * Scoring is Manged in the World class * cleaning up some code */ public class MyWorld extends World { private Counter levelCounter = new Counter( "Level: " ); private Counter scoreCounter = new Counter( "Score: " ); private Counter timeCounter = new Counter( "Time: " ); private Counter healthCounter = new Counter( "Health: " ); private Ambulance ambulance = new Ambulance(); private static int time = 5000 ; private static int level = 1 ; private static int score = 0 ; private static int health = 100 ; private static int y = 1 ; private static int x = 1 ; /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 780 , 430 , 1 ); prepare(); } /** * Create new Floating Burger and Cars at random. * * Version 1 * create burgers and cars * Brett */ public void act() { showTime(); showLevel(); gameStatus(); showHealth(); if (Greenfoot.getRandomNumber( 300 ) < y) { addObject( new Burger(), 779 , Greenfoot.getRandomNumber( 430 )); } if (Greenfoot.getRandomNumber( 200 ) < x) { addObject( new Car(),Greenfoot.getRandomNumber( 600 ) + 100 , Greenfoot.getRandomNumber( 430 )); } } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { setPaintOrder(Counter. class ,StatusBar. class ); StatusBar statusbar = new StatusBar(); addObject(statusbar, 386 , 19 ); addObject(ambulance, 110 , 269 ); Zombie zombie = new Zombie(); addObject(zombie, 655 , 377 ); Car car = new Car(); addObject(car, 601 , 300 ); Car car2 = new Car(); addObject(car2, 600 , 250 ); addObject(scoreCounter, 77 , 20 ); addObject(healthCounter, 556 , 20 ); addObject(levelCounter, 205 , 20 ); addObject(timeCounter, 702 , 20 ); Hospital hospital = new Hospital(); addObject(hospital, 704 , 108 ); } /** * Add a Game time function * */ private void countTime() { time--; } /** * Show the time of the game * */ private void showTime() { countTime(); timeCounter.setValue(time); } /** * Show score of trips to the Hospital * */ public void showScore( int points) { scoreCounter.add(points); } /** * Add to the score * */ public void addScore( int newpoints) { score = score + newpoints; showScore(newpoints); } /** * Game Status * */ private void gameStatus() { if (health <= 0 ) { gameOver(); } if (time < 1 ) { timesUp(); } if (score > 100000 ) { youWin(); } } /** * Keep track of your health * */ public void showHealth() { healthCounter.setValue(health); } /** * Adjust your Health * */ public void addHealth( int newhealth) { health = health + newhealth; healthCounter.add(health); } /** * Keep track of your Level * */ public void showLevel() { levelCounter.setValue(level); } public void levelUp() { level++; time = 6000 ; x = x + 1 ; } /** * Provide Access to the score * */ public int getScore() { return score; } /** * Game Over How the game ends * */ public void gameOver() { String gameover = "Game Over!! " ; GameOverScreen gameoverscreen = new GameOverScreen(gameover,score, level); addObject(gameoverscreen, 780 / 2 , 430 / 2 ); Greenfoot.playSound( "game-over.wav" ); Greenfoot.stop(); } public void youWin() { String youwin = "You Win!!" ; Greenfoot.playSound( "fanfare.wav" ); GameOverScreen gameoverscreen2 = new GameOverScreen(youwin, score, level); addObject(gameoverscreen2, 780 / 2 , 430 / 2 ); Greenfoot.stop(); } public void timesUp() { String timesUp = "Time is Up!" ; Greenfoot.playSound( "fanfare.wav" ); GameOverScreen gameoverscreen3 = new GameOverScreen(timesUp, score, level); addObject(gameoverscreen3, 780 / 2 , 430 / 2 ); Greenfoot.stop(); } } |