Hier die Programmdateien Stand 31.03.2020:
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() { if(Math.random() < 0.05) { Enemy e = new Enemy(); enemies.add(e); } } private void checkCollisionsBulletsWithEnemies() { CollisionPair[] pairs = bullets.getCollisionPairs(enemies, true); for(int i = 0; i < pairs.length; i++) { CollisionPair cp = pairs[i]; cp.shapeA.destroy(); Rectangle r =(Rectangle)cp.shapeB; double le = Math.sqrt(r.getWidth() * r.getHeight()); new Explosion(r.getCenterX(), r.getCenterY(), le / 50); points += 10; updateText(); cp.shapeB.destroy(); } } 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; } }
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) { sidescroller.addBullet(getCenterX() + 15, getCenterY(), 15, 0, 10); } else { sidescroller.addBullet(getCenterX() + 15, getCenterY(), 15, 0, 5); } timeSinceLastBullet = 0; } } } }
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(); } } }
class Enemy extends Rectangle { double vx; double vy; double vw; public Enemy() { super(850, Math.random() * 600, Math.random() * 250 + 50, Math.random() * 250 + 50); vy = Math.random()*5 - 2.5; vx = -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); setFillColor(Color.fromRGBA(red, green, blue, Math.random() * 0.7 + 0.3)); } public void act() { move(vx, vy); rotate(vw); if(getCenterX() < - 1000) { destroy(); } } }
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); } }