Aufgaben 4

This commit is contained in:
Andreas Greiner 2021-04-15 17:35:11 +02:00
parent aa617adc65
commit da866e5dd6
61 changed files with 439 additions and 169 deletions

10
.idea/artifacts/Programmieren_2_jar.xml generated Normal file
View File

@ -0,0 +1,10 @@
<component name="ArtifactManager">
<artifact type="jar" name="Programmieren 2:jar">
<output-path>$PROJECT_DIR$/out/artifacts/Programmieren_2_jar</output-path>
<root id="archive" name="Programmieren 2.jar">
<element id="module-output" name="Programmieren 2" />
<element id="module-output" name="SuperPangWorld" />
<element id="dir-copy" path="$PROJECT_DIR$/SuperPangWorld/src/resources" />
</root>
</artifact>
</component>

View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: de.thdeg.greiner.superpangworld.game.Start

View File

@ -0,0 +1,51 @@
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 java.awt.*;
/** Der Manager zur Verwaltung der Spielschleife. */
public class GameLoopManager {
private GameView gameView;
private Bubble bubble;
private Harpoon harpoon;
private LevelProgressBar levelProgressBar;
private LevelLabel levelLabel;
/** 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);
gameView.setColorForBlockImage('k',Color.LIGHT_GRAY);
}
/** Startet das Spiel. */
public void startGame() {
while(true){
bubble.updatePosition();
bubble.addToCanvas();
harpoon.addToCanvas();
levelProgressBar.addToCanvas();
levelLabel.addToCanvas();
gameView.printCanvas();
}
}
}

View File

@ -1,4 +1,4 @@
package view; package de.thdeg.greiner.superpangworld.game;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.sound.sampled.*; import javax.sound.sampled.*;

View File

@ -0,0 +1,13 @@
package de.thdeg.greiner.superpangworld.game;
import de.thdeg.greiner.superpangworld.game.GameLoopManager;
/**
* Die Startklasse für das Spiel.
*/
public class Start {
public static void main(String[] args) {
GameLoopManager gameLoopManager = new GameLoopManager();
gameLoopManager.startGame();
}
}

View File

@ -0,0 +1,83 @@
package de.thdeg.greiner.superpangworld.objects;
import de.thdeg.greiner.superpangworld.game.GameView;
import java.awt.*;
/**
* A Bubble, which moves around on the screen and can be shot by the player.
*/
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 */
private boolean flyFromLeftToRight;
/**
* Create a bubble with default values.
* @param gameView the {@link GameView} to display the bubble
*/
public Bubble(GameView gameView){
super(gameView);
rotation = 90;
size = 5;
width = (int) size * 12;
height = (int) size * 12;
speedInPixel = 5;
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
public String toString() {
return "Bubble:" + getPosition().toString();
}
/**
* Moves the bubble a tick further.
*/
@Override
public void updatePosition(){
if(flyFromLeftToRight && getPosition().x + width >= GameView.WIDTH){
flyFromLeftToRight = false;
}else if (!flyFromLeftToRight && getPosition().x <= 0){
flyFromLeftToRight = true;
}
if(flyFromLeftToRight){
getPosition().right(1);
}else{
getPosition().left(1);
}
}
/**
* Pop the bubble.
*/
private void popBubble(){}
/**
* Freezes the time.
*/
private void freezeTime(){}
}

View File

@ -0,0 +1,55 @@
package de.thdeg.greiner.superpangworld.objects;
import de.thdeg.greiner.superpangworld.game.GameView;
/**
* A basic game object.
*/
class GameObject {
/** The game view to display the game object on. */
protected final GameView gameView;
/** The position of the game object. */
protected final Position position;
/** The size of the game object. */
protected double size;
/** The speed in pixel per tick of the game object. */
protected double speedInPixel;
/** The rotation of the game object. */
protected double rotation;
/** The width of the game object. */
protected int width;
/** The height of the game object. */
protected int height;
/**
* Create a game object with default values.
* @param gameView the gameView
*/
protected GameObject(GameView gameView){
this.gameView = gameView;
position = new Position(0,0);
}
/**
* Update the position of the game object.
*/
public void updatePosition(){
}
/**
* Add the game object to the canvas.
*/
public void addToCanvas(){
}
/**
* Get the position of the game object.
* @return the position
*/
public Position getPosition(){
return position;
}
}

View File

@ -0,0 +1,53 @@
package de.thdeg.greiner.superpangworld.objects;
import de.thdeg.greiner.superpangworld.game.GameView;
import java.awt.*;
/**
* A harpoon which can be fired upwards by the player.
*/
public class Harpoon extends GameObject{
private final static String HARPOON =
" B \n"+
" BBB \n"+
" kkkkk \n"+
" k kkk k\n"+
" k kkk k\n";
/**
* Create a harpoon with default values
* @param gameView the {@link GameView} to display the bubble
*/
public Harpoon(GameView gameView){
super(gameView);
speedInPixel = 10;
size = 3;
width = (int) size * 11;
height =(int) size * 11;
rotation = 0;
getPosition().setTo(300,300);
}
/**
* Zeichnet die Blase auf die {@link GameView}.
*/
@Override
public void addToCanvas(){
gameView.addBlockImageToCanvas(HARPOON, getPosition().x, getPosition().y,size, rotation);
}
@Override
public String toString() {
return "Harpoon:" +getPosition().toString();
}
/**
* Bewegt die Harpune einen Schritt weiter.
*/
@Override
public void updatePosition(){
getPosition().up(speedInPixel);
}
}

View File

@ -0,0 +1,45 @@
package de.thdeg.greiner.superpangworld.objects;
import de.thdeg.greiner.superpangworld.game.GameView;
import java.awt.*;
/**
* The label to display the current level.
*/
public class LevelLabel extends GameObject{
/** The level to display */
private int level;
/**
* Create a progress bar with default values.
* @param gameView the {@link GameView} to display the progress bar
*/
public LevelLabel(GameView gameView){
super(gameView);
size = 3;
width = (int) size * 101;
height = (int) size * 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}.
*/
@Override
public void addToCanvas(){
gameView.addTextToCanvas("Level "+level,getPosition().x,getPosition().y,40,Color.YELLOW,0);
}
@Override
public void updatePosition() {
super.updatePosition();
}
@Override
public String toString() {
return "LevelLabel:" +getPosition().toString();
}
}

View File

@ -1,24 +1,14 @@
package superpangworld; package de.thdeg.greiner.superpangworld.objects;
import de.thdeg.greiner.superpangworld.game.GameView;
import java.awt.*; import java.awt.*;
/** /**
* The progress bar of a level. * The progress bar of a level.
*/ */
public class LevelProgressBar { public class LevelProgressBar extends GameObject{
/** The position of the progress bar */
private Position position;
/** The color of the progress bar */
private final Color color;
/** The size of the progress bar */
private final double size;
/** The width of the progress bar */
private final double width;
/** The height of the progress bar */
private final double height;
/** The GameView to display the progress bar*/
private final GameView gameView;
/** The progress of the level ranging from <code>0</code> to <code>100</code> */ /** The progress of the level ranging from <code>0</code> to <code>100</code> */
private int levelProgress; private int levelProgress;
/** The top and bottom border of the progress bar */ /** The top and bottom border of the progress bar */
@ -29,30 +19,35 @@ public class LevelProgressBar {
* @param gameView the {@link GameView} to display the progress bar * @param gameView the {@link GameView} to display the progress bar
*/ */
public LevelProgressBar(GameView gameView){ public LevelProgressBar(GameView gameView){
color = Color.BLUE; super(gameView);
size = 3; size = 3;
width = size * 101; width = (int) size * 101;
height = size * 8; height = (int) size * 8;
levelProgress = 30; levelProgress = 30;
this.gameView = gameView; getPosition().setTo(GameView.WIDTH/2 - (width/2),GameView.HEIGHT-height);
position = new Position(GameView.WIDTH/2 - (width/2),GameView.HEIGHT-height);
} }
/** /**
* Draws the level progress bar onto the canvas of the {@link GameView}. * Draws the level progress bar onto the canvas of the {@link GameView}.
*/ */
@Override
public void addToCanvas(){ public void addToCanvas(){
String pixelArt = PROGRESS_BAR_BORDER; String pixelArt = PROGRESS_BAR_BORDER;
String progressBarMiddle = getProgressBarMiddle(levelProgress); String progressBarMiddle = getProgressBarMiddle(levelProgress);
pixelArt += progressBarMiddle.repeat(6); pixelArt += progressBarMiddle.repeat(6);
pixelArt += PROGRESS_BAR_BORDER; pixelArt += PROGRESS_BAR_BORDER;
gameView.addBlockImageToCanvas(pixelArt, position.x, position.y,size, 0); gameView.addBlockImageToCanvas(pixelArt, getPosition().x, getPosition().y,size, 0);
}
@Override
public void updatePosition() {
super.updatePosition();
} }
@Override @Override
public String toString() { public String toString() {
return "Harpoon:" +position.toString(); return "LevelProgressBar:" +getPosition().toString();
} }
/** /**

View File

@ -0,0 +1,106 @@
package de.thdeg.greiner.superpangworld.objects;
/**
* Die Position eines Objekts auf einem Fenster anhand zweidimensionaler Koordinaten.
*/
public class Position {
/** Die x-Koordinate */
public double x;
/** Die y-Koordinate */
public double y;
/**
* Erzeugt eine Position mit den übergebenen Koordinaten.
*
* @param x die x-Koordinate des Objekts
* @param y die y-Koordinate des Objekts
*/
public Position(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Erzeugt eine Standardposition für die Koordinaten (0, 0).
*/
public Position(){
this(0,0);
}
/**
* Veränderung der Position um einen Pixel nach links.
*/
public void left() {
x--;
}
/**
* Veränderung der Position nach links.
* @param pixel die Anzahl der Pixel
*/
public void left(double pixel){
x -= pixel;
}
/**
* Veränderung der Position um einen Pixel nach rechts.
*/
public void right() {
x++;
}
/**
* Veränderung der Position nach rechts.
* @param pixel die Anzahl der Pixel
*/
public void right(double pixel){
x += pixel;
}
/**
* Veränderung der Position um einen Pixel nach oben.
*/
public void up() {
y--;
}
/**
* Veränderung der Position nach oben.
* @param pixel die Anzahl der Pixel
*/
public void up(double pixel){
y -= pixel;
}
/**
* Veränderung der Position um einen Pixel nach unten.
*/
public void down() {
y++;
}
/**
* Veränderung der Position nach unten.
* @param pixel die Anzahl der Pixel
*/
public void down(double pixel){
y += pixel;
}
/**
* Set the position to given x and y coordinates.
* @param x the x coordinate
* @param y the y coordinate
*/
public void setTo(double x, double y){
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Position(" + (int) Math.round(x) + ", " + (int) Math.round(y) + ')';
}
}

View File

@ -1,2 +0,0 @@
package superpangworld;public class Bubble {
}

View File

@ -1,2 +0,0 @@
package superpangworld;public class GameLoopManager {
}

View File

@ -1,2 +0,0 @@
package superpangworld;public class Harpoon {
}

View File

@ -1,68 +0,0 @@
package superpangworld;
import java.awt.*;
/**
* The progress of a level.
*/
public class LevelProgress {
private final static String HARPOON =
" B \n"+
" BBB \n"+
" kkkkk \n"+
" k kkk k\n"+
" k kkk k\n";
/** The position of the harpoon */
private Position position;
/** The speed of the harpune in pixel per tick */
private final double speedInPixel;
/** The color of the harpoon */
private final Color color;
/** The size of the harpoon */
private final double size;
/** The rotation of the harpoon */
private final double rotation;
/** The width of the harpoon */
private final double width;
/** The height of the harpoon */
private final double height;
/** The GameView to display the harpoon*/
private final GameView gameView;
/**
* Create a harpoon with default values
* @param gameView the {@link GameView} to display the bubble
*/
public LevelProgress(GameView gameView){
color = Color.BLUE;
position = new Position(300,300);
speedInPixel = 10;
size = 3;
width = size * 11;
height = size * 11;
rotation = 0;
this.gameView = gameView;
}
/**
* Zeichnet die Blase auf die {@link GameView}.
*/
public void addToCanvas(){
gameView.addBlockImageToCanvas(HARPOON, position.x, position.y,size, rotation);
}
@Override
public String toString() {
return "Harpoon:" +position.toString();
}
/**
* Bewegt die Harpune einen Schritt weiter.
*/
public void updatePosition(){
position.up(speedInPixel);
}
}

View File

@ -1,57 +0,0 @@
package superpangworld;
/**
* Die Position eines Objekts auf einem Fenster anhand zweidimensoinaler Koordinaten
*/
public class Position {
/** Die x-Koordinate */
public double x;
/** Die y-Koordinate */
public double y;
/**
* Erzeugt eine Position mit den übergebenen Koordinaten
*
* @param x die x-Koordinate des Objekts
* @param y die y-Koordinate des Objekts
*/
public Position(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Veränderung der Position um einen Pixel nach links
*/
public void left() {
x--;
}
/**
* Veränderung der Position um einen Pixel nach rechts
*/
public void right() {
x++;
}
/**
* Veränderung der Position um einen Pixel nach oben
*/
public void up() {
y--;
}
/**
* Veränderung der Position um einen Pixel nach unten
*/
public void down() {
y++;
}
@Override
public String toString() {
return "Position(" + (int) Math.round(x) + ", " + (int) Math.round(y) + ')';
}
}

View File

@ -1,16 +0,0 @@
package superpangworld;
public class Start {
public static void main(String[] args) {
System.out.println("Spiel");
Position pos1 = new Position(100, 110);
Position pos2 = new Position(200, 300);
pos2.down();
pos2.right();
System.out.println(pos1);
System.out.println(pos2);
}
}

Binary file not shown.

View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: de.thdeg.greiner.superpangworld.game.Start

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 641 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB