Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
schule:klassen:2019:10abc:sidescrollernach4 [2020/05/03 19:31] – angelegt martin | schule:klassen:2019:10abc:sidescrollernach4 [2021/12/29 10:40] (aktuell) – Externe Bearbeitung 127.0.0.1 | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
====== Projekt Sidescroller - Stand nach der 4. Hausaufgabe ====== | ====== Projekt Sidescroller - Stand nach der 4. Hausaufgabe ====== | ||
+ | === Bullet === | ||
+ | <code java> | ||
+ | class Bullet extends Circle { | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | |||
+ | super(x, y, radius); | ||
+ | vx = vx1; | ||
+ | vy = vy1; | ||
+ | setFillColor(Color.white); | ||
+ | |||
+ | } | ||
+ | |||
+ | | ||
+ | move(vx, vy); | ||
+ | if(isOutsideView()) { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Spaceship === | ||
+ | <code java> | ||
+ | class Spaceship extends Sprite { | ||
+ | |||
+ | | ||
+ | | ||
+ | int timeSinceLastBullet = 0; | ||
+ | |||
+ | | ||
+ | |||
+ | super(80, 270, SpriteLibrary.Ship_Adrian, | ||
+ | scale(2); | ||
+ | sidescroller = s; | ||
+ | |||
+ | } | ||
+ | |||
+ | | ||
+ | |||
+ | if(sidescroller.state != 1) return; | ||
+ | |||
+ | if(isKeyDown(Key.ArrowUp)) { | ||
+ | | ||
+ | move(0, -v); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if(isKeyDown(Key.ArrowDown)) { | ||
+ | | ||
+ | move(0, v); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if(isKeyDown(Key.ArrowRight)) { | ||
+ | | ||
+ | move(v, 0); | ||
+ | | ||
+ | } | ||
+ | |||
+ | if(isKeyDown(Key.ArrowLeft)) { | ||
+ | | ||
+ | move(-v, 0); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | timeSinceLastBullet++; | ||
+ | if(isKeyDown(" | ||
+ | | ||
+ | System.playSound(Sound.laser_shoot); | ||
+ | |||
+ | double r = timeSinceLastBullet / 30 * 10; | ||
+ | if(r > 40) r = 40; | ||
+ | if(r < 5) r = 5; | ||
+ | | ||
+ | sidescroller.addBullet(getCenterX() + 15, getCenterY(), | ||
+ | timeSinceLastBullet = 0; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Sidescroller === | ||
+ | <code java> | ||
+ | World w = new World(800, 600); | ||
+ | |||
+ | new Sidescroller(); | ||
+ | |||
+ | |||
+ | class Sidescroller extends Actor { | ||
+ | |||
+ | | ||
+ | Group bullets = new Group(); | ||
+ | Group flyingObjects = 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; | ||
+ | |||
+ | |||
+ | | ||
+ | super(); | ||
+ | // Hier startet das Programm | ||
+ | spaceship = new Spaceship(this); | ||
+ | livesText.setAlignment(Alignment.right); | ||
+ | großerText.setAlignment(Alignment.center); | ||
+ | kleinerText.setAlignment(Alignment.center); | ||
+ | setState(0); | ||
+ | } | ||
+ | |||
+ | | ||
+ | if(key == " | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | | ||
+ | Bullet b = new Bullet(x, y, vx, vy, radius); | ||
+ | bullets.add(b); | ||
+ | } | ||
+ | |||
+ | | ||
+ | if(state == 1) { | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | | ||
+ | Shape[] collidingEnemies = flyingObjects.getCollidingShapes(spaceship); | ||
+ | |||
+ | if(collidingEnemies.length > 0) { | ||
+ | |||
+ | | ||
+ | Enemy e =(Enemy)collidingEnemies[0]; | ||
+ | |||
+ | double le = Math.sqrt(e.getWidth() * e.getHeight()); | ||
+ | |||
+ | new Explosion(e.getCenterX(), | ||
+ | |||
+ | e.destroy(); | ||
+ | lives--; | ||
+ | if(lives == 0) { | ||
+ | | ||
+ | | ||
+ | } else { | ||
+ | | ||
+ | } | ||
+ | } else { | ||
+ | Powerup pu = (Powerup)collidingEnemies[0]; | ||
+ | lives++; | ||
+ | pu.destroy(); | ||
+ | } | ||
+ | |||
+ | | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | double p = 0.03 * Math.sqrt(points) / 100; | ||
+ | if(p < 0.04) p = 0.04; | ||
+ | if(p > 0.3) p = 0.3; | ||
+ | | ||
+ | if(Math.random() < p) { | ||
+ | Enemy e = new Enemy(this); | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | if(Math.random() < 0.005) { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | | ||
+ | |||
+ | CollisionPair[] pairs = bullets.getCollisionPairs(flyingObjects, | ||
+ | |||
+ | for(int i = 0; i < pairs.length; | ||
+ | |||
+ | | ||
+ | |||
+ | | ||
+ | Enemy enemy = (Enemy)cp.shapeB; | ||
+ | |||
+ | double le = Math.sqrt(enemy.getWidth() * enemy.getHeight()); | ||
+ | |||
+ | |||
+ | points += Math.round(200000 / enemy.getFläche() * enemy.getGeschwindigkeit()); | ||
+ | |||
+ | updateText(); | ||
+ | |||
+ | if(enemy.alpha > 0.2) { | ||
+ | | ||
+ | | ||
+ | } else { | ||
+ | new Explosion(enemy.getCenterX(), | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | Bullet b = (Bullet)cp.shapeA; | ||
+ | |||
+ | double r = b.getRadius(); | ||
+ | |||
+ | if(r <= 5) { | ||
+ | | ||
+ | } else { | ||
+ | int anzahl = Math.round(r / 5.0); | ||
+ | | ||
+ | | ||
+ | addBullet(b.getCenterX(), | ||
+ | } | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | // double r = b.getRadius() - 5; | ||
+ | // if(r <= 0) { | ||
+ | // | ||
+ | // } else { | ||
+ | // | ||
+ | // } | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | | ||
+ | pointsText.setText(points + " Punkte" | ||
+ | livesText.setText(lives + " Leben" | ||
+ | } | ||
+ | |||
+ | | ||
+ | points = points + p; | ||
+ | updateText(); | ||
+ | |||
+ | } | ||
+ | |||
+ | | ||
+ | |||
+ | if(newState == 0) { | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } else if(newState == 1) { | ||
+ | | ||
+ | lives = 3; | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } else if(newState == 2) { | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | state = newState; | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Explosion === | ||
+ | < | ||
+ | class Explosion extends Sprite { | ||
+ | |||
+ | | ||
+ | |||
+ | super(x, y, SpriteLibrary.Explosion_1, | ||
+ | |||
+ | playAnimation(0, | ||
+ | |||
+ | scale(size); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Enemy === | ||
+ | < | ||
+ | class Enemy extends Rectangle { | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | |||
+ | | ||
+ | |||
+ | super(850, Math.random() * 600, Math.random() * 250 + 50, Math.random() * 250 + 50); | ||
+ | |||
+ | double vMax = s.points / 1000.0 * 5; | ||
+ | if(vMax < 2) { | ||
+ | vMax = 2; | ||
+ | } | ||
+ | |||
+ | if(vMax > 10) { | ||
+ | vMax = 10; | ||
+ | } | ||
+ | |||
+ | vy = Math.random() * vMax - vMax / 2; | ||
+ | vx = -Math.random() * vMax - vMax / 2; | ||
+ | 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, | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | | ||
+ | move(vx, vy); | ||
+ | rotate(vw); | ||
+ | |||
+ | if(getCenterX() < - 1000) { | ||
+ | | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | return Math.sqrt(vx * vx + vy * vy); | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | | ||
+ | return getWidth() * getHeight(); | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Powerup === | ||
+ | < | ||
+ | class Powerup extends Circle { | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | super(850, Math.random() * 500 + 50, 10); | ||
+ | | ||
+ | vx = Math.random() *(-5) - 5; | ||
+ | vy = Math.random() * 5 - 2.5; | ||
+ | | ||
+ | setFillColor(null); | ||
+ | setBorderColor(Color.red); | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | | ||
+ | | ||
+ | move(vx, vy); | ||
+ | | ||
+ | if(getCenterX() < - 300) { | ||
+ | | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ |