Processing 3.0 Sketches


Proccessing


VIEW SOURCE CODE BACK TO CATEGORIES







Up down arrow


Processing code

Processing


Processing is a fun way to learn how to program. It is a subset language derived from Java and is great for digital artists, designers and visual experimenters. The good thing with Processing is that all your output programs can be visually represented which aids in the learning process and being able to see precisely what your program is doing. If your totally new to programming then Processing would make a good candidate for a first language.

If you have any questions or suggestions send me a message.

Remember, to force a browser cache refresh and reload any page press Ctrl + F5. Or hold down Shift + Reload.

divider
1.0 Multi-Directional Control

This sketch uses a single state to control multiple directions. Instead of using speed = speed * -1 to rebound an object we simply use appropriate x = x + speed, or x = x - speed or y = y + speed or y = y - 1 to move the object in the desired direction.

Main Class
    
float x, y, w, h;

float direction = 0;

float speed = 12;

void setup() {
  size(800, 300);
  smooth(4);
  background(0);
  noStroke();
  fill (255, 0, 0);
  frameRate(30);
  rectMode(CORNER);

  w = 35;
  h = 35;

  x = 0;
  y = height/2;
}

void draw() {

  background(0);
  rect(x, y, w, h);

  if (direction == 0) {
    y = y - speed;
    if (y < 0) {
      y = 0;
      direction = 1;
    }
  } else if (direction == 1) {
    x = x + speed;
    if (x > width - w) {
      x = width - w;
      direction = 2;
    }
  } else if (direction == 2) {
    y = y + speed;
    if (y > height - h) {
      y = height - h;
      direction = 3;
    }
  } else if (direction == 3) {
    x = x - speed;
    if (x < 0) {
      x = 0;
      direction = 0;
    }
  }
}
    
Up Arrow