From 5982c0ced212ed27f06815f36afd72d82a4aea9e Mon Sep 17 00:00:00 2001 From: JJTCM Date: Tue, 11 Nov 2014 13:00:45 +0100 Subject: [PATCH] Added Score Added Interface fr Health, Sheild and Score Resized Game Window for the Interface Added Score for Enemy Kills --- src/de/teamteamteam/spacescooter/Main.java | 2 +- .../background/StarBackground.java | 9 ++-- .../spacescooter/datastructure/Score.java | 25 ++++++++++ .../spacescooter/entity/LivingEntity.java | 14 ++++++ .../spacescooter/entity/Player.java | 1 + .../spacescooter/entity/enemy/EnemyFour.java | 1 + .../spacescooter/entity/enemy/EnemyOne.java | 1 + .../spacescooter/entity/enemy/EnemyThree.java | 1 + .../spacescooter/entity/enemy/EnemyTwo.java | 1 + .../spacescooter/gui/HealthBar.java | 22 +++++++-- .../spacescooter/gui/InterfaceBar.java | 24 ++++++++++ .../spacescooter/gui/ScoreBar.java | 25 ++++++++++ .../spacescooter/gui/ShieldBar.java | 46 +++++++++++++++++++ .../spacescooter/screen/GameScreen.java | 10 +++- 14 files changed, 171 insertions(+), 11 deletions(-) create mode 100644 src/de/teamteamteam/spacescooter/datastructure/Score.java create mode 100644 src/de/teamteamteam/spacescooter/gui/InterfaceBar.java create mode 100644 src/de/teamteamteam/spacescooter/gui/ScoreBar.java create mode 100644 src/de/teamteamteam/spacescooter/gui/ShieldBar.java diff --git a/src/de/teamteamteam/spacescooter/Main.java b/src/de/teamteamteam/spacescooter/Main.java index 0f5e702..af7f1a3 100644 --- a/src/de/teamteamteam/spacescooter/Main.java +++ b/src/de/teamteamteam/spacescooter/Main.java @@ -26,7 +26,7 @@ public class Main { GraphicsSettings gs = new GraphicsSettings(); //Get settings GameConfig.windowWidth = 800; - GameConfig.windowHeight = 600; + GameConfig.windowHeight = 650; //Instantiate the GameFrame final GameFrame gameFrame = new GameFrame(); diff --git a/src/de/teamteamteam/spacescooter/background/StarBackground.java b/src/de/teamteamteam/spacescooter/background/StarBackground.java index faddf83..62f7ecc 100644 --- a/src/de/teamteamteam/spacescooter/background/StarBackground.java +++ b/src/de/teamteamteam/spacescooter/background/StarBackground.java @@ -4,9 +4,12 @@ import java.awt.Graphics2D; public class StarBackground extends Background { + + private int y; public StarBackground(int x, int y) { super(x, y); + this.y = y; this.setImage("images/starbackground.png"); } @@ -21,9 +24,9 @@ public class StarBackground extends Background { } public void paint(Graphics2D g) { - g.drawImage(this.getImage(), (0+this.offset), 0, null); - g.drawImage(this.getImage(), (this.getImage().getWidth()+this.offset), 0, null); - g.drawImage(this.getImage(), (2*this.getImage().getWidth()+this.offset), 0, null); + g.drawImage(this.getImage(), (0+this.offset), this.y, null); + g.drawImage(this.getImage(), (this.getImage().getWidth()+this.offset), this.y, null); + g.drawImage(this.getImage(), (2*this.getImage().getWidth()+this.offset), this.y, null); } } diff --git a/src/de/teamteamteam/spacescooter/datastructure/Score.java b/src/de/teamteamteam/spacescooter/datastructure/Score.java new file mode 100644 index 0000000..408f888 --- /dev/null +++ b/src/de/teamteamteam/spacescooter/datastructure/Score.java @@ -0,0 +1,25 @@ +package de.teamteamteam.spacescooter.datastructure; + +public class Score { + + private static int score = 0; + + public static int getScore() { + return score; + } + + public static void setScore(int score) { + Score.score = score; + } + + public static void addScore(int score) { + if (Score.score != 99999999) + Score.score += score; + } + + public static void removeScore(int score) { + if (Score.score != 0) + Score.score -= score; + } + +} diff --git a/src/de/teamteamteam/spacescooter/entity/LivingEntity.java b/src/de/teamteamteam/spacescooter/entity/LivingEntity.java index 661ee90..9aed34f 100644 --- a/src/de/teamteamteam/spacescooter/entity/LivingEntity.java +++ b/src/de/teamteamteam/spacescooter/entity/LivingEntity.java @@ -2,6 +2,7 @@ package de.teamteamteam.spacescooter.entity; import java.awt.Rectangle; +import de.teamteamteam.spacescooter.datastructure.Score; import de.teamteamteam.spacescooter.entity.enemy.Enemy; import de.teamteamteam.spacescooter.entity.explosion.Explosion; import de.teamteamteam.spacescooter.entity.shot.Shot; @@ -33,6 +34,11 @@ public abstract class LivingEntity extends Entity implements Collidable, Hittabl */ private int shieldPoints; + /** + * The LivingEntities shield points. + */ + private int ScorePoints; + /** * Default constructor. @@ -106,6 +112,7 @@ public abstract class LivingEntity extends Entity implements Collidable, Hittabl //Set the correct values for gui indicators this.healthPoints = 0; this.shieldPoints = 0; + Score.addScore(ScorePoints); if(GameConfig.DEBUG) System.out.println(this + " ist gestorben. RIP"); this.die(); } @@ -155,5 +162,12 @@ public abstract class LivingEntity extends Entity implements Collidable, Hittabl public int getShieldPoints() { return this.shieldPoints; } + + /** + * Set the current score points. + */ + public void setScore(int s) { + this.ScorePoints = s; + } } diff --git a/src/de/teamteamteam/spacescooter/entity/Player.java b/src/de/teamteamteam/spacescooter/entity/Player.java index 0037fa8..e2e6bb0 100644 --- a/src/de/teamteamteam/spacescooter/entity/Player.java +++ b/src/de/teamteamteam/spacescooter/entity/Player.java @@ -25,6 +25,7 @@ public class Player extends ShootingEntity implements KeyboardListener { this.setShootSpeed(10); this.setCollisionDamage(10); this.setHealthPoints(100); + this.setShieldPoints(100); this.registerOnKeyboard(Keyboard.getInstance()); } diff --git a/src/de/teamteamteam/spacescooter/entity/enemy/EnemyFour.java b/src/de/teamteamteam/spacescooter/entity/enemy/EnemyFour.java index 825a8f6..ed93c2a 100644 --- a/src/de/teamteamteam/spacescooter/entity/enemy/EnemyFour.java +++ b/src/de/teamteamteam/spacescooter/entity/enemy/EnemyFour.java @@ -23,6 +23,7 @@ public class EnemyFour extends Enemy{ this.setShootDamage(5); this.setCollisionDamage(5); this.setHealthPoints(5); + this.setScore(40); this.points = points; this.x = x; this.y = y; diff --git a/src/de/teamteamteam/spacescooter/entity/enemy/EnemyOne.java b/src/de/teamteamteam/spacescooter/entity/enemy/EnemyOne.java index 8aff0e1..516c802 100644 --- a/src/de/teamteamteam/spacescooter/entity/enemy/EnemyOne.java +++ b/src/de/teamteamteam/spacescooter/entity/enemy/EnemyOne.java @@ -13,6 +13,7 @@ public class EnemyOne extends Enemy { this.setCollisionDamage(5); this.setHealthPoints(5); this.setCollisionDamage(this.getHealthPoints()); + this.setScore(10); } @Override diff --git a/src/de/teamteamteam/spacescooter/entity/enemy/EnemyThree.java b/src/de/teamteamteam/spacescooter/entity/enemy/EnemyThree.java index 23ea3be..22a2520 100644 --- a/src/de/teamteamteam/spacescooter/entity/enemy/EnemyThree.java +++ b/src/de/teamteamteam/spacescooter/entity/enemy/EnemyThree.java @@ -27,6 +27,7 @@ public class EnemyThree extends Enemy{ this.setShootDamage(5); this.setHealthPoints(15); this.setCollisionDamage(10); + this.setScore(30); this.setPosition(GameConfig.windowWidth, random.nextInt(GameConfig.windowHeight - this.getHeight())); this.newY = this.getY(); this.entityIterator = Screen.currentScreen.createEntityIterator(); diff --git a/src/de/teamteamteam/spacescooter/entity/enemy/EnemyTwo.java b/src/de/teamteamteam/spacescooter/entity/enemy/EnemyTwo.java index 002d57f..a21fed7 100644 --- a/src/de/teamteamteam/spacescooter/entity/enemy/EnemyTwo.java +++ b/src/de/teamteamteam/spacescooter/entity/enemy/EnemyTwo.java @@ -17,6 +17,7 @@ public class EnemyTwo extends Enemy{ this.setShootDamage(5); this.setCollisionDamage(5); this.setHealthPoints(5); + this.setScore(20); this.setCollisionDamage(this.getHealthPoints()); this.setPosition(GameConfig.windowWidth, random.nextInt(GameConfig.windowHeight - this.getHeight())); } diff --git a/src/de/teamteamteam/spacescooter/gui/HealthBar.java b/src/de/teamteamteam/spacescooter/gui/HealthBar.java index c46fe87..279adf7 100644 --- a/src/de/teamteamteam/spacescooter/gui/HealthBar.java +++ b/src/de/teamteamteam/spacescooter/gui/HealthBar.java @@ -1,6 +1,7 @@ package de.teamteamteam.spacescooter.gui; import java.awt.Color; +import java.awt.Font; import java.awt.Graphics2D; import de.teamteamteam.spacescooter.datastructure.ConcurrentIterator; @@ -10,8 +11,9 @@ import de.teamteamteam.spacescooter.screen.Screen; public class HealthBar extends Entity { - private int width = 100; - private int height = 24; + private int width = 150; + private int height = 14; + private int health = 0; private ConcurrentIterator entityIterator; @@ -29,10 +31,20 @@ public class HealthBar extends Entity { player = ((Player) e); } } - g.setColor(Color.GREEN); - g.fillRect(this.getX(), this.getY(), (this.width / 100) * player.getHealthPoints(), this.height); + this.health = ((this.width) * player.getHealthPoints()); g.setColor(Color.WHITE); - g.drawRect(this.getX(), this.getY(), this.width, this.height); + g.setFont(new Font("Monospace", 0, 16)); + g.drawString("Health:", this.getX(), this.getY()+12); + if (player.getHealthPoints() <= 15) { + g.setColor(Color.RED); + } else if (player.getHealthPoints() <= 50) { + g.setColor(Color.YELLOW); + } else { + g.setColor(Color.GREEN); + } + g.fillRect(this.getX()+70, this.getY(), this.health / 100, this.height); + g.setColor(Color.WHITE); + g.drawRect(this.getX()+70, this.getY(), this.width, this.height); } public void update() {} diff --git a/src/de/teamteamteam/spacescooter/gui/InterfaceBar.java b/src/de/teamteamteam/spacescooter/gui/InterfaceBar.java new file mode 100644 index 0000000..48ff71f --- /dev/null +++ b/src/de/teamteamteam/spacescooter/gui/InterfaceBar.java @@ -0,0 +1,24 @@ +package de.teamteamteam.spacescooter.gui; + +import java.awt.Color; +import java.awt.Graphics2D; + +import de.teamteamteam.spacescooter.entity.Entity;; + +public class InterfaceBar extends Entity { + + private int width = 800; + private int height = 50; + + public InterfaceBar(int x, int y) { + super(x, y); + } + + public void paint(Graphics2D g) { + g.setColor(Color.BLACK); + g.fillRect(this.getX(), this.getY(), this.width, this.height); + } + + public void update() {} + +} diff --git a/src/de/teamteamteam/spacescooter/gui/ScoreBar.java b/src/de/teamteamteam/spacescooter/gui/ScoreBar.java new file mode 100644 index 0000000..0e01908 --- /dev/null +++ b/src/de/teamteamteam/spacescooter/gui/ScoreBar.java @@ -0,0 +1,25 @@ +package de.teamteamteam.spacescooter.gui; + +import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics2D; + +import de.teamteamteam.spacescooter.entity.Entity; +import de.teamteamteam.spacescooter.datastructure.Score; + +public class ScoreBar extends Entity { + + public ScoreBar(int x, int y) { + super(x, y); + } + + public void paint(Graphics2D g) { + g.setColor(Color.WHITE); + g.setFont(new Font("Monospace", 0, 24)); + g.drawString("Score:", this.getX(), this.getY()); + g.drawString(String.format("%08d", Integer.parseInt("" + Score.getScore())), this.getX()+77, this.getY()); + } + + public void update() {} + +} diff --git a/src/de/teamteamteam/spacescooter/gui/ShieldBar.java b/src/de/teamteamteam/spacescooter/gui/ShieldBar.java new file mode 100644 index 0000000..1b7bbdd --- /dev/null +++ b/src/de/teamteamteam/spacescooter/gui/ShieldBar.java @@ -0,0 +1,46 @@ +package de.teamteamteam.spacescooter.gui; + +import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics2D; + +import de.teamteamteam.spacescooter.datastructure.ConcurrentIterator; +import de.teamteamteam.spacescooter.entity.Entity; +import de.teamteamteam.spacescooter.entity.Player; +import de.teamteamteam.spacescooter.screen.Screen; + +public class ShieldBar extends Entity { + + private int width = 150; + private int height = 14; + private int shield = 0; + + private ConcurrentIterator entityIterator; + + public ShieldBar(int x, int y) { + super(x, y); + this.entityIterator = Screen.currentScreen.createEntityIterator(); + } + + public void paint(Graphics2D g) { + Player player = null; + this.entityIterator.reset(); + while(this.entityIterator.hasNext()) { + Entity e = this.entityIterator.next(); + if(e instanceof Player){ + player = ((Player) e); + } + } + this.shield = ((this.width) * player.getShieldPoints()); + g.setColor(Color.WHITE); + g.setFont(new Font("Monospace", 0, 16)); + g.drawString("Shield:", this.getX(), this.getY()+12); + g.setColor(Color.BLUE); + g.fillRect(this.getX()+70, this.getY(), this.shield / 100, this.height); + g.setColor(Color.WHITE); + g.drawRect(this.getX()+70, this.getY(), this.width, this.height); + } + + public void update() {} + +} diff --git a/src/de/teamteamteam/spacescooter/screen/GameScreen.java b/src/de/teamteamteam/spacescooter/screen/GameScreen.java index 07c4351..8aa8eb0 100644 --- a/src/de/teamteamteam/spacescooter/screen/GameScreen.java +++ b/src/de/teamteamteam/spacescooter/screen/GameScreen.java @@ -12,6 +12,9 @@ import de.teamteamteam.spacescooter.entity.enemy.EnemyFour; import de.teamteamteam.spacescooter.entity.enemy.EnemyThree; import de.teamteamteam.spacescooter.entity.item.ItemChance; import de.teamteamteam.spacescooter.gui.HealthBar; +import de.teamteamteam.spacescooter.gui.InterfaceBar; +import de.teamteamteam.spacescooter.gui.ScoreBar; +import de.teamteamteam.spacescooter.gui.ShieldBar; import de.teamteamteam.spacescooter.utility.CollisionHandler; /** @@ -32,9 +35,12 @@ public class GameScreen extends Screen { points.add(new Point(300,300)); points.add(new Point(600,100)); points.add(new Point(0,500)); - new StarBackground(0, 0); + new StarBackground(0, 50); this.player = new Player(200, 300); - new HealthBar(10, 10); + new InterfaceBar(0, 0); + new HealthBar(10, 5); + new ShieldBar(10, 27); + new ScoreBar(575, 33); new EnemyFour(800, 400, points); new EnemyThree(650, 300); new EnemyThree(450, 100);