lunes, 23 de mayo de 2016

Sketch V

              // declare global variables
int xspeed, yspeed;
int xpos,ypos,wdth,ht;
            // initialize sketch
void setup() {
             // set sketch window size + background color
  size(600,400);
  background(0);
              // ball speed
  xspeed=3;
  yspeed=6;
              // ball size
  wdth = 200;
  ht=690;
              // turn off shapestroke rendering
  noStroke ();
              // initial ball placement
  xpos=width/2;
  ypos=height/2;
  frameRate(30);
}
            // begin animation loop
void draw(){
              // draw ball
  smooth ();
  fill ((random (250)),(random (290)),(random (290)));
  ellipse(xpos,ypos,wdth,ht);
             // upgrade position values
  xpos+=xspeed;
  ypos+=yspeed;
  /*conditionals 
   detects ball collission with sketch window edges
   also accounts for thickness of ball
   */
  if(xpos>=width-wdth/2 || xpos<=wdth/2){
    xspeed*=-1;
  }
  if (ypos>=height-ht/2 || ypos<=ht/2){
    yspeed*=-1;
  }
}


No hay comentarios:

Publicar un comentario