I'm currently making a game for a school project and I've run into a problem that I just can't seem to get rid of. It takes some time and a few code fragments to explain so please bare with me. The problem I have is that I need to get a number of points from an enemy that I shoot down and send that to the scoreboard which will display it either when the game is over or when the level is complete. I have the method that gets the points from each enemy when it is shot down, but I have no idea how to send it to the Scoreboard object in my game.
I had the idea to send the points into an array list as well as the variable that would determine from which type of enemy it came from. I know this sounds rather overcomplicated but it needs to be done because the scoreboard I have will display the scores per type of enemy I shoot down in the game and then combine everything to get the total score.
Below here are the code fragments that apply to this problem. Please note that a part of my code is incomplete as I had absolutely no idea what to do. This first piece of code if from a specific type of enemy.
The second piece of code is from the class Enemy that holds all subclasses of the enemy types.
The third piece of code is of the class Bullet. This is where my problem starts. In the eatEnemy method I have the code needed to request the points from the enemy when the PlayerBullet (my bullets) shoot the enemy down. I however have no idea how to send it to the Scoreboard object and put it into an array list.
NOTE: since the bullet class is very long, I only added the eatEnemy method into this code piece.
The code from the Scoreboard object that I have is pretty much nothing yet since I haven't been able to continue with that because of the problem that I have. What I have are two array lists, but no idea how to add things into them or pick matches from the lists for each enemy type and display them at the proper location on the Scoreboard object.
Any advice is more than welcome as I have been breaking my head over this problem for nearly two weeks already. If there is any code that I have missed for you to get a more clear view of the problem then please say so and I'll post it in a reply.
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 | public class Enemy1 extends Enemy { private GreenfootImage image1; private GreenfootImage image2; private GreenfootImage image3; private GreenfootImage image4; private GreenfootImage image5; public Enemy1() { points = 100 ; } /** * Act - do whatever the Enemy1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { enemyFireBlue(); moveEnemy(); enemyAnimate(); getPoints(); } private int firecounter = 50 ; private void enemyFireBlue() { firecounter++; if (firecounter == 100 ) { World world; BlueBullet bullet1 = new BlueBullet(); getWorld().addObject(bullet1, getX()- 50 , getY()); firecounter = 0 ; } } private int timer = 0 ; private void enemyAnimate() { image1 = new GreenfootImage( "Ship1-1.png" ); image2 = new GreenfootImage( "Ship1-2.png" ); image3 = new GreenfootImage( "Ship1-3.png" ); image4 = new GreenfootImage( "Ship1-4.png" ); image5 = new GreenfootImage( "Ship1-5.png" ); timer++; if (timer == 1 ) { setImage(image1); } if (timer == 10 ) { setImage(image2); } if (timer == 20 ) { setImage(image3); } if (timer == 30 ) { setImage(image4); } if (timer == 40 ) { setImage(image5); } if (timer == 50 ) { timer = 0 ; } } } |
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 | public class Enemy extends Actor { public int points; /** * This class is set here to move the enemies around. */ public void moveEnemy() { move(- 2 ); int left = 25 - Greenfoot.getRandomNumber( 50 ); } public void moveEnemyDown() { setLocation(getX(), getY()+ 1 ); } public void moveEnemyUp() { setLocation(getX(), getY()- 1 ); } public void moveEnemySurprise() { move( 2 ); int left = 25 - Greenfoot.getRandomNumber( 50 ); } public void moveEnemyFast() { move(- 4 ); int left = 25 - Greenfoot.getRandomNumber( 50 ); } public void moveEnemyDownFast() { setLocation(getX(), getY()+ 2 ); } public void moveEnemyUpFast() { setLocation(getX(), getY()- 2 ); } public void moveEnemySurpriseFast() { move( 4 ); int left = 25 - Greenfoot.getRandomNumber( 50 ); } public void moveEnemyRapid() { move(- 6 ); int left = 25 - Greenfoot.getRandomNumber( 50 ); } public void moveEnemyDownRapid() { setLocation(getX(), getY()+ 3 ); } public void moveEnemyUpRapid() { setLocation(getX(), getY()- 3 ); } public void moveEnemySurpriseRapid() { move( 6 ); int left = 25 - Greenfoot.getRandomNumber( 50 ); } public int getPoints() { return points; } } |
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 | public class Bullet extends Actor { (other code. Not important for this problem.) public void eatEnemy() { Actor enemy = getOneIntersectingObject(Enemy. class ); if (enemy != null ) { World world; String whatEnemy = enemy.getClass().getName(); if (whatEnemy.equals( "Enemy1" )) { Enemy1 e = new Enemy1(); LevelCompletedScoreboard s = new LevelCompletedScoreboard(); //somehow, send scores to LevelCompleteScoreboard. System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy2" )) { Enemy2 e = new Enemy2(); System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy3" )) { Enemy3 e = new Enemy3(); System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy4" )) { Enemy4 e = new Enemy4(); System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy5" )) { Enemy5 e = new Enemy5(); System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy6" )) { Enemy6 e = new Enemy6(); System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy7" )) { Enemy7 e = new Enemy7(); System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy8" )) { Enemy8 e = new Enemy8(); System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy9" )) { Enemy9 e = new Enemy9(); System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy10" )) { Enemy10 e = new Enemy10(); System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy11" )) { Enemy11 e = new Enemy11(); System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy12" )) { Enemy12 e = new Enemy12(); System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy13" )) { Enemy13 e = new Enemy13(); System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy14" )) { Enemy14 e = new Enemy14(); System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy15" )) { Enemy15 e = new Enemy15(); System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy16" )) { Enemy16 e = new Enemy16(); System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy17" )) { Enemy17 e = new Enemy17(); System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy18" )) { Enemy18 e = new Enemy18(); System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy19" )) { Enemy19 e = new Enemy19(); System.out.println(e.getPoints()); } if (whatEnemy.equals( "Enemy20" )) { Enemy20 e = new Enemy20(); System.out.println(e.getPoints()); } Explosion Explosion = new Explosion(); Greenfoot.playSound( "blast.mp3" ); getWorld().removeObject(enemy); getWorld().removeObject( this ); } } } |
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 | public class LevelCompletedScoreboard extends Actor { public ArrayList<String> kills = new ArrayList<String>(); public ArrayList<String> scores = new ArrayList<String>(); /** * Act - do whatever the LevelCompletedScoreboard wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { AddScore(); AddKills(); } public void AddScore() { //Add code here. } public void AddKills() { //Add code here. } } |