Initial Commit
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
// A class to describe a Polygon (with a PShape)
|
||||
|
||||
class Polygon {
|
||||
// The PShape object
|
||||
PShape s;
|
||||
// The location where we will draw the shape
|
||||
float x, y;
|
||||
// Variable for simple motion
|
||||
float speed;
|
||||
|
||||
Polygon(PShape s_) {
|
||||
x = random(width);
|
||||
y = random(-500, -100);
|
||||
s = s_;
|
||||
speed = random(2, 6);
|
||||
}
|
||||
|
||||
// Simple motion
|
||||
void move() {
|
||||
y += speed;
|
||||
if (y > height+100) {
|
||||
y = -100;
|
||||
}
|
||||
}
|
||||
|
||||
// Draw the object
|
||||
void display() {
|
||||
pushMatrix();
|
||||
translate(x, y);
|
||||
shape(s);
|
||||
popMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* PolygonPShapeOOP.
|
||||
*
|
||||
* Wrapping a PShape inside a custom class
|
||||
* and demonstrating how we can have a multiple objects each
|
||||
* using the same PShape.
|
||||
*/
|
||||
|
||||
|
||||
// A list of objects
|
||||
ArrayList<Polygon> polygons;
|
||||
|
||||
void setup() {
|
||||
size(640, 360, P2D);
|
||||
|
||||
// Make a PShape
|
||||
PShape star = createShape();
|
||||
star.beginShape();
|
||||
star.noStroke();
|
||||
star.fill(0, 127);
|
||||
star.vertex(0, -50);
|
||||
star.vertex(14, -20);
|
||||
star.vertex(47, -15);
|
||||
star.vertex(23, 7);
|
||||
star.vertex(29, 40);
|
||||
star.vertex(0, 25);
|
||||
star.vertex(-29, 40);
|
||||
star.vertex(-23, 7);
|
||||
star.vertex(-47, -15);
|
||||
star.vertex(-14, -20);
|
||||
star.endShape(CLOSE);
|
||||
|
||||
// Make an ArrayList
|
||||
polygons = new ArrayList<Polygon>();
|
||||
|
||||
// Add a bunch of objects to the ArrayList
|
||||
// Pass in reference to the PShape
|
||||
// We coud make polygons with different PShapes
|
||||
for (int i = 0; i < 25; i++) {
|
||||
polygons.add(new Polygon(star));
|
||||
}
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(255);
|
||||
|
||||
// Display and move them all
|
||||
for (Polygon poly : polygons) {
|
||||
poly.display();
|
||||
poly.move();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user