Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
schule:klassen:2018:10bc:particleengine [2019/03/18 13:43] – angelegt martin | schule:klassen:2018:10bc:particleengine [2021/12/29 10:40] (aktuell) – Externe Bearbeitung 127.0.0.1 | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | ====== Projekt: Particle Engine ====== | ||
+ | |||
+ | ===== Die Klasse Particle ===== | ||
+ | <code java> | ||
+ | import sge.*; | ||
+ | import java.awt.Color; | ||
+ | |||
+ | public class Particle | ||
+ | { | ||
+ | protected Kreis k; | ||
+ | double vx, vy; | ||
+ | int alter = 0; | ||
+ | double ay; | ||
+ | double lebensdauer; | ||
+ | int radius; | ||
+ | | ||
+ | public Particle(double x, double y, double v0x, double v0y){ | ||
+ | | ||
+ | vx = v0x; | ||
+ | vy = v0y; | ||
+ | | ||
+ | } | ||
+ | | ||
+ | public boolean zeitschritt(double deltaT){ | ||
+ | | ||
+ | alter = alter + (int)deltaT; | ||
+ | | ||
+ | if(alter <= lebensdauer){ | ||
+ | | ||
+ | } | ||
+ | | ||
+ | k.verschieben(deltaT*vx, | ||
+ | vy = vy + deltaT * ay; | ||
+ | | ||
+ | return k.istAußerhalbDesFensters() || alter > lebensdauer; | ||
+ | | ||
+ | } | ||
+ | | ||
+ | public void vernichten(){ | ||
+ | k.vernichten(); | ||
+ | } | ||
+ | | ||
+ | public double getX(){ | ||
+ | return k.getMitteX(); | ||
+ | } | ||
+ | |||
+ | public double getY(){ | ||
+ | return k.getMitteY(); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Die Klasse Konfetti ===== | ||
+ | <code java> | ||
+ | import sge.*; | ||
+ | import java.awt.Color; | ||
+ | |||
+ | public class Konfetti extends Particle { | ||
+ | | ||
+ | public Konfetti(double x, double y, double v0x, double v0y){ | ||
+ | |||
+ | super(x, y, v0x, v0y); | ||
+ | | ||
+ | ay = 0.0005; | ||
+ | lebensdauer = 1000; | ||
+ | radius = 10; | ||
+ | | ||
+ | k = new Kreis(x, y, radius); | ||
+ | | ||
+ | int rot = (int)(Math.random()*256); | ||
+ | int grün = (int)(Math.random()*256); | ||
+ | int blau = (int)(Math.random()*256); | ||
+ | | ||
+ | k.setFuellfarbe(new Color(rot, grün, blau)); | ||
+ | | ||
+ | } | ||
+ | | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Die Klasse Fetzen ===== | ||
+ | <code java> | ||
+ | import sge.*; | ||
+ | import java.awt.Color; | ||
+ | |||
+ | public class Fetzen extends Particle { | ||
+ | | ||
+ | public Fetzen(double x, double y, double v0x, double v0y){ | ||
+ | |||
+ | super(x, y, v0x, v0y); | ||
+ | | ||
+ | ay = 0.0; | ||
+ | lebensdauer = 500; | ||
+ | radius = 2; | ||
+ | | ||
+ | k = new Kreis(x, y, radius); | ||
+ | | ||
+ | int rot = 255; | ||
+ | int grün = 255; | ||
+ | int blau = 0; | ||
+ | | ||
+ | k.setFuellfarbe(new Color(rot, grün, blau)); | ||
+ | | ||
+ | } | ||
+ | | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Die Klasse ParticleEngine ===== | ||
+ | <code java> | ||
+ | import sge.*; | ||
+ | |||
+ | public class ParticleEngine implements TimerListener, | ||
+ | { | ||
+ | Particle[] particles = new Particle[1000]; | ||
+ | int x = 300; | ||
+ | int y = 300; | ||
+ | | ||
+ | | ||
+ | public ParticleEngine(){ | ||
+ | |||
+ | Fenster f = new Fenster(); | ||
+ | f.addTastaturListener(this); | ||
+ | | ||
+ | Timer t = new Timer(this, 20); // 20 ms entspricht 50 FPS | ||
+ | t.start(); | ||
+ | |||
+ | } | ||
+ | |||
+ | public void tasteGedrueckt(char key, int keyCode, boolean erstmals){ | ||
+ | | ||
+ | if(key == ' | ||
+ | | ||
+ | x--; // | ||
+ | | ||
+ | } | ||
+ | | ||
+ | | ||
+ | } | ||
+ | | ||
+ | public void timerSignalVerarbeiten(){ | ||
+ | |||
+ | // Zeitschritt für alle Partikel aufrufen und | ||
+ | // Partikel außerhalb des Fensters vernichten | ||
+ | int i = 0; | ||
+ | while (i < particles.length){ | ||
+ | |||
+ | Particle p = particles[i]; | ||
+ | if(p != null){ | ||
+ | |||
+ | boolean außerhalb = p.zeitschritt(20); | ||
+ | if(außerhalb){ | ||
+ | particles[i] = null; | ||
+ | |||
+ | if(p instanceof Konfetti){ | ||
+ | |||
+ | int j = 0; | ||
+ | while(j < 10){ | ||
+ | erzeugeFetzen(p.getX(), | ||
+ | j++; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | p.vernichten(); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | i++; | ||
+ | |||
+ | } | ||
+ | |||
+ | if(Math.random() < 0.25){ | ||
+ | // Neue Partikel erzeugen | ||
+ | i = 0; | ||
+ | while(i < particles.length){ | ||
+ | |||
+ | if(particles[i] == null){ | ||
+ | |||
+ | double vx = Math.random() * 0.2 - 0.1; | ||
+ | double vy = Math.random() * -0.3 - 0.3; | ||
+ | |||
+ | particles[i] = new Konfetti(x, y, vx, vy); | ||
+ | |||
+ | break; // raus aus der while-Wiederholung | ||
+ | |||
+ | } | ||
+ | i++; // i = i + 1; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | private void erzeugeFetzen(double x, double y){ | ||
+ | // nächste freie Position im Array particles finden: | ||
+ | int i = 0; | ||
+ | while(i < particles.length){ | ||
+ | if(particles[i] == null){ | ||
+ | double vx = Math.random() * 2 - 1; | ||
+ | double vy = Math.random() * 2 - 1; | ||
+ | |||
+ | Particle p = new Fetzen(x, y, vx, vy); | ||
+ | particles[i] = p; | ||
+ | return; | ||
+ | } | ||
+ | i++; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||