P4P2010-05-27

From Noisebridge
Revision as of 21:10, 27 May 2010 by Jtfoote (talk | contribs) (Created page with 'Programming for Poets, class 5/27 We went through varibles and functions again, and introduced conditionals ("if") statements. We updated our pongball program to bounce! int …')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Programming for Poets, class 5/27

We went through varibles and functions again, and introduced conditionals ("if") statements. We updated our pongball program to bounce!


int ballx = 0; // start at left
int bally = 200; // and halfway down
int pballx = 0;
int pbally = 200; 
int speed = 5; // the ball moves this number of pixels between frames
int deltax = speed; // start with positive x speed (right)
int deltay = speed; // start with positive y speed (down)

 
void setup() {
       // size of our drawing window
       size(400, 400);
       // set the background color to gray
       background(128);
} 

void draw() {

      // draw over the previous ball location
      stroke(128); // gray
      fill(128); 
      drawball(pballx,pbally);

      // update the ball location
      ballx = ballx + deltax;
      bally = bally + deltay; 
      
      // check that ball is on the screen
      if(ballx>400)
      {
        println("right bounce x");
        deltax = -speed; // move left
        
      }
      
      if(bally>400)
      {
        println("bottom bounce y");
        deltay = -speed; // now move up
      }

      if(ballx<1)
      {
        println("left bounce x");
        deltax = speed; // now move right
        
      }
      
      if(bally<1)
      {
        println("top bounce y");
        deltay = speed;
      }

      // now draw ball in new location
      stroke(255);
      fill(255); //white
      drawball(ballx,bally);
      
      // save the new location for next time
      pballx = ballx;
      pbally = bally;
      
      
} 

// draw a paddle-shaped box at the given coordinates
void drawbox(int x, int y) {
      rect(30,y,10,40);
}

// draw a ball at the given coordinates
void drawball(int foo, int bar) {
      ellipse(foo,bar,10,10);
}

int myadd(int A, int B) {
  return (A+B);
}