Week 12: INTERFACES : CIRCLE GAME


This week for HTM(A)A, our task was to add an interface to one of our existing boards from one of the previous weeks. micro-controller board and program it to do something. I decided to make a game with my phototransistor board...that I am apparently not very good at!



I decided to use processing for this week. The way the game works, is that you want to match the radius of the red circle. The grey circle is reading from the sensor. So by moving you finger far or close, you can control the size and color of the circle. To make this difficult, I added a time limit. To start out, 10 seconds is given to you...but every ten levels it reduces by 1 second!


I also included a enlarging "YOU LOSE" screen when time runs out...a very sad moment. I've seen this screen a lot! Not too proud of that. My High Score? Level 56! But anyways, check out the video and code below!

Here's my source code below:

import processing.serial.*;

Serial myPort;  // The serial port
   float k = 0.25; //alpha level for lowpass function
      float cleanSignal; // global veriable for lowpass function
      
void setup() {
  size (640,480);
  // List all the available serial ports
   cleanSignal = 0; //initialize global value for lowpass function
  println(Serial.list());
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, Serial.list()[0], 9600);
    noStroke();
    smooth();
}
int x = 5;
int y = 5;
int tempx = 0;
int const_ = 20;
int tempy = 0;
int text_size = 0; 
int next_level_text_size = 0;
int level=1;
int current_time = 0;
int timeLimit = 10;
int radius_circle = int(random(60, 200)/2);
int ideal_circle = 0;
void draw() {
  while (myPort.available() > 0) {
    int inByte = checkIdle(myPort.read());
    if (inByte !=0){
      println(lowpass(inByte,k));


 background(0);
 timeLimit = 10-int(level/10);
  if (((current_time+(timeLimit*1000))-(millis()))/1000<=0)
  {
    if (text_size<=0){
    text_size=300;
    }
    bigText("you lose!", "red");
    text_size--;
  }else{
    fill(255);
    textSize(12);
    text("level: "+level, 100,100);
    text("Time left:"+((current_time+(timeLimit*1000))-(millis()))/1000, 500,100);
    ellipseMode(CENTER);
    fill(255,0,0,150);
    noStroke();
    ellipse(width/2, height/2, radius_circle*2, radius_circle*2);
      fill(255-lowpass(inByte,k), 150);
      stroke(255);
    ellipse(width/2, height/2, (255-lowpass(inByte,k))/2*2, (255-lowpass(inByte,k))/2*2);
    if (int((255-lowpass(inByte,k))/2*2)==int(radius_circle*2)){
      level++;
      current_time=millis();
      radius_circle = int(random(60, 200)/2);
      if (text_size==0){
        text_size = 100;
      }
       bigText("next Level = "+ level, "green");
    }
   
  }
    
} } }
     
    
void bigText(String text_, String color_){
  
  if (text_size>0){
    text_size=text_size-20; 
  }
  if (color_=="green"){
      fill(0,255,0);
  }
  if (color_=="red"){
      fill(255,0,0);
  }
  textAlign(CENTER);
  textSize(text_size);
  text(text_, width/2, height/2);
}
    
int checkIdle(int val){
if (val==1 || val==2 || val==3 || val==4){
  return 0;
}
else{
  return val;
}
}

 float lowpass(float signal, float k) 
{
	float oldSignal = cleanSignal;
    cleanSignal = oldSignal + (k * (signal - oldSignal));
    return cleanSignal;
}
  

			

Back to the Top