import ddf.minim.signals.*; import ddf.minim.*; import ddf.minim.analysis.*; import ddf.minim.effects.*; int xPos; int yPos; int squareSize; int halfOfSquareSize; int movementSpeed; int framerate; int framecount; int xDifference; int yDifference; ArrayList enemies; Minim minim; AudioSample coinSound; AudioSample explosionSound; AudioSample hitSound; AudioSample selectSound; boolean programStarted; int cycle; int beat; int timerheight; // ================================================================ // SETUP void setup() { // noLoop(); size(800,450); background(0,0,0); // Set initial variables squareSize = 20; halfOfSquareSize = squareSize/2; movementSpeed = 8; framerate = 30; framecount = 0; xDifference = 0; yDifference = 0; frameRate(framerate); // Start the player in the center of the screen xPos = width/2; yPos = height/2; // Initialize enemies enemies = new ArrayList(); enemies.add(new Enemy()); // Initialize sound effects minim = new Minim(this); coinSound = minim.loadSample("coin.wav"); explosionSound = minim.loadSample("explosion.wav"); hitSound = minim.loadSample("hit.wav"); selectSound = minim.loadSample("select.wav"); programStarted = false; cycle = 0; beat = 0; timerheight = 20; deathLinger = 0; } // End setup() // ================================================================ // DRAW LOOP int deathLinger; void draw() { // Basic set up stuff background(0,0,0); stroke(255); fill(0,0,0); rectMode(CORNER); rect( 0, 0, width-1, height-1); // ================================ // DEATH if( squareSize < 1 ) { deathLinger++; if( deathLinger > 30 ) { deathLinger = 0; setup(); } rectMode(CENTER); rect( xPos, yPos, squareSize, squareSize ); for( int i = 0; i < enemies.size(); i++ ) { Enemy currentEnemy = (Enemy)enemies.get(i); rect( currentEnemy.xPosition, currentEnemy.yPosition, currentEnemy.squareSize, currentEnemy.squareSize ); line( xPos, yPos, currentEnemy.xPosition, currentEnemy.yPosition ); } // End for loop ( ) return; } // End if ( player loses ) // ================================ // MENU SCREEN if( !programStarted ) { // Top icons rect( 390, 83, 20, 20 ); // UP rect( 370, 103, 20, 20 ); // LEFT rect( 390, 103, 20, 20 ); // DOWN rect( 410, 103, 20, 20 ); // RIGHT // Bottom icons rect(390, 307, 20, 20); // UP rect(370, 327, 20, 20); // LEFT rect(390, 327, 20, 20); // DOWN rect(410, 327, 20, 20); // RIGHT // UP Cross line( 390, 83, 410, 103 ); line( 390, 103, 410, 83 ); // DOWN Cross line( 390, 103, 410, 123 ); line( 390, 123, 410, 103 ); // Show, don't tell if( checkKey(UP) ) { line(390, 307, 410, 327); line(390, 327, 410, 307); } if( checkKey(DOWN) ) { line(390, 327, 410, 347); line(390, 347, 410, 327); } if( checkKey(LEFT) ) { line(370, 327, 390, 347); line(370, 347, 390, 327); } if( checkKey(RIGHT) ) { line(410, 327, 430, 347); line(410, 347, 430, 327); } // Must come up with a better screen than this... if( checkKey(UP) && checkKey(DOWN) ) { programStarted = true; } return; } // End if timerheight = squareSize; /* // Don't move if the user isn't pressing anything... if( checkKey(UP) || checkKey(DOWN) || checkKey(LEFT) || checkKey(RIGHT) ) { // well, keep going } else { return; } */ // A hack to make the game beats and stuff move faster framecount += 6; // Simple attempt to make the game move faster as time goes on framecount += cycle; // Top bars - Timer if(framecount >= (width-20)) { cycle++; framecount = 0; // NEW ENEMY!!! enemies.add( new Enemy() ); coinSound.trigger(); } // End if ( cycle is over ) // ================ // Draw the background bars and beats // Someone help me... I don't remember how this crazy piece of code works! // Draw the past cycles for(int i=0; i framecount) { beat = framecount; } for(int i=0; i < (cycle+1); i++) { if( i % 2 == 0 ) { rect( 10, 10 + (30*i), beat, timerheight); rect( width - 10 - beat, height - 30 - (30*i), beat, timerheight); } else { rect( width - 10 - beat, 10 + (30*i), beat, timerheight); rect( 10, height - 30 - (30*i), beat, timerheight); } } // ================ // Moving the user's square based on what the user is pressing if( checkKey(UP) ) { yPos -= movementSpeed; } if( checkKey(DOWN) ) { yPos += movementSpeed; } if( checkKey(LEFT) ) { xPos -= movementSpeed; } if( checkKey(RIGHT) ) { xPos += movementSpeed; } // End if ( user is pressing a direction ) halfOfSquareSize = squareSize/2; // Checking for collision against the sides of the screen if( xPos < halfOfSquareSize ) { xPos = halfOfSquareSize; } else if( xPos > width - halfOfSquareSize ) { xPos = width - halfOfSquareSize; } // End if/else if( yPos < halfOfSquareSize ) { yPos = halfOfSquareSize; } else if( yPos > height - halfOfSquareSize ) { yPos = height - halfOfSquareSize; } // End if/else // User's square rectMode(CENTER); rect( xPos, yPos, squareSize, squareSize ); // Enemy loop for( int i = 0; i < enemies.size(); i++ ) { Enemy currentEnemy = (Enemy)enemies.get(i); // Checking if the enemy fell off the screen, if so, killing it if( (currentEnemy.xPosition > width + currentEnemy.squareSize) || (currentEnemy.xPosition < -currentEnemy.squareSize) || (currentEnemy.yPosition > height + currentEnemy.squareSize) || (currentEnemy.yPosition < -currentEnemy.squareSize) ) { enemies.remove(i); explosionSound.trigger(); continue; } // Checking for enemy collision, and hopefully doing something more interesting than just playing a sound if( currentEnemy.collision ) { squareSize -= 1; hitSound.trigger(); } // End if // Draw the enemy and the line from the enemy to the player currentEnemy.update(); line( xPos, yPos, currentEnemy.xPosition, currentEnemy.yPosition ); } // End for loop ( ) } // End draw() // ================================================================ // ENEMY CLASS class Enemy { float xPosition; float yPosition; float xVelocity; float yVelocity; float velocityMax; float velocityMin; float acceleration; boolean collision; int squareSize; Enemy() { this.xPosition = random( 10, (width-10)); this.yPosition = random( 10, (height-10)); if( abs( xPos - this.xPosition ) < this.squareSize && abs( yPos - this.yPosition ) < this.squareSize ) { this.xPosition = random( 10, (width-10)); this.yPosition = random( 10, (height-10)); } this.velocityMax = random( 11, 19 ); this.velocityMin = -this.velocityMax; this.acceleration = random( 0.8, 2 ); // this.acceleration = 9.5/this.velocityMax; this.xVelocity = 0; this.yVelocity = 0; this.squareSize = 20; } // End constructor Enemy( float x, float y, float max, float accel ) { this.xPosition = x; this.yPosition = y; this.velocityMax = max; this.velocityMin = -max; this.acceleration = accel; this.xVelocity = 0; this.yVelocity = 0; } // End constructor void update() { if( ( xPos - this.xPosition ) > 0 ) { if( this.xVelocity < this.velocityMax ) { this.xVelocity += this.acceleration; } // End if ( max acceleration ) } else { if( this.xVelocity > this.velocityMin ) { this.xVelocity -= this.acceleration; } // End if ( max acceleration ) } // End if/else ( x acceleration of the chaser ) if( ( yPos - this.yPosition ) > 0 ) { if( this.yVelocity < this.velocityMax ) { this.yVelocity += this.acceleration; } // End if ( max acceleration ) } else { if( this.yVelocity > this.velocityMin ) { this.yVelocity -= this.acceleration; } // End if ( max acceleration ) } // End if/else ( y acceleration of the chaser ) this.xPosition += this.xVelocity; this.yPosition += this.yVelocity; // check for collision if( abs( xPos - this.xPosition ) < this.squareSize && abs( yPos - this.yPosition ) < this.squareSize ) { this.collision = true; this.xVelocity = -this.xVelocity; this.yVelocity = -this.yVelocity; } else if( this.collision ) { this.collision = false; } // End if/else ( ) rect( this.xPosition, this.yPosition, this.squareSize, this.squareSize ); } // End update() } // End Enemy class // ================================================================ // CHECK KEY FUNCTION /* multiplekeys taken from http://processinghacks.com/hacks:multiplekeys @author Yonas Sandbæk http://seltar.wliia.org Modified by Ryan De La Torre for optimization */ // usage: // if(checkKey("ctrl") && checkKey("s")) println("CTRL+S"); // key[] has been cut to 4, narrowing this down to arrow keys // "LEFT" is 37 and "DOWN" is 40 int keyOffset = LEFT; boolean[] keys = new boolean[4]; boolean checkKey(int k) { for(int i = 0; i < keys.length; i++) { if( (i+keyOffset) == k ) { return keys[i]; }// else { // println( KeyEvent.getKeyText(i+keyOffset).toLowerCase() ); //} // End if ( key pressed is there ) } // End for ( each valid key ) return false; } // End checkKey void keyPressed() { if( keyCode > (keyOffset-1) && keyCode < (keyOffset+4) ) { keys[(keyCode-keyOffset)] = true; // println("keyPressed"); } else if( key == ENTER ) { setup(); } // End if/else ( valid keycode ) } // End keyPressed void keyReleased() { if( keyCode > (keyOffset-1) && keyCode < (keyOffset+4) ) { keys[keyCode-keyOffset] = false; } // End if ( valid keycode ) } // End keyReleased