Benutzer-Werkzeuge

Webseiten-Werkzeuge


spaceshooter:start

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen RevisionVorhergehende Überarbeitung
spaceshooter:start [2023/03/03 07:40] – [Gedanken bei der Erstellung des Klassendiagramms] Martin Pabstspaceshooter:start [2023/06/11 15:22] (aktuell) – [Gedanken bei der Erstellung des Klassendiagramms] Martin Pabst
Zeile 31: Zeile 31:
 Beachte die Zusätze "{ abstract }" im Klassendiagramm erstmal nicht. Du wirst weiter unten erfahren, was es damit auf sich hat. Beachte die Zusätze "{ abstract }" im Klassendiagramm erstmal nicht. Du wirst weiter unten erfahren, was es damit auf sich hat.
 </WRAP> </WRAP>
 +
 +===== Einfachere Variante... =====
 +<HTML>
 +<div class="java-online" style="height: 80vh; width: 100%" data-java-online="{'withBottomPanel': true, 'id': 'Spaceshooter_einfach'}">
 +
 +<script type="text/plain" title="Main.java">
 +new Main();
 +
 +class Main extends Actor {
 +   
 +   Spaceship spaceship;
 +   String state;
 +
 +   Text caption;
 +   
 +   int score = 0;
 +   Text scoreDisplay;
 +
 +   int lastTimeSpacePressed;
 +
 +   public Main() {
 +
 +      caption = new Text(400, 200, 60, "Space shooter");
 +      caption.setAlignment(Alignment.center);
 +
 +      scoreDisplay = new Text(10, 10, 32, "0");
 +
 +      spaceship = new Spaceship(400, 550, this);
 +      
 +      setState("startscreen");
 +   }
 +
 +   public void act() {
 +      if(state == "gamerunning") {
 +
 +         if(Math.random() < 0.05) {
 +            new Target(this);
 +         }
 +
 +         if(Math.random() < 0.01) {
 +            new Powerup(this);
 +         }
 +
 +      } else if(isKeyDown(" ") && System.currentTimeMillis() - lastTimeSpacePressed > 700) {
 +         lastTimeSpacePressed = System.currentTimeMillis();
 +         switch(state) {
 +            case "gameoverscreen"
 +               setState("startscreen");
 +               break;
 +            case "startscreen"
 +               setState("gamerunning");
 +               break;
 +            default : 
 +                
 +         }
 +      }
 +      
 +   }
 +
 +   public void setState(String newState) {
 +      
 +      switch(newState) {
 +         case "startscreen"
 +            caption.setVisible(true);
 +            spaceship.setVisible(false);
 +            caption.setText("Space shooter");
 +            break;
 +         case "gamerunning"
 +            score = 0;
 +            displayScore();
 +            caption.setVisible(false);
 +            spaceship.setVisible(true);
 +            break;
 +         case "gameoverscreen"
 +            caption.setVisible(true);
 +            spaceship.setVisible(false);
 +            caption.setText("Game over");
 +            break;
 +         default : 
 +             
 +      }
 +
 +
 +      state = newState;
 +   }
 +
 +   public void increaseScore(int delta) {
 +      score += delta; 
 +      displayScore();
 +   }
 +   
 +   public void displayScore() {
 +      scoreDisplay.setText(score);
 +   }
 +
 +}
 +</script>
 +<script type="text/plain" title="Spaceship.java">
 +class Spaceship extends Sprite {
 +
 +   int spaceLastPressedTime = 0;
 +   Main main;
 +
 +   Gun gun;
 +
 +   public Spaceship(double x, double y, Main main) {
 +      super(x, y, SpriteLibrary.Space_Shooter_2, 4);
 +      this.main = main;
 +      this.gun = new SimpleGun();
 +      scale(2);
 +   }
 +
 +   public void act() {
 +      if(main.state != "gamerunning") return;
 +      
 +      if(isKeyDown(Key.ArrowLeft)) {
 +         move(-5, 0);
 +      }
 +
 +      if(isKeyDown(Key.ArrowRight)) {
 +         move(5, 0);
 +      }
 +
 +      int time = System.currentTimeMillis();
 +
 +      if(isKeyDown(" ") && time - spaceLastPressedTime > gun.getTimeToReload()) {
 +         spaceLastPressedTime = time;
 +         gun.shoot(getCenterX(), getCenterY(), main);
 +      }
 +
 +      if(getFirstCollidingSprite(9) != null) {
 +         main.setState("gameoverscreen");
 +      }
 +
 +   }
 +
 +}</script>
 +<script type="text/plain" title="Bullet.java">
 +class Bullet extends Circle {
 +
 +   double vx;
 +   double vy;
 +
 +   public Bullet(double x, double y, double vx, double vy) {
 +      super(x, y, 10);
 +      this.vx = vx;
 +      this.vy = vy;
 +      this.sendToBack();
 +      this.setFillColor(0xff0000);
 +   }
 +
 +   public void act() {
 +      move(vx, vy);
 +      if(isOutsideView()) {
 +         destroy();
 +      }
 +   }
 +
 +
 +   
 +}</script>
 +<script type="text/plain" title="Gun.java">
 +interface Gun {
 +
 +   public void shoot(double x, double y, Main main);
 +
 +   public double getTimeToReload();
 +
 +}
 +
 +class SimpleGun implements Gun {
 +   
 +  public void shoot(double x, double y, Main main) {
 +    new Bullet(x, y, 0, -10);
 +  }
 +
 +  public double getTimeToReload() {
 +    return 700;       // 700 ms
 +  }
 +
 +}
 +
 +class TripleGun implements Gun {
 +  public void shoot(double x, double y, Main main) {
 +    new Bullet(x, y, -7, -7);
 +    new Bullet(x, y, 0, -10);
 +    new Bullet(x, y, 7, -7);
 +  }
 +
 +  public double getTimeToReload() {
 +    return 500;       // 700 ms
 +  }
 +
 +}</script>
 +<script type="text/plain" title="Target.java">
 +public class Target extends Sprite {
 +   
 +   double vx;
 +   double vy;
 +   Main main;
 +
 +   public Target(Main main) {
 +      super(Random.randint(0, 800), -50, SpriteLibrary.Breakin, 9);
 +      this.vx = Random.randint(-5, 5);
 +      this.vy = Random.randint(3, 10);
 +      this.main = main;
 +   }
 +
 +   public void act() {
 +
 +      move(vx, vy);
 +
 +      if(getCenterY() > 620) {
 +         destroy();
 +         return;
 +      } 
 +
 +      if(collidesWithFillColor(0xff0000)) {
 +         Sprite explosion = new Sprite(getCenterX(), getCenterY(), SpriteLibrary.Explosion_1);
 +         explosion.playAnimation(0, 55, RepeatType.once, 30);
 +         destroy();
 +         main.increaseScore(10);
 +      }
 +
 +   }
 +
 +}
 +</script>
 +<script type="text/plain" title="Powerup.java">
 +public class Powerup extends Sprite {
 +   
 +   double vx;
 +   double vy;
 +   Main main;
 +
 +   public Powerup(Main main) {
 +      super(Random.randint(0, 800), -50, SpriteLibrary.Plattforms, 301);
 +      this.vx = Random.randint(-5, 5);
 +      this.vy = Random.randint(3, 10);
 +      this.main = main;
 +   }
 +
 +   public void act() {
 +
 +      move(vx, vy);
 +      rotate(3);
 +
 +      if(getCenterY() > 620) {
 +         destroy();
 +         return;
 +      } 
 +
 +      if(getFirstCollidingSprite(4) != null) {
 +         destroy();
 +         main.spaceship.gun = new TripleGun();
 +      }
 +
 +   }
 +
 +}
 +</script>
 +
 +
 +</div>
 +
 +</HTML>
  
spaceshooter/start.1677829220.txt.gz · Zuletzt geändert: 2023/03/03 07:40 von Martin Pabst

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki