Praktikum 4
This commit is contained in:
parent
da866e5dd6
commit
b4d721cdb1
@ -1,21 +1,26 @@
|
||||
package de.thdeg.greiner.superpangworld.game;
|
||||
|
||||
import de.thdeg.greiner.superpangworld.objects.Bubble;
|
||||
import de.thdeg.greiner.superpangworld.objects.Harpoon;
|
||||
import de.thdeg.greiner.superpangworld.objects.LevelLabel;
|
||||
import de.thdeg.greiner.superpangworld.objects.LevelProgressBar;
|
||||
import de.thdeg.greiner.superpangworld.objects.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.security.Key;
|
||||
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 Spielfigur spielfigur;
|
||||
|
||||
/** Erzeugt den GameLoopManager mit Standardwerten. */
|
||||
public GameLoopManager() {
|
||||
@ -29,6 +34,9 @@ public class GameLoopManager {
|
||||
|
||||
this.levelProgressBar = new LevelProgressBar(gameView);
|
||||
this.levelLabel = new LevelLabel(gameView);
|
||||
this.scoreLabel = new ScoreLabel(gameView);
|
||||
|
||||
this.spielfigur = new Spielfigur(gameView);
|
||||
|
||||
gameView.setColorForBlockImage('k',Color.LIGHT_GRAY);
|
||||
}
|
||||
@ -36,16 +44,44 @@ public class GameLoopManager {
|
||||
/** Startet das Spiel. */
|
||||
public void startGame() {
|
||||
while(true){
|
||||
updateUserInputs();
|
||||
|
||||
|
||||
bubble.updatePosition();
|
||||
bubble.addToCanvas();
|
||||
harpoon.addToCanvas();
|
||||
|
||||
levelProgressBar.addToCanvas();
|
||||
levelLabel.addToCanvas();
|
||||
scoreLabel.addToCanvas();
|
||||
|
||||
spielfigur.addToCanvas();
|
||||
|
||||
gameView.printCanvas();
|
||||
}
|
||||
}
|
||||
|
||||
void updateUserInputs() {
|
||||
Integer[] gedruekteTasten = gameView.getKeyCodesOfCurrentlyPressedKeys();
|
||||
if(Arrays.stream(gedruekteTasten).anyMatch(k -> k == KeyEvent.VK_SPACE)){
|
||||
spielfigur.shoot();
|
||||
}
|
||||
for (int keyCode : gedruekteTasten) {
|
||||
switch(keyCode) {
|
||||
case KeyEvent.VK_LEFT:
|
||||
spielfigur.left();
|
||||
break;
|
||||
case KeyEvent.VK_RIGHT:
|
||||
spielfigur.right();
|
||||
break;
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
if(!DIAGONAL_MOVEMENT_ALLOWED){
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -35,8 +35,8 @@ public class Bubble extends GameObject{
|
||||
super(gameView);
|
||||
rotation = 90;
|
||||
size = 5;
|
||||
width = (int) size * 12;
|
||||
height = (int) size * 12;
|
||||
width = 12;
|
||||
height = 12;
|
||||
speedInPixel = 5;
|
||||
flyFromLeftToRight = true;
|
||||
}
|
||||
|
@ -24,8 +24,8 @@ public class Harpoon extends GameObject{
|
||||
super(gameView);
|
||||
speedInPixel = 10;
|
||||
size = 3;
|
||||
width = (int) size * 11;
|
||||
height =(int) size * 11;
|
||||
width = 11;
|
||||
height = 11;
|
||||
rotation = 0;
|
||||
getPosition().setTo(300,300);
|
||||
}
|
||||
|
@ -13,20 +13,20 @@ public class LevelLabel extends GameObject{
|
||||
private int level;
|
||||
|
||||
/**
|
||||
* Create a progress bar with default values.
|
||||
* @param gameView the {@link GameView} to display the progress bar
|
||||
* Create a label for the level with default values.
|
||||
* @param gameView the {@link GameView} to display the level label
|
||||
*/
|
||||
public LevelLabel(GameView gameView){
|
||||
super(gameView);
|
||||
size = 3;
|
||||
width = (int) size * 101;
|
||||
height = (int) size * 8;
|
||||
width = 101;
|
||||
height = 8;
|
||||
level = 8;
|
||||
getPosition().setTo(GameView.WIDTH/2 - (width/2),GameView.HEIGHT-height-60);
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws the level progress bar onto the canvas of the {@link GameView}.
|
||||
* Draws the level label onto the canvas of the {@link GameView}.
|
||||
*/
|
||||
@Override
|
||||
public void addToCanvas(){
|
||||
|
@ -21,10 +21,10 @@ public class LevelProgressBar extends GameObject{
|
||||
public LevelProgressBar(GameView gameView){
|
||||
super(gameView);
|
||||
size = 3;
|
||||
width = (int) size * 101;
|
||||
height = (int) size * 8;
|
||||
width = 101;
|
||||
height = 8;
|
||||
levelProgress = 30;
|
||||
getPosition().setTo(GameView.WIDTH/2 - (width/2),GameView.HEIGHT-height);
|
||||
getPosition().setTo(GameView.WIDTH/2 - (width/2),GameView.HEIGHT-(height*size));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,38 @@
|
||||
package de.thdeg.greiner.superpangworld.objects;
|
||||
|
||||
import de.thdeg.greiner.superpangworld.game.GameView;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* The label to display the score.
|
||||
*/
|
||||
public class ScoreLabel extends GameObject{
|
||||
|
||||
/** The score to display */
|
||||
private int score;
|
||||
|
||||
/**
|
||||
* Create a label for the score with default values.
|
||||
* @param gameView the {@link GameView} to display the score label
|
||||
*/
|
||||
public ScoreLabel(GameView gameView) {
|
||||
super(gameView);
|
||||
score = 1300;
|
||||
size = 50;
|
||||
width = 100;
|
||||
height = 8;
|
||||
|
||||
position.setTo(GameView.WIDTH/2 - (width/2) -250,GameView.HEIGHT-height-60);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addToCanvas() {
|
||||
gameView.addTextToCanvas(Integer.toString(score),getPosition().x,getPosition().y,40, Color.YELLOW,0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updatePosition() {
|
||||
super.updatePosition();
|
||||
}
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
package de.thdeg.greiner.superpangworld.objects;
|
||||
|
||||
import de.thdeg.greiner.superpangworld.game.GameView;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* The game character controlled by the player.
|
||||
*/
|
||||
public class Spielfigur extends GameObject{
|
||||
|
||||
/** Flag, if the player is shooting */
|
||||
private boolean shooting;
|
||||
/** Debug flag, to display X and O or the real image */
|
||||
private static boolean SHOW_X = false;
|
||||
/** The pixel art of the character */
|
||||
private static String SPIELFIGUR =
|
||||
" kkk \n"+
|
||||
" BBBBk \n"+
|
||||
" BBBBBB \n"+
|
||||
" BBBBBBBB \n"+
|
||||
" BBBBBBB \n"+
|
||||
" BBBBBBB \n"+
|
||||
"kBBBBBBB \n"+
|
||||
"kkBBBBBB \n"+
|
||||
"kkBBBBBBkkk\n"+
|
||||
"kkBBBBBkkkk\n"+
|
||||
" BBBBBBkk \n"+
|
||||
" BBBBBBBk \n"+
|
||||
" kBBBBBBk \n"+
|
||||
" BBBBBkb \n"+
|
||||
" bbbk kbb \n"+
|
||||
" BBBb kBBb\n"+
|
||||
" BBBk kkkB";
|
||||
|
||||
/**
|
||||
* Create the game character with default values
|
||||
* @param gameView the {@link GameView} to display the character
|
||||
*/
|
||||
public Spielfigur(GameView gameView){
|
||||
super(gameView);
|
||||
position.setTo(GameView.WIDTH/2, GameView.HEIGHT/2);
|
||||
shooting = false;
|
||||
size = 3;
|
||||
width = (int) size * 12;
|
||||
height = (int) size * 12;
|
||||
speedInPixel = 5;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updatePosition(){
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addToCanvas(){
|
||||
if(SHOW_X) {
|
||||
gameView.addTextToCanvas(shooting ? "O" : "X", position.x, position.y, 50, Color.RED, rotation);
|
||||
}else{
|
||||
gameView.addBlockImageToCanvas(SPIELFIGUR, position.x, position.y, size, rotation);
|
||||
}
|
||||
shooting = false;
|
||||
}
|
||||
|
||||
/** Moves the character to the left */
|
||||
public void left(){
|
||||
position.left(speedInPixel);
|
||||
}
|
||||
/** Moves the character to the right */
|
||||
public void right(){
|
||||
position.right(speedInPixel);
|
||||
}
|
||||
/** Moves the character upwards */
|
||||
public void up(){
|
||||
position.up(speedInPixel);
|
||||
}
|
||||
/** Moves the character downwards */
|
||||
public void down(){
|
||||
position.down(speedInPixel);
|
||||
}
|
||||
/** Lets the character shoot */
|
||||
public void shoot(){
|
||||
shooting = true;
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user