Browse Source

Merge remote-tracking branch 'prof/main'

master
fdai7303 3 years ago
parent
commit
827ab47f5b
  1. 4
      .gitignore
  2. 7
      GameProject/.classpath
  3. 4
      GameProject/src/base/GameLoop.java
  4. 33
      GameProject/src/base/MovingObjectsGame.java
  5. 7
      GameProject/src/collider/CircleCollider.java
  6. 2
      GameProject/src/collider/Collider.java
  7. 5
      GameProject/src/collider/RectCollider.java
  8. 9
      GameProject/src/collider/package-info.java
  9. 14
      GameProject/src/controller/CollisionAwareEgoController.java
  10. 46
      GameProject/src/controller/EgoController.java
  11. 5
      GameProject/src/controller/EnemyController.java
  12. 1
      GameProject/src/controller/MineController.java
  13. 2
      GameProject/src/controller/ObjectController.java
  14. 2
      GameProject/src/gameobjects/AnimatedGameobject.java
  15. 8
      GameProject/src/gameobjects/EgoObject.java
  16. 2
      GameProject/src/gameobjects/FallingStar.java
  17. 2
      GameProject/src/gameobjects/RectObject.java
  18. 6
      GameProject/src/gameobjects/TextObject.java
  19. 7
      GameProject/src/gameobjects/package-info.java
  20. 2
      GameProject/src/log4j2.xml
  21. 2
      GameProject/src/playground/Animation.java
  22. 2
      GameProject/src/playground/Level2.java
  23. 2
      GameProject/src/playground/Level4.java
  24. 5
      GameProject/src/playground/LevelHitTwice.java
  25. 21
      GameProject/src/playground/LevelMovingObjects.java
  26. 3
      GameProject/src/playground/SpaceInvadersLevel.java
  27. 81
      GameProject/src/playground/SpaceInvadersLevelTest.java
  28. 9
      GameProject/src/playground/package-info.java
  29. 2
      GameProject/src/rendering/RectArtist.java
  30. 9
      GameProject/src/rendering/TextArtist.java
  31. 1
      GameProject/src/ui/GameUI.java
  32. BIN
      GameProject/video/alexG.jpg

4
.gitignore

@ -1,5 +1,7 @@
/.metadata/
*.DS_Store
*.metadata/
*.class *.class
*.prefs
/GameProject/doc/ /GameProject/doc/
/GameProject/log/ /GameProject/log/
/GameProject/highscore.txt /GameProject/highscore.txt

7
GameProject/.classpath

@ -1,8 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/apache-log4j-2.13.3-bin/log4j-1.2-api-2.13.3.jar"/> <classpathentry kind="lib" path="lib/apache-log4j-2.13.3-bin/log4j-1.2-api-2.13.3.jar"/>
<classpathentry kind="lib" path="lib/apache-log4j-2.13.3-bin/log4j-api-2.13.3.jar"/> <classpathentry kind="lib" path="lib/apache-log4j-2.13.3-bin/log4j-api-2.13.3.jar"/>
<classpathentry kind="lib" path="lib/apache-log4j-2.13.3-bin/log4j-core-2.13.3.jar"/> <classpathentry kind="lib" path="lib/apache-log4j-2.13.3-bin/log4j-core-2.13.3.jar"/>

4
GameProject/src/base/GameLoop.java

@ -217,8 +217,8 @@ public class GameLoop {
/** /**
* main to start the whole application.
* initializes the {@link #levels} ArrayList of Playground instances (levels) to be played with one level {@link SpaceInvadersLevel} in constructor of {@link #GameLoop}.
* main to start the whole application. It calls. {@link #runGame(String[])}.
* (levels are automatically added/loaded by constructor of {@link #GameLoop}).
* *
* @param args Java default command line args, forwarded to {@link #runGame(String[])} * @param args Java default command line args, forwarded to {@link #runGame(String[])}
* @throws IOException in case highscore.txt cannot be written. * @throws IOException in case highscore.txt cannot be written.

33
GameProject/src/base/MovingObjectsGame.java

@ -0,0 +1,33 @@
package base;
import java.io.IOException;
import playground.LevelMovingObjects;
/**
* main class to start a game with only one level {@link playground.LevelMovingObjects}.
*
*/
public class MovingObjectsGame extends GameLoop {
/**
* starts this game.
*
* @param args command line parameters (forwarded to {@link GameLoop#runGame(String[])}).
* @throws IOException if highscore.txt file cannot be written or accessed, the exception is
* thrown (and game ends).
*/
public static void main(String[] args) throws IOException {
GameLoop myGame = new MovingObjectsGame();
myGame.runGame(args);
}
/**
* adds only one level to play ({@link playground.LevelMovingObjects}).
*/
@Override
public void defineLevels() {
this.resetLevels();
this.addLevel(new LevelMovingObjects());
}
}

7
GameProject/src/collider/CircleCollider.java

@ -1,6 +1,5 @@
package collider; package collider;
import java.awt.Color;
import gameobjects.*; import gameobjects.*;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -48,7 +47,7 @@ public class CircleCollider extends Collider {
public boolean checkCollisionCircCirc(Collider _c2) throws Exception { public boolean checkCollisionCircCirc(Collider _c2) throws Exception {
CircleCollider c2 = (CircleCollider) _c2; CircleCollider c2 = (CircleCollider) _c2;
CircleCollider c1 = this; CircleCollider c1 = this;
logger.trace(c1.x + " " + c1.y + " " + c1.r + " " + c2.x + " " + c2.y+ " " + c2.r);
logger.trace(c1.x + " " + c1.y + " " + c1.r + " " + c2.x + " " + c2.y + " " + c2.r);
int kathete1 = (int) (Math.abs(c2.gameobject.getX() - c1.gameobject.getX())); int kathete1 = (int) (Math.abs(c2.gameobject.getX() - c1.gameobject.getX()));
int kathete2 = (int) (Math.abs(c2.gameobject.getX() - c1.gameobject.getY())); int kathete2 = (int) (Math.abs(c2.gameobject.getX() - c1.gameobject.getY()));
int hypothenuse = (int) (c1.r + c2.r); int hypothenuse = (int) (c1.r + c2.r);
@ -81,8 +80,4 @@ public class CircleCollider extends Collider {
throw new RuntimeException("Collider type not implemented!"); throw new RuntimeException("Collider type not implemented!");
} }
private Color color = Color.WHITE;
} }

2
GameProject/src/collider/Collider.java

@ -1,7 +1,5 @@
package collider; package collider;
import java.awt.Graphics2D;
import java.util.LinkedList;
import gameobjects.GameObject; import gameobjects.GameObject;
import playground.Playground; import playground.Playground;
import controller.ObjectController; import controller.ObjectController;

5
GameProject/src/collider/RectCollider.java

@ -1,7 +1,5 @@
package collider; package collider;
import java.awt.Color;
import gameobjects.*; import gameobjects.*;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -13,11 +11,8 @@ public class RectCollider extends Collider {
//double x; //double x;
//double y; //double y;
//double vx;
//double vy;
double w, h; double w, h;
private Color color = Color.WHITE;
private static Logger logger = LogManager.getLogger(RectCollider.class); private static Logger logger = LogManager.getLogger(RectCollider.class);

9
GameProject/src/collider/package-info.java

@ -0,0 +1,9 @@
/**
* The package contains classes implementing a 'bounding box' area around game objects. <br>
* The abstract base class {@link Collider} provides the abstract method {@link Collider#collidesWith(Collider)},
* which needs to be implemented by child classes to detect and decide whether or not an object with such instance really collides with the other.
* {@link Collider} instances are to be used for game objects ({@link gameobjects}); see constructors.<br>
*
* The benefit of seperating Colliders from visual representations is that the area for collisions can be smaller/bigger/other shape to improve game play experience.
*/
package collider;

14
GameProject/src/controller/CollisionAwareEgoController.java

@ -37,6 +37,9 @@ public class CollisionAwareEgoController extends EgoController {
this.shot = soundOnShot; this.shot = soundOnShot;
} }
/**
* Copies current values of x,y position and speed vx,vy into attributes. These can be restored by call to {@link #restoreDynamicState()}.
*/
public void saveDynamicState() { public void saveDynamicState() {
this.savex = this.getX(); this.savex = this.getX();
this.savey = this.getY(); this.savey = this.getY();
@ -45,6 +48,10 @@ public class CollisionAwareEgoController extends EgoController {
} }
/**
* Restores formally saved values of x,y position and speed vx,vy from attributes back to the ego object.
* These values should have been stored before by a call to {@link #saveDynamicState()}, otherwise all values will be 0.00.
*/
public void restoreDynamicState() { public void restoreDynamicState() {
this.setX(savex); this.setX(savex);
this.setY(savey); this.setY(savey);
@ -53,6 +60,10 @@ public class CollisionAwareEgoController extends EgoController {
} }
/**
* extends parent class implementation by a check whether or not the ego object collides with any other "obstacle" object.
* If yes, the position stays fixed (by using {@link #saveDynamicState()} and {@link #restoreDynamicState()}.
*/
public boolean stopObject() { public boolean stopObject() {
boolean s = super.stopObject(); boolean s = super.stopObject();
@ -73,6 +84,9 @@ public class CollisionAwareEgoController extends EgoController {
return s; return s;
} }
/**
* calls superclass {@link EgoController#onSpace(KeyEvent, GameObject)} only, if the time elapsed since last pressing of space is above 0.1 ms.
*/
public void onSpace(KeyEvent e, GameObject ego) { public void onSpace(KeyEvent e, GameObject ego) {
double cgt = ego.getGameTime(); double cgt = ego.getGameTime();
if ((cgt - this.lastSpaceAt) > 0.1) { if ((cgt - this.lastSpaceAt) > 0.1) {

46
GameProject/src/controller/EgoController.java

@ -42,26 +42,51 @@ public class EgoController extends ObjectController {
} }
/**
* moves ego up by {@link SpaceInvadersLevel#EGOSPEED}.
* @param kc KeyEvent to process (ignored)
* @param ego the ego object
*/
public void onUp(KeyEvent kc, GameObject ego) { public void onUp(KeyEvent kc, GameObject ego) {
ego.setVX(0.0); ego.setVX(0.0);
ego.setVY(-SpaceInvadersLevel.EGOSPEED); ego.setVY(-SpaceInvadersLevel.EGOSPEED);
} }
/**
* moves ego down by {@link SpaceInvadersLevel#EGOSPEED}.
* @param kc KeyEvent to process (ignored)
* @param ego the ego object
*/
public void onDown(KeyEvent kc, GameObject ego) { public void onDown(KeyEvent kc, GameObject ego) {
ego.setVX(0.0); ego.setVX(0.0);
ego.setVY(SpaceInvadersLevel.EGOSPEED); ego.setVY(SpaceInvadersLevel.EGOSPEED);
} }
/**
* moves ego left by {@link SpaceInvadersLevel#EGOSPEED}.
* @param kc KeyEvent to process (ignored)
* @param ego the ego object
*/
public void onLeft(KeyEvent kc, GameObject ego) { public void onLeft(KeyEvent kc, GameObject ego) {
ego.setVY(0.0); ego.setVY(0.0);
ego.setVX(-SpaceInvadersLevel.EGOSPEED); ego.setVX(-SpaceInvadersLevel.EGOSPEED);
} }
/**
* moves ego right by {@link SpaceInvadersLevel#EGOSPEED}.
* @param kc KeyEvent to process (ignored)
* @param ego the ego object
*/
public void onRight(KeyEvent kc, GameObject ego) { public void onRight(KeyEvent kc, GameObject ego) {
ego.setVY(0.0); ego.setVY(0.0);
ego.setVX(SpaceInvadersLevel.EGOSPEED); ego.setVX(SpaceInvadersLevel.EGOSPEED);
} }
/**
* sets speed to 0.0
* @param kc KeyEvent to process (ignored)
* @param ego the ego object
*/
public void onStop(KeyEvent kc, GameObject ego) { public void onStop(KeyEvent kc, GameObject ego) {
ego.setVY(0.0); ego.setVY(0.0);
ego.setVX(0.0); ego.setVX(0.0);
@ -72,7 +97,7 @@ public class EgoController extends ObjectController {
/** checks the position and respects level boundaries and own radius or width/height set on constructor. /** checks the position and respects level boundaries and own radius or width/height set on constructor.
* *
* @return true if the object reached the boundaries of the level, false otherwise
* @return true if the object reached the boundaries of the level, false otherwise.
*/ */
public boolean stopObject() { public boolean stopObject() {
// check whether ego object is at level boundaries // check whether ego object is at level boundaries
@ -93,7 +118,7 @@ public class EgoController extends ObjectController {
} }
/** behavior for shooting on key space
/** behavior for shooting on key space. Creates a new shot using {#link SimpleShotController} with a {@link RectObject}.
* *
* @param e KeyEvent of the space key * @param e KeyEvent of the space key
* @param ego EgoObject instance (used to determine position of shot object's start) * @param ego EgoObject instance (used to determine position of shot object's start)
@ -119,7 +144,7 @@ public class EgoController extends ObjectController {
/** /**
* updates position based on key events (mouse currently ignored)
* updates position based on key events (mouse currently ignored).
*/ */
public void updateObject() { public void updateObject() {
@ -140,23 +165,14 @@ public class EgoController extends ObjectController {
released = false; released = false;
} }
/**
* Generelle Idee: Wenn eine Taste gedrückt wird wird sie gespeichert. wenn die zuvor
* gespeicherte Taste wieder losgelassen wird stoppt das Ego-Objekt. Falls vor dem Loslassen
* eine andere Taste gedrückt wird, wird diese gespeichert und die alte vergessen. Dh das
* loslassen der alten Taste stoppt das Objekt nicht. Spezialfall: space, das loslassen von
* space stoppt das Objekt nicht!
*/
if (pressed == true) { if (pressed == true) {
lastPressedKey = pressedKey; lastPressedKey = pressedKey;
pressedKey = kc; pressedKey = kc;
} }
/**
* Nur eine losgelassene Taste die auch vorher gedrückt wurde stoppt das Objekt. Eine
* losgelassene Taste die nicht vorher gedrückt wurde bzw vergessen wurde stoppt das Objekt
* nicht
/*
* Only if the released key is the same as the before pressed one, it stops the ego object movement.
*/ */
if (released == true) { if (released == true) {
if (pressedKey != null) { if (pressedKey != null) {
@ -194,7 +210,7 @@ public class EgoController extends ObjectController {
// shot // shot
if (kc == KeyEvent.VK_SPACE) { if (kc == KeyEvent.VK_SPACE) {
// space is not registered! Releasing space does not stop the egoobject
// space is not registered! Releasing space does not stop the ego object
this.onSpace(e, ego); this.onSpace(e, ego);
} }
} }

5
GameProject/src/controller/EnemyController.java

@ -1,6 +1,7 @@
package controller; package controller;
import playground.Playground;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -27,8 +28,8 @@ public class EnemyController extends ObjectController {
if (gameObject.getY() >= this.getPlayground().getSizeY()) { if (gameObject.getY() >= this.getPlayground().getSizeY()) {
this.getPlayground().deleteObject(gameObject.getId()); this.getPlayground().deleteObject(gameObject.getId());
// add to points counter // add to points counter
Integer pts = (Integer) this.getPlayground().getGlobalFlag("points");
this.getPlayground().setGlobalFlag("points", pts - 200);
Integer pts = (Integer) Playground.getGlobalFlag("points");
Playground.setGlobalFlag("points", pts - 200);
} }
applySpeedVector(); applySpeedVector();

1
GameProject/src/controller/MineController.java

@ -1,6 +1,5 @@
package controller; package controller;
import controller.ObjectController;
import gameobjects.GameObject; import gameobjects.GameObject;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;

2
GameProject/src/controller/ObjectController.java

@ -56,7 +56,7 @@ public abstract class ObjectController {
public void applySpeedVector() { public void applySpeedVector() {
double ts = this.getPlayground().getTimestep(); double ts = this.getPlayground().getTimestep();
this.setX(this.getX() + this.getVX() * ts); this.setX(this.getX() + this.getVX() * ts);
gameObject.setY(this.getY() + this.getVY() * ts);
this.setY(this.getY() + this.getVY() * ts);
} }

2
GameProject/src/gameobjects/AnimatedGameobject.java

@ -1,9 +1,7 @@
package gameobjects; package gameobjects;
import java.util.LinkedList;
import playground.Playground; import playground.Playground;
import playground.Animation; import playground.Animation;
import collider.Collider;
import collider.RectCollider; import collider.RectCollider;
import rendering.*; import rendering.*;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;

8
GameProject/src/gameobjects/EgoObject.java

@ -3,14 +3,6 @@ package gameobjects;
import java.awt.Color; import java.awt.Color;
import collider.*; import collider.*;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import javax.imageio.ImageIO;
import collider.Collider;
import controller.ObjectController;
import playground.Playground; import playground.Playground;
import rendering.*; import rendering.*;

2
GameProject/src/gameobjects/FallingStar.java

@ -8,7 +8,6 @@ import rendering.*;
public class FallingStar extends GameObject { public class FallingStar extends GameObject {
private Color color = Color.WHITE;
protected double rad = -1; protected double rad = -1;
@ -17,7 +16,6 @@ public class FallingStar extends GameObject {
Color color, double rad) { Color color, double rad) {
super(id, playground, x, y, vx, vy); super(id, playground, x, y, vx, vy);
this.rad = rad; this.rad = rad;
this.color = color;
LinkedList<Collider> cols = new LinkedList<Collider>(); LinkedList<Collider> cols = new LinkedList<Collider>();
CircleCollider cc = new CircleCollider("cc", this, rad); CircleCollider cc = new CircleCollider("cc", this, rad);
cols.add(cc); cols.add(cc);

2
GameProject/src/gameobjects/RectObject.java

@ -1,10 +1,8 @@
package gameobjects; package gameobjects;
import java.awt.Color; import java.awt.Color;
import java.io.File;
import collider.RectCollider; import collider.RectCollider;
import playground.Playground; import playground.Playground;
import playground.SpaceInvadersLevel;
import rendering.RectArtist; import rendering.RectArtist;
/** /**

6
GameProject/src/gameobjects/TextObject.java

@ -1,14 +1,8 @@
package gameobjects; package gameobjects;
import java.awt.Color; import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import java.util.LinkedList; import java.util.LinkedList;
import collider.*; import collider.*;
import controller.ObjectController;
import playground.Playground; import playground.Playground;
import rendering.*; import rendering.*;

7
GameProject/src/gameobjects/package-info.java

@ -0,0 +1,7 @@
/**
* The package gameobjects contains all objects with a visual representation on screen.
* They can be combined to use controller instances for their behavior (subclasses of {@link controller.ObjectController}).
* The abstract base class is {@link GameObject}, which forces child-classes to implement the method
* {@link GameObject#updateObject()}.
*/
package gameobjects;

2
GameProject/src/log4j2.xml

@ -7,7 +7,7 @@
<PatternLayout pattern="%d %-5level %logger{36} - %msg%n" /> <PatternLayout pattern="%d %-5level %logger{36} - %msg%n" />
</Console> </Console>
<File name="File" fileName="log\log4j.log">
<File name="File" fileName="log/log4j.log">
<PatternLayout pattern="%d %-5level %logger{36} - %msg%n" /> <PatternLayout pattern="%d %-5level %logger{36} - %msg%n" />
</File> </File>

2
GameProject/src/playground/Animation.java

@ -38,7 +38,6 @@ public class Animation {
String zeile; String zeile;
double zeit; double zeit;
int it = 0;
while (scanner.hasNext()) { while (scanner.hasNext()) {
if (scanner.hasNextDouble()) { if (scanner.hasNextDouble()) {
@ -57,7 +56,6 @@ public class Animation {
logger.warn(file + " not found!!"); logger.warn(file + " not found!!");
} }
it++;
logger.trace(basePath.getParent().toString() + "/" + zeile); logger.trace(basePath.getParent().toString() + "/" + zeile);
} }
} }

2
GameProject/src/playground/Level2.java

@ -3,7 +3,7 @@ package playground;
/** /**
* extends extends {@link SpaceInvadersLevel} with a different startup message.
* extends {@link SpaceInvadersLevel} with a different startup message.
*/ */
public class Level2 extends SpaceInvadersLevel { public class Level2 extends SpaceInvadersLevel {

2
GameProject/src/playground/Level4.java

@ -10,7 +10,7 @@ import org.apache.logging.log4j.Logger;
/** /**
* extends extends {@link SpaceInvadersLevel}
* extends {@link SpaceInvadersLevel} with aliens that need two hits to be destroyed.
* <ul> * <ul>
* <li>Hit aliens twice to kill them * <li>Hit aliens twice to kill them
* <li>they say AUA when not destroyed * <li>they say AUA when not destroyed

5
GameProject/src/playground/LevelHitTwice.java

@ -1,11 +1,6 @@
package playground; package playground;
import controller.FallingStarController;
import gameobjects.EgoObject;
import gameobjects.FallingStar;
import gameobjects.GameObject; import gameobjects.GameObject;
import gameobjects.RectObject;
import java.awt.Color;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;

21
GameProject/src/playground/LevelMovingObjects.java

@ -0,0 +1,21 @@
package playground;
/** This level adds two distracting objects to the canvas that cannot collide but bounce around all the time.
*/
public class LevelMovingObjects extends SpaceInvadersLevel {
// TODO your code here
/** "Moving Objects Level!" is the message.
*
* @return String "Moving Objects Level!"
*/
@Override
protected String getStartupMessage() {
return "Moving Objects Level!";
}
}

3
GameProject/src/playground/SpaceInvadersLevel.java

@ -1,6 +1,5 @@
package playground; package playground;
// import utilities.* ;
import java.awt.Color; import java.awt.Color;
import java.awt.Font; import java.awt.Font;
import java.awt.Graphics2D; import java.awt.Graphics2D;
@ -237,7 +236,6 @@ public class SpaceInvadersLevel extends Playground {
// check whether all enemies have been destroyed or escaped // check whether all enemies have been destroyed or escaped
if (enemies.size() == 0) { if (enemies.size() == 0) {
HighscoreManager hsm = new HighscoreManager();
HighscoreManager.writeHSToFile((Integer) Playground.getGlobalFlag("points"), HighscoreManager.writeHSToFile((Integer) Playground.getGlobalFlag("points"),
(Integer) Playground.getGlobalFlag("highscore")); (Integer) Playground.getGlobalFlag("highscore"));
this.doneLevel = true; this.doneLevel = true;
@ -487,7 +485,6 @@ public class SpaceInvadersLevel extends Playground {
} }
void createCollectables() { void createCollectables() {
double gameTime = this.getGameTime();
// create collectables // create collectables
for (int i = 0; i < this.calcNrCollect(); i++) { for (int i = 0; i < this.calcNrCollect(); i++) {

81
GameProject/src/playground/SpaceInvadersLevelTest.java

@ -1,81 +0,0 @@
package playground;
import static org.junit.Assert.assertTrue;
import java.awt.Color;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import gameobjects.EgoObject;
import gameobjects.GameObject;
import gameobjects.RectObject;
/**
* Tests {@link SpaceInvadersLevel} for
* <ol>
* <li>calcEnemySpeedX() returns the same value as constant SpaceInvadersLevel.ENEMYSPEEDX
* <li>calcEnemySpeedY() returns the same value as constant SpaceInvadersLevel.ENEMYSPEEDY
* <li>calcNrEnemies() returns the same value as constant SpaceInvadersLevel.NR_ENEMIES
* <li>actionIfEnemyIsHit() adds 200 points to score
* <li>actionIfEgoObjectIsHit() reduces number of lives (egoLives)
* </ol>
* @author jkonert
*
*/
class SpaceInvadersLevelTest {
private static SpaceInvadersLevel myLevel;
@BeforeAll
static void setUpBeforeClass() throws Exception {
myLevel = new SpaceInvadersLevel();
SpaceInvadersLevel.setGlobalFlag("egoLives", 5);
SpaceInvadersLevel.setGlobalFlag("points", 500);
SpaceInvadersLevel.setGlobalFlag("highscore", 5000);
}
@AfterAll
static void tearDownAfterClass() throws Exception {
// nothing
}
@Test
void testCalcEnemySpeedX() {
assertTrue("EnemySpeedX is as in SpaceInvadersLevel defined", myLevel.calcEnemySpeedX() == SpaceInvadersLevel.ENEMYSPEEDX);
}
@Test
void testCalcEnemySpeedY() {
assertTrue("EnemySpeedY is as in SpaceInvadersLevel defined", myLevel.calcEnemySpeedY() == SpaceInvadersLevel.ENEMYSPEEDY);
}
@Test
void testCalcNrEnemies() {
assertTrue("NrOfEnemies is as in SpaceInvadersLevel defined", myLevel.calcNrEnemies() == SpaceInvadersLevel.NR_ENEMIES);
}
@Test
void testActionIfEnemyIsHitPointsUp() {
Integer numPointsBefore = (Integer)myLevel.getGlobalFlag("points");
GameObject dummyShot = new RectObject("shot1", myLevel, 0,0,0,0, 12, 12, Color.WHITE);
GameObject dummyEnemy = new RectObject("ego1", myLevel, 0,0,0,0, 12, 12, Color.BLACK);
myLevel.addObject(dummyShot);
myLevel.addObject(dummyEnemy);
myLevel.actionIfEnemyIsHit(dummyEnemy, dummyShot);; // this is the call under test
Integer numPointsAfter = (Integer)myLevel.getGlobalFlag("points"); // changed?
assertTrue("numPoints is up +200 after EnemyIsHit", numPointsAfter == numPointsBefore + 200); // points are set +200 , check.
}
@Test
void testActionIfEgoObjectIsHitLivesDown() {
Integer numLivesBefore = (Integer)myLevel.getGlobalFlag("egoLives");
GameObject dummyShot = new RectObject("shot1", myLevel, 0,0,0,0, 12, 12, Color.RED);
GameObject dummyEgo = new EgoObject("ego1", myLevel, 0,0,0,0, 5);
myLevel.addObject(dummyShot);
myLevel.actionIfEgoObjectIsHit(dummyShot, dummyEgo); // this is the call under test
Integer numLivesAfter = (Integer)myLevel.getGlobalFlag("egoLives"); // changed?
assertTrue("numLives is reduced by one ifEgoIsHit", numLivesAfter == numLivesBefore - 1); // lives is reduced by one
}
}

9
GameProject/src/playground/package-info.java

@ -0,0 +1,9 @@
/**
* The package playground contains all level specific logic and control of level logic.
* The structure and general logic (with global and local flags to be stored/used)
* is provided in abstract base class {@link Playground}.<br>
* Child-classes implement specific logic for one level and game type (e.g. {@link SpaceInvadersLevel}).<b>
*
* Generally, the base class {@link Playground} supports totally different game types to be implemented.
*/
package playground;

2
GameProject/src/rendering/RectArtist.java

@ -23,8 +23,6 @@ public class RectArtist extends Artist {
// g.drawLine((int) (Math.round(this.getX())), (int) (Math.round(this.getY()-this.height/2.)), // g.drawLine((int) (Math.round(this.getX())), (int) (Math.round(this.getY()-this.height/2.)),
// (int) Math.round(this.getX()), // (int) Math.round(this.getX()),
// (int) Math.round(this.getY() + this.height/2.)); // (int) Math.round(this.getY() + this.height/2.));
int x = (int) this.getX();
int y = (int) this.getY();
g.fillRect((int) (this.getX() - this.width / 2.), (int) (this.getY() - this.height / 2.), g.fillRect((int) (this.getX() - this.width / 2.), (int) (this.getY() - this.height / 2.),
(int) this.width, (int) this.height); (int) this.width, (int) this.height);

9
GameProject/src/rendering/TextArtist.java

@ -14,14 +14,19 @@ import java.text.AttributedString;
public class TextArtist extends Artist { public class TextArtist extends Artist {
private String text = null; private String text = null;
private int size = 1;
private Color textColor = null; private Color textColor = null;
protected double textWidth, textHeight; protected double textWidth, textHeight;
Font serifFont = null; Font serifFont = null;
/** Constructor to intitialize the TextArtist attributes
*
* @param go GameObject to be used for xy coordinate reference
* @param text the text to draw
* @param size point size to be used for font "Serif"
* @param textColor color to draw the text with (foreground)
*/
public TextArtist(GameObject go, String text, int size, Color textColor) { public TextArtist(GameObject go, String text, int size, Color textColor) {
super(go); super(go);
this.size = size;
this.text = text; this.text = text;
this.serifFont = new Font("Serif", Font.PLAIN, size); this.serifFont = new Font("Serif", Font.PLAIN, size);

1
GameProject/src/ui/GameUI.java

@ -1,6 +1,5 @@
package ui; package ui;
import java.awt.Dimension;
import java.util.*; import java.util.*;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JMenu; import javax.swing.JMenu;

BIN
GameProject/video/alexG.jpg

Before

Width: 1664  |  Height: 2496  |  Size: 181 KiB

Loading…
Cancel
Save