Skip to content

Instantly share code, notes, and snippets.

@CSchoel
Created January 27, 2015 13:20
Show Gist options
  • Save CSchoel/db6d14d376f2647482b2 to your computer and use it in GitHub Desktop.
Save CSchoel/db6d14d376f2647482b2 to your computer and use it in GitHub Desktop.
A bird destroys flying balloons
//Autor: Christopher Schölzel
//Abstrakte Superklasse für Bird und Balloon
//Vermeidet, dass z.b. visible doppelt implementiert werden muss
abstract class Movable {
float w,h;
float x,y;
Movable(float w, float h, float x, float y) {
this.w = w;
this.h = h;
this.x = x;
this.y = y;
}
abstract void move(float speed);
/*
//Allgemeine intersects-funktion
boolean intersects(Movable other) {
float tolerance = 0.01; //Toleranz für Fließkommafehler
float xIntersection = min(other.x+other.w, x+w)-max(other.x, x);
float yIntersection = min(other.y+other.h, x+h)-max(other.y, y);
return xIntersection > tolerance && yIntersection > tolerance;
}*/
boolean visible() {
return withinBox(x,y,-w/2.0,-h/2.0,width+w,height+h);
}
float getX() {
return x;
}
float getY() {
return y;
}
void setX(float x) {
this.x = x;
}
void setY(float y) {
this.y = y;
}
float getWidth() {
return w;
}
float getHeight() {
return h;
}
}
//Hilfsfunktion zum testen ob punkt (x,y) im Rechteck liegt, das durch den oberen
//linken Punkt (boxX,boxY) und durch die breite boxW sowie die Höhe boxH gegeben ist
boolean withinBox(float x, float y, float boxX, float boxY, float boxW, float boxH) {
boolean withinX = x >= boxX && x <= boxX+boxW;
boolean withinY = y >= boxY && y <= boxY+boxH;
return withinX && withinY;
}
class Bird extends Movable {
Bird(float w, float h, float x, float y) {
super(w,h,x,y);
}
void move(float vx) {
x += vx;
}
void display() {
fill(0);
ellipse(x,y,w,h);
fill(255,255,0);
triangle(
x-w*0.7, y,
x-w*0.4, y+h*0.25,
x-w*0.4, y-h*0.25
); //Schnabel
//Flügel
fill(255,0,0);
pushMatrix();
translate(x-w*0.1,y);
rotate(sin(x*0.1));
float wingWidth = w*0.9;
float wingHeight = h*0.7;
ellipse(wingWidth/2.0-w*0.1,0,wingWidth,wingHeight);
popMatrix();
}
//testet ob der Schnabel des Vogels in der Bounding Box eines Ballons ist
boolean intersects(Balloon balloon) {
float beakX = x-w*0.7;
float beakY = y;
return withinBox(
beakX,beakY,
balloon.getX()-balloon.getWidth()/2.0,balloon.getY()-balloon.getHeight()/2.0,
balloon.getWidth(),balloon.getHeight()
);
}
}
class Balloon extends Movable {
color c;
boolean visible;
Balloon(float w, float h, float x, float y) {
super(w,h,x,y);
c = color(random(256),random(256),random(256));
visible = true;
}
void move(float vy) {
if(!visible) return;
y += vy;
}
void display() {
if(!visible) return;
fill(c);
ellipse(x,y,w,h);
}
void poof() {
visible = false;
}
}
Balloon[] balloons;
Bird bird;
boolean flying = false;
void setup() {
size(800,600);
balloons = new Balloon[100];
for(int i = 0; i < balloons.length; i++) {
balloons[i] = new Balloon(random(15,30),random(30,50),random(0,width),random(0,height));
}
bird = new Bird(50,20,width,0);
}
void draw() {
background(255);
//Vogel bewegen
if (flying) {
bird.move(-2);
if (!bird.visible()) {
flying = false;
bird.setX(width);
}
} else bird.setY(mouseY);
//Ballons bewegen und zeichnen
for(Balloon b : balloons) {
if(bird.intersects(b)) b.poof();
b.move(-1);
b.display();
if(!b.visible()) {
b.setY(height+b.getHeight()/2.0-1);
}
}
//Vogel zeichnen
bird.display();
}
void mousePressed() {
flying = true;
bird.setY(mouseY);
bird.setX(width);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment