Praktikum 7

This commit is contained in:
Andreas Greiner 2021-05-30 17:31:56 +02:00
parent f0d73b7cf0
commit 937d74a0db
13 changed files with 58 additions and 19 deletions

View File

@ -94,4 +94,21 @@ class GameObjectManager {
public LinkedList<Harpoon> getHarpoons() {
return harpoons;
}
/**
* Adapts the position of all game objects.
*
* @param adaptX Adaption to the right.
* @param adaptY Adaption downwards.
*/
public void moveWorld(double adaptX, double adaptY) {
for (GameObject gameObject : gameObjects) {
if (gameObject.getClass() != LevelLabel.class
&& gameObject.getClass() != LevelProgressBar.class
&& gameObject.getClass() != LivesIcon.class
&& gameObject.getClass() != ScoreLabel.class){
gameObject.adaptPosition(adaptX, adaptY);
}
}
}
}

View File

@ -104,4 +104,22 @@ public class GamePlayManager {
public void destroy(Harpoon harpoon){
gameObjectManager.getHarpoons().remove(harpoon);
}
/**
* Moves the World to the left.
*
* @param speedInPixel Speed to move the World
*/
public void playerMovingRight(double speedInPixel) {
gameObjectManager.moveWorld(-speedInPixel, 0);
}
/**
* Moves the World to the right.
*
* @param speedInPixel Speed to move the World
*/
public void playerMovingLeft(double speedInPixel) {
gameObjectManager.moveWorld(speedInPixel, 0);
}
}

View File

@ -71,4 +71,15 @@ public abstract class GameObject {
return position;
}
/**
* Adapts the position of this game object, in case the whole game world is moved.
*
* @param adaptX Adaption to the right.
* @param adaptY Adaption downwards.
*/
public void adaptPosition(double adaptX, double adaptY){
position.x += adaptX;
position.y += adaptY;
}
}

View File

@ -10,9 +10,6 @@ import java.util.Random;
*/
public class Background extends GameObject {
private final String pixelArt;
/**
* Create a background object.
*
@ -24,24 +21,12 @@ public class Background extends GameObject {
size = 1;
rotation = 0;
Random r = new Random();
StringBuilder sb = new StringBuilder();
for(int h=0;h<450;h++){
for(int i=0;i<960;i++){
if(r.nextBoolean()){
sb.append("y");
}else{
sb.append("o");
}
}
sb.append("\n");
}
pixelArt = sb.toString();
}
@Override
public void addToCanvas() {
gameView.addBlockImageToCanvas(pixelArt,position.x,position.y,size, rotation);
gameView.addImageToCanvas("background.png",position.x,position.y,size,rotation);
}
@Override

View File

@ -66,11 +66,19 @@ public class Player extends GameObject {
/** Moves the character to the left */
public void left(){
if (position.x > 300) {
position.left(speedInPixel);
} else {
gamePlayManager.playerMovingLeft(speedInPixel);
}
}
/** Moves the character to the right */
public void right(){
if (position.x < GameView.WIDTH - width - 300) {
position.right(speedInPixel);
} else {
gamePlayManager.playerMovingRight(speedInPixel);
}
}
/** Moves the character upwards */
public void up(){

Binary file not shown.

After

Width:  |  Height:  |  Size: 305 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 305 KiB