Lesson Notes 31

by golan @ 2:51 pm September 22, 2011

While{} blocK:

int a = 0;
int b = 1;
int sum = a+b;
 
while (sum < 10000) { // some boolean test!
  println(sum);
  a = b;
  b = sum;
  sum = a+b;
}

Trigger an event periodically:

int poopPeriod = 3000;
int lastPoopTime = 0;
 
void setup(){ 
}
 
void draw(){
 if ((millis() - lastPoopTime) >= poopPeriod) {
   poop();
 }
}
 
void poop(){
  // only printed output for now; visuals are up to you
  println("At " + millis() + ", I pooped"); 
  lastPoopTime = millis(); 
}

Function that returns a value:

void setup(){
  int mySum = addThreeInts (3,8,11); 
  float f = cos(4.0);
  println("My Sum = " + mySum); 
}
 
int addThreeInts (int a, int b, int c){
  int sum = a + b + c;
  return sum;
}

Interpolation (Blurred Integrator / “Zeno’s Interpolation”) to the Mouse

float px = 0;
float py = 0;
 
void setup(){
  size(400,400);
}
 
void draw(){
  background(127);
 
  float A = 0.95;
  float B = 1.0-A;
  px = A*px + B*mouseX;
  py = A*py + B*mouseY;
  ellipse (px,py, 30,30);
}

Damping a variable down to zero / Triggering an event sequence

float py, px;
float vy, vx;
void setup(){
  size(400,400);
  initiateMovementSequence();
}
 
void draw(){
  float grayColor = map(px, 0,width, 0,255);
  background(grayColor);
  py += vy;
  px += vx;
  vy *= 0.96;
  vx *= 0.96;
  ellipse(px,py, 30,30);
}
 
void mousePressed(){
  // clicking resets the action
  initiateMovementSequence();
}
 
void initiateMovementSequence(){
  px = width/2;
  py = height/2;
  vx = random(-5,5);
  vy = random(-5,5);
}

Simple Pong

float px, py;
float vx, vy;
float diam = 80;
 
 
void setup(){
  size(400,350);
  px = width/2;
  py = height/2;
  vx = random(-10,10);
  vy = random(-10,10);
}
 
 
void draw(){
  background(127);
  px += vx;
  py += vy;
  if ((px < (diam/2)) || (px > (width - diam/2))){
    vx = -vx;
  }
  if ((py < (diam/2)) || (py > (height - diam/2))){
    vy = -vy;
  }
 
  ellipse(px,py, 80,80);
}
 
void mousePressed(){
  // clicking the mouse randomizes direction
  vx = random(-10,10);
  vy = random(-10,10);
}

Bounce-triggered Speech Synthesizer (just for yuks):

import guru.ttslib.*; // gotta download and install that; don't forget.
 
float px, py;
float vx, vy;
float diam = 40;
int myLetterCount = 0;
SimpleThread thread1;
 
void setup() {
  size(400,400);
  px = width/2;
  py = height/2;
  vx = random(-2,2);
  vy = random(-2,2);
  thread1 = new SimpleThread("voicer");
  thread1.start();
}
 
void exit(){
  thread1.quit();
}
 
void draw(){
  background(127);
  px += vx;
  py += vy;
 
  boolean didBounce = false;
  if ((px < (diam/2)) || (px > (width - diam/2))){
    vx = -vx;
    didBounce = true;
  }
  if ((py < (diam/2)) || (py > (height - diam/2))){
    vy = -vy;
    didBounce = true;
  }
 
  if (didBounce){
    char myChar = (char)(myLetterCount + 'a');
    String myString = "" + myChar;
    thread1.speak(myString);
    myLetterCount = (myLetterCount + 1)%26; // 0..25
  }
 
  ellipse(px,py, diam,diam);
}
 
 
//========================================================================
 
public class SimpleThread extends Thread {
  // From Dan Shiffman's Tutorial. 
  private boolean running;             // Is the thread running?  Yes or no?
    private int toWait;                // How many milliseconds should we wait in between executions?
    private String id;                 // Thread name
    private int count;                 // counter
    TTS tts;
    String sayString =  "";
    boolean freshMeat;
 
    // Constructor, create the thread. It is not running by default
    public SimpleThread (String threadName){
        toWait = 3;
        running = false;
        freshMeat = false;
        id = threadName;
        count = 0;
        tts = new TTS();
    }
 
    public void speak(String toSay){
      sayString = toSay;
      freshMeat = true;
    }
 
    public void start () {
        running = true;
        println ("Starting");
        super.start();
    }
 
    public void run () {
        while (running){
            count++;
            if (freshMeat){
              tts.speak(sayString);
              freshMeat = false;
            }
            try {
                sleep((long)(toWait));
            } 
            catch (Exception e) {
            }
        }
        println(id + " thread is done!");  // The thread is done when we get to the end of run()
    }
 
    public void quit(){
        println("Quitting Thread."); 
        running = false;  // Setting running to false ends the loop in run()
        interrupt(); // in case the thread is waiting. . .
    }
}

»

TrackBack URI

Leave a comment

You must be logged in to post a comment.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2017 CMU Electronic Media Studio II, Fall 2011, Section A | powered by WordPress with Barecity