fadeenk

RGB game

Nov 18th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.25 KB | None | 0 0
  1. //importing libraries
  2. import cc.arduino.*;
  3. import processing.serial.*;
  4. import controlP5.*;
  5. import ddf.minim.*;
  6.  
  7. //declare library entities
  8. Minim minim;
  9. AudioSample beep;
  10. AudioPlayer player;
  11. ControlP5 cp5;
  12. Arduino arduino;
  13.  
  14. //creating a struct class
  15. public class struct{
  16.   boolean R;
  17.   boolean G;
  18.   boolean B;
  19.   boolean all;
  20. }
  21.  
  22. //declaring global variables and pins
  23. struct correct = new struct();
  24. boolean colorSet = false;
  25. boolean timeOver = false;
  26. float ValueR, ValueG, ValueB, InR, InG, InB;
  27. int pins[] = {0,1,2};
  28. int difficulity;
  29. long startTime;
  30. PFont font;
  31.  
  32. void setup(){ //setup function (runs only once)
  33.   size(360,480); //window size
  34.   background(0); //background color
  35.  
  36.   //declare font type and size
  37.   font = createFont("Times",35);
  38.  
  39.   //set up library entities
  40.   arduino = new Arduino(this, "COM5", 57600);
  41.   minim = new Minim(this);
  42.   cp5 = new ControlP5(this);
  43.   beep = minim.loadSample("beep.mp3", 2048);
  44.   player = minim.loadFile("moon.mp3", 2048);
  45.  
  46.   //set up the pins as Input pins
  47.   for(int i =0; i<3;i++){
  48.     arduino.pinMode(pins[i], arduino.INPUT);
  49.   }
  50.  
  51.   // play the music
  52.   player.play();
  53.   player.loop();
  54.  
  55.   //select the font for the difficulity selection menu
  56.   cp5.setControlFont(font);
  57.  
  58.   //create buttons for the difficulites
  59.   cp5.addButton("Easy",0,20,20,320,45);
  60.   cp5.addButton("Normal",1,20,90,320,45);
  61.   cp5.addButton("Hard",2,20,160,320,45);
  62.   cp5.addButton("Extreme",3,20,230,320,45);
  63. }
  64.  
  65. void controlEvent(ControlEvent theEvent) { //a function that reads event occurance
  66.   if(theEvent.isController()) { //checks to see if the even is a controller (a button)
  67.     generate(); //generates the random color
  68.     difficulity = (int)theEvent.controller().value(); //sets difficulity
  69.     bhide(); //hides the buttons
  70.     startTime = millis(); //intializes the start Time
  71.   }
  72. }
  73.  
  74. void generate(){ //genrate a random color using the build in random function
  75.   ValueR = random(0,255);  
  76.   ValueG = random(0,255);
  77.   ValueB = random(0,255);
  78.   colorSet = true;
  79. }
  80.  
  81. void bhide(){ //hides all the buttons
  82.   cp5.controller("Easy").hide();
  83.   cp5.controller("Normal").hide();
  84.   cp5.controller("Hard").hide();
  85.   cp5.controller("Extreme").hide();
  86. }
  87.  
  88. void draw(){ //main function (loops) when the main menu is loaded it wont do anything
  89.   if(correct.all == true){ // if all the input is correct displays the correct values then a you win message
  90.     fill(255);
  91.     text((int)ValueR+"  "+(int)ValueG+"  "+(int)ValueB, 60 ,70);
  92.     long currentTime = millis();
  93.     if(currentTime-startTime>5000){
  94.       background(255);
  95.       fill(0);
  96.       text("You Win!",100, 220);
  97.     }
  98.   }
  99.   else if(timeOver == true){ //if the user fails to get the right values he/she will get a game over message
  100.     background(255);
  101.     text("Game Over",100, 220);
  102.   }
  103.   else if (colorSet == true){ //when the color is set (after a difficulity is selected)
  104.     //read the values of the potentiometers and map them to the colors range (0-255)
  105.     float In0 = arduino.analogRead(pins[0]);
  106.     InR = map(In0, 0, 1023, 0, 255);
  107.     float In1 = arduino.analogRead(pins[1]);
  108.     InG = map(In1, 0, 1023, 0, 255);
  109.     float In2 = arduino.analogRead(pins[2]);
  110.     InB = map(In2, 0, 1023, 0, 255);
  111.    
  112.     color cur = color(InR,InG,InB); //set the current color to a color data type to be used to display it in hex
  113.    
  114.     background(255); //draw the background
  115.     fill(ValueR, ValueG, ValueB);
  116.     rect(10,10,340,100); //draws the rectangle with color that the user has to guess
  117.     fill(InR,InG,InB);
  118.     rect(10,120,340,200); //draws a rectangle with the color thet the user inputs through the potentiometers
  119.     fill(255);
  120.     text("#"+hex(cur,6),110,230); //gives the hex value of the user inputed color in hex
  121.     fill(InR,0,0);
  122.     rect(10,330,110,100); //draw the red rectanlge for the user input
  123.     fill(255);
  124.     text((int)InR,50,390);//Write the value of the user input for red
  125.     fill(0,InG,0);
  126.     rect(125,330,110,100);//draw the Green rectanlge for the user input
  127.     fill(255);
  128.     text((int)InG,160,390);//Write the value of the user input for green
  129.     fill(0,0,InB);
  130.     rect(240,330,110,100);//draw the blue rectanlge for the user input
  131.     fill(255);
  132.     text((int)InB,280,390);//Write the value of the user input for blue
  133.     fill(0);
  134.     switch(difficulity){ //a switch statement to switch between each difficulity
  135.       case(0):
  136.         text("Easy",145,470); //displays easy instead of timer because the easy mode has no time limit
  137.         check(20); //checks the values with a tolarence of 40 (20 higher and 20 less than the right value)
  138.         break;
  139.       case(1):
  140.         text(counter(90),165,470);//displays a countdown from 90
  141.         check(15); //checks the values with tolarence of 30
  142.         break;
  143.       case(2):
  144.         text(counter(60),165,470);
  145.         check(10);
  146.         break;
  147.       case(3):
  148.         text(counter(30),165,470);
  149.         check(5);
  150.         break;
  151.     }
  152.   }
  153. }
  154.  
  155. void check(int n){ //compares the input values with the random value
  156.   if(abs(ValueR-InR) <= n){//if the values are in range sets the color to correctness to true and plays a tone
  157.     if(correct.R == false){
  158.       correct.R = true;
  159.       beep.trigger();
  160.     }
  161.   }
  162.   else{ //otherwise the color is not correct
  163.     correct.R = false;
  164.   }
  165.   if(abs(ValueG-InG) <= n){
  166.     if(correct.G == false){
  167.     correct.G = true;
  168.     beep.trigger();
  169.     }
  170.   }
  171.   else{
  172.     correct.G = false;
  173.   }
  174.   if(abs(ValueB-InB) <= n){
  175.     if(correct.B == false){
  176.     correct.B = true;
  177.     beep.trigger();
  178.     }
  179.   }
  180.   else{
  181.     correct.B = false;
  182.   }
  183.   if(correct.R == true && correct.G == true && correct.B == true){//when all the colors are correct set the value of all to true and reset the timer
  184.     correct.all = true;
  185.     startTime = millis();
  186.   }
  187. }
  188.  
  189.  
  190. int counter(int secs){ //a timer function
  191.   long counter;
  192.   long currentTime = millis();
  193.   secs = secs * 1000;
  194.   counter = (startTime+secs) - currentTime;
  195.     if(counter < 0){
  196.       timeOver = true;
  197.       return 0;
  198.     }
  199.     else{return (int)counter/1000;}
  200. }
  201.  
  202. void stop() //terminates the sound when the processing app is closed while pressing a key
  203. { //used to prevent the problem of having the sound on after the app exits
  204.   minim.stop();
  205.   super.stop();
  206. }
Advertisement
Add Comment
Please, Sign In to add comment