Arduino Pong

next chunk o' code

// do the main game logic regarding the ball and collisions
void updateBall(boolean xEvent)
{
  // if checking the x coords of the ball
  if(xEvent)
  {
    /*
    TODO
    
    have the yvel start 1t 1
    
    */
    
     //check for collision with left player
     if(ballXPos <PLAYER_LEFT_X+2 & (playerLeft == ballYPos or playerLeft+1 == ballYPos))
     {
       ballXVel = -1 * ballXVel;
       ballXPos=PLAYER_LEFT_X+1;
     }
     //check for collision with right player
     if(ballXPos > PLAYER_RIGHT_X-2  & (playerRight == ballYPos or playerRight+1 == ballYPos))
     {
       ballXVel = -1 * ballXVel;
       ballXPos=PLAYER_RIGHT_X-1;
     }
     
     //move the ball in the x direction
     if(ballXVel < 1)
     {
       ballXPos--;
     }
     else
     {
         ballXPos++;
     }
   }
  
  //if checking the y coords
  if(!xEvent)
  {
     //check for top of screen hit
     if( ballYPos <= SCORE_BAR )
     {
       ballYVel = -1 * ballYVel; 
       ballYPos = SCORE_BAR;
     }
     //check for bottom of screen hit
     if( ballYPos>BASE_BAR )
     {
       ballYVel = -1 * ballYVel; 
       ballYPos = BASE_BAR+1;
     }
     
     // move the ball in the y direction
     if(ballYVel < 1)
     {
       ballYPos--;
     }
     else
     {
       ballYPos++;
     }     
  }
  
  //always check for scoring of points
  
  //right player scores
  if(ballXPos==PLAYER_LEFT_X)//0
  {
    //reset the position of the ball
    ballXPos = PLAYER_RIGHT_X - 5;
    ballYPos=7;
    
    // change the other players life
    clearPixel(PLAYER_LEFT_LIFE+2*playerLeftLife-2,SCORE_BAR-1);
    playerLeftLife--;
    
    // indicate that someone scored so the wait doesn't start a new game
    justScored=true;
    // indicate that we should wait next loop
    waiting=true;

    // check if game is over
    if(playerLeftLife==0)
      justScored=false;       
   }
 
 //left player scores
 if(ballXPos==PLAYER_RIGHT_X)//38
 {
    //reset position of ball
    ballXPos = PLAYER_LEFT_X + 5;
    ballYPos=7;
    
    //update other players score
    clearPixel(PLAYER_RIGHT_LIFE+2*(MAX_LIFE-playerRightLife)+2,SCORE_BAR-1);
    playerRightLife--;
    
    // indicate that someone scored so the wain doesn't start a new game
    justScored=true;
    // indicate that we should wait next loop
    waiting=true;

    // check for game over
    if(playerRightLife==0)
      justScored=false;
 }
 
}

// draw the player life bars
void initScreen()
{
  for(index=1;index<=MAX_LIFE;++index)
  {
    grayPixel(index*2,SCORE_BAR-1);
    grayPixel(index*2+PLAYER_RIGHT_LIFE,SCORE_BAR-1);
  }
}


// draw the ball
void drawBall(byte col)
{
 frameBuffer[ballXPos][ballYPos]=col; 
}

// clear the screen
void clearScreen()
{
    for (index = 0; index < WIDTH; index++)
      for (index2=0;index2<=HEIGHT;++index2)
        {
         frameBuffer[index][index2] = _BLACK;
        } 
}

// the setup routine
void setup()
{
  pinMode (SYNC_PIN, OUTPUT);
  pinMode (DATA_PIN, OUTPUT);
  digitalWrite (SYNC_PIN, HIGH);
  digitalWrite (DATA_PIN, HIGH);
  clearScreen();
  drawArduinoPong();
}

void loop()
{
  // iterate over the lines on the tv
  for ( line =0;line< DISPLAY_LINES;++line)
    {
    
      // HSync
      // front porch (1.5 us)
      PORTB = _BLACK;
      delayMicroseconds(1.5);
      //sync (4.7 us)
      PORTB = _SYNC;
      delayMicroseconds(4.7);
      // breezeway (.6us) + burst (2.5us) + colour back borch (1.6 us)
      PORTB = _BLACK;
      delayMicroseconds(0.6+2.5+1.6);


      //calculate which line to draw to
      newLine = line >>4;
      delayMicroseconds(1);      
      
      //display the array for this line
      // a loop would have been smaller, but it messes the timing up
      PORTB = frameBuffer[0][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[1][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[2][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[3][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[4][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[5][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[6][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[7][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[8][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[9][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[10][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[11][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[12][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[13][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[14][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[15][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[16][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[17][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[18][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[19][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[20][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[21][newLine];
      delayMicroseconds(1);
      PORTB = frameBuffer[22][newLine];
      delayMicroseconds(1);