Praktikum 6

This commit is contained in:
Andreas Greiner 2021-04-29 15:06:00 +02:00
parent dc1f89a176
commit 2523cc67cc
58 changed files with 99 additions and 18 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
./out/production

View File

@ -14,8 +14,8 @@ public class GameLoopManager {
*/ */
public GameLoopManager() { public GameLoopManager() {
this.gameView = new GameView(); this.gameView = new GameView();
this.gameView.setWindowTitle("UFO-Fight"); this.gameView.setWindowTitle("Super Pang World");
this.gameView.setStatusText("Andreas Berl - Java Programmierung SS 2021"); this.gameView.setStatusText("Andreas Greiner - Java Programmierung SS 2021");
this.gameView.setWindowIcon("Target.png"); this.gameView.setWindowIcon("Target.png");
this.gameObjectManager = new GameObjectManager(gameView); this.gameObjectManager = new GameObjectManager(gameView);
this.inputManager = new InputManager(gameView, gameObjectManager.getPlayer()); this.inputManager = new InputManager(gameView, gameObjectManager.getPlayer());

View File

@ -17,9 +17,9 @@ class GameObjectManager {
/** The GameView to display the objects */ /** The GameView to display the objects */
private GameView gameView; private GameView gameView;
/** The game objects */
private LinkedList<Bubble> bubbles; private LinkedList<Bubble> bubbles;
private LinkedList<Harpoon> harpoons; private LinkedList<Harpoon> harpoons;
private LevelProgressBar levelProgressBar; private LevelProgressBar levelProgressBar;
private LevelLabel levelLabel; private LevelLabel levelLabel;
private ScoreLabel scoreLabel; private ScoreLabel scoreLabel;
@ -41,22 +41,13 @@ class GameObjectManager {
harpoons = new LinkedList<>(); harpoons = new LinkedList<>();
gameObjects = new LinkedList<>(); gameObjects = new LinkedList<>();
bubbles.add(new RoundBubble(gameView));
bubbles.add(new HexagonalBubble(gameView));
bubbles.add(new SpecialBubble(gameView));
harpoons.add(new Harpoon(gameView));
this.levelProgressBar = new LevelProgressBar(gameView); this.levelProgressBar = new LevelProgressBar(gameView);
this.levelLabel = new LevelLabel(gameView); this.levelLabel = new LevelLabel(gameView);
this.scoreLabel = new ScoreLabel(gameView); this.scoreLabel = new ScoreLabel(gameView);
this.livesIcon = new LivesIcon(gameView); this.livesIcon = new LivesIcon(gameView);
this.background = new Background(gameView); this.background = new Background(gameView);
this.player = new Player(gameView); this.player = new Player(gameView);
} }
/** /**

View File

@ -1,6 +1,13 @@
package de.thdeg.greiner.superpangworld.game.managers; package de.thdeg.greiner.superpangworld.game.managers;
import de.thdeg.greiner.superpangworld.gameview.GameView; import de.thdeg.greiner.superpangworld.gameview.GameView;
import de.thdeg.greiner.superpangworld.graphics.base.Position;
import de.thdeg.greiner.superpangworld.graphics.moveable.Harpoon;
import de.thdeg.greiner.superpangworld.graphics.moveable.HexagonalBubble;
import de.thdeg.greiner.superpangworld.graphics.moveable.RoundBubble;
import de.thdeg.greiner.superpangworld.graphics.moveable.SpecialBubble;
import java.util.Random;
/** /**
* The manager which handles the gameplay. * The manager which handles the gameplay.
@ -11,6 +18,10 @@ public class GamePlayManager {
private GameView gameView; private GameView gameView;
/** The manager, which handles the game objects */ /** The manager, which handles the game objects */
private GameObjectManager gameObjectManager; private GameObjectManager gameObjectManager;
/** Flag, if the gameobjects were already deleted once */
private boolean listHasBeenDeleted = false;
/** Generator for random values */
private Random random;
/** /**
* Create the manager handling the gameplay. * Create the manager handling the gameplay.
@ -20,12 +31,68 @@ public class GamePlayManager {
public GamePlayManager(GameView gameView, GameObjectManager gameObjectManager) { public GamePlayManager(GameView gameView, GameObjectManager gameObjectManager) {
this.gameView = gameView; this.gameView = gameView;
this.gameObjectManager = gameObjectManager; this.gameObjectManager = gameObjectManager;
this.random = new Random();
gameObjectManager.getPlayer().setGamePlayManager(this);
} }
/** /**
* Handle the gameplay. * Handle the gameplay.
*/ */
public void updateGamePlay(){ public void updateGamePlay(){
spawnAndDestroyBubbles();
}
/**
* Spawn and destory bubbles.
*/
private void spawnAndDestroyBubbles(){
// Spawn a bubble every second
if(gameView.timerExpired("SpawnBubble","GamePlayManager")){
addRandomBubble();
gameView.setTimer("SpawnBubble","GamePlayManager",1000);
}
// Remove a bubble every 5 seconds
if(!gameObjectManager.getBubbles().isEmpty() && gameView.timerExpired("DestroyBubble","GamePlayManager")){
gameObjectManager.getBubbles().remove(random.nextInt(gameObjectManager.getBubbles().size()));
gameView.setTimer("DestroyBubble","GamePlayManager",5000);
}
// Delete all bubbles after 10 seconds of playing
if(!listHasBeenDeleted && gameView.getGameTimeInMilliseconds()>=10000){
gameObjectManager.getBubbles().clear();
listHasBeenDeleted = true;
}
} }
/**
* Adds a random bubble to the game. Percentages:
* - 60% normal bubble
* - 30% hexagonal bubble
* - 10% special bubble
*/
private void addRandomBubble(){
int randomNumber = random.nextInt(100);
if(randomNumber<60){
gameObjectManager.getBubbles().add(new RoundBubble(gameView));
}else if(randomNumber<90){
gameObjectManager.getBubbles().add(new HexagonalBubble(gameView));
}else{
gameObjectManager.getBubbles().add(new SpecialBubble(gameView));
}
}
public void shootHarpoon(Position startPosition){
if(gameView.timerExpired("ShootHarpoon","GamePlayManager")){
Harpoon harpoon = new Harpoon(gameView);
harpoon.getPosition().setTo(startPosition.x, startPosition.y);
harpoon.setGamePlayManager(this);
gameObjectManager.getHarpoons().add(harpoon);
gameView.setTimer("ShootHarpoon","GamePlayManager",300);
}
}
public void destroy(Harpoon harpoon){
gameObjectManager.getHarpoons().remove(harpoon);
}
} }

View File

@ -31,7 +31,7 @@ public class Harpoon extends GameObject {
} }
/** /**
* Zeichnet die Blase auf die {@link GameView}. * Draws the harpoon onto the {@link GameView}.
*/ */
@Override @Override
public void addToCanvas(){ public void addToCanvas(){
@ -44,13 +44,20 @@ public class Harpoon extends GameObject {
} }
/** /**
* Bewegt die Harpune einen Schritt weiter. * Moves the harpoon a tick further upwards.
*/ */
@Override @Override
public void updatePosition(){ public void updatePosition(){
getPosition().up(speedInPixel); getPosition().up(speedInPixel);
if(getPosition().y < 0){ }
getPosition().setTo(300,300);
/**
* Deletes the harpoon if it hits the roof.
*/
@Override
public void updateStatus() {
if(position.y <=0 ){
gamePlayManager.destroy(this);
} }
} }
} }

View File

@ -3,6 +3,8 @@ package de.thdeg.greiner.superpangworld.graphics.moveable;
import de.thdeg.greiner.superpangworld.gameview.GameView; import de.thdeg.greiner.superpangworld.gameview.GameView;
import de.thdeg.greiner.superpangworld.graphics.base.Bubble; import de.thdeg.greiner.superpangworld.graphics.base.Bubble;
import java.util.Random;
/** /**
* The rare hexagonal bubble. * The rare hexagonal bubble.
*/ */
@ -15,7 +17,10 @@ public class HexagonalBubble extends Bubble {
*/ */
public HexagonalBubble(GameView gameView) { public HexagonalBubble(GameView gameView) {
super(gameView); super(gameView);
position.setTo(100,200);
Random rand = new Random();
position.setTo(rand.nextInt(GameView.WIDTH),rand.nextInt(GameView.HEIGHT));
size=0.6; size=0.6;
speedInPixel = 2; speedInPixel = 2;
} }

View File

@ -82,5 +82,6 @@ public class Player extends GameObject {
/** Lets the character shoot */ /** Lets the character shoot */
public void shoot(){ public void shoot(){
shooting = true; shooting = true;
gamePlayManager.shootHarpoon(this.position);
} }
} }

View File

@ -3,6 +3,8 @@ package de.thdeg.greiner.superpangworld.graphics.moveable;
import de.thdeg.greiner.superpangworld.gameview.GameView; import de.thdeg.greiner.superpangworld.gameview.GameView;
import de.thdeg.greiner.superpangworld.graphics.base.Bubble; import de.thdeg.greiner.superpangworld.graphics.base.Bubble;
import java.util.Random;
/** /**
* The classic red, round bubble. * The classic red, round bubble.
@ -31,6 +33,9 @@ public class RoundBubble extends Bubble {
*/ */
public RoundBubble(GameView gameView) { public RoundBubble(GameView gameView) {
super(gameView); super(gameView);
Random rand = new Random();
position.setTo(rand.nextInt(GameView.WIDTH),rand.nextInt(GameView.HEIGHT));
} }
/** /**

View File

@ -3,6 +3,8 @@ package de.thdeg.greiner.superpangworld.graphics.moveable;
import de.thdeg.greiner.superpangworld.gameview.GameView; import de.thdeg.greiner.superpangworld.gameview.GameView;
import de.thdeg.greiner.superpangworld.graphics.base.Bubble; import de.thdeg.greiner.superpangworld.graphics.base.Bubble;
import java.util.Random;
/** /**
* The green bubble with special abilities. * The green bubble with special abilities.
*/ */
@ -30,7 +32,9 @@ public class SpecialBubble extends Bubble {
*/ */
public SpecialBubble(GameView gameView) { public SpecialBubble(GameView gameView) {
super(gameView); super(gameView);
position.setTo(100, 100);
Random rand = new Random();
position.setTo(rand.nextInt(GameView.WIDTH),rand.nextInt(GameView.HEIGHT));
} }
@Override @Override

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB