Praktikum 5

This commit is contained in:
Andreas Greiner 2021-04-26 18:27:12 +02:00
parent f8b69c9e16
commit 70d431a32d
23 changed files with 232 additions and 134 deletions

View File

@ -1,87 +0,0 @@
package de.thdeg.greiner.superpangworld.game;
import de.thdeg.greiner.superpangworld.gameview.GameView;
import de.thdeg.greiner.superpangworld.graphics.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Arrays;
/** Der Manager zur Verwaltung der Spielschleife. */
public class GameLoopManager {
private static final boolean DIAGONAL_MOVEMENT_ALLOWED = false;
private GameView gameView;
private Bubble bubble;
private Harpoon harpoon;
private LevelProgressBar levelProgressBar;
private LevelLabel levelLabel;
private ScoreLabel scoreLabel;
private Player player;
/** Erzeugt den GameLoopManager mit Standardwerten. */
public GameLoopManager() {
this.gameView = new GameView();
this.gameView.setWindowTitle("Super Pang World");
this.gameView.setStatusText("Andreas Greiner - Java Programmierung SS 2021");
this.gameView.setWindowIcon("Target.png");
this.bubble = new Bubble(gameView);
this.harpoon = new Harpoon(gameView);
this.levelProgressBar = new LevelProgressBar(gameView);
this.levelLabel = new LevelLabel(gameView);
this.scoreLabel = new ScoreLabel(gameView);
this.player = new Player(gameView);
gameView.setColorForBlockImage('k',Color.LIGHT_GRAY);
}
/** Startet das Spiel. */
public void startGame() {
while(true){
updateUserInputs();
bubble.updatePosition();
bubble.addToCanvas();
harpoon.addToCanvas();
levelProgressBar.addToCanvas();
levelLabel.addToCanvas();
scoreLabel.addToCanvas();
player.addToCanvas();
gameView.printCanvas();
}
}
void updateUserInputs() {
Integer[] gedruekteTasten = gameView.getKeyCodesOfCurrentlyPressedKeys();
if(Arrays.stream(gedruekteTasten).anyMatch(k -> k == KeyEvent.VK_SPACE)){
player.shoot();
}
for (int keyCode : gedruekteTasten) {
switch(keyCode) {
case KeyEvent.VK_LEFT:
player.left();
break;
case KeyEvent.VK_RIGHT:
player.right();
break;
default:
continue;
}
if(!DIAGONAL_MOVEMENT_ALLOWED){
return;
}
}
}
}

View File

@ -1,6 +1,6 @@
package de.thdeg.greiner.superpangworld.game; package de.thdeg.greiner.superpangworld.game;
import de.thdeg.greiner.superpangworld.game.GameLoopManager; import de.thdeg.greiner.superpangworld.managers.GameLoopManager;
/** /**
* Die Startklasse für das Spiel. * Die Startklasse für das Spiel.

View File

@ -0,0 +1,39 @@
package de.thdeg.greiner.superpangworld.managers;
import de.thdeg.greiner.superpangworld.gameview.GameView;
import de.thdeg.greiner.superpangworld.managers.GameObjectManager;
import de.thdeg.greiner.superpangworld.managers.GamePlayManager;
import de.thdeg.greiner.superpangworld.managers.InputManager;
/** This class manages the main game loop of the game. */
public class GameLoopManager {
private final GameView gameView;
private final InputManager inputManager;
private final GameObjectManager gameObjectManager;
private final GamePlayManager gamePlayManager;
/**
* Creates the main loop.
*/
public GameLoopManager() {
this.gameView = new GameView();
this.gameView.setWindowTitle("UFO-Fight");
this.gameView.setStatusText("Andreas Berl - Java Programmierung SS 2021");
this.gameView.setWindowIcon("Target.png");
this.gameObjectManager = new GameObjectManager(gameView);
this.inputManager = new InputManager(gameView, gameObjectManager.getPlayer());
this.gamePlayManager = new GamePlayManager(gameView, gameObjectManager);
}
/**
* Starts the main loop of the game.
*/
public void startGame() {
while (true) { // The "Game-Loop"
gamePlayManager.updateGamePlay();
inputManager.updateUserInputs();
gameObjectManager.updateGameObjects();
gameView.printCanvas();
}
}
}

View File

@ -0,0 +1,86 @@
package de.thdeg.greiner.superpangworld.managers;
import de.thdeg.greiner.superpangworld.gameview.GameView;
import de.thdeg.greiner.superpangworld.graphics.immovable.*;
import de.thdeg.greiner.superpangworld.graphics.moveable.*;
import java.awt.*;
/**
* The manager handling the display and passive movement of the game objects.
*/
public class GameObjectManager {
/** The GameView to display the objects */
private GameView gameView;
/** The game objects */
private RoundBubble roundBubble;
private HexagonalBubble hexBubble;
private SpecialBubble specialBubble;
private Harpoon harpoon;
private LevelProgressBar levelProgressBar;
private LevelLabel levelLabel;
private ScoreLabel scoreLabel;
private LivesIcon livesIcon;
private Background background;
private Player player;
/**
* Create a manager handling the display and passive movement of the game objects.
* @param gameView the GameView
*/
public GameObjectManager(GameView gameView){
this.gameView = gameView;
gameView.setColorForBlockImage('k', Color.LIGHT_GRAY);
this.roundBubble = new RoundBubble(gameView);
this.hexBubble = new HexagonalBubble(gameView);
this.specialBubble = new SpecialBubble(gameView);
this.harpoon = new Harpoon(gameView);
this.levelProgressBar = new LevelProgressBar(gameView);
this.levelLabel = new LevelLabel(gameView);
this.scoreLabel = new ScoreLabel(gameView);
this.livesIcon = new LivesIcon(gameView);
this.background = new Background(gameView);
this.player = new Player(gameView);
}
/**
* Add the objects to the canvas and handle update their positions.
*/
public void updateGameObjects(){
background.addToCanvas();
roundBubble.updatePosition();
roundBubble.addToCanvas();
hexBubble.updatePosition();
hexBubble.addToCanvas();
specialBubble.updatePosition();
specialBubble.addToCanvas();
harpoon.updatePosition();
harpoon.addToCanvas();
levelProgressBar.addToCanvas();
levelLabel.addToCanvas();
scoreLabel.addToCanvas();
livesIcon.addToCanvas();
player.addToCanvas();
}
/**
* Get the player object.
* @return the player
*/
public Player getPlayer(){
return player;
}
}

View File

@ -0,0 +1,31 @@
package de.thdeg.greiner.superpangworld.managers;
import de.thdeg.greiner.superpangworld.gameview.GameView;
/**
* The manager which handles the gameplay.
*/
public class GamePlayManager {
/** The GameView on which the game is displayed */
private GameView gameView;
/** The manager, which handles the game objects */
private GameObjectManager gameObjectManager;
/**
* Create the manager handling the gameplay.
* @param gameView
* @param gameObjectManager
*/
public GamePlayManager(GameView gameView, GameObjectManager gameObjectManager) {
this.gameView = gameView;
this.gameObjectManager = gameObjectManager;
}
/**
* Handle the gameplay.
*/
public void updateGamePlay(){
}
}

View File

@ -0,0 +1,58 @@
package de.thdeg.greiner.superpangworld.managers;
import de.thdeg.greiner.superpangworld.gameview.GameView;
import de.thdeg.greiner.superpangworld.graphics.moveable.Player;
import java.awt.event.KeyEvent;
import java.util.Arrays;
/**
* The manager which handles the user input and controls the player object.
*/
public class InputManager {
/** The gameView, which displays the player object */
private GameView gameView;
/** The player object */
private Player player;
/** Flag, if diagonal movement is allowed */
private static final boolean DIAGONAL_MOVEMENT_ALLOWED = false;
/**
* Create a manager handling the user input
* @param gameView the GameView
* @param player the player object
*/
public InputManager(GameView gameView, Player player){
this.gameView = gameView;
this.player = player;
}
/**
* Read the user inputs and perform actions accordingly
*/
public void updateUserInputs() {
Integer[] gedruekteTasten = gameView.getKeyCodesOfCurrentlyPressedKeys();
if(Arrays.stream(gedruekteTasten).anyMatch(k -> k == KeyEvent.VK_SPACE)){
player.shoot();
}
for (int keyCode : gedruekteTasten) {
switch(keyCode) {
case KeyEvent.VK_LEFT:
player.left();
break;
case KeyEvent.VK_RIGHT:
player.right();
break;
default:
continue;
}
if(!DIAGONAL_MOVEMENT_ALLOWED){
return;
}
}
}
}

View File

@ -1,18 +0,0 @@
package de.thdeg.greiner.superpangworld.graphics;
import de.thdeg.greiner.superpangworld.gameview.GameView;
/**
* The label for the remaining lives count.
*/
public class LivesLabe extends GameObject{
/**
* Create a lives label with default values.
*
* @param gameView the gameView
*/
protected LivesLabe(GameView gameView) {
super(gameView);
}
}

View File

@ -1,26 +1,11 @@
package de.thdeg.greiner.superpangworld.graphics; package de.thdeg.greiner.superpangworld.graphics.base;
import de.thdeg.greiner.superpangworld.gameview.GameView; import de.thdeg.greiner.superpangworld.gameview.GameView;
/** /**
* A Bubble, which moves around on the screen and can be shot by the player. * A Bubble, which moves around on the screen and can be shot by the player.
*/ */
public class Bubble extends GameObject{ public class Bubble extends GameObject {
/** The pixel art for the bubble */
private final static String BUBBLE =
" RRRR \n"+
" RWRRRR \n"+
" RWRRRRRR \n"+
" RWRRRRRRRR \n"+
"RWRRRRRRRRRR\n"+
"RWRRRRRRRRRR\n"+
"RWRRRRRRRRWR\n"+
"RRRRRRRRRWWR\n"+
" RRRRRRRWWR \n"+
" RRRRRWWR \n"+
" RRRWWR \n"+
" RRRR \n";
/** Flag, if the bubble flies from left to right */ /** Flag, if the bubble flies from left to right */
private boolean flyFromLeftToRight; private boolean flyFromLeftToRight;
@ -39,14 +24,6 @@ public class Bubble extends GameObject{
flyFromLeftToRight = true; flyFromLeftToRight = true;
} }
/**
* Draws the bubble onto the canvas of the {@link GameView}.
*/
@Override
public void addToCanvas(){
gameView.addBlockImageToCanvas(BUBBLE, getPosition().x, getPosition().y,size, rotation);
}
@Override @Override
public String toString() { public String toString() {
return "Bubble:" + getPosition().toString(); return "Bubble:" + getPosition().toString();
@ -57,7 +34,7 @@ public class Bubble extends GameObject{
*/ */
@Override @Override
public void updatePosition(){ public void updatePosition(){
if(flyFromLeftToRight && getPosition().x + width >= GameView.WIDTH){ if(flyFromLeftToRight && getPosition().x + (width*size) >= GameView.WIDTH){
flyFromLeftToRight = false; flyFromLeftToRight = false;
}else if (!flyFromLeftToRight && getPosition().x <= 0){ }else if (!flyFromLeftToRight && getPosition().x <= 0){
flyFromLeftToRight = true; flyFromLeftToRight = true;

View File

@ -1,5 +1,6 @@
package de.thdeg.greiner.superpangworld.graphics; package de.thdeg.greiner.superpangworld.graphics.base;
import de.thdeg.greiner.superpangworld.managers.GamePlayManager;
import de.thdeg.greiner.superpangworld.gameview.GameView; import de.thdeg.greiner.superpangworld.gameview.GameView;
/** /**
@ -21,6 +22,9 @@ class GameObject {
protected int width; protected int width;
/** The height of the game object. */ /** The height of the game object. */
protected int height; protected int height;
/** The responsible GamePlayManager */
protected GamePlayManager gamePlayManager;
/** /**
* Create a game object with default values. * Create a game object with default values.
@ -45,6 +49,14 @@ class GameObject {
} }
/**
* Set the responsible GamePlayManager.
* @param gamePlayManager the responsible GamePlayManager
*/
public void setGamePlayManager(GamePlayManager gamePlayManager){
this.gamePlayManager = gamePlayManager;
}
/** /**
* Get the position of the game object. * Get the position of the game object.
* @return the position * @return the position

View File

@ -1,4 +1,4 @@
package de.thdeg.greiner.superpangworld.graphics; package de.thdeg.greiner.superpangworld.graphics.base;
/** /**
* Die Position eines Objekts auf einem Fenster anhand zweidimensionaler Koordinaten. * Die Position eines Objekts auf einem Fenster anhand zweidimensionaler Koordinaten.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 641 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB