saltydave
YaBB Newbies
Offline
Posts: 6
Re: Salty Dave's Drawing Machine
Reply #1 - Feb 1st , 2007, 1:39am
/* Salty Dave's Drawing Machine An engine designed to allow the user to several pylons on the screen. Once the pre-set number of pylons is created a pre-set number of balls appear in the upper left corner and begin moving about. When a ball collides with a pylon it bounces off and the pylon generates a colored ring that expands and fades away. While the balls are bouncing around the user can click on a pylon and drag it to change its position. */ boolean release = true; //Used to prevent multiple pylons being placed with one click. boolean ontarget = false; //Used to determine if the cursor is ontop of a pylon for the click and drag interface. int pylonTarget; //Stores the ID number of the pylon the cursor is ontop of. int pylonTotal = 10; //Sets the total number of pylons. int ballTotal = 10; //Sets the total number of balls. int[] pylonX = new int[pylonTotal]; //Stores the X coordinates for the pylons. int[] pylonY = new int[pylonTotal]; //Stores the Y coordinates for the pylons. int[] ringX = new int[50]; //Stores the X coordinates for the rings. int[] ringY = new int[50]; //Stores the Y coordinates for the rings. int[] ringD = new int[50]; //Stores the duration coutners for the rings. This is used to control the ring's size and alpha. int[] ringR = new int[50]; //Stores the red value for the rings' colors. int[] ringG = new int[50]; //Stores the green value for the rings' colors. int[] ringB = new int[50]; //Stores the blue value for the rings' colors. int ringC = 0; //Ring Counter, used to keep track of how many rings exist and to limit the overlapping of ring data. float[] ballX = new float[ballTotal]; //Stores the X coordinates for the balls. float[] ballY = new float[ballTotal]; //Stores the Y coordinates for the balls. float[] dX = new float[ballTotal]; //Stores the X direction value for the balls. float[] dY = new float[ballTotal]; //Stores the Y direction value for the balls. float speed = 10; //Simple speed multiplier. int pylonCounter = 0; //Counts the number of pylons currently created during the set up phase of the program. void setup(){ frameRate(20); //Limiting the framerate to try and reduce the slow-down. size(400,400); noStroke(); background(255); smooth(); //Initialize the position and direction of all the balls. for(int i=0; i<ballTotal; i++){ ballX[i]=6; ballY[i]=6; dX[i]=random(0,1); dY[i]=1-dX[i]; } } void draw(){ if(pylonCounter < pylonTotal){ //When there are fewer than 10 pylons on the screen. if(mousePressed){ if(release==true){ release=false; pylonX[pylonCounter] = mouseX; pylonY[pylonCounter] = mouseY; for(int i=0; i<30; i++){ fill(0+i*2); ellipse(pylonX[pylonCounter]-i/4,pylonY[pylonCounter]-i/4,30-i,30-i); } pylonCounter++; } } if(mousePressed){ } else { release = true; } } else{ //This section is for when all the pylons are on the screen. //Erase the screen. fill(255); rect(0,0,width,height); //Draw the rings. for(int i=0; i<50; i++){ if(ringD[i] != 0 && ringD[i]<150){ strokeWeight(10); stroke(ringR[i],ringG[i],ringB[i],240-ringD[i]*2); noFill(); ellipse(ringX[i],ringY[i],ringD[i]*4,ringD[i]*4); ringD[i] = ringD[i]+1; } else { ringD[i] = 0; } } //Draw the pylons. noStroke(); for(int c=0; c<pylonTotal; c++){ for(int i=0; i<30; i++){ fill(0+i*2); ellipse(pylonX[c]-i/4,pylonY[c]-i/4,30-i,30-i); } } //Drawing the balls. for(int c=0; c<ballTotal; c++){ for(int i=0; i<10; i++){ fill(40+i*10); ellipse(ballX[c]-i/4,ballY[c]-i/4,10-i,10-i); } } //Moving the ball. for(int i=0; i<ballTotal; i++){ ballX[i] += dX[i]*speed; ballY[i] += dY[i]*speed; } //Constrain the ball to the window. for(int i=0; i<ballTotal; i++){ if (ballX[i] <= 5 || ballX[i] >= width-5){ dX[i] = dX[i]*-1; } if (ballY[i] <= 5 || ballY[i] >= height-5){ dY[i] = dY[i]*-1; } }