home
|
featured sketches
|
gallery
|
write a sketch
|
community
|
Copy sketch
Printouts
// just type here // and click "save" when your done //this is going to be the main stage - the black blob biological asteroid game // tribute game to Ira Greenberg and Springy Dude, from Creative Coding Hero myHero1; Asteroid[] asteroid; int totalAsteroids = 0; void setup() { size (500, 500); colorMode(HSB); asteroid = new Asteroid[40]; myHero1 = new Hero(color(255,0,0), 0, 100); } void draw() { background(255,255,255); myHero1.drawShape(); myHero1.moveShape(); for (int i=0; i<asteroid.length; i++) { asteroid[totalAsteroids] = new Asteroid(color(i, i*50, i*50, i*10), i*1); } totalAsteroids++; if (totalAsteroids >= asteroid.length) { totalAsteroids = 0; } for (int i=0; i<totalAsteroids; i++) { asteroid[i].drawAsteroid(); asteroid[i].moveAsteroid(); if (myHero1.intersect(asteroid[i])) { myHero1.highlight(0, 0, 200); } } } class Asteroid { float ellipseSpeed; float x, y; color ellipseCol; float r; Asteroid(color tempC, int tempEllipseSpeed) { ellipseCol = (tempC); ellipseSpeed = tempEllipseSpeed; x = width; y = random(0, height); r = 8; } void drawAsteroid() { for (int i = 2; i < r; i++) { smooth(); fill(ellipseCol); pushMatrix(); ellipse(x+random(0,6)*4, y+i*4, r, r); popMatrix(); } } void moveAsteroid() { x -= ellipseSpeed; ellipseSpeed = ellipseSpeed * 0.75; } } // void explodeAsteroid() class Hero { float centerX, centerY; float radius, rotAngle; float accelX, accelY; float springing, damping; int nodes; float nodeStartX[]; float nodeStartY[]; float nodeX[]; float nodeY[]; float angle[]; float frequency[]; float organicConstant; color c; Hero(color tempC, float tempXpos, float tempYpos) { c = tempC; radius = 30; rotAngle = -90; accelX = 5; accelY = 5; springing = .0085; damping = .98; //corner nodes nodes = 5; nodeStartX = new float[nodes]; nodeStartY = new float[nodes]; nodeX = new float[nodes]; nodeY = new float[nodes]; angle = new float[nodes]; frequency = new float[nodes]; //soft body dynamics organicConstant = 1; centerX = tempXpos; centerY = tempYpos; for (int i=0; i<nodes; i++) { frequency[i] = random(5,12); } } void drawShape() { //calculate node starting points for (int i=0; i<nodes; i++) { nodeStartX[i] = centerX+cos(radians(rotAngle))*radius; nodeStartY[i] = centerY+sin(radians(rotAngle))*radius; rotAngle += 360.0/nodes; } //draw polygon curveTightness(organicConstant); fill(c); beginShape(); for (int i=0; i<nodes; i++) { curveVertex(nodeX[i], nodeY[i]); } for (int i=0; i<nodes-1; i++) { curveVertex(nodeX[i], nodeY[i]); } endShape(); } void moveShape() { //move center point float deltaX = mouseX-centerX; float deltaY = mouseY-centerY; //create springing effect deltaX *= springing; deltaY *= springing; accelX += deltaX; accelY += deltaY; //move polygon center centerX += accelX; centerY += accelY; //slow down springing accelX *= damping; accelY *= damping; //change curve tightness organicConstant = 1-((abs(accelX)+abs(accelY))*.1); //move nodes for (int i=0; i<nodes; i++) { nodeX[i] = nodeStartX[i]+sin(radians(angle[i]))*(accelX*2); nodeY[i] = nodeStartY[i]+sin(radians(angle[i]))*(accelY*2); angle[i] += frequency[i]; } } void highlight(int hX, int hY, int hR) { int highColor = color(120, 150, 180); hX = int(centerX); hY = int(centerY); noFill(); ellipse(hX, hY, hR, hR); ellipse(hX, hY, hX - hR/2, hX - hR/2); ellipse(hX, hY, hX - hR/4, hX - hR/4); ellipse(hX, hY, hX - hR/8, hX - hR/8); //ellipse(hX, hY , hR/2, hR/2); //if (hR > 8){ // highlight(hX + hR/2, hY, hR/2); // highlight(hX - hR/2, hY, hR/2); // highlight(hX, hY + hR/2, hR/2); // highlight(hX, hY - hR/2, hR/2); //} } boolean intersect(Asteroid d) { float distance = dist(centerX, centerY, d.x, d.y); if (distance < radius + d.r) { return true; } else { return false; } } }
Sketches you submit on sketchPatch will be licensed under the
Creative Commons Attribution 3.0 Unported License
. If you upload code based on other people's work, please check the licence compatibility.
Click below to see an example
Get Started...
Copy this code into the text area to set up a basic sketch...
void setup() {
size(500,500);
}
void draw() {
background(200,30,90);
fill(20,40,150);
rect(100,100, 100,100);
}
Draw basic Shapes
Rectangle
Copy this code into the 'void draw ()' area of your code in the text area to draw a basic rectangle...
rect(100,100,100,100);
Triangle
Copy this code into the 'void draw ()' area of your code in the text area to draw a basic triangle...
triangle(30, 75, 58, 20, 86, 75);
Ellipse
Copy this code into the 'void draw ()' area of your code in the text area to draw a basic ellipse...
ellipse(56, 46, 55, 55);
Randomise
You can change how things look at random by using "random", all you need to do is specify the minimum and the maximum value you want.
Copy this code into the text area to randomly change the size and colour of your ellipse:
fill(random(0,255),20,200);
stroke(255,255,0);
ellipse(150, 150, random(10,150), 150);
Animate
You can animate your graphics by replacing numbers with "mouseX" , "mouseY" or "frameCount". In this example we change colors and shape size using the mouse:
background(20,30,mouseX);
fill(20,mouseY,150);
rect(100,100, 100,mouseX);
Drawing Tool
Copy this code into the 'void draw ()' area of your code to make a drawing tool...
fill(0,0,0);
noStroke();
ellipse(mouseX,mouseY, 2,2);
Then remove the line 'background();'
Repeat
You can repeat any piece of code by using a 'for loop'. A for loop changes a variable of a quantity you decide, until it reaches a number you want.
Example: add 10 each time to a variable called "i", until it gets to 200:
Copy this code into the text area to see a rectangle repeat across the sketch:
for (int i=0; i<200; i=i+10) {
rect(i,i, 100,100);
}
Ooops, found some glitches.
Write your sketch here
Check the Processing language
reference
(
most
of it works).
Need inspiration? Play with the examples in
the gallery
.
Title
Tags
Publish
(come on, let other people see your sketch!)
Your comments about the sketch
A tribute to Ira Greenberg, written mostly in a Peterborough coffee shop, so perhaps should also be dedicated to coffee