Submitted on 2009-07-18 22:10:49.
Username:
Title:
Code://Include LED control #include "LedControl.h" //Include header to handle text output #include "font.h" //New instance of LedControl. d12 = DataIn(1), d11 = CLK(13), 10 = LOAD(12) LedControl lc=LedControl(12,11,10,1); //Regulate ball speed int ballDelayLimit = 500; int ballDelay = 0; //Controller Analog input pin int controller = 0; //Define byte-map for bat byte bat = B11100000; int cval = 0; int pos = 0; float epos = 2; float eskill = 1; float br = 4; float bc = 2; float brr = 1; float bcc = 1; //Scores, 0 = you, 1 = enemy int score[2] = {0, 0}; boolean gameOver = false; //Setup code void setup() { //Wake up the driver IC lc.shutdown(0, false); //Set a medium brightness for the LEDs lc.setIntensity(0, 8); //Clear the display lc.clearDisplay(0); //Seed random number generator randomSeed(analogRead(0)); } //Game code void loop(){ if(gameOver){ animExplode(); //Run endgame sequence if(score[0] < 3 && score[1] < 3){ showScores(); }else{ showScores(); endGame(); } //Reset game br = (int)random(2, 5); bc = (int)random(2, 5); brr = 1; bcc = 1; eskill = 1; gameOver = false; ballDelayLimit = 500; delay(1000); lc.clearDisplay(0); }else{ //Read controller value cval = analogRead(controller); //Map the value pos = map(cval, 50, 950, 5, 0); //Draw my bat drawBat(pos, 0); //Update ball position/enemy position with delay if(ballDelayLimit == 70){ eskill = 0.9; } if(ballDelay > ballDelayLimit){ //Turn off current ball lc.setLed(0, br, bc, false); //Make new position br = br + brr; bc = bc + bcc; //Draw the new ball lc.setLed(0, br, bc, true); //Reset the delay ballDelay = 0; //Increase ball speed ballDelayLimit = max(ballDelayLimit - 0.5, 70); //Move the enemy bat if(bc < epos){ epos = epos - eskill; }else if(bc > epos+2){ epos = epos + eskill; } //Draw enemy bat drawBat(epos, 7); //Figure out bounces/angles for next draw bounce(); }else{ ballDelay++; } } } void drawBat(int offset, int row){ offset = constrain(offset, 0, 5); lc.setRow(0, row, bat>>offset); } void bounce(){ //Add bouncing on top/bottom walls (column checks) if(bc <= 0){ bcc = bcc * -1; }else if(bc >= 7){ bcc = 0-bcc; } //Add bouncing off bats with angles for where on the bat got hit (row checks) if(br < 2 && br >= 1 && brr < 0){ if((int)bc == pos){ brr = brr * -1; if(bcc > 0){ bcc = bcc * 0.9; }else{ bcc = bcc * 1.1; } }else if((int)bc == pos+1){ brr = brr * -1; }else if((int)bc == pos+2){ brr = brr * -1; if(bcc > 0){ bcc = bcc * 1.1; }else{ bcc = bcc * 0.9; } } return; }else if(br < 7 && br >= 6 && brr > 0){ if((int)bc == epos){ brr = 0-brr; if(bcc > 0){ bcc = bcc * 0.9; }else{ bcc = bcc * 1.1; } }else if((int)bc == epos+1){ brr = 0-brr; }else if((int)bc == epos+2){ brr = 0-brr; if(bcc > 0){ bcc = bcc * 1.1; }else{ bcc = bcc * 0.9; } } return; } //Check for losers if(br < 0){ //Game over, you lose score[1] = score[1]+1; gameOver = true; }else if(br > 7){ //Game over, they lose score[0] = score[0]+1; gameOver = true; } }
(Edit the above source them submit it as a reply)
Your username:
Download here