Text
Broken Collisions
Particles universe;
void setup() {
size(1552,739);
frameRate(60);
universe = new Particles(100);
Particle p;
for(int i=0; i < universe.numberOfParticles(); i++) {
p = universe.particleAt(i);
p.volume = 50.0;
}
}
void draw() {
background(0);
universe.update();
universe.draw();
}
class Particles {
ArrayList particles;
Particles(int numParticles) {
particles = new ArrayList();
for(int i=0; i < numParticles; i++) {
particles.add(new Particle(i, random(width), random(height), this));
}
}
void update() {
Particle p;
for(int i=0; i < numberOfParticles(); i++) {
particleAt(i).updateVelocity();
}
// going backwards, because we may delete particles
for(int i=numberOfParticles() - 1; i >= 0; i--) {
p = particleAt(i);
if(p.destroyed) {
particles.remove(i);
}
else {
p.updatePosition();
}
}
}
void draw() {
for(int i=0; i < numberOfParticles(); i++) {
Particle p = particleAt(i);
p.draw();
}
}
int numberOfParticles() {
return particles.size();
}
Particle particleAt(int i) {
return particles.get(i);
}
void addNewParticle(Particle p) {
particles.add(p);
p.myId == numberOfParticles() - 1;
}
}
class Particle {
int myId;
float mass = 100;
float volume = 20;
float minVolume = 1;
PVector acceleration;
PVector velocity;
PVector position;
Particles universe;
boolean destroyed;
boolean collided;
color myColor = color(random(255), random(255), random(255));
Particle(int inMyId, float positionX, float positionY, Particles inUniverse) {
myId = inMyId;
universe = inUniverse;
destroyed = false;
acceleration = new PVector(0,0);
velocity = new PVector(0,0);
position = new PVector(positionX, positionY);
}
void updateVelocity() {
acceleration.set(0,0,0);
for(int i=0; i < universe.numberOfParticles(); i++) {
if(i != myId) {
Particle other = universe.particleAt(i);
float diffX = other.position.x - position.x;
float diffY = other.position.y - position.y;
float distance = abs(PVector.dist(position, other.position));
if((distance) < volume && !collided && !other.collided)
{
hitOtherParticle(other);
}
PVector attraction = new PVector(diffX, diffY);
attraction.normalize(); // make it a unit vector
// we'll figure out how to handle collision in a later post
// attraction to the other object is inverse to the
// square of the distance to the other object.
attraction.mult(other.mass / sq(distance));
acceleration.add(attraction);
}
}
velocity.add(acceleration);
velocity.limit(20);
}
void hitOtherParticle(Particle other) {
PVector v1 = velocity;
PVector v2 = other.velocity;
PVector v1f = new PVector();
PVector v2f = new PVector();
// Apply the formula for elastic collision
v1f.x = (v1.x - v2.x) / 2 + v2.x; // Final velocity
v2f.x = (v2.x - v1.x) / 2 + v1.x; // Final velocity
velocity.set(v1f);
other.velocity.set(v2f);
collided = true;
other.collided = true;
}
void updatePosition() {
position.add(velocity);
if(position.x < 0) {
position.x = 0;
velocity.set(-velocity.x, velocity.y, velocity.z);
}
else if(position.x > width) {
position.x = width;
velocity.set(-velocity.x, velocity.y, velocity.z);
}
if(position.y < 0) {
position.y = 0;
velocity.set(velocity.x, -velocity.y, velocity.z);
}
else if(position.y > height) {
position.y = height;
velocity.set(velocity.x, -velocity.y, velocity.z);
}
collided = false;
}
void draw() {
drawPosition();
}
void drawPosition() {
stroke(myColor);
strokeWeight(volume);
point(position.x, position.y);
}
void drawVelocity() {
stroke(255,0,0);
strokeWeight(1);
line(position.x, position.y,
position.x + velocity.x,
position.y + velocity.y);
}
void drawAcceleration() {
stroke(0,0,255);
strokeWeight(1);
line(position.x, position.y,
position.x + acceleration.x,
position.y + acceleration.y);
}
}
0 notes
Text
Grid Balls
float massMax = 2000.0;
Grid grid;
Particles universe;
void setup() {
size(1558,743);
grid = new Grid(64);
frameRate(60);
universe = new Particles(100);
colorMode(HSB, 100, 255, 255, 255);
Particle p;
for(int i=0; i < universe.numberOfParticles(); i++) {
p = universe.particleAt(i);
p.position.x = random(width);
p.position.y = random(height);
p.velocity.x = random(2) - random(2);
p.velocity.y = random(2) - random(2);
p.mass = random(2000.0)
p.volume = p.mass / 40;
}
background(255);
}
void draw() {
background(0);
grid.update();
grid.draw();
colorMode(HSB, 100, 255, 255, 255);
universe.update();
universe.draw();
}
class Cell {
void draw(float x, float y, int cellSize) {
}
void update(float x, float y, Cell[][] cells) {
}
}
class Grid {
boolean drawCellBorder = true;
int gridWidth, gridHeight;
int cellSize;
Cell[][] cells;
Grid(int size) {
cellSize = floor(2 * height / (size + 4));
gridHeight = size;
gridWidth = floor(width / (cellSize * 1.5));
cells = new Cell[gridWidth][gridHeight];
for(int x=0; x < gridWidth; x++) {
for(int y=0; y < gridHeight; y++) {
cells[x][y] = null;
}
}
}
void draw() {
for(int x=0; x < gridWidth; x++) {
for(int y=0; y < gridHeight; y++) {
if(drawCellBorder) {
int dcellSize = cellSize;
int dX = x * dcellSize * 1.5;
int dY = y * (dcellSize/2);
if(y % 2 == 1) {
dX += dcellSize - dcellSize/4;
}
dX += dcellSize/2;
dY += dcellSize;
float massWithin = universe.massWithin(dX, dY, cellSize);
colorMode(RGB);
strokeWeight(0.125);
strokeWeight(massWithin / massMax);
stroke(0, random(55) + 200, 0);
noFill();
beginShape();
vertex(dX-dcellSize/2, dY);
vertex(dX-dcellSize/4, dY-dcellSize/2);
vertex(dX+dcellSize/4, dY-dcellSize/2);
vertex(dX+dcellSize/2, dY);
vertex(dX+dcellSize/4, dY+dcellSize/2);
vertex(dX-dcellSize/4, dY+dcellSize/2);
endShape(CLOSE);
strokeWeight(0.125);
}
if(cells[x][y] != null) {
cells[x][y].draw(x, y, cellSize);
}
}
}
}
void update() {
for(int x=0; x < gridWidth; x++) {
for(int y=0; y < gridHeight; y++) {
if(cells[x][y] != null) {
cells[x][y].update(x, y, cells);
}
}
}
}
}
class Particles {
ArrayList particles;
ArrayList newParticles;
Particles(int numParticles) {
particles = new ArrayList();
newParticles = new ArrayList();
for(int i=0; i < numParticles; i++) {
particles.add(new Particle(random(width), random(height), this));
}
}
void update() {
Particle p;
int numParticles = numberOfParticles();
for(int i = numParticles - 1; i >= 0; i--) {
particleAt(i).updateVelocity(numParticles);
}
for(int i=0; i < newParticles.size(); i++) {
particles.add(newParticles.get(i));
newParticles.remove(i);
}
for(int i=numberOfParticles() - 1; i >= 0; i--) {
p = particleAt(i);
if(p.destroyed) {
particles.remove(i);
}
else {
p.updatePosition();
}
}
}
float massWithin(float x, float y, float distance) {
float mass = 0.0;
for(int i=0; i < numberOfParticles(); i++) {
Particle part = particleAt(i);
PVector pos = part.position;
if(abs(pos.x - x) < distance &&
abs(pos.y - y) < distance) {
mass += part.mass;
}
}
return mass;
}
void draw() {
for(int i=0; i < numberOfParticles(); i++) {
particleAt(i).drawPosition();
}
}
int numberOfParticles() {
return particles.size();
}
Particle particleAt(int i) {
return particles.get(i);
}
void addNewParticle(Particle p) {
newParticles.add(p);
}
}
int particleId = 0;
class Particle {
float GravitationalConstant = 0.000667384;
int myId;
float mass = 1;
float volume = 4;
float minVolume = 4;
float minDestructionForce = 4;
PVector acceleration;
PVector velocity;
PVector position;
Particles universe;
boolean destroyed;
color myColor;
Particle(float positionX, float positionY, Particles inUniverse) {
myId = ++particleId;
universe = inUniverse;
destroyed = false;
acceleration = new PVector(0,0);
velocity = new PVector(0,0);
position = new PVector(positionX, positionY);
myColor = color(myId, 255, 255, 200);
}
int getId() {
return myId;
}
void updateVelocity(int numParticles) {
acceleration.x = acceleration.y = 0;
for(int i=numParticles - 1; i >= 0; i--) {
Particle other = universe.particleAt(i);
if(other.getId() != myId) {
float diffX = other.position.x - position.x;
float diffY = other.position.y - position.y;
float distance = PVector.dist(position, other.position);
PVector attraction = new PVector(diffX, diffY);
attraction.normalize();
if(distance >= (other.volume/2 + volume/2)) {
attraction.mult((mass * other.mass) / sq(distance));
attraction.mult(GravitationalConstant);
acceleration.add(attraction);
}
else {
hitOtherParticle(other);
}
}
}
acceleration.div(mass);
velocity.add(acceleration);
velocity.limit(20);
}
void splitParticle(float numPieces,
PVector collisionVelocity,
Particle other) {
Particle p;
destroyed = true;
collisionVelocity.div(numPieces);
volume = volume / numPieces;
mass = mass / numPieces;
for(int i=0; i < numPieces; i++) {
p = new Particle(position.x, position.y, universe);
p.mass = mass;
p.volume = volume;
p.velocity.x = sin(TWO_PI - random(2*TWO_PI));
p.velocity.y = cos(TWO_PI - random(2*TWO_PI));
p.velocity.normalize();
p.velocity.mult(collisionVelocity.mag());
p.velocity.limit(20);
universe.addNewParticle(p);
}
}
void hitOtherParticle(Particle other) {
if(volume <= minVolume || mass < 4) {
destroyed = true;
}
else {
PVector collisionVelocity = PVector.sub(velocity, other.velocity);
float collisionMagnitude = collisionVelocity.mag();
float force = collisionMagnitude * mass;
float otherForce = collisionMagnitude * other.mass;
if(collisionMagnitude > 0) {
acceleration.sub(collisionVelocity);
}
else {
float tx, ty;
tx = velocity.x;
ty = velocity.y;
velocity.x = other.velocity.x;
velocity.y = other.velocity.y;
other.velocity.y = ty;
other.velocity.x = tx;
}
}
}
void updatePosition() {
position.add(velocity);
if(position.x < 0) {
position.x = width;
}
else if(position.x > width) {
position.x = 0;
}
if(position.y < 0) {
position.y = height;
}
else if(position.y > height) {
position.y = 0;
}
}
void drawPosition() {
stroke(myId, 255, 255, 127);
strokeWeight(volume);
point(position.x, position.y);
}
}
0 notes
Text
Cave Hunter
// Music by Rob Beschizza available at https://www.boingboing.net/2011/03/28/game-deaths-mp3.html String COPYRIGHT = 'Cave Hunter version 0,3 Copyright (C) 2011 by Bill Bereza'; SegmentFont font; Game game; var songs = 1; var[] sounds = new var[songs]; soundManager.onready(function(){ for(int i=0; i <= songs; i++) { sounds[i] = soundManager.createSound({ id: 'track'+i, url: 'https://klasl.com/scripts/mp3/cavehunter'+i+'.mp3', autoPlay: false, autoLoad: false }); } }); function loopSound(i) { sounds[i].play({ onfinish: function() { loopSound(int(i+1) % songs); } }); } void setup() { size(); frameRate(30); font = new SegmentFont(); game = new CaveHunter(); reset(); background(255); loopSound(0); } void draw() { background(255); game.draw(); if(game.over()) { drawInstructions('CLICK MOUSE TO PLAY AGAIN'); drawFlasher('GAME OVER'); } else if(focused) { game.update(); } else if(!focused) { drawPauseScreen(); } colorMode(HSB); drawScore(); } void mouseClicked() { if(game.over()) { reset(); } } void keyPressed() { game.keyPressed(); } void keyReleased() { game.keyReleased(); } void reset() { game.reset(); } class Ship extends Sprite { int gun; int x, y; int newCount; int upDownMove; int leftRightMove; boolean crashed; int length = 40; Ship() { super(); reset(); } void reset() { gun = 0; x = int(floor(width/4)); y = int(floor(height/2)); newCount = 30; upDownMove = 0; leftRightMove = 0; crashed = false; } int upDownMotion() { return upDownMove; } int leftRightMotion() { return leftRightMove; } void setMotion(int leftRight, int upDown) { if(!crashed) { upDownMove = upDown; leftRightMove = leftRight; } } void update() { x += leftRightMove; if(x < length) { x = length; } if(x > width) { x = width; } y += upDownMove; if(y < 0) { y = 0; } if(y > height) { y = height; } } void crash() { newCount = 30; crashed = true; } void draw() { colorMode(RGB); stroke(0,0,255); strokeWeight(2); if(crashed) { if(newCount > 0) { newCount = newCount - 1; if(newCount % 2) { fill(255,127,0); ellipse(x, y, 40, 40); } } else { reset(); } } else { // not crashed if(newCount > 0) { newCount = newCount - 1; if(newCount % 2) { fill(0, 255, 0); } else { fill(255,255,255,1); } } else { fill(0,255,0); } if(upDownMotion() == 0) { triangle(x, y, x-40, y+20, x-40, y-20); } else if(upDownMotion() < 0) { triangle(x, y, x-45, y+15, x-35, y-15); } else if(upDownMotion() > 0) { triangle(x, y, x-35, y+15, x-45, y-15); } if(leftRightMotion() > 0) { fill(255,127,0); triangle(x-60, y, x-40, y+10, x-40, y-10); } else if(leftRightMotion() < 0) { fill(255, 127, 0); triangle(x, y, x-20, y+10, x+5, y+15); triangle(x, y, x-20, y-10, x+5, y-15); } fill(255,0,0); ellipse(x-15, y, 10, 10); } } boolean collisionWith(int ox, oy) { return (ox >= x-40 && ox <= x && oy >= y-20 && oy <= y+20); } boolean isCrashed() { return crashed; } boolean finished() { return false; } int xLeft() { return x-40; } int yTop() { return y-20; } int xRight() { return x; } int yBottom() { return y+20; } } class Dancer extends Bullet { int dist = 10; int count = 0; Dancer(int ix, int iy) { super(color(0,255,0), ix, iy, -8, 0); radius = dist * 2; ay = random(dist) - random(dist); } void update() { if((++count % dist) == 0) { ay = random(dist) - random(dist); } super.update(); } int kind() { return 2; } } class Bobber extends Bullet { int dist = 10; int count = 0; Bobber(int ix, int iy) { super(color(0,0,255), ix, iy, -8, 0); radius = dist * 2; ay = random(dist) - random(dist); ax = -8 - random(dist); } void update() { if((++count % dist) == 0) { ay = random(dist) - random(dist); ax = -8 - random(dist); } super.update(); } int kind() { return 3; } } class Bullet extends Sprite { color c; int ax, ay; int x, y; boolean crashed; int crashCount; int radius; Bullet(color ci, int ix, int iy, int vx, int vy) { c = ci; x = ix; y = iy; ax = vx; ay = vy; crashed = false; crashCount = 0; radius = 4; } int kind() { return 666; } void draw() { colorMode(RGB); stroke(c); strokeWeight(radius); point(x, y); if(crashed && (crashCount-- % 2 == 0)) { stroke(255,0,0); strokeWeight(radius * 2); point(x, y); } } void update() { x += ax; y += ay; if(x > width || x < 0 || y > height || y < 0) { crashed = true; crashCount = 0; } } void setMotion(int ix, int iy) { ax = ix; ay = iy; } void crash() { crashed = true; crashCount = 10; } boolean isCrashed() { return crashed; } boolean finished() { return (crashed && crashCount <= 0); } int xLeft() { return (x - (radius/2)); } int xRight() { return (x + (radius/2)); } int yTop() { return (y - (radius/2)); } int yBottom() { return (y + (radius/2)); } } class Sprite { Sprite() { } int kind() { return 0; } void setMotion(int ix, int iy) { } void draw() { } void update() { } void crash() { } boolean isCrashed() { return false; } boolean finished() { } int xLeft() { } int yTop() { } int xRight() { } int yBottom() { } boolean overlaps(Sprite o) { return (xLeft() < o.xRight() && xRight() > o.xLeft() && yTop() < o.yBottom() && yBottom() > o.yTop()); } } class CaveHunter extends Game { Landscape landscape; Landscape ceiling; Ship ship; ArrayList sprites; int shipSpeed = 5; int scrollSpeed = 8; int ships; int points; CaveHunter() { reset(); } void reset() { sprites = new ArrayList(); landscape = new Landscape(width, Landscape.FLOOR); ceiling = new Landscape(width, Landscape.CEILING); ship = new Ship(); ships = 5; sprites.add(ship); points = 0; } String name() { return 'Cave Hunter'; } String instructions() { return 'USE THE ARROW KEYS TO MOVE YOUR SHIP AROUND.\nPRESS CONTROL KEY TO FIRE YOUR CANNON.\nPICK UP THE POWERUPS TO ENHANCE YOUR FIREPOWER.'; } void keyPressed() { if(key == CODED) { if(keyCode == UP) { ship.setMotion(0, -shipSpeed); } else if(keyCode == DOWN) { ship.setMotion(0, shipSpeed); } else if(keyCode == LEFT) { ship.setMotion(-shipSpeed, 0); } else if(keyCode == RIGHT) { ship.setMotion(shipSpeed, 0); } else if(keyCode == CONTROL) { sprites.add(new Bullet(color(0,0,255), ship.x + 4, ship.y, 20, 0)); } } } void keyReleased() { ship.setMotion(0,0); } int lives() { return ships; } boolean over() { return lives() == 0; } void update() { landscape.scroll(Landscape.RIGHT, scrollSpeed); ceiling.scroll(Landscape.RIGHT, scrollSpeed); if(random(100) >= 97) { sprites.add(new Dancer(width-1, height/2)); } else if(random(100) >= 98) { Sprite d = new Bobber(width-1, height/2); sprites.add(d); } for(int i=sprites.size() - 1; i >= 0; i--) { Sprite sprite = (Sprite) sprites.get(i); if(sprite.finished()) { sprites.remove(i); } else { sprite.update(); } } checkCollisions(); } void draw() { landscape.draw(); ceiling.draw(); for(int i=0; i < sprites.size(); i++) { Sprite sprite = (Sprite) sprites.get(i); sprite.draw(); } } int score() { return points; } void checkCollisions() { for(int i=0; i < sprites.size(); i++) { Sprite a = (Sprite) sprites.get(i); if(!a.isCrashed()) { for(int j=i+1; j < sprites.size(); j++) { Sprite b = (Sprite) sprites.get(j); if(!b.isCrashed() && a.overlaps(b)) { a.crash(); b.crash(); if(a == ship || b == ship) { ships--; } else if(a.kind() == 666 || b.kind() == 666) { points-=1000; } } } } if(!a.isCrashed()) { if(a.yBottom() > landscape.verticalAt(a.xRight()) || a.yTop() < ceiling.verticalAt(a.xRight())) { a.setMotion(-scrollSpeed, 0); a.crash(); if(a == ship) { ships--; } } } } points++; if((points % 1000) == 0) { ships++; } } } float lastMillis = 0; void drawScore() { float nowMillis = millis(); float fps = 1000/(nowMillis - lastMillis); lastMillis = nowMillis; float textScale = 4; strokeWeight(0.75); colorMode(RGB); stroke(0, random(55) + 200, 0); font.text("SCORE> " + game.score() + " LIVES> " + game.lives() + " FPS> " + floor(fps), 8, (font.charHeight*textScale + 4), textScale); } float titleScale[] = { 2, 1.8, 1.6, 1.4, 1.2, 1.0, 1.2, 1.4, 1.6, 1.8, 2 }; void drawFlasher(String title) { float titleSize=16; colorMode(HSB, title.length(), 255, 255); strokeWeight(0.25); for(int i=0; i < title.length(); i++) { stroke(i, 255, random(55) + 200); font.character(title.charAt(i), (width - (title.length()*font.charWidth*titleSize)) / 2.0 + i*font.charWidth*titleSize, height/2.5, titleSize*sin(titleScale[i])); titleScale[i] += 0.2; } colorMode(RGB,255); } void drawInstructions(String instr) { float instSize = 5; stroke(255,0,0); strokeWeight(0.5 + random(0.25)); font.text(instr, (width - (instr.length()*font.charWidth*instSize))/2.0, height/2, instSize); } void drawCopyright(String copyr) { float copySize = 3; String instructions = "\n\n" + game.instructions(); stroke(0,255,0); strokeWeight(0.25 + random(0.25)); font.text(copyr + instructions, (width - (copyr.length()*font.charWidth*copySize))/2.0, height - height/2.5, copySize); } void drawPauseScreen() { String title = game.name(); String instr = 'CLICK IN THE WINDOW TO PLAY'; drawInstructions(instr); drawFlasher(title); drawCopyright(COPYRIGHT); } class Game { boolean over() { return false; } void update() { } void draw() { } void reset() { } void keyPressed() { } void keyReleased() { } int score() { return 0; } int level() { return 0; } int lives() { return 0; } String instructions() { return ""; } String name() { return "Game"; } } class SegmentFont { int charWidth = 4; int charHeight = 6; HashMap chars; SegmentFont() { chars = new HashMap(); // char pattern // sixteen segment display // _ _ //|\|/| // - - //|/|\| // - - // // 0xF000 = X // 0x0F00 = + // 0x00F0 = top half of O // 0x000F = bottom half of O chars.put("0", 0x50FF); chars.put("1", 0x0011); chars.put("2", 0x057E); chars.put("3", 0x0577); chars.put("4", 0x0591); chars.put("5", 0x21E6); chars.put("6", 0x05EF); chars.put("7", 0x0071); chars.put("8", 0x05FF); chars.put("9", 0x05F1); chars.put("A", 0x05F9); chars.put("B", 0x0E77); chars.put("C", 0x00EE); chars.put("D", 0x0A77); chars.put("E", 0x05EE); chars.put("F", 0x05E8); chars.put("G", 0x04EF); chars.put("H", 0x0599); chars.put("I", 0x0A66); chars.put("J", 0x001F); chars.put("K", 0x6188); chars.put("L", 0x008E); chars.put("M", 0xC099); chars.put("N", 0xA099); chars.put("O", 0x00FF); chars.put("P", 0x05F8); chars.put("Q", 0x20FF); chars.put("R", 0x25F8); chars.put("S", 0x05E7); chars.put("T", 0x0A60); chars.put("U", 0x009F); chars.put("V", 0x5088); chars.put("W", 0x3099); chars.put("X", 0xF000); chars.put("Y", 0xC200); chars.put("Z", 0x5066); chars.put("a", 0x030E); chars.put("b", 0x038C); chars.put("c", 0x010C); chars.put("d", 0x0B0C); chars.put("e", 0x110C); chars.put("f", 0x4600); chars.put("g", 0x2403); chars.put("h", 0x0388); chars.put("i", 0x0200); chars.put("j", 0x0204); chars.put("k", 0x2E00); chars.put("l", 0x0202); chars.put("m", 0x0709); chars.put("n", 0x2201); chars.put("o", 0x030C); chars.put("p", 0x1108); chars.put("q", 0x1308); chars.put("r", 0x0600); chars.put("s", 0x2402); chars.put("t", 0x0700); chars.put("u", 0x020C); chars.put("v", 0x1008); chars.put("w", 0x3009); chars.put("x", 0x3500); chars.put("y", 0x9800); chars.put("z", 0x1104); chars.put(" ", 0x0000); chars.put("-", 0x0500); chars.put("+", 0x0F00); chars.put("_", 0x0006); chars.put("/", 0x5000); chars.put("\\",0xA000); chars.put("*", 0xFF00); chars.put("%", 0x5FC3); chars.put("(", 0x0A22); chars.put(")", 0x0A44); chars.put("[", 0x00EE); chars.put("]", 0x0077); chars.put("<", 0x6000); chars.put(">", 0x9000); chars.put(".", 0x130C); chars.put(",", 0x1000); chars.put("\"",0x4000); chars.put("?", 0x0670); chars.put("!", 0x0800); chars.put("$", 0x0FE7); chars.put("`", 0x8000); chars.put("=", 0x0560); chars.put("{", 0x0B22); chars.put("}", 0x0E44); chars.put("|", 0x0A00); chars.put("'", 0x0090); chars.put(":", 0x0280); chars.put(";", 0x1800); chars.put("&", 0x2DCF); chars.put("#", 0x0A99); chars.put("@", 0x0DFE); chars.put("~", 0x0518); chars.put("^", 0x00F0); } void character(char c, float x, float y, float scalez) { int map; map = chars.get(c); if(map != null) { pushMatrix(); translate(x,y); scale(scalez); if(map & 0xF000) { if(map & 0x8000) { line(0,-4,1,-2); } if(map & 0x4000) { line(2,-4,1,-2); } if(map & 0x2000) { line(2,-0,1,-2); } if(map & 0x1000) { line(0,-0,1,-2); } } if(map & 0xF00) { if(map & 0x800) { line(1,-4,1,-2); } if(map & 0x400) { line(2,-2,1,-2); } if(map & 0x200) { line(1,-0,1,-2); } if(map & 0x100) { line(0,-2,1,-2); } } if(map & 0xF0) { if(map & 0x80) { line(0,-2,0,-4); } if(map & 0x40) { line(0,-4,1,-4); } if(map & 0x20) { line(1,-4,2,-4); } if(map & 0x10) { line(2,-4,2,-2); } } if(map & 0xF) { if(map & 0x8) { line(0,-0,0,-2); } if(map & 0x4) { line(0,-0,1,-0); } if(map & 0x2) { line(1,-0,2,-0); } if(map & 0x1) { line(2,-0,2,-2); } } popMatrix(); } } void text(String str, float x, float y, float scalez) { float ix = x; for(int i=0; i < str.length(); i++) { if(str.charAt(i) == "\n") { x = ix; y += scalez * charHeight; } else { character(str.charAt(i), x, y, scalez); x += (charWidth * scalez); } } } } class Landscape { static int FLOOR = 1; static int CEILING = 3; static float STEP = 0.005; static float RIDGE_STEP = 0.001; static float SCALE; static int LEFT = -1; static int RIGHT = 1; static float WSCALE = 1.0; int[] points; int position = 0; int index = 0; float ridges = 0; int orientation; Landscape(int length, int ori) { length = floor(length / WSCALE); orientation = ori; if(orientation == Landscape.CEILING) { position += length; } SCALE = height * .75; points = new int[length]; for(int i=0; i < length; i++) { points[i] = int(floor(noise(position * STEP) * SCALE * abs(sin(position * RIDGE_STEP)))); position++; } } void scroll(int direction, int amount) { for(int i=0; i < amount; i++) { if(direction == LEFT) { index--; if(index < 0) { index = points.length - 1; } position--; points[index] = int(floor(noise((position - points.length) * STEP) * SCALE * abs(sin((position - points.length) * RIDGE_STEP)))); } else { position++; points[index] = int(floor(noise(position * STEP) * SCALE * abs(sin(position * RIDGE_STEP)))); index = (index + 1) % points.length; } } } int verticalAt(int x) { int p; x = floor(x / WSCALE); if(x < (points.length - index)) { p = points[x+index]; } else { p = points[x - (points.length - index)]; } if(orientation == Landscape.FLOOR) { return height - p; } else { return p; } } void draw() { pushMatrix(); scale(WSCALE, 1.0); stroke(0); //strokeWeight(WSCALE); for(int i=index; i < points.length; i++) { if(orientation == Landscape.FLOOR) { line(i-index, height, i - index, height - points[i]); } else { line(i-index, 0, i - index, points[i]); } } for(int i=0; i < index; i++) { if(orientation == Landscape.FLOOR) { line((points.length - index) + i, height, (points.length - index) + i, height - points[i]); } else { line((points.length - index) + i, 0, (points.length - index) + i, points[i]); } } popMatrix(); } }
0 notes
Text
Why spin, yarn?
int blades = 100; int bladesPer = 10; Anemone[] anemone = new Anemone[int(floor(blades/bladesPer))]; void setup() { size(); smooth(); frameRate(60); reset(); colorMode(HSB); } void reset() { for(int i=0; i < blades/bladesPer; i++) { int x = random(width); int y = random(height); int hue = random(256); anemone[i] = new Anemone(x, y, hue, bladesPer); } } void mouseClicked() { reset(); } void draw() { background(0); for(int i=0; i < blades/bladesPer; i++) { int xDiff = random(2) - random(2); int yDiff = random(2) - random(2); anemone[i].draw(); anemone[i].update(); anemone[i].x += xDiff; anemone[i].y += yDiff; } } class Anemone { GrassBlade[] blade; float x, y; float rotation; int rotationCount; float angle; float rotSpeed = 0.2; int rotFrames = 60; float magnificationSpeed = 0.02; int magnificationFrames = 10; float magnification; int magnificationCount; float magnificationChange; int h; Anemone(float ix, float iy, int hue, int bladesPer) { h = hue; x = ix; y = iy; rotation = rotSpeed * (random() - random()); angle = 0; rotationCount = 0; magnificationChange = magnificationSpeed * (random() - random()); magnification = 1.0; magnificationCount = 0; blade = new GrassBlade[bladesPer]; for(int j=0; j < bladesPer; j++) { blade[j] = new GrassBlade(height/4 + (10 - random(20))); } } void update() { for(int i=0; i < blade.length; i++) { blade[i].update(); } } void draw() { pushMatrix(); noFill(); strokeWeight(14.0); if((++rotationCount % rotFrames) == 0) { rotation = rotSpeed * (random() - random()); } angle += rotation; if((++magnificationCount % magnificationFrames) == 0) { magnificationChange = magnificationSpeed * (random() - random()); } magnification += magnificationChange; translate(x, y); rotate(angle); scale(magnification); for(int i=0; i < blade.length; i++) { stroke(h, 255, (256 - bladesPer) + i, 255 * magnification); blade[i].draw(); } popMatrix(); } } class GrassBlade { float cx1, cy1, cx2, cy2, x2, y2; float cyMouse, cxMouse; GrassBlade(float length) { cyMouse = cxMouse = 0; cx1 = random(length/4) - random(length/4); cy1 = random(length/4) - random(length/4); cx2 = random(length*2) - random(length*2); cy2 = random(length*2) - random(length*2); PVector randVec = new PVector(random() - random(), random() - random()); randVec.normalize(); randVec.mult(length); x2 = randVec.x; y2 = randVec.y; } void update() { cyMouse += 2 - random(4); //mouseY - (height/2.0); cxMouse += 2 - random(4); //mouseX - (width/2.0); } void draw() { bezier(0, 0, cx1, cy1, cx2, cy2 + cyMouse, x2 + cxMouse, y2 - cyMouse); } }
0 notes
Text
They're coming for you.
int blades = 200; int bladesPer = 40; GrassBlade[] blade = new GrassBlade[blades]; void setup() { size(); smooth(); frameRate(30); reset(); colorMode(HSB); } void reset() { for(int i=0; i < blades/bladesPer; i++) { int x = random(width); int y = random(height); int hue = random(256); for(int j=0; j < bladesPer; j++) { blade[(i*bladesPer) + j] = new GrassBlade(x,y, height/4 + (10 - random(20)), hue, j); } } } void mouseClicked() { reset(); } void draw() { background(0); for(int i=0; i < blades/bladesPer; i++) { int xDiff = random(2) - random(2); int yDiff = random(2) - random(2); for(int j=0; j < bladesPer; j++) { blade[(i*bladesPer) + j].draw(); blade[(i*bladesPer) + j].update(); blade[(i*bladesPer) + j].x += xDiff; blade[(i*bladesPer) + j].y += yDiff; } } } class GrassBlade { float x, y, cx1, cy1, cx2, cy2, x2, y2; float cyMouse, cxMouse; int b, h; float rotation; int rotationCount; float angle; float rotSpeed = 0.02; int rotFrames = 60; float magnificationSpeed = 0.2; int magnificationFrames = 300; float magnification; int magnificationCount; float magnificationChange; GrassBlade(float ix, float iy, float length, int hue, int order) { cyMouse = cxMouse = 0; h = hue; b = (256 - bladesPer) + order; x = ix; y = iy; rotation = rotSpeed * (random() - random()); angle = 0; rotationCount = 0; magnificationChange = magnificationSpeed * (random() - random()); magnification = 1.0; magnificationCount = 0; cx1 = random(length/4) - random(length/4); cy1 = random(length/4) - random(length/4); cx2 = random(length*2) - random(length*2); cy2 = random(length*2) - random(length*2); PVector randVec = new PVector(random() - random(), random() - random()); randVec.normalize(); randVec.mult(length); x2 = randVec.x; y2 = randVec.y; } void update() { cyMouse += 2 - random(4); //mouseY - (height/2.0); cxMouse += 2 - random(4); //mouseX - (width/2.0); } void draw() { noFill(); strokeWeight(14.0); stroke(h, 255, b, 227); pushMatrix(); if((++rotationCount % rotFrames) == 0) { rotation = rotSpeed * (random() - random()); } angle += rotation; if((++magnificationCount % magnificationFrames) == 0) { magnificationChange = magnificationSpeed * (random() - random()); } magnification += magnificationChange; translate(x, y); rotate(angle); scale(magnification); bezier(0, 0, cx1, cy1, cx2, cy2 + cyMouse, x2 + cxMouse, y2 - cyMouse); popMatrix(); } }
0 notes
Text
Disturbingly Jiggly Anemone
int blades = 200; int bladesPer = 40; GrassBlade[] blade = new GrassBlade[blades]; void setup() { size(); smooth(); frameRate(30); for(int i=0; i < blades/bladesPer; i++) { int x = random(width); int y = random(height); int hue = random(256); for(int j=0; j < bladesPer; j++) { blade[(i*bladesPer) + j] = new GrassBlade(x,y, height/4 + (10 - random(20)), hue, j); } } colorMode(HSB); } void draw() { background(0); for(int i=0; i < blades/bladesPer; i++) { int xDiff = random(2) - random(2); int yDiff = random(2) - random(2); for(int j=0; j < bladesPer; j++) { blade[(i*bladesPer) + j].draw(); blade[(i*bladesPer) + j].update(); blade[(i*bladesPer) + j].x += xDiff; blade[(i*bladesPer) + j].y += yDiff; } } } class GrassBlade { float x, y, cx1, cy1, cx2, cy2, x2, y2; float cyMouse, cxMouse; int b, h; float rotation; int rotationCount; float angle; float rotSpeed = 0.02; int rotFrames = 60; GrassBlade(float ix, float iy, float length, int hue, int order) { cyMouse = cxMouse = 0; h = hue; b = (256 - bladesPer) + order; x = ix; y = iy; rotation = random(rotSpeed) - random(rotSpeed); angle = 0; rotationCount = 0; cx1 = random(length/4) - random(length/4); cy1 = random(length/4) - random(length/4); cx2 = random(length*2) - random(length*2); cy2 = random(length*2) - random(length*2); PVector randVec = new PVector(random() - random(), random() - random()); randVec.normalize(); randVec.mult(length); x2 = randVec.x; y2 = randVec.y; } void update() { cyMouse += 2 - random(4); //mouseY - (height/2.0); cxMouse += 2 - random(4); //mouseX - (width/2.0); } void draw() { noFill(); strokeWeight(14.0); stroke(h, 255, b, 227); pushMatrix(); if((++rotationCount % rotFrames) == 0) { rotation = random(rotSpeed) - random(rotSpeed); } angle += rotation; translate(x, y); rotate(angle); bezier(0, 0, cx1, cy1, cx2, cy2 + cyMouse, x2 + cxMouse, y2 - cyMouse); popMatrix(); } }
0 notes
Text
Squiggles
int flakes = 300; ArrayList crystal = new ArrayList(); void setup() { size(); smooth(); frameRate(60); background(0); for(int i=0; i < flakes; i++) { crystal.add(new Flake(random(width), random(height), crystal, i)); } } void draw() { for(int i=0; i < crystal.size(); i++) { crystal.get(i).update(); crystal.get(i).draw(); } } class Flake { ArrayList otherFlake; PVector start, current, last; int index; float r, g, b; boolean alive; int minDist = 20; int moveSpeed = 4; float ax, ay; int counter = 0; Flake(float ix, float iy, ArrayList iFlakes, int i) { alive = true; otherFlake = iFlakes; start = new PVector(ix, iy); current = new PVector(ix, iy); last = new PVector(ix, iy); index = i; r = 100 + random(155); g = 100 + random(155); b = 100 + random(155); fill(r, g, b); strokeWeight(1.0); stroke(r, g, b); ellipse(current.x, current.y, 2, 2); ax = random(moveSpeed) - random(moveSpeed); ay = random(moveSpeed) - random(moveSpeed); counter = 0; } void die() { alive = false; current.x = start.x current.y = start.y; r=g=b=0; } void update() { for(int i=0; alive & i < otherFlake.size(); i++) { if(i != index & otherFlake.get(i).alive & current.dist(otherFlake.get(i).start) < minDist) { //otherFlake.get(i).die(); current.x = start.x; current.y = start.y; } } last.x = current.x; last.y = current.y; if((++counter % 30) == 0) { ax = random(moveSpeed) - random(moveSpeed); ay = random(moveSpeed) - random(moveSpeed); } current.x += ax; current.y += ay; } void draw() { stroke(r, g, b); line(last.x, last.y, current.x, current.y); } }
0 notes
Text
Spinning Anemone
int blades = 200; int bladesPer = 40; float rotation; int rotationCount; float angle; GrassBlade[] blade = new GrassBlade[blades]; void setup() { size(); smooth(); frameRate(30); rotation = random(0.2) - random(0.2); angle = 0; rotationCount = 0; for(int i=0; i < blades/bladesPer; i++) { int x = random(width/2) - random(width/2); int y = random(height/2) - random(height/2); int hue = random(256); for(int j=0; j < bladesPer; j++) { blade[(i*bladesPer) + j] = new GrassBlade(x,y, height/4 + (10 - random(20)), hue, j); } } colorMode(HSB); } void draw() { background(0); translate(width/2, height/2); if((++rotationCount % 10) == 0) { rotation = random(0.2) - random(0.2); } angle += rotation; rotate(angle); for(int i=0; i < blades/bladesPer; i++) { int xDiff = random(2) - random(2); int yDiff = random(2) - random(2); for(int j=0; j < bladesPer; j++) { blade[(i*bladesPer) + j].draw(); blade[(i*bladesPer) + j].update(); blade[(i*bladesPer) + j].x += xDiff; blade[(i*bladesPer) + j].y += yDiff; } } } class GrassBlade { float x, y, cx1, cy1, cx2, cy2, x2, y2; float cyMouse, cxMouse; int b, h; GrassBlade(float ix, float iy, float length, int hue, int order) { cyMouse = cxMouse = 0; h = hue; b = (256 - bladesPer) + order; x = ix; y = iy; cx1 = x + random(length/4) - random(length/4); cy1 = y + random(length/4) - random(length/4); cx2 = x + random(length*2) - random(length*2); cy2 = y + random(length*2) - random(length*2); PVector randVec = new PVector(random() - random(), random() - random()); randVec.normalize(); randVec.mult(length); x2 = x + randVec.x; y2 = y + randVec.y; } void update() { cyMouse += 2 - random(4); //mouseY - (height/2.0); cxMouse += 2 - random(4); //mouseX - (width/2.0); } void draw() { noFill(); strokeWeight(14.0); stroke(h, 255, b, 227); bezier(x, y, cx1, cy1, cx2, cy2 + cyMouse, x2 + cxMouse, y2 - cyMouse); } }
0 notes
Text
Cave Dodger
// Music by Rob Beschizza available at https://www.boingboing.net/2011/03/28/game-deaths-mp3.html String COPYRIGHT = 'Cave Hunter version 0,2 Copyright (C) 2011 by Bill Bereza'; SegmentFont font; Game game; var songs = 1; var[] sounds = new var[songs]; soundManager.onready(function(){ for(int i=0; i <= songs; i++) { sounds[i] = soundManager.createSound({ id: 'track'+i, url: 'https://klasl.com/scripts/mp3/cavehunter'+i+'.mp3', autoPlay: false, autoLoad: false }); } }); function loopSound(i) { sounds[i].play({ onfinish: function() { loopSound(int(i+1) % songs); } }); } void setup() { size(); frameRate(30); font = new SegmentFont(); game = new CaveHunter(); reset(); background(0); loopSound(0); } void draw() { background(255); game.draw(); if(game.over()) { drawInstructions('CLICK MOUSE TO PLAY AGAIN'); drawFlasher('GAME OVER'); } else if(focused) { game.update(); } else if(!focused) { drawPauseScreen(); } colorMode(HSB); drawScore(); } void mouseClicked() { if(game.over()) { reset(); } } void keyPressed() { game.keyPressed(); } void keyReleased() { game.keyReleased(); } void reset() { game.reset(); } class Ship extends Sprite { int gun; int x, y; int newCount; int upDownMove; int leftRightMove; boolean crashed; int length = 40; Ship() { super(); reset(); } void reset() { gun = 0; x = int(floor(width/4)); y = int(floor(height/2)); newCount = 30; upDownMove = 0; leftRightMove = 0; crashed = false; } int upDownMotion() { return upDownMove; } int leftRightMotion() { return leftRightMove; } void setMotion(int leftRight, int upDown) { if(!crashed) { upDownMove = upDown; leftRightMove = leftRight; } } void update() { x += leftRightMove; if(x < length) { x = length; } if(x > width) { x = width; } y += upDownMove; if(y < 0) { y = 0; } if(y > height) { y = height; } } void crash() { newCount = 30; crashed = true; } void draw() { colorMode(RGB); stroke(0,0,255); strokeWeight(2); if(crashed) { if(newCount > 0) { newCount = newCount - 1; if(newCount % 2) { fill(255,127,0); ellipse(x, y, 40, 40); } } else { reset(); } } else { // not crashed if(newCount > 0) { newCount = newCount - 1; if(newCount % 2) { fill(0, 255, 0); } else { fill(255,255,255,1); } } else { fill(0,255,0); } if(upDownMotion() == 0) { triangle(x, y, x-40, y+20, x-40, y-20); } else if(upDownMotion() < 0) { triangle(x, y, x-45, y+15, x-35, y-15); } else if(upDownMotion() > 0) { triangle(x, y, x-35, y+15, x-45, y-15); } if(leftRightMotion() > 0) { fill(255,127,0); triangle(x-60, y, x-40, y+10, x-40, y-10); } else if(leftRightMotion() < 0) { fill(255, 127, 0); triangle(x, y, x-20, y+10, x+5, y+15); triangle(x, y, x-20, y-10, x+5, y-15); } fill(255,0,0); ellipse(x-15, y, 10, 10); } } boolean collisionWith(int ox, oy) { return (ox >= x-40 && ox <= x && oy >= y-20 && oy <= y+20); } boolean isCrashed() { return crashed; } boolean finished() { return false; } int xLeft() { return x-40; } int yTop() { return y-20; } int xRight() { return x; } int yBottom() { return y+20; } } class Dancer extends Bullet { int dist = 10; int count = 0; Dancer(int ix, int iy) { super(color(0,255,0), ix, iy, -8, 0); radius = dist * 2; ay = random(dist) - random(dist); } void update() { if((++count % dist) == 0) { ay = random(dist) - random(dist); } super.update(); } int kind() { return 2; } } class Bullet extends Sprite { color c; int ax, ay; int x, y; boolean crashed; int crashCount; int radius; Bullet(color ci, int ix, int iy, int vx, int vy) { c = ci; x = ix; y = iy; ax = vx; ay = vy; crashed = false; crashCount = 0; radius = 4; } int kind() { return 666; } void draw() { colorMode(RGB); stroke(c); strokeWeight(radius); point(x, y); if(crashed && (crashCount-- % 2 == 0)) { stroke(255,0,0); strokeWeight(radius * 2); point(x, y); } } void update() { x += ax; y += ay; if(x > width || x < 0 || y > height || y < 0) { crashed = true; crashCount = 0; } } void setMotion(int ix, int iy) { ax = ix; ay = iy; } void crash() { crashed = true; crashCount = 10; } boolean isCrashed() { return crashed; } boolean finished() { return (crashed && crashCount <= 0); } int xLeft() { return (x - (radius/2)); } int xRight() { return (x + (radius/2)); } int yTop() { return (y - (radius/2)); } int yBottom() { return (y + (radius/2)); } } class Sprite { Sprite() { } int kind() { return 0; } void setMotion(int ix, int iy) { } void draw() { } void update() { } void crash() { } boolean isCrashed() { return false; } boolean finished() { } int xLeft() { } int yTop() { } int xRight() { } int yBottom() { } boolean overlaps(Sprite o) { return (xLeft() < o.xRight() && xRight() > o.xLeft() && yTop() < o.yBottom() && yBottom() > o.yTop()); } } class CaveHunter extends Game { Landscape landscape; Landscape ceiling; Ship ship; ArrayList sprites; int shipSpeed = 5; int scrollSpeed = 8; int ships; int points; CaveHunter() { reset(); } void reset() { sprites = new ArrayList(); landscape = new Landscape(width, Landscape.FLOOR); ceiling = new Landscape(width, Landscape.CEILING); ship = new Ship(); ships = 5; sprites.add(ship); points = 0; } String name() { return 'Cave Hunter'; } String instructions() { return 'USE THE ARROW KEYS TO MOVE YOUR SHIP AROUND.\nPRESS CONTROL KEY TO FIRE YOUR CANNON.\nPICK UP THE POWERUPS TO ENHANCE YOUR FIREPOWER.'; } void keyPressed() { if(key == CODED) { if(keyCode == UP) { ship.setMotion(0, -shipSpeed); } else if(keyCode == DOWN) { ship.setMotion(0, shipSpeed); } else if(keyCode == LEFT) { ship.setMotion(-shipSpeed, 0); } else if(keyCode == RIGHT) { ship.setMotion(shipSpeed, 0); } else if(keyCode == CONTROL) { sprites.add(new Bullet(color(0,0,255), ship.x + 4, ship.y, 20, 0)); } } } void keyReleased() { ship.setMotion(0,0); } int lives() { return ships; } boolean over() { return lives() == 0; } void update() { landscape.scroll(Landscape.RIGHT, scrollSpeed); ceiling.scroll(Landscape.RIGHT, scrollSpeed); if(random(100) >= 95) { sprites.add(new Dancer(width-1, height/2)); } for(int i=sprites.size() - 1; i >= 0; i--) { Sprite sprite = (Sprite) sprites.get(i); if(sprite.finished()) { sprites.remove(i); } else { sprite.update(); } } checkCollisions(); } void draw() { landscape.draw(); ceiling.draw(); for(int i=0; i < sprites.size(); i++) { Sprite sprite = (Sprite) sprites.get(i); sprite.draw(); } } int score() { return points; } void checkCollisions() { for(int i=0; i < sprites.size(); i++) { Sprite a = (Sprite) sprites.get(i); if(!a.isCrashed()) { for(int j=i+1; j < sprites.size(); j++) { Sprite b = (Sprite) sprites.get(j); if(!b.isCrashed() && a.overlaps(b)) { a.crash(); b.crash(); if(a == ship || b == ship) { ships--; } else if(a.kind() == 666 || b.kind() == 666) { points-=1000; } } } } if(!a.isCrashed()) { if(a.yBottom() > landscape.verticalAt(a.xRight()) || a.yTop() < ceiling.verticalAt(a.xRight())) { a.setMotion(-scrollSpeed, 0); a.crash(); if(a == ship) { ships--; } } } } points++; if((points % 1000) == 0) { ships++; } } } float lastMillis = 0; void drawScore() { float nowMillis = millis(); float fps = 1000/(nowMillis - lastMillis); lastMillis = nowMillis; float textScale = 4; strokeWeight(0.75); colorMode(RGB); stroke(0, random(55) + 200, 0); font.text("SCORE> " + game.score() + " LIVES> " + game.lives() + " FPS> " + floor(fps), 8, (font.charHeight*textScale + 4), textScale); } float titleScale[] = { 2, 1.8, 1.6, 1.4, 1.2, 1.0, 1.2, 1.4, 1.6, 1.8, 2 }; void drawFlasher(String title) { float titleSize=16; colorMode(HSB, title.length(), 255, 255); strokeWeight(0.25); for(int i=0; i < title.length(); i++) { stroke(i, 255, random(55) + 200); font.character(title.charAt(i), (width - (title.length()*font.charWidth*titleSize)) / 2.0 + i*font.charWidth*titleSize, height/2.5, titleSize*sin(titleScale[i])); titleScale[i] += 0.2; } colorMode(RGB,255); } void drawInstructions(String instr) { float instSize = 5; stroke(255,0,0); strokeWeight(0.5 + random(0.25)); font.text(instr, (width - (instr.length()*font.charWidth*instSize))/2.0, height/2, instSize); } void drawCopyright(String copyr) { float copySize = 3; String instructions = "\n\n" + game.instructions(); stroke(0,255,0); strokeWeight(0.25 + random(0.25)); font.text(copyr + instructions, (width - (copyr.length()*font.charWidth*copySize))/2.0, height - height/2.5, copySize); } void drawPauseScreen() { String title = game.name(); String instr = 'CLICK IN THE WINDOW TO PLAY'; drawInstructions(instr); drawFlasher(title); drawCopyright(COPYRIGHT); } class Game { boolean over() { return false; } void update() { } void draw() { } void reset() { } void keyPressed() { } void keyReleased() { } int score() { return 0; } int level() { return 0; } int lives() { return 0; } String instructions() { return ""; } String name() { return "Game"; } } class SegmentFont { int charWidth = 4; int charHeight = 6; HashMap chars; SegmentFont() { chars = new HashMap(); // char pattern // sixteen segment display // _ _ //|\|/| //- - //|/|\| // - - // // 0xF000 = X // 0x0F00 = + // 0x00F0 = top half of O // 0x000F = bottom half of O chars.put("0", 0x50FF); chars.put("1", 0x0011); chars.put("2", 0x057E); chars.put("3", 0x0577); chars.put("4", 0x0591); chars.put("5", 0x21E6); chars.put("6", 0x05EF); chars.put("7", 0x0071); chars.put("8", 0x05FF); chars.put("9", 0x05F1); chars.put("A", 0x05F9); chars.put("B", 0x0E77); chars.put("C", 0x00EE); chars.put("D", 0x0A77); chars.put("E", 0x05EE); chars.put("F", 0x05E8); chars.put("G", 0x04EF); chars.put("H", 0x0599); chars.put("I", 0x0A66); chars.put("J", 0x001F); chars.put("K", 0x6188); chars.put("L", 0x008E); chars.put("M", 0xC099); chars.put("N", 0xA099); chars.put("O", 0x00FF); chars.put("P", 0x05F8); chars.put("Q", 0x20FF); chars.put("R", 0x25F8); chars.put("S", 0x05E7); chars.put("T", 0x0A60); chars.put("U", 0x009F); chars.put("V", 0x5088); chars.put("W", 0x3099); chars.put("X", 0xF000); chars.put("Y", 0xC200); chars.put("Z", 0x5066); chars.put("a", 0x030E); chars.put("b", 0x038C); chars.put("c", 0x010C); chars.put("d", 0x0B0C); chars.put("e", 0x110C); chars.put("f", 0x4600); chars.put("g", 0x2403); chars.put("h", 0x0388); chars.put("i", 0x0200); chars.put("j", 0x0204); chars.put("k", 0x2E00); chars.put("l", 0x0202); chars.put("m", 0x0709); chars.put("n", 0x2201); chars.put("o", 0x030C); chars.put("p", 0x1108); chars.put("q", 0x1308); chars.put("r", 0x0600); chars.put("s", 0x2402); chars.put("t", 0x0700); chars.put("u", 0x020C); chars.put("v", 0x1008); chars.put("w", 0x3009); chars.put("x", 0x3500); chars.put("y", 0x9800); chars.put("z", 0x1104); chars.put(" ", 0x0000); chars.put("-", 0x0500); chars.put("+", 0x0F00); chars.put("_", 0x0006); chars.put("/", 0x5000); chars.put("\\",0xA000); chars.put("*", 0xFF00); chars.put("%", 0x5FC3); chars.put("(", 0x0A22); chars.put(")", 0x0A44); chars.put("[", 0x00EE); chars.put("]", 0x0077); chars.put("<", 0x6000); chars.put(">", 0x9000); chars.put(".", 0x130C); chars.put(",", 0x1000); chars.put("\"",0x4000); chars.put("?", 0x0670); chars.put("!", 0x0800); chars.put("$", 0x0FE7); chars.put("`", 0x8000); chars.put("=", 0x0560); chars.put("{", 0x0B22); chars.put("}", 0x0E44); chars.put("|", 0x0A00); chars.put("'", 0x0090); chars.put(":", 0x0280); chars.put(";", 0x1800); chars.put("&", 0x2DCF); chars.put("#", 0x0A99); chars.put("@", 0x0DFE); chars.put("~", 0x0518); chars.put("^", 0x00F0); } void character(char c, float x, float y, float scalez) { int map; map = chars.get(c); if(map != null) { pushMatrix(); translate(x,y); scale(scalez); if(map & 0xF000) { if(map & 0x8000) { line(0,-4,1,-2); } if(map & 0x4000) { line(2,-4,1,-2); } if(map & 0x2000) { line(2,-0,1,-2); } if(map & 0x1000) { line(0,-0,1,-2); } } if(map & 0xF00) { if(map & 0x800) { line(1,-4,1,-2); } if(map & 0x400) { line(2,-2,1,-2); } if(map & 0x200) { line(1,-0,1,-2); } if(map & 0x100) { line(0,-2,1,-2); } } if(map & 0xF0) { if(map & 0x80) { line(0,-2,0,-4); } if(map & 0x40) { line(0,-4,1,-4); } if(map & 0x20) { line(1,-4,2,-4); } if(map & 0x10) { line(2,-4,2,-2); } } if(map & 0xF) { if(map & 0x8) { line(0,-0,0,-2); } if(map & 0x4) { line(0,-0,1,-0); } if(map & 0x2) { line(1,-0,2,-0); } if(map & 0x1) { line(2,-0,2,-2); } } popMatrix(); } } void text(String str, float x, float y, float scalez) { float ix = x; for(int i=0; i < str.length(); i++) { if(str.charAt(i) == "\n") { x = ix; y += scalez * charHeight; } else { character(str.charAt(i), x, y, scalez); x += (charWidth * scalez); } } } } class Landscape { static int FLOOR = 1; static int CEILING = 3; static float STEP = 0.005; static float RIDGE_STEP = 0.001; static float SCALE; static int LEFT = -1; static int RIGHT = 1; static float WSCALE = 1.0; int[] points; int position = 0; int index = 0; float ridges = 0; int orientation; Landscape(int length, int ori) { length = floor(length / WSCALE); orientation = ori; if(orientation == Landscape.CEILING) { position += length; } SCALE = height * .75; points = new int[length]; for(int i=0; i < length; i++) { points[i] = int(floor(noise(position * STEP) * SCALE * abs(sin(position * RIDGE_STEP)))); position++; } } void scroll(int direction, int amount) { for(int i=0; i < amount; i++) { if(direction == LEFT) { index--; if(index < 0) { index = points.length - 1; } position--; points[index] = int(floor(noise((position - points.length) * STEP) * SCALE * abs(sin((position - points.length) * RIDGE_STEP)))); } else { position++; points[index] = int(floor(noise(position * STEP) * SCALE * abs(sin(position * RIDGE_STEP)))); index = (index + 1) % points.length; } } } int verticalAt(int x) { int p; x = floor(x / WSCALE); if(x < (points.length - index)) { p = points[x+index]; } else { p = points[x - (points.length - index)]; } if(orientation == Landscape.FLOOR) { return height - p; } else { return p; } } void draw() { pushMatrix(); scale(WSCALE, 1.0); stroke(0); //strokeWeight(WSCALE); for(int i=index; i < points.length; i++) { if(orientation == Landscape.FLOOR) { line(i-index, height, i - index, height - points[i]); } else { line(i-index, 0, i - index, points[i]); } } for(int i=0; i < index; i++) { if(orientation == Landscape.FLOOR) { line((points.length - index) + i, height, (points.length - index) + i, height - points[i]); } else { line((points.length - index) + i, 0, (points.length - index) + i, points[i]); } } popMatrix(); } }
0 notes
Text
Lines in the Road
Driver drive; void setup() { size(); drive = new Driver(); frameRate(30); } void draw() { drive.draw(); } void mouseClicked() { drive.mouseClicked(); } class Driver { int score; SegmentFont font; Driver() { font = new SegmentFont(); reset(); } void reset() { score = 0; } void mouseClicked() { } void drawScore() { float textScale = 4; strokeWeight(0.75); stroke(0, random(55) + 200, 0); font.text("SCORE - " + score, width/8, font.charHeight*textScale + 4, textScale); } float titleScale[] = { 2, 1.8, 1.6, 1.4, 1.2, 1.0, 1.2, 1.4, 1.6, 1.8 }; void drawFlasher(String title) { float titleSize=16; colorMode(HSB, title.length(), 255, 255); strokeWeight(0.25); for(int i=0; i < title.length(); i++) { stroke(i, 255, random(55) + 200); font.character(title.charAt(i), (width - (title.length()*font.charWidth*titleSize)) / 2.0 + i*font.charWidth*titleSize, height/2.5, titleSize*sin(titleScale[i])); titleScale[i] += 0.2; } colorMode(RGB,255); } void drawInstructions(String instr) { float instSize = 5; stroke(255,0,0); strokeWeight(0.5 + random(0.25)); font.text(instr, (width - (instr.length()*font.charWidth*instSize))/2.0, height/2, instSize); } void drawCopyright(String copyr) { float copySize = 3; stroke(0,255,0); strokeWeight(0.25 + random(0.25)); font.text(copyr, (width - (copyr.length()*font.charWidth*copySize))/2.0, height - height/2.5, copySize); } void drawPauseScreen() { String title = 'BIZLODRIVE'; String instr = 'CLICK IN THE WINDOW TO PLAY'; drawInstructions(instr); drawFlasher(title); drawCopyright('BizloDrive version 1,0 Copyright (C) 2011 by Bill Bereza'); } int lines = 0; void drawRoad() { strokeWeight(1); stroke(200); fill(20); quad(width/2 - 10, height/4, 10 + width/2, height/4, width - width/4, height, width/4, height); stroke(255); fill(255); lines = (lines + 1) % floor(40); int i = lines; quad(lerp(width/2 - 10, width/4, (i*2)/10.0), lerp(height/4, height, (i*2)/10.0), lerp(width/2 + 10, width/2+width/4, (i*2)/10.0), lerp(height/4, height, (i*2)/10.0), lerp(width/2 + 10, width/2+width/4, ((i*2)+1)/10.0), lerp(height/4, height, ((i*2)+1)/10.0), lerp(width/2 - 10, width/4, ((i*2)+1)/10.0), lerp(height/4, height, ((i*2)+1)/10.0)); } int halfCarWidth = 50; int halfCarHeight = 150; void drawCar() { strokeWeight(1); stroke(random(55) + 200); fill(127); quad(mouseX - halfCarWidth/2, height - halfCarHeight, mouseX + halfCarWidth/2, height - halfCarHeight, mouseX + halfCarWidth, height - halfCarHeight/2, mouseX - halfCarWidth, height - halfCarHeight/2); } void draw() { background(0); if(focused) { drawRoad(); drawCar(); } else { drawPauseScreen(); } drawScore(); } } class SegmentFont { int charWidth = 4; int charHeight = 6; HashMap chars; SegmentFont() { chars = new HashMap(); // char pattern // sixteen segment display //_ _ //|\|/| //- - //|/|\| //- - // // 0xF000 = X // 0x0F00 = + // 0x00F0 = top half of O // 0x000F = bottom half of O chars.put("0", 0x50FF); chars.put("1", 0x0011); chars.put("2", 0x057E); chars.put("3", 0x0577); chars.put("4", 0x0591); chars.put("5", 0x21E6); chars.put("6", 0x05EF); chars.put("7", 0x0071); chars.put("8", 0x05FF); chars.put("9", 0x05F1); chars.put("A", 0x05F9); chars.put("B", 0x0E77); chars.put("C", 0x00EE); chars.put("D", 0x0A77); chars.put("E", 0x05EE); chars.put("F", 0x05E8); chars.put("G", 0x04EF); chars.put("H", 0x0599); chars.put("I", 0x0A66); chars.put("J", 0x001F); chars.put("K", 0x6188); chars.put("L", 0x008E); chars.put("M", 0xC099); chars.put("N", 0xA099); chars.put("O", 0x00FF); chars.put("P", 0x05F8); chars.put("Q", 0x20FF); chars.put("R", 0x25F8); chars.put("S", 0x05E7); chars.put("T", 0x0A60); chars.put("U", 0x009F); chars.put("V", 0x5088); chars.put("W", 0x3099); chars.put("X", 0xF000); chars.put("Y", 0xC200); chars.put("Z", 0x5066); chars.put("a", 0x030E); chars.put("b", 0x038C); chars.put("c", 0x010C); chars.put("d", 0x0B0C); chars.put("e", 0x110C); chars.put("f", 0x4600); chars.put("g", 0x2403); chars.put("h", 0x0388); chars.put("i", 0x0200); chars.put("j", 0x0204); chars.put("k", 0x2E00); chars.put("l", 0x0202); chars.put("m", 0x0709); chars.put("n", 0x2201); chars.put("o", 0x030C); chars.put("p", 0x1108); chars.put("q", 0x1308); chars.put("r", 0x0600); chars.put("s", 0x2402); chars.put("t", 0x0700); chars.put("u", 0x020C); chars.put("v", 0x1008); chars.put("w", 0x3009); chars.put("x", 0x3500); chars.put("y", 0x9800); chars.put("z", 0x1104); chars.put(" ", 0x0000); chars.put("-", 0x0500); chars.put("+", 0x0F00); chars.put("_", 0x0006); chars.put("/", 0x5000); chars.put("\\",0xA000); chars.put("*", 0xFF00); chars.put("%", 0x5FC3); chars.put("(", 0x0A22); chars.put(")", 0x0A44); chars.put("[", 0x00EE); chars.put("]", 0x0077); chars.put("<", 0x6000); chars.put(">", 0x9000); chars.put(".", 0x130C); chars.put(",", 0x1000); chars.put("\"",0x4000); chars.put("?", 0x0670); chars.put("!", 0x0800); chars.put("$", 0x0FE7); chars.put("`", 0x8000); chars.put("=", 0x0560); chars.put("{", 0x0B22); chars.put("}", 0x0E44); chars.put("|", 0x0A00); chars.put("'", 0x0090); chars.put(":", 0x0280); chars.put(";", 0x1800); chars.put("&", 0x2DCF); chars.put("#", 0x0A99); chars.put("@", 0x0DFE); chars.put("~", 0x0518); chars.put("^", 0x00F0); } void character(char c, float x, float y, float scalez) { int map; map = chars.get(c); if(map != null) { pushMatrix(); translate(x,y); scale(scalez); if(map & 0xF000) { if(map & 0x8000) { line(0,-4,1,-2); } if(map & 0x4000) { line(2,-4,1,-2); } if(map & 0x2000) { line(2,-0,1,-2); } if(map & 0x1000) { line(0,-0,1,-2); } } if(map & 0xF00) { if(map & 0x800) { line(1,-4,1,-2); } if(map & 0x400) { line(2,-2,1,-2); } if(map & 0x200) { line(1,-0,1,-2); } if(map & 0x100) { line(0,-2,1,-2); } } if(map & 0xF0) { if(map & 0x80) { line(0,-2,0,-4); } if(map & 0x40) { line(0,-4,1,-4); } if(map & 0x20) { line(1,-4,2,-4); } if(map & 0x10) { line(2,-4,2,-2); } } if(map & 0xF) { if(map & 0x8) { line(0,-0,0,-2); } if(map & 0x4) { line(0,-0,1,-0); } if(map & 0x2) { line(1,-0,2,-0); } if(map & 0x1) { line(2,-0,2,-2); } } popMatrix(); } } void text(String str, float x, float y, float scalez) { float ix = x; for(int i=0; i < str.length(); i++) { if(str.charAt(i) == "\n") { x = ix; y += scalez * charHeight; } else { character(str.charAt(i), x, y, scalez); x += (charWidth * scalez); } } } }
0 notes
Text
Cave Exploration
String COPYRIGHT = 'Cave Hunter version 0,1 Copyright (C) 2011 by Bill Bereza'; SegmentFont font; Game game; class Ship { int gun; int x, y; int newCount; int upDownMove; int leftRightMove; boolean crashed; int length = 40; Ship() { reset(); } void reset() { gun = 0; x = int(floor(width/4)); y = int(floor(height/2)); newCount = 30; upDownMove = 0; leftRightMove = 0; crashed = false; } int upDownMotion() { return upDownMove; } int leftRightMotion() { return leftRightMove; } void setMotion(int upDown, int leftRight) { if(!crashed) { upDownMove = upDown; leftRightMove = leftRight; } } void update() { x += leftRightMove; if(x < length) { x = length; } if(x > width) { x = width; } y += upDownMove; if(y < 0) { y = 0; } if(y > height) { y = height; } } void crash() { newCount = 30; crashed = true; } void draw() { colorMode(RGB); stroke(0,0,255); strokeWeight(2); if(crashed) { if(newCount > 0) { newCount = newCount - 1; if(newCount % 2) { fill(255,127,0); ellipse(x, y, 40, 40); } } else { reset(); } } else { // not crashed if(newCount > 0) { newCount = newCount - 1; if(newCount % 2) { fill(0, 255, 0); } else { fill(255,255,255,1); } } else { fill(0,255,0); } if(upDownMotion() == 0) { triangle(x, y, x-40, y+20, x-40, y-20); } else if(upDownMotion() < 0) { triangle(x, y, x-45, y+15, x-35, y-15); } else if(upDownMotion() > 0) { triangle(x, y, x-35, y+15, x-45, y-15); } if(leftRightMotion() > 0) { fill(255,127,0); triangle(x-60, y, x-40, y+10, x-40, y-10); } else if(leftRightMotion() < 0) { fill(255, 127, 0); triangle(x, y, x-20, y+10, x+5, y+15); triangle(x, y, x-20, y-10, x+5, y-15); } fill(255,0,0); ellipse(x-15, y, 10, 10); } } boolean collisionWith(int ox, oy) { return (ox >= x-40 && ox <= x && oy >= y-20 && oy <= y+20); } } class CaveHunter extends Game { Landscape landscape; Landscape ceiling; Ship ship; int shipSpeed = 5; int scrollSpeed = 2; int ships; CaveHunter() { reset(); } void reset() { landscape = new Landscape(width, Landscape.FLOOR); ceiling = new Landscape(width, Landscape.CEILING); ship = new Ship(); ships = 3; } String name() { return 'Cave Hunter'; } String instructions() { return 'Use the arrow keys to move your ship around.'; //\nPress Control key to fire your cannon.\nPick up the powerups to enhance your firepower.'; } void keyPressed() { if(key == CODED) { if(keyCode == UP) { ship.setMotion(-shipSpeed, 0); } else if(keyCode == DOWN) { ship.setMotion(shipSpeed, 0); } else if(keyCode == LEFT) { ship.setMotion(0, -shipSpeed); } else if(keyCode == RIGHT) { ship.setMotion(0, shipSpeed); } } } void keyReleased() { ship.setMotion(0,0); } int lives() { return ships; } boolean over() { return lives() == 0; } void update() { landscape.scroll(Landscape.RIGHT, scrollSpeed); ceiling.scroll(Landscape.RIGHT, scrollSpeed); ship.update(); checkCollisions(); } void draw() { landscape.draw(); ceiling.draw(); ship.draw(); } void checkCollisions() { if(!ship.crashed) { for(int i=0; i < ship.length; i++) { if(ship.collisionWith(ship.x-i, landscape.verticalAt(floor(ship.x - i))) || ship.collisionWith(ship.x-i, ceiling.verticalAt(floor(ship.x - i)))) { ship.setMotion(0, -scrollSpeed); ship.crash(); ships--; break; } } } } } void setup() { size(); frameRate(30); font = new SegmentFont(); game = new CaveHunter(); reset(); background(0); } void draw() { background(255); game.draw(); if(game.over()) { drawInstructions('CLICK MOUSE TO PLAY AGAIN'); drawFlasher('GAME OVER'); } else if(focused) { game.update(); } else if(!focused) { drawPauseScreen(); } colorMode(HSB); drawScore(); } void mouseClicked() { if(game.over()) { reset(); } } void keyPressed() { game.keyPressed(); } void keyReleased() { game.keyReleased(); } void reset() { game.reset(); } float lastMillis = 0; void drawScore() { float nowMillis = millis(); float fps = 1000/(nowMillis - lastMillis); lastMillis = nowMillis; float textScale = 4; strokeWeight(0.75); colorMode(RGB); stroke(0, random(55) + 200, 0); font.text("SCORE> " + game.score() + " LIVES> " + game.lives() + " FPS> " + floor(fps), 8, (font.charHeight*textScale + 4), textScale); } float titleScale[] = { 2, 1.8, 1.6, 1.4, 1.2, 1.0, 1.2, 1.4, 1.6, 1.8, 2 }; void drawFlasher(String title) { float titleSize=16; colorMode(HSB, title.length(), 255, 255); strokeWeight(0.25); for(int i=0; i < title.length(); i++) { stroke(i, 255, random(55) + 200); font.character(title.charAt(i), (width - (title.length()*font.charWidth*titleSize)) / 2.0 + i*font.charWidth*titleSize, height/2.5, titleSize*sin(titleScale[i])); titleScale[i] += 0.2; } colorMode(RGB,255); } void drawInstructions(String instr) { float instSize = 5; stroke(255,0,0); strokeWeight(0.5 + random(0.25)); font.text(instr, (width - (instr.length()*font.charWidth*instSize))/2.0, height/2, instSize); } void drawCopyright(String copyr) { float copySize = 3; String instructions = "\n\n" + game.instructions(); stroke(0,255,0); strokeWeight(0.25 + random(0.25)); font.text(copyr + instructions, (width - (copyr.length()*font.charWidth*copySize))/2.0, height - height/2.5, copySize); } void drawPauseScreen() { String title = game.name(); String instr = 'CLICK IN THE WINDOW TO PLAY'; drawInstructions(instr); drawFlasher(title); drawCopyright(COPYRIGHT); } class Game { boolean over() { return false; } void update() { } void draw() { } void reset() { } void keyPressed() { } void keyReleased() { } int score() { return 0; } int level() { return 0; } int lives() { return 0; } String instructions() { return ""; } String name() { return "Game"; } } class SegmentFont { int charWidth = 4; int charHeight = 6; HashMap chars; SegmentFont() { chars = new HashMap(); // char pattern // sixteen segment display //_ _ //|\|/| //- - //|/|\| //- - // // 0xF000 = X // 0x0F00 = + // 0x00F0 = top half of O // 0x000F = bottom half of O chars.put("0", 0x50FF); chars.put("1", 0x0011); chars.put("2", 0x057E); chars.put("3", 0x0577); chars.put("4", 0x0591); chars.put("5", 0x21E6); chars.put("6", 0x05EF); chars.put("7", 0x0071); chars.put("8", 0x05FF); chars.put("9", 0x05F1); chars.put("A", 0x05F9); chars.put("B", 0x0E77); chars.put("C", 0x00EE); chars.put("D", 0x0A77); chars.put("E", 0x05EE); chars.put("F", 0x05E8); chars.put("G", 0x04EF); chars.put("H", 0x0599); chars.put("I", 0x0A66); chars.put("J", 0x001F); chars.put("K", 0x6188); chars.put("L", 0x008E); chars.put("M", 0xC099); chars.put("N", 0xA099); chars.put("O", 0x00FF); chars.put("P", 0x05F8); chars.put("Q", 0x20FF); chars.put("R", 0x25F8); chars.put("S", 0x05E7); chars.put("T", 0x0A60); chars.put("U", 0x009F); chars.put("V", 0x5088); chars.put("W", 0x3099); chars.put("X", 0xF000); chars.put("Y", 0xC200); chars.put("Z", 0x5066); chars.put("a", 0x030E); chars.put("b", 0x038C); chars.put("c", 0x010C); chars.put("d", 0x0B0C); chars.put("e", 0x110C); chars.put("f", 0x4600); chars.put("g", 0x2403); chars.put("h", 0x0388); chars.put("i", 0x0200); chars.put("j", 0x0204); chars.put("k", 0x2E00); chars.put("l", 0x0202); chars.put("m", 0x0709); chars.put("n", 0x2201); chars.put("o", 0x030C); chars.put("p", 0x1108); chars.put("q", 0x1308); chars.put("r", 0x0600); chars.put("s", 0x2402); chars.put("t", 0x0700); chars.put("u", 0x020C); chars.put("v", 0x1008); chars.put("w", 0x3009); chars.put("x", 0x3500); chars.put("y", 0x9800); chars.put("z", 0x1104); chars.put(" ", 0x0000); chars.put("-", 0x0500); chars.put("+", 0x0F00); chars.put("_", 0x0006); chars.put("/", 0x5000); chars.put("\\",0xA000); chars.put("*", 0xFF00); chars.put("%", 0x5FC3); chars.put("(", 0x0A22); chars.put(")", 0x0A44); chars.put("[", 0x00EE); chars.put("]", 0x0077); chars.put("<", 0x6000); chars.put(">", 0x9000); chars.put(".", 0x130C); chars.put(",", 0x1000); chars.put("\"",0x4000); chars.put("?", 0x0670); chars.put("!", 0x0800); chars.put("$", 0x0FE7); chars.put("`", 0x8000); chars.put("=", 0x0560); chars.put("{", 0x0B22); chars.put("}", 0x0E44); chars.put("|", 0x0A00); chars.put("'", 0x0090); chars.put(":", 0x0280); chars.put(";", 0x1800); chars.put("&", 0x2DCF); chars.put("#", 0x0A99); chars.put("@", 0x0DFE); chars.put("~", 0x0518); chars.put("^", 0x00F0); } void character(char c, float x, float y, float scalez) { int map; map = chars.get(c); if(map != null) { pushMatrix(); translate(x,y); scale(scalez); if(map & 0xF000) { if(map & 0x8000) { line(0,-4,1,-2); } if(map & 0x4000) { line(2,-4,1,-2); } if(map & 0x2000) { line(2,-0,1,-2); } if(map & 0x1000) { line(0,-0,1,-2); } } if(map & 0xF00) { if(map & 0x800) { line(1,-4,1,-2); } if(map & 0x400) { line(2,-2,1,-2); } if(map & 0x200) { line(1,-0,1,-2); } if(map & 0x100) { line(0,-2,1,-2); } } if(map & 0xF0) { if(map & 0x80) { line(0,-2,0,-4); } if(map & 0x40) { line(0,-4,1,-4); } if(map & 0x20) { line(1,-4,2,-4); } if(map & 0x10) { line(2,-4,2,-2); } } if(map & 0xF) { if(map & 0x8) { line(0,-0,0,-2); } if(map & 0x4) { line(0,-0,1,-0); } if(map & 0x2) { line(1,-0,2,-0); } if(map & 0x1) { line(2,-0,2,-2); } } popMatrix(); } } void text(String str, float x, float y, float scalez) { float ix = x; for(int i=0; i < str.length(); i++) { if(str.charAt(i) == "\n") { x = ix; y += scalez * charHeight; } else { character(str.charAt(i), x, y, scalez); x += (charWidth * scalez); } } } } class Landscape { static int FLOOR = 1; static int CEILING = 3; static float STEP = 0.005; static float RIDGE_STEP = 0.001; static float SCALE; static int LEFT = -1; static int RIGHT = 1; int[] points; int position = 0; int index = 0; float ridges = 0; int orientation; Landscape(int length, int ori) { orientation = ori; if(orientation == Landscape.CEILING) { position += width; } SCALE = height * .75; points = new int[length]; for(int i=0; i < length; i++) { points[i] = int(floor(noise(position * STEP) * SCALE * abs(sin(position * RIDGE_STEP)))); position++; } } void scroll(int direction, int amount) { for(int i=0; i < amount; i++) { if(direction == LEFT) { index--; if(index < 0) { index = points.length - 1; } position--; points[index] = int(floor(noise((position - points.length) * STEP) * SCALE * abs(sin((position - points.length) * RIDGE_STEP)))); } else { position++; points[index] = int(floor(noise(position * STEP) * SCALE * abs(sin(position * RIDGE_STEP)))); index = (index + 1) % points.length; } } } int verticalAt(int x) { int p; if(x < (points.length - index)) { p = points[x+index]; } else { p = points[x - (points.length - index)]; } if(orientation == Landscape.FLOOR) { return height - p; } else { return p; } } void draw() { stroke(0); for(int i=index; i < points.length; i++) { if(orientation == Landscape.FLOOR) { line(i-index, height, i - index, height - points[i]); } else { line(i-index, 0, i - index, points[i]); } } for(int i=0; i < index; i++) { if(orientation == Landscape.FLOOR) { line((points.length - index) + i, height, (points.length - index) + i, height - points[i]); } else { line((points.length - index) + i, 0, (points.length - index) + i, points[i]); } } } }
0 notes
Text
Cave
Landscape landscape; Landscape ceiling; void setup() { size(); frameRate(30); landscape = new Landscape(width, Landscape.FLOOR); ceiling = new Landscape(width, Landscape.CEILING); background(255); colorMode(HSB); } void draw() { background(200); landscape.draw(); ceiling.draw(); if(mouseX < width/2) { landscape.scroll(Landscape.LEFT, abs(mouseX - width/2) / 8); ceiling.scroll(Landscape.LEFT, abs(mouseX - width/2) / 8); } else { landscape.scroll(Landscape.RIGHT, abs(mouseX - width/2) / 8); ceiling.scroll(Landscape.RIGHT, abs(mouseX - width/2) / 8); } } class Landscape { static int FLOOR = 1; static int CEILING = 3; static float STEP = 0.005; static float RIDGE_STEP = 0.001; static float SCALE; static int LEFT = -1; static int RIGHT = 1; int[] points; int position = 0; int index = 0; float ridges = 0; int orientation; Landscape(int length, int ori) { orientation = ori; if(orientation == Landscape.CEILING) { position += width; } SCALE = height * .75; points = new int[length]; for(int i=0; i < length; i++) { points[i] = noise(position * STEP) * SCALE * abs(sin(position * RIDGE_STEP)); position++; } } void scroll(int direction, int amount) { for(int i=0; i < amount; i++) { if(direction == LEFT) { index--; if(index < 0) { index = points.length - 1; } position--; points[index] = noise((position - points.length) * STEP) * SCALE * abs(sin((position - points.length) * RIDGE_STEP)); } else { position++; points[index] = noise(position * STEP) * SCALE * abs(sin(position * RIDGE_STEP)); index = (index + 1) % points.length; } } } void draw() { stroke(0); for(int i=index; i < points.length; i++) { if(orientation == Landscape.FLOOR) { line(i-index, height, i - index, height - points[i]); } else { line(i-index, 0, i - index, points[i]); } } for(int i=0; i < index; i++) { if(orientation == Landscape.FLOOR) { line((points.length - index) + i, height, (points.length - index) + i, height - points[i]); } else { line((points.length - index) + i, 0, (points.length - index) + i, points[i]); } } } }
0 notes
Text
Mousescape
Landscape landscape; void setup() { size(); frameRate(30); landscape = new Landscape(width); background(255); colorMode(HSB); } void draw() { background(0); landscape.draw(); if(mouseX < width/2) { landscape.scroll(Landscape.LEFT, abs(mouseX - width/2) / 8); } else { landscape.scroll(Landscape.RIGHT, abs(mouseX - width/2) / 8); } } class Landscape { static float STEP = 0.005; static float RIDGE_STEP = 0.001; static float SCALE; static int LEFT = -1; static int RIGHT = 1; int[] points; int position = 0; int index = 0; float ridges = 0; Landscape(int length) { SCALE = height; points = new int[length]; for(int i=0; i < length; i++) { points[i] = noise(position * STEP) * SCALE * abs(sin(position * RIDGE_STEP)); position++; } } void scroll(int direction, int amount) { for(int i=0; i < amount; i++) { if(direction == LEFT) { index--; if(index < 0) { index = points.length - 1; } position--; points[index] = noise((position - points.length) * STEP) * SCALE * abs(sin((position - points.length) * RIDGE_STEP)); } else { position++; points[index] = noise(position * STEP) * SCALE * abs(sin(position * RIDGE_STEP)); index = (index + 1) % points.length; } } } void draw() { stroke(255); for(int i=index; i < points.length; i++) { line(i-index, height, i - index, height - points[i]); } for(int i=0; i < index; i++) { line((points.length - index) + i, height, (points.length - index) + i, height - points[i]); } } }
0 notes
Text
Landscape
Landscape landscape; void setup() { size(); frameRate(60); landscape = new Landscape(width); background(255); colorMode(HSB); } void draw() { background(0); landscape.draw(); landscape.scroll(Landscape.RIGHT); } class Landscape { static float STEP = 0.005; static float RIDGE_STEP = 0.001; static float SCALE; static int LEFT = -1; static int RIGHT = 1; int[] points; int position = 0; int index = 0; float ridges = 0; Landscape(int length) { SCALE = height; points = new int[length]; for(int i=0; i < length; i++) { points[i] = 0; } } void scroll(int direction) { if(direction == LEFT && position > 0) { index--; if(index < 0) { index = points.length - 1; } position--; points[index] = noise(position * STEP) * SCALE * abs(sin(position * RIDGE_STEP)); } else { position++; points[index] = noise(position * STEP) * SCALE * abs(sin(position * RIDGE_STEP)); index = (index + 1) % points.length; } } void draw() { stroke(255); for(int i=index; i < points.length; i++) { line(i-index, height, i - index, height - points[i]); } for(int i=0; i < index; i++) { line((points.length - index) + i, height, (points.length - index) + i, height - points[i]); } } }
0 notes
Text
Spinning Sickness if You Keep Clicking
int hue = 0; float angle = 0; float diameter = 100; float circleSize = 100; boolean fillCircles = false; int speed = 5; int circles = 0; int rate = 60; int MAX_CIRCLES=1000; int[] circlesX = new int[MAX_CIRCLES]; int[] circlesY = new int[MAX_CIRCLES]; void setup() { size(); frameRate(rate); colorMode(HSB); ellipseMode(CENTER); background(0); addCircle(); } void addCircle() { circlesX[circles] = random(width/2) - random(width/2); circlesY[circles] = random(height/2) - random(height/2); circles++; } void draw() { translate(width/2, height/2); rotate(radians(angle % 360)); background(0); if(fillCircles) { noStroke(); fill(hue++%255, 255, 255); } else { strokeWeight(4); noFill(); stroke(hue++%255, 255, 255); } for(int i=0; i < circles; i++) { ellipse(circlesX[i], circlesY[i], diameter + random(4), circleSize + random(4)); } angle += speed; } void mouseMoved() { diameter = mouseX/2; circleSize = mouseY/2; } void mouseClicked() { background(0); //fillCircles = !fillCircles; speed += 5; if(circles < MAX_CIRCLES) { addCircle(); //rate += 5; } }
0 notes
Text
Bip Bop
Driver drive; void setup() { size(); drive = new Driver(); frameRate(30); } void draw() { drive.draw(); } void mouseClicked() { drive.mouseClicked(); } class Driver { int score; SegmentFont font; Driver() { font = new SegmentFont(); reset(); } void reset() { score = 0; } void mouseClicked() { } void drawScore() { float textScale = 4; strokeWeight(0.75); stroke(0, random(55) + 200, 0); font.text("SCORE - " + score, width/8, font.charHeight*textScale + 4, textScale); } float titleScale[] = { 2, 1.8, 1.6, 1.4, 1.2, 1.0, 1.2, 1.4, 1.6, 1.8 }; void drawFlasher(String title) { float titleSize=16; colorMode(HSB, title.length(), 255, 255); strokeWeight(0.25); for(int i=0; i < title.length(); i++) { stroke(i, 255, random(55) + 200); font.character(title.charAt(i), (width - (title.length()*font.charWidth*titleSize)) / 2.0 + i*font.charWidth*titleSize, height/2.5, titleSize*sin(titleScale[i])); titleScale[i] += 0.2; } colorMode(RGB,255); } void drawInstructions(String instr) { float instSize = 5; stroke(255,0,0); strokeWeight(0.5 + random(0.25)); font.text(instr, (width - (instr.length()*font.charWidth*instSize))/2.0, height/2, instSize); } void drawCopyright(String copyr) { float copySize = 3; stroke(0,255,0); strokeWeight(0.25 + random(0.25)); font.text(copyr, (width - (copyr.length()*font.charWidth*copySize))/2.0, height - height/2.5, copySize); } void drawPauseScreen() { String title = 'BIZLODRIVE'; String instr = 'CLICK IN THE WINDOW TO PLAY'; drawInstructions(instr); drawFlasher(title); drawCopyright('BizloDrive version 1,0 Copyright (C) 2011 by Bill Bereza'); } void drawRoad() { font.text('NOW, IMAGINE THERE IS A ROAD.', 10, height/2, 4); } int halfCarWidth = 50; int halfCarHeight = 150; void drawCar() { strokeWeight(1); stroke(random(55) + 200); fill(127); quad(mouseX - halfCarWidth/2, height - 2*halfCarHeight, mouseX + halfCarWidth/2, height - 2*halfCarHeight, mouseX + halfCarWidth, height - halfCarHeight, mouseX - halfCarWidth, height - halfCarHeight); } void draw() { background(0); if(focused) { drawRoad(); drawCar(); } else { drawPauseScreen(); } drawScore(); } } class SegmentFont { int charWidth = 4; int charHeight = 6; HashMap chars; SegmentFont() { chars = new HashMap(); // char pattern // sixteen segment display //_ _ //|\|/| //- - //|/|\| //- - // // 0xF000 = X // 0x0F00 = + // 0x00F0 = top half of O // 0x000F = bottom half of O chars.put("0", 0x50FF); chars.put("1", 0x0011); chars.put("2", 0x057E); chars.put("3", 0x0577); chars.put("4", 0x0591); chars.put("5", 0x21E6); chars.put("6", 0x05EF); chars.put("7", 0x0071); chars.put("8", 0x05FF); chars.put("9", 0x05F1); chars.put("A", 0x05F9); chars.put("B", 0x0E77); chars.put("C", 0x00EE); chars.put("D", 0x0A77); chars.put("E", 0x05EE); chars.put("F", 0x05E8); chars.put("G", 0x04EF); chars.put("H", 0x0599); chars.put("I", 0x0A66); chars.put("J", 0x001F); chars.put("K", 0x6188); chars.put("L", 0x008E); chars.put("M", 0xC099); chars.put("N", 0xA099); chars.put("O", 0x00FF); chars.put("P", 0x05F8); chars.put("Q", 0x20FF); chars.put("R", 0x25F8); chars.put("S", 0x05E7); chars.put("T", 0x0A60); chars.put("U", 0x009F); chars.put("V", 0x5088); chars.put("W", 0x3099); chars.put("X", 0xF000); chars.put("Y", 0xC200); chars.put("Z", 0x5066); chars.put("a", 0x030E); chars.put("b", 0x038C); chars.put("c", 0x010C); chars.put("d", 0x0B0C); chars.put("e", 0x110C); chars.put("f", 0x4600); chars.put("g", 0x2403); chars.put("h", 0x0388); chars.put("i", 0x0200); chars.put("j", 0x0204); chars.put("k", 0x2E00); chars.put("l", 0x0202); chars.put("m", 0x0709); chars.put("n", 0x2201); chars.put("o", 0x030C); chars.put("p", 0x1108); chars.put("q", 0x1308); chars.put("r", 0x0600); chars.put("s", 0x2402); chars.put("t", 0x0700); chars.put("u", 0x020C); chars.put("v", 0x1008); chars.put("w", 0x3009); chars.put("x", 0x3500); chars.put("y", 0x9800); chars.put("z", 0x1104); chars.put(" ", 0x0000); chars.put("-", 0x0500); chars.put("+", 0x0F00); chars.put("_", 0x0006); chars.put("/", 0x5000); chars.put("\\",0xA000); chars.put("*", 0xFF00); chars.put("%", 0x5FC3); chars.put("(", 0x0A22); chars.put(")", 0x0A44); chars.put("[", 0x00EE); chars.put("]", 0x0077); chars.put("<", 0x6000); chars.put(">", 0x9000); chars.put(".", 0x130C); chars.put(",", 0x1000); chars.put("\"",0x4000); chars.put("?", 0x0670); chars.put("!", 0x0800); chars.put("$", 0x0FE7); chars.put("`", 0x8000); chars.put("=", 0x0560); chars.put("{", 0x0B22); chars.put("}", 0x0E44); chars.put("|", 0x0A00); chars.put("'", 0x0090); chars.put(":", 0x0280); chars.put(";", 0x1800); chars.put("&", 0x2DCF); chars.put("#", 0x0A99); chars.put("@", 0x0DFE); chars.put("~", 0x0518); chars.put("^", 0x00F0); } void character(char c, float x, float y, float scalez) { int map; map = chars.get(c); if(map != null) { pushMatrix(); translate(x,y); scale(scalez); if(map & 0xF000) { if(map & 0x8000) { line(0,-4,1,-2); } if(map & 0x4000) { line(2,-4,1,-2); } if(map & 0x2000) { line(2,-0,1,-2); } if(map & 0x1000) { line(0,-0,1,-2); } } if(map & 0xF00) { if(map & 0x800) { line(1,-4,1,-2); } if(map & 0x400) { line(2,-2,1,-2); } if(map & 0x200) { line(1,-0,1,-2); } if(map & 0x100) { line(0,-2,1,-2); } } if(map & 0xF0) { if(map & 0x80) { line(0,-2,0,-4); } if(map & 0x40) { line(0,-4,1,-4); } if(map & 0x20) { line(1,-4,2,-4); } if(map & 0x10) { line(2,-4,2,-2); } } if(map & 0xF) { if(map & 0x8) { line(0,-0,0,-2); } if(map & 0x4) { line(0,-0,1,-0); } if(map & 0x2) { line(1,-0,2,-0); } if(map & 0x1) { line(2,-0,2,-2); } } popMatrix(); } } void text(String str, float x, float y, float scalez) { float ix = x; for(int i=0; i < str.length(); i++) { if(str.charAt(i) == "\n") { x = ix; y += scalez * charHeight; } else { character(str.charAt(i), x, y, scalez); x += (charWidth * scalez); } } } }
0 notes
Text
Life Clock
SegmentFont font; float birthsPerDay = 447586; float deathsPerDay = 172915; float birthsPerSecond = birthsPerDay / 24.0 / 60.0 / 60.0; float deathsPerSecond = deathsPerDay / 24.0 / 60.0 / 60.0; String birthsSinceStartStr = ""; String deathsSinceStartStr = ""; float birthsSinceStart; float deathsSinceStart; String startTime; void setup() { size(); font = new SegmentFont(); frameRate(30); startTime = "Since " + str(year()) + "-" + str(month()) + "-" + str(day()) + " " + str(hour()) + "h " + str(minute()) + "m " + str(second()) + "s"; } void draw() { int s = 4; birthsSinceStart = millis()/1000 * birthsPerSecond; deathsSinceStart = millis()/1000 * deathsPerSecond; birthsSinceStartStr = str(floor(birthsSinceStart)); deathsSinceStartStr = str(floor(deathsSinceStart)); background(0); stroke(0,random(55) + 200,0); s = 40; strokeWeight(4/s); font.text(birthsSinceStartStr, 10, height - 240, s); font.text(deathsSinceStartStr, width - (10 + deathsSinceStartStr.length()*40*4), height - 120, s); s = 10; strokeWeight(4/s); font.text('BIRTHS', 10, height - 16*s, s); font.text('DEATHS', width - (10 + 6*s*4), height - 4*s, s); s = 4; strokeWeight(1/s); font.text(str(floor(millis() / 1000)) + "seconds\n"+startTime, 10, height - 40, s); } class SegmentFont { int charWidth = 3; int charHeight = 5; HashMap chars; SegmentFont() { chars = new HashMap(); // char pattern // sixteen segment display //_ _ //|\|/| //- - //|/|\| //- - // // 0xF000 = X // 0x0F00 = + // 0x00F0 = top half of O // 0x000F = bottom half of O chars.put("0", 0x50FF); chars.put("1", 0x0011); chars.put("2", 0x057E); chars.put("3", 0x0577); chars.put("4", 0x0591); chars.put("5", 0x21E6); chars.put("6", 0x05EF); chars.put("7", 0x0071); chars.put("8", 0x05FF); chars.put("9", 0x05F1); chars.put("A", 0x05F9); chars.put("B", 0x0E77); chars.put("C", 0x00EE); chars.put("D", 0x0A77); chars.put("E", 0x05EE); chars.put("F", 0x05E8); chars.put("G", 0x04EF); chars.put("H", 0x0599); chars.put("I", 0x0A66); chars.put("J", 0x001F); chars.put("K", 0x6188); chars.put("L", 0x008E); chars.put("M", 0xC099); chars.put("N", 0xA099); chars.put("O", 0x00FF); chars.put("P", 0x05F8); chars.put("Q", 0x20FF); chars.put("R", 0x25F8); chars.put("S", 0x05E7); chars.put("T", 0x0A60); chars.put("U", 0x009F); chars.put("V", 0x5088); chars.put("W", 0x3099); chars.put("X", 0xF000); chars.put("Y", 0xC200); chars.put("Z", 0x5066); chars.put("a", 0x030E); chars.put("b", 0x038C); chars.put("c", 0x010C); chars.put("d", 0x0B0C); chars.put("e", 0x110C); chars.put("f", 0x4600); chars.put("g", 0x2403); chars.put("h", 0x0388); chars.put("i", 0x0200); chars.put("j", 0x0204); chars.put("k", 0x2E00); chars.put("l", 0x0202); chars.put("m", 0x0709); chars.put("n", 0x2201); chars.put("o", 0x030C); chars.put("p", 0x1108); chars.put("q", 0x1308); chars.put("r", 0x0600); chars.put("s", 0x2402); chars.put("t", 0x0700); chars.put("u", 0x020C); chars.put("v", 0x1008); chars.put("w", 0x3009); chars.put("x", 0x3500); chars.put("y", 0x9800); chars.put("z", 0x1104); chars.put(" ", 0x0000); chars.put("-", 0x0500); chars.put("+", 0x0F00); chars.put("_", 0x0006); chars.put("/", 0x5000); chars.put("\\",0xA000); chars.put("*", 0xFF00); chars.put("%", 0x5FC3); chars.put("(", 0x0A22); chars.put(")", 0x0A44); chars.put("[", 0x00EE); chars.put("]", 0x0077); chars.put("<", 0x6000); chars.put(">", 0x9000); chars.put(".", 0x130C); chars.put(",", 0x1000); chars.put("\"",0x4000); chars.put("?", 0x0670); chars.put("!", 0x0800); chars.put("$", 0x0FE7); chars.put("`", 0x8000); chars.put("=", 0x0560); chars.put("{", 0x0B22); chars.put("}", 0x0E44); chars.put("|", 0x0A00); chars.put("'", 0x0090); chars.put(":", 0x0280); chars.put(";", 0x1800); chars.put("&", 0x2DCF); chars.put("#", 0x0A99); chars.put("@", 0x0DFE); chars.put("~", 0x0518); chars.put("^", 0x00F0); } void character(char c, float x, float y, float scalez) { int map; map = chars.get(c); if(map != null) { pushMatrix(); translate(x,y); scale(scalez); if(map & 0xF000) { if(map & 0x8000) { line(0,-4,1,-2); } if(map & 0x4000) { line(2,-4,1,-2); } if(map & 0x2000) { line(2,-0,1,-2); } if(map & 0x1000) { line(0,-0,1,-2); } } if(map & 0xF00) { if(map & 0x800) { line(1,-4,1,-2); } if(map & 0x400) { line(2,-2,1,-2); } if(map & 0x200) { line(1,-0,1,-2); } if(map & 0x100) { line(0,-2,1,-2); } } if(map & 0xF0) { if(map & 0x80) { line(0,-2,0,-4); } if(map & 0x40) { line(0,-4,1,-4); } if(map & 0x20) { line(1,-4,2,-4); } if(map & 0x10) { line(2,-4,2,-2); } } if(map & 0xF) { if(map & 0x8) { line(0,-0,0,-2); } if(map & 0x4) { line(0,-0,1,-0); } if(map & 0x2) { line(1,-0,2,-0); } if(map & 0x1) { line(2,-0,2,-2); } } popMatrix(); } } void text(String str, float x, float y, float scalez) { float ix = x; for(int i=0; i < str.length(); i++) { if(str.charAt(i) == "\n") { x = ix; y += scalez * (charHeight + 1); } else { character(str.charAt(i), x, y, scalez); x += ((charWidth+1)*scalez); } } } }
0 notes