no way to compare when less than two revisions
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Letzte Überarbeitung | |||
— | schule:klassen:2019:10abc:sidescroller4 [2020/05/03 21:24] – angelegt martin | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | ====== Projekt Sidescroller - Stand vor 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.shoot); | ||
+ | if(timeSinceLastBullet > 30) { | ||
+ | | ||
+ | | ||
+ | radius = 20; | ||
+ | | ||
+ | | ||
+ | } else { | ||
+ | | ||
+ | } | ||
+ | timeSinceLastBullet = 0; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Sidescroller === | ||
+ | <code java> | ||
+ | World w = new World(800, 600); | ||
+ | |||
+ | new Sidescroller(); | ||
+ | |||
+ | |||
+ | class Sidescroller extends Actor { | ||
+ | |||
+ | | ||
+ | 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; | ||
+ | |||
+ | |||
+ | | ||
+ | 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 = enemies.getCollidingShapes(spaceship); | ||
+ | | ||
+ | if(collidingEnemies.length > 0) { | ||
+ | |||
+ | Enemy e = (Enemy)collidingEnemies[0]; | ||
+ | |||
+ | | ||
+ | |||
+ | new Explosion(e.getCenterX(), | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | System.playSound(Sound.far_bomb); | ||
+ | setState(2); | ||
+ | } else { | ||
+ | System.playSound(Sound.cannon_boom); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | 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); | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | CollisionPair[] pairs = bullets.getCollisionPairs(enemies, | ||
+ | |||
+ | for(int i = 0; i < pairs.length; | ||
+ | |||
+ | | ||
+ | |||
+ | | ||
+ | |||
+ | | ||
+ | b.destroy(); | ||
+ | } else if(b.getRadius() > 10) { | ||
+ | | ||
+ | int anzahl = Math.round(b.getRadius() / 5); | ||
+ | for(int i = 0; i < anzahl; i++) { | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | b.destroy(); | ||
+ | } else | ||
+ | { | ||
+ | b.setRadius(b.getRadius() - 5); | ||
+ | } | ||
+ | |||
+ | | ||
+ | Enemy r = (Enemy)cp.shapeB; | ||
+ | points += Math.round(200000 / r.getFläche() * r.getGeschwindigkeit()); | ||
+ | | ||
+ | if(r.alpha < 0.2) { | ||
+ | | ||
+ | |||
+ | new Explosion(r.getCenterX(), | ||
+ | | ||
+ | | ||
+ | } else { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } else { | ||
+ | cp.shapeB.destroy(); | ||
+ | } | ||
+ | |||
+ | | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | | ||
+ | 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() * 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, | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | | ||
+ | move(vx, vy); | ||
+ | rotate(vw); | ||
+ | |||
+ | if(getCenterX() < - 1000) { | ||
+ | | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | | ||
+ | return getWidth() * getHeight(); | ||
+ | } | ||
+ | |||
+ | | ||
+ | return Math.sqrt(vx * vx + vy * vy); | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | </ | ||