// Bryan Blackford - bblackfo@ucsc.edu // Rocklebobber: Devourer of Trains // a Processing Monster born Spring 2010 // UCSC Film 20C - Lindsay Kelley. TA: Andrew Pascoe. // main sketch // global vars int lastX=-1; int lastY=-1; Train[] trains; int maxTrains; Mountain[] mountains; int maxMountains; Monster monster; // monster int carsEaten=0; // score //screen dimensions int Width=400; int Height=400; color black = color(0,0,0); color white = color(255,255,255); // setup window and objects void setup() { size(Width,Height); frameRate(60); // 60=default framerate smooth(); // smooth curves //init font for score PFont font; font = loadFont("Ziggurat-HTF-Black-32.vlw"); textFont(font); // create objects // inaugural trams // trains come into the world randomly in the draw function maxTrains = 20; trains = new Train[maxTrains]; for (int t = 0 ; t < maxTrains ; ++t) trains[t] = new Train(0,0,0); // init trains with 0 cars, indicating availability // initial train trains[0] = new Train(Width,int(random(150,Height-50)),int(random(2,20))); // moving mountains // mountains jump to left side of screen after departing right side maxMountains = 20; mountains = new Mountain[maxMountains]; for (int m = 0 ; m < maxMountains;++m) { mountains[m] = new Mountain(random(100,Width),100,int(random(10,50)),int(random(30,60))); mountains[m].xpos -= mountains[m].wide; // shift mountain over so it doesn't start offscreen } mountains[0].xpos = 50; // at least one mountain close to monster // monster! (0,85) is hiding spot monster = new Monster("data/monster.svg","data/jaw.svg",0,85); } // draw stuff and do periodic effects (creating trains) void draw() { background(0); // clear screen DrawBackground(150,5); // draw gradient background //display number of cars eaten fill(255); text(carsEaten,30,30); // draw monster (if hidden) monster.update(); if (monster.bHiding) monster.draw(); // draw ground base fill(0); stroke(0); rect(0,100,Width,Height-100); // draw mountains for (int m = 0 ; m < mountains.length ; ++m) { mountains[m].update(); if (mountains[m].xpos > Width) { // mountain has moved off screen // reshape and move mountain to other side of screen mountains[m].wide = int(random(30,60)); mountains[m].high = int(random(10,50)); mountains[m].xpos = -mountains[m].wide; } } // draw trains for (int t = 0 ; t < trains.length ; ++t) trains[t].update(); // draw monster (if in front) if (!monster.bHiding) monster.draw(); // check if monster is chewing a train if (monster.jawMoves == 0) { // middle Traincar tc = FindLivingCarAtPos(monster.getMidX(),monster.getMouthY()); if (tc != null) Eat(tc); // and left tc = FindLivingCarAtPos(monster.getLeftMouth(),monster.getMouthY()); if (tc != null) Eat(tc); // and right tc = FindLivingCarAtPos(monster.getRightMouth(),monster.getMouthY()); if (tc != null) Eat(tc); } if (int(random(0,200))==1) // random chance to create new train { // find a space for a train int t = FindFreeTrain(trains); //println(t); if (t >= 0) { trains[t] = new Train(Width,int(random(150,Height-50)),int(random(2,20))); } } } // eat car void Eat(Traincar car) { car.alive = false; carsEaten++; } // background gradient from y=0 to ystop, x=0 to width, ystep bars void DrawBackground(float ystop,float ystep) { pushMatrix(); // transform matrix // loop through horizontal bars for (float y=0;y <= ystop;y += ystep) { color barc = lerpColor(black,white,(y/ystop)); // gradiated color stroke(barc); fill(barc); rect(0,y,Width,ystep); // draw bar } popMatrix(); // clear transform stack } // find next available space in memory for a train int FindFreeTrain(Train[] trains) { for (int t = 0 ; t < trains.length ; ++t) if (trains[t].cars <= 0) // 0 cars indicates free train return t; return -1; // no elements available in array } // return the traincar at this position, or null if none Traincar FindLivingCarAtPos(float x,float y) { Traincar tc; // loop through trains for (int t = 0 ; t < trains.length ; ++t) { // loop through cars of this train for (int c = 0 ; c < trains[t].cars ; ++c) { tc = trains[t].carArr[c]; // current car if (tc.alive) { // check if point is within the car if (x >= tc.xpos && x <= tc.xpos+tc.carlength && y >= tc.ypos && y <= tc.ypos+tc.carheight ) return tc; // car found, return it } } } return null; // no car found at position } // called on mouse click void mousePressed() { // save click position lastX = mouseX; lastY = mouseY; // if hiding and waiting, come out! if (monster.bHiding == true && monster.move==0) { monster.setMove(1,mouseX,mouseY); } else if (monster.move != 1 && monster.move != 4 && monster.move != 5) // click moves if not moving into or out of hiding { monster.setMove(3,mouseX,mouseY); } // sparkle motion drawX(mouseX,mouseY,random(4,10)); randomX(); } // keyboard reactivity void keyPressed() { // take screenshot if (key == 'p') saveFrame(); //hide! if (monster.bHiding == false) monster.setMove(4,0,0); // shake screen, bouncing away trains } // draw an X of size s at x,y..leftover from tutorial void drawX(int x, int y, float s) { line(x-s,y-s,x+s,y+s); line(x+s,y-s,x-s,y+s); } void randomX(){ drawX(int(random(0,width)),int(random(0,height)),int(random(1,100))); } // String[] lines = loadStrings("something.txt"); // each string is a line // PImage image = loadImage("picture.jpg"); /* html to add to index.html
Rocklebobber: Devourer of Trains.
Mouse and keyboard reactive.
A processing monster born Spring 2010 for UCSC Film 20C: Intro to digital media, Lindsay Kelley. TA: Andrew Pascoe.
*/