Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

schule:klassen:2018:10bc:particleengine [2019/03/18 14:43] – angelegt martinschule:klassen:2018:10bc:particleengine [2021/12/29 11: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.setFuellfarbeAlpha(255 - (int)(255.0/lebensdauer * alter));
 +        }
 +        
 +        k.verschieben(deltaT*vx, deltaT*vy);
 +        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();
 +    }
 +
 +}
 +</code>
 +
 +===== 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));
 +    
 +    }
 +    
 +    
 +}
 +</code>
 +
 +===== 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));
 +    
 +    }
 +    
 +    
 +}
 +</code>
 +
 +===== Die Klasse ParticleEngine =====
 +<code java>
 +import sge.*;
 +
 +public class ParticleEngine implements TimerListener, TastaturListener
 +{
 +    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 == 'a'){
 +        
 +            x--;     //Kurzschreibweise für x = x - 1;
 +        
 +        }
 +        
 +        
 +    }
 +    
 +    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(), p.getY());
 +                            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++;
 +        }
 +    }
 +}
 +</code>
 +
  
Drucken/exportieren
QR-Code
QR-Code schule:klassen:2018:10bc:particleengine (erstellt für aktuelle Seite)