Dies ist eine alte Version des Dokuments!


Projekt Sidescroller - Stand vor der 4. Hausaufgabe

Bullet

class Bullet extends Circle {
 
   double vx;
   double vy;
 
   public Bullet(double x, double y, double vx1, double vy1, double radius) {
 
      super(x, y, radius);
      vx = vx1;
      vy = vy1;
      setFillColor(Color.white);
 
   }
 
   public void act() {
      move(vx, vy);
      if(isOutsideView()) {
         destroy();
      }
   }
}

Spaceship

class Spaceship extends Sprite {
 
   double v = 6;
   Sidescroller sidescroller;
   int timeSinceLastBullet = 0;
 
   public Spaceship(Sidescroller s) {
 
      super(80, 270, SpriteLibrary.Ship_Adrian, 2);
      scale(2);
      sidescroller = s;
 
   }
 
   public void act() {
 
      if(sidescroller.state != 1) return;
 
      if(isKeyDown(Key.ArrowUp)) {
         if(getCenterY() > 30) {
            move(0, -v);
         }
      }
 
      if(isKeyDown(Key.ArrowDown)) {
         if(getCenterY() < 570) {
            move(0, v);
         }
      }
 
      if(isKeyDown(Key.ArrowRight)) {
         if(getCenterX() < 370) {
            move(v, 0);
         }
      }
 
      if(isKeyDown(Key.ArrowLeft)) {
         if(getCenterX() > 80) {
            move(-v, 0);
         }
      }
 
      timeSinceLastBullet++; 
      if(isKeyDown(" ")) {
         if(timeSinceLastBullet > 5) {
            System.playSound(Sound.shoot);
            if(timeSinceLastBullet > 30) {
               double radius = timeSinceLastBullet / 3;
               if(radius > 20) {
                  radius = 20; 
               } 
               sidescroller.addBullet(getCenterX() + 15, getCenterY(), 15, 0, radius);
            } else {
               sidescroller.addBullet(getCenterX() + 15, getCenterY(), 15, 0, 5);
            }
            timeSinceLastBullet = 0;
         }
 
      }
 
 
   }
 
 
 
}

Sidescroller

World w = new World(800, 600);
 
new Sidescroller();
 
 
class Sidescroller extends Actor {
 
   Spaceship spaceship;
   Group bullets = new Group();
   Group enemies = new Group();
 
   int points = 0; 
   Text pointsText = new Text(5, 5, 40, "0 Punkte");
 
   int lives = 3; 
   Text livesText = new Text(795, 5, 40, lives + " Leben");
 
   Text großerText = new Text(400, 200, 60, "");
   Text kleinerText = new Text(400, 300, 40, "Press s to start!");
 
   // zustand == 0 bedeutet: Warten auf Spielbeginn
   // zustand == 1 bedeutet: im Spiel
   // zustand == 2 bedeutet: zwischen zwei Spielen
   int state = 0;
 
 
   public Sidescroller() {
      super();
      // Hier startet das Programm
      spaceship = new Spaceship(this);
      livesText.setAlignment(Alignment.right);
      großerText.setAlignment(Alignment.center);
      kleinerText.setAlignment(Alignment.center);
      setState(0);
   }
 
   public void onKeyTyped(String key) {
      if(key == "s" && state != 1) {
         setState(1); 
      }
   }
 
 
   public void addBullet(double x, double y, double vx, double vy, double radius) {
      Bullet b = new Bullet(x, y, vx, vy, radius);
      bullets.add(b);
   }
 
   public void act() {
      if(state == 1) { 
         generateEnemies();
         checkCollisionsBulletsWithEnemies();
         checkCollisionSpaceshipWithEnemies();
      }
   }
 
   private void checkCollisionSpaceshipWithEnemies() {
      Shape[] collidingEnemies = enemies.getCollidingShapes(spaceship);
 
      if(collidingEnemies.length > 0) {
 
         Enemy e = (Enemy)collidingEnemies[0]; 
 
         double le = Math.sqrt(e.getWidth() * e.getHeight());
 
         new Explosion(e.getCenterX(), e.getCenterY(), le / 50);
 
         e.destroy();
         lives--;
         updateText();
 
         if(lives == 0) {
            System.playSound(Sound.far_bomb);
            setState(2);
         } else {
            System.playSound(Sound.cannon_boom);
         }
 
      }
 
   }
 
   private void generateEnemies() {
 
      double p = 0.03 * Math.sqrt(points) / 100;
      if(p < 0.02) p = 0.02;
      if(p > 0.3) p = 0.3;
 
      if(Math.random() < p) {
         Enemy e = new Enemy(this);
         enemies.add(e);
      }
   }
 
   private void checkCollisionsBulletsWithEnemies() {
 
      CollisionPair[] pairs = bullets.getCollisionPairs(enemies, true);
 
      for(int i = 0; i < pairs.length; i++) {
 
         CollisionPair cp = pairs[i];
 
         Bullet b = (Bullet)cp.shapeA;
 
         if(b.getRadius() <= 5) {
            b.destroy(); 
         } else if(b.getRadius() > 10) {
 
            int anzahl = Math.round(b.getRadius() / 5);
            for(int i = 0; i < anzahl; i++) {
               double vx = Math.random() * 10 + 2;
               double vy = Math.random() * 10 - 5;
               addBullet(b.getCenterX(), b.getCenterY(), vx, vy, 5);
            }
            b.destroy();
         } else 
         {
            b.setRadius(b.getRadius() - 5);
         }
 
         if(cp.shapeB instanceof Enemy) { 
            Enemy r = (Enemy)cp.shapeB;
            points += Math.round(200000 / r.getFläche() * r.getGeschwindigkeit());
 
            if(r.alpha < 0.2) {
               double le = Math.sqrt(r.getWidth() * r.getHeight());
 
               new Explosion(r.getCenterX(), r.getCenterY(), le / 50);
 
               cp.shapeB.destroy();
            } else {
               r.alpha -= 0.2;
               r.setAlpha(r.alpha);
            }
         } else {
            cp.shapeB.destroy();
         }
 
         updateText();
 
      }
 
 
 
   }
 
 
   public void updateText() {
      pointsText.setText(points + " Punkte");
      livesText.setText(lives + " Leben");
   }
 
   public void addPoints(int p) {
      points = points + p;
      updateText();
 
   }
 
   private void setState(int newState) {
 
      if(newState == 0) {
         spaceship.setVisible(false);
         großerText.setText("Spielname");
         großerText.setFillColor(Color.darkgreen);
         großerText.setVisible(true);
         kleinerText.setVisible(true);
      } else if(newState == 1) {
         spaceship.setVisible(true);
         lives = 3;
         points = 0;
         spaceship.setCenter(100, 300);
         updateText();
         großerText.setVisible(false);
         kleinerText.setVisible(false);
      } else if(newState == 2) {
         spaceship.setVisible(false);
         großerText.setText("Game over");
         großerText.setFillColor(Color.darkred);
         großerText.setVisible(true);
         kleinerText.setVisible(true);
      }
 
      state = newState;
   }
 
 
 
 
}

Explosion

class Explosion extends Sprite {


   public Explosion(double x, double y, double size) {
            
      super(x, y, SpriteLibrary.Explosion_1, 0);

      playAnimation(0, 60, RepeatType.once, 20);

      scale(size);

   }
}

Enemy

class Enemy extends Rectangle {

   double vx; 
   double vy;
   double vw;
   
   double alpha = 1.0;

   public Enemy(Sidescroller s) {

      super(850, Math.random() * 550, Math.random() * 250 + 50, Math.random() * 250 + 50);
      
      double faktor = 5.0 * Math.sqrt(Math.sqrt(s.points)) / 20;
      if(faktor < 0.3) faktor = 0.3;
      if(faktor > 3) faktor = 3;

      vy = faktor * (Math.random() * 5 - 2.5);
      vx = faktor * (-Math.random() * 5 - 5);
      vw = Math.random() * 10 - 5;

      int red = Math.floor(Math.random() * 200 + 56); 
      int green = Math.floor(Math.random() * 200 + 56); 
      int blue = Math.floor(Math.random() * 200 + 56); 

      alpha = Math.random() * 0.7 + 0.3;
      
      setFillColor(Color.fromRGBA(red, green, blue, alpha));


   }

   public void act() {
      move(vx, vy);
      rotate(vw);

      if(getCenterX() < - 1000) {
         destroy();
      }

   }


   public double getFläche() {
      return getWidth() * getHeight();
   }

   public double getGeschwindigkeit() {
      return Math.sqrt(vx * vx + vy * vy);
   }


}
Drucken/exportieren
QR-Code
QR-Code schule:klassen:2019:10abc:sidescroller4 (erstellt für aktuelle Seite)