I'm making a game for my course, and I really need help as the deadline is fast approaching! I have a game that is almost fully functional apart from a few slight problems, this being one of the biggest. I have a working score counter, it increases correctly and displays correctly on level 1, but I don't know how to display the score on level 2 while still keeping the score from level 1. I'm still learning code and I suck with the terminology, so I politely ask if you guys could keep it simple for me!
But firstly, when I try to display the counter on level 2, the game crashes when I touch the portal that takes me from level 1 to level 2.
Code for my Main Character (Subject003), Score (ScoreTracker), Level 1 (SpaceShipLevel1), and Level 2 (SpaceShipLevel2) will be posted in this order.
Here is the code for my main character
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Code for my score counter
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Code for Level 1
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Code for level 2
If you got this far thank you very much for reading! And all the help offered is very much appreciated!
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Subject003 extends Characters { private int x; // X variable private int y; // Y variable int speed = 2; // speed variable int reload = 0; // reload variable int abc = 0; // abc variable int gr = 0; // gr variable private int healthPoints = 1; // healthPoints variable public boolean poweredUp = false; // Default setting for poweredUp public Subject003() // When Subject003 hasn't picked up the Captive { // No instructions } public Subject003(boolean powerMeUp, int newSpeed, int newReload) // When Subject003 has picked up the captive { if(powerMeUp == true) // If Subject003 has picked up the powerup { speed = newSpeed; // Keep new increased speed reload = newReload; // Keep new increased reload } } public void act() { checkKeys(); // Check which keys are pressed and move in the appropriate direction rotateImage(); // Rotate the image when the correct keys are pressed collision(); // Collide with Walls touchCaptive(); // Receive movement speed and firerate buff when Captive is touched touchPortal(); // Send to level 2 when touched damageTaken(); // Detect damage taken killed(); // Detect if health falls below 1 touchControlPanel(); // Detect if intersecting with control panel touchInfectedDormous(); // Receive health buff when Captive2 is touched collectCheese(); // Increase the score by 25 when cheese is collected } private void checkKeys() // Moves Subject003 in the direction of the arrow keys pressed. { if(Greenfoot.isKeyDown("up")) // Check if up arrow key is pressed { setLocation(getX(), getY() - speed); // Decrease position on Y axis to move upwards } if(Greenfoot.isKeyDown("down")) // Check if down arrow key is pressed { setLocation(getX(), getY() + speed); // Increase position on Y axis to move downwards } if(Greenfoot.isKeyDown("left")) // Check if left arrow key is pressed { setLocation(getX() - speed, getY()); // Decrease position on the X axis to move left } if(Greenfoot.isKeyDown("right")) // Check if right arrow key is pressed { setLocation(getX() + speed, getY()); // Increase position on the X axis to move right } if(Greenfoot.mouseClicked(null)) // Detect if the mouse is clicked { if(reload <= 0) // When reload is 0, a bullet can be fired { getWorld().addObject(new DormousBullet(getRotation()),getX(),getY()); // Adds a new bullet object when moused is clicked and reload equals 0 reload = 80 - abc; // Reload resets to 80 every time mouse is clicked Greenfoot.playSound("AWP.mp3"); // Play sound whenever mouse is clicked and reload equals 0 } } reload--; // Decrease reload by 1 every act } private void rotateImage() // Rotate Subject003 in the direction of arrow keys pressed { if(Greenfoot.isKeyDown("down")) // Detect if down key is pressed { setRotation(180); // Rotate image by 180 degrees to the right } if(Greenfoot.isKeyDown("right")) { setRotation(90); // Rotate image 90 degrees to the right } if(Greenfoot.isKeyDown("left")) { setRotation(-90); // Rotate image 90 degrees to the left } if(Greenfoot.isKeyDown("up")) { setRotation(0); // Reset image to original position } } public void collision() // Collide with walls { if (getOneIntersectingObject(Wall2.class) != null) // Detect if intersecting with Wall2 { setLocation(x,y); // Set location to X and Y } if (getOneIntersectingObject(Wall.class) != null) // Detect if intersecting with Wall { setLocation(x,y); // Set location to X and Y } else { x = getX(); // Get X position y = getY(); // Get Y position } } public void touchCaptive() // Receive buff if captive is touched { Actor Captive = getOneIntersectingObject(Captive.class); // Detect if Captive is intersecting with Subject003 if(Captive != null) // If Captive is detected { SpaceShipLevel1 no = (SpaceShipLevel1) getWorld(); // Get information from SpaceShipLevel1 world class no.Scoretracker.add(50); // Add 100 speed += 1; // Increase speed by 1 abc += 30; // Increase abc by 15 poweredUp = true; // Whether or not Subject003 has picked up the captive Greenfoot.playSound("Wow.mp3"); // Play sound getWorld().removeObject(Captive); // Remove Captive } } public void killed() // When health falls below 1 { healthPoints = healthPoints; // Detect healthPoints if(healthPoints <= 0) // If healthPoints fall below 1 { Greenfoot.playSound("HITMARKER.mp3"); // Play sound Greenfoot.setWorld(new GameOver()); // Set world image to GameOver } } public void damageTaken() // Detect if damage has been taken { if(getOneIntersectingObject(GoblinBullet.class) != null) // If GoblinBullet is intersecting with Subject003 { healthPoints -= 100; // Reduce healthPoints by 1 getWorld().removeObject(getOneIntersectingObject(GoblinBullet.class)); // Remove the GoblinBullet } } public void touchPortal() // Detect if touching portal { if (getOneIntersectingObject(Portal.class) != null && poweredUp == false) // If Portal is intersecting with Subject003 and did not pick up Captive { Greenfoot.setWorld(new SpaceShipLevel2(poweredUp, speed, reload)); // Change level to SpaceShipLevel2 false } else if (getOneIntersectingObject(Portal.class) != null && poweredUp == true) // If Portal is intersecting with Subject003 and did pick up the captive { Greenfoot.setWorld(new SpaceShipLevel2(poweredUp, speed, reload)); // Change level to SpaceShipLevel2 true } } public void touchInfectedDormous() // Detect if touching Captive2 { if (getOneIntersectingObject(Captive2.class) != null) // If Captive2 is intersecting with Subject003 { if(gr == 0) // Only takes effect if variable gr equals 0 { healthPoints += 100; // adds 100 healthPoint Greenfoot.playSound("Wow.mp3"); // Plays sound gr = 50; // Increase gr variable by 50 } } } public void touchControlPanel() // Detect if touching control panel { if (getOneIntersectingObject(ControlPanel.class) != null) // If ControlPanel is intersecting with Subject003 { Greenfoot.setWorld(new Win()); // Change level to Win } } public void collectCheese() // Increase score if cheese is collected { if (getOneIntersectingObject(Cheese.class) != null) // Detect if intersecting { SpaceShipLevel1 no = (SpaceShipLevel1) getWorld(); // Get information from SpaceShipLevel1 world class no.Scoretracker.add(25); // Add 25 to score getWorld().removeObject(getOneIntersectingObject(Cheese.class)); // Remove Cheese when intersecting with it } } }
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; import java.awt.Graphics; public class ScoreTracker extends Counters { private static final Color textColor = new Color(255, 180, 150); int value = 0; int target = 0; private String text; private int stringLength; public ScoreTracker(int currentScore) { this(""); value = currentScore; } public ScoreTracker(String prefix) { text = prefix; stringLength = (text.length() + 2) * 10; setImage(new GreenfootImage(stringLength, 16)); GreenfootImage image = getImage(); image.setColor(textColor); updateImage(); } public void act() { if(value < target) { value++; updateImage(); } else if(value > target) { value--; updateImage(); } } public void add(int score) { target += score; } public void subtract(int score) { target -= score; } public int getValue() { return value; } /** * Make the image */ private void updateImage() { GreenfootImage image = getImage(); image.clear(); image.drawString(text + value, 1, 12); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class SpaceShipLevel1 extends World { private static final int SIZE = 20; // Size of the array private int[][] mapArray = new int[SIZE][SIZE]; private int[][] map = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0,}, {0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,} }; // Array of numbers representing Wall positions public ScoreTracker Scoretracker; public SpaceShipLevel1() { super(800, 600, 1); // Sets world size Scoretracker = new ScoreTracker("Score: "); // Places Score tracker on screen fillArray(); // Sets the size of the array popMap(); // Seperates the Walls so they are not all on one position Switch(); // Add the switch that will control the removable walls collision2(); // Add two special removable walls place003(); // Add Subject003 on the map captive(); // Add the Captive which will buff Subject003 when touched portal(); // Add the portal which will take Subject003 to the next level spaceGoblin(); // Add the Goblins which will be trying to stop Subject003 trackers(); // Places the score tracker cheese(); // Places the cheese collectibles } public void fillArray() { for (int j = 0; j < SIZE; j++) { for (int i = 0; i < SIZE; i++) { mapArray[i][j] = 1; // Allows for the array to be 2D along with easy size modification } } } public void popMap() { for (int j = 0; j < 20; j++) { for (int i = 0; i < 20; i++) { if (map[i][j] == 0) { Wall block = new Wall(); // Adds the wall class addObject(block, ((j * 40) + 15), ((i * 30) + 15)); // Adds the object while also seperating them } } } } public void place003() { addObject(new Subject003(),736, 580); // Adds Subject003 on the X, Y, axis } public void collision2() { addObject(new Wall2(), 695, 555); // Adds one of the special removable walls on the X, Y, axis addObject(new Wall2(), 135, 255); // Adds one of the special removable walls on the X, Y, axis } public void Switch() { addObject(new Switch(), 745, 59); // Adds the switch which will remove the walls on the X, Y, axis } public void captive() { addObject(new Captive(), 131, 323); // Adds the captive which will buff Subject003 when touched on the X, Y, axis } public void spaceGoblin() { addObject(new SpaceGoblin(), 450, 115); addObject(new SpaceGoblin(), 320, 350); addObject(new SpaceGoblin(), 100, 450); // Adds the Space Goblins which will try to kill Subject003 on the X, Y, axis addObject(new SpaceGoblin(), 450, 390); addObject(new SpaceGoblin(), 450, 490); } public void portal() { addObject(new Portal(), 655, 405); // Adds the portal which will take Subject003 to the next level on the X, Y, axis } public void trackers() { addObject(Scoretracker, 60, 380); } public void cheese() { addObject(new Cheese(), 708, 63); addObject(new Cheese(), 328, 134); addObject(new Cheese(), 656, 443); addObject(new Cheese(), 289, 195); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class SpaceShipLevel2 extends World { private static final int SIZE = 20; // Size of the array private int[][] mapArray = new int[SIZE][SIZE]; private int[][] map = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, {0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,}, {0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0,}, {0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0,}, {0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0,}, {0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0,}, {0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0,}, {0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0,}, {0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0,}, {0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,}, {0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0,}, {0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0,}, {0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0,}, {0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,}, {0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0,}, {0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0,}, {0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0,}, {0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0,}, {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0,}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,} }; // Array of numbers representing Wall positions public ScoreTracker Scoretracker; public SpaceShipLevel2() { super(800, 600, 1); fillArray(); // Sets the size of the array popMap(); // Seperates the Walls so they are not all on one position populateWorld(); // Populates the world with Goblins, Subject003, Switch and Captive2 Scoretracker = new ScoreTracker("Score: "); } public SpaceShipLevel2(boolean keepPowerUp, int currentSpeed, int currentReload) // If Subject003 picked up the Captive power up on level 1 use this method { super(800, 600, 1); fillArray(); // Sets the size of the array popMap(); // Seperates the Walls so they are not all on one position populateWorld(keepPowerUp, currentSpeed, currentReload); // Keep the current speed and reload from level 1 Scoretracker = new ScoreTracker("Score: "); } public void fillArray() { for (int j = 0; j < SIZE; j++) { for (int i = 0; i < SIZE; i++) { mapArray[i][j] = 1; // Allows for the array to be 2D along with easy size modification } } } public void popMap() { for (int j = 0; j < 20; j++) { for (int i = 0; i < 20; i++) { if (map[i][j] == 0) { Wall block = new Wall(); // Adds the wall class addObject(block, ((j * 40) + 15), ((i * 30) + 15)); // Adds the object while also seperating them } } } } public void populateWorld() // Populates the level with Subject003, SpaceGoblins, EngineControls, Portal2, switch and Wall2s { addObject(new Captive2(), 96, 344); addObject(new Portal2(), 55, 105); addObject(new Switch(), 626, 543); addObject(new SpaceGoblin(), 184, 494); addObject(new SpaceGoblin(), 93, 494); addObject(new SpaceGoblin(), 141, 468); addObject(new SpaceGoblin(), 719, 516); addObject(new SpaceGoblin(), 376, 371); addObject(new SpaceGoblin(), 556, 77); addObject(new SpaceGoblin(), 600, 78); addObject(new SpaceGoblin(), 651, 79); addObject(new SpaceGoblin(), 583, 213); addObject(new SpaceGoblin(), 551, 214); addObject(new ControlPanel(), 715, 555); addObject(new Subject003(), 55, 105); addObject(new Wall2(), 695, 465); //addObject(Scoretracker, 60, 380); } public void populateWorld(boolean powerUpSubject, int subjectSpeed, int subjectReload) // Populates the level with Subject003, SpaceGoblins, EngineControls, Portal2, switch and Wall2s { addObject(new Captive2(), 96, 344); addObject(new Portal2(), 55, 105); addObject(new Switch(), 626, 543); addObject(new SpaceGoblin(), 184, 494); addObject(new SpaceGoblin(), 93, 494); addObject(new SpaceGoblin(), 141, 468); addObject(new SpaceGoblin(), 719, 516); addObject(new SpaceGoblin(), 376, 371); addObject(new SpaceGoblin(), 556, 77); addObject(new SpaceGoblin(), 600, 78); addObject(new SpaceGoblin(), 651, 79); addObject(new SpaceGoblin(), 583, 213); addObject(new SpaceGoblin(), 551, 214); addObject(new ControlPanel(), 715, 555); addObject(new Subject003(powerUpSubject, subjectSpeed, subjectReload), 55, 105); // If boolean is true passes the previous values onto this level addObject(new Wall2(), 695, 465); //addObject(Scoretracker, 60, 380); }