fadeenk

RGB Game Revised

Nov 26th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.48 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. ControlWindow controlWindow;
  14. Textlabel Title;
  15. Textlabel title;
  16. Textlabel line1;
  17. Textlabel line2;
  18. Textlabel line3;
  19. Textlabel line4;
  20. Textlabel line5;
  21. Textlabel line6;
  22. Textlabel line7;
  23.  
  24. //creating a struct class
  25. public class struct{
  26.   boolean R;
  27.   boolean G;
  28.   boolean B;
  29.   boolean all;
  30. }
  31.  
  32. //declaring global variables and pins
  33. struct correct = new struct();
  34. boolean colorSet = false;
  35. boolean timeOver = false;
  36. float ValueR, ValueG, ValueB, InR, InG, InB;
  37. int pins[] = {0,1,2};
  38. int difficulity;
  39. long startTime;
  40. PFont font;
  41.  
  42. void setup(){ //setup function (runs only once)
  43.   size(360,480); //window size
  44.   background(0); //background color
  45.  
  46.   //declare font type and size
  47.   font = createFont("Times",35);
  48.  
  49.   //set up library entities
  50.   arduino = new Arduino(this, "COM5", 57600);
  51.   minim = new Minim(this);
  52.   cp5 = new ControlP5(this);
  53.   beep = minim.loadSample("beep.mp3", 2048);
  54.   player = minim.loadFile("moon.mp3", 2048);
  55.  
  56.   //creates a new window for the instructions
  57.   controlWindow = cp5.addControlWindow("Instructions", 100, 100, 820, 480)
  58.     .hideCoordinates()
  59.       .setBackground(color(0));
  60.  
  61.   //hides the window till the button is pressed    
  62.   cp5.window("Instructions").hide();
  63.  
  64.   //generate the game title at the top of the main menu
  65.   title = cp5.addTextlabel("RGB")
  66.                     .setText("RGB Game")
  67.                     .setPosition(85,20)
  68.                     .setColorValue(0xff0000ff)
  69.                     ;
  70.  
  71.   //generate the text in the instruction window
  72.   title = cp5.addTextlabel("Title")
  73.                     .setText("Instructions")
  74.                     .setPosition(10,10)
  75.                     .setColorValue(0xffffff00)
  76.                     ;
  77.            
  78.   line1 = cp5.addTextlabel("line1")
  79.                     .setText("Welcome to Fadee Kannah's RGB Arduino Game.")
  80.                     .setPosition(10,60)
  81.                     ;
  82.                    
  83.   line2 = cp5.addTextlabel("line2")
  84.                     .setText("The Objective is to use the knobs to generate a")
  85.                     .setPosition(10,100)
  86.                     ;
  87.  
  88.   line3 = cp5.addTextlabel("line3")
  89.                     .setText("color that is the same or close enough to the random")
  90.                     .setPosition(10,140)
  91.                     ;
  92.  
  93.   line4 = cp5.addTextlabel("line4")
  94.                     .setText("color generated by the computer.")
  95.                     .setPosition(10,180)
  96.                     ;
  97.  
  98.   line5 = cp5.addTextlabel("line5")
  99.                     .setText("This game is basically a value guessing game but")
  100.                     .setPosition(10,240)
  101.                     ;
  102.  
  103.   line6 = cp5.addTextlabel("line6")
  104.                     .setText("it uses RGB values and turns them into colors rather")
  105.                     .setPosition(10,280)
  106.                     ;
  107.   line7 = cp5.addTextlabel("line7")
  108.                     .setText("than just numbers.")
  109.                     .setPosition(10,320)
  110.                     ;                  
  111.                    
  112.   //transfer the text generated to the instruction window                  
  113.   cp5.getController("Title").setWindow(controlWindow);
  114.   cp5.getController("line1").setWindow(controlWindow);
  115.   cp5.getController("line2").setWindow(controlWindow);
  116.   cp5.getController("line3").setWindow(controlWindow);
  117.   cp5.getController("line4").setWindow(controlWindow);
  118.   cp5.getController("line5").setWindow(controlWindow);
  119.   cp5.getController("line6").setWindow(controlWindow);
  120.   cp5.getController("line7").setWindow(controlWindow);
  121.                
  122.      
  123.   //set up the pins as Input pins
  124.   for(int i =0; i<3;i++){
  125.     arduino.pinMode(pins[i], arduino.INPUT);
  126.   }
  127.  
  128.   // play the music
  129.   player.play();
  130.   player.loop();
  131.  
  132.   //select the font for the difficulity selection menu
  133.   cp5.setControlFont(font);
  134.  
  135.   //create buttons for the main menu and instuctions
  136.   cp5.addButton("Instructions",0,20,90,320,45);
  137.   cp5.addButton("Easy",0,20,160,320,45);
  138.   cp5.addButton("Normal",1,20,230,320,45);
  139.   cp5.addButton("Hard",2,20,300,320,45);
  140.   cp5.addButton("Extreme",3,20,370,320,45);
  141.   cp5.addButton("Close",0,300,420,150,45).setWindow(controlWindow);
  142. }
  143.  
  144. void controlEvent(ControlEvent theEvent) { //a function that reads event occurance
  145.   if(theEvent.isController()) { //checks to see if the even is a controller (a button)
  146.     if (theEvent.isFrom(cp5.getController("Instructions"))) {
  147.       cp5.window("Instructions").show();
  148.     }
  149.     else if (theEvent.isFrom(cp5.getController("Close"))) {
  150.       cp5.window("Instructions").hide();
  151.     }
  152.     else{  
  153.       generate(); //generates the random color
  154.       difficulity = (int)theEvent.controller().value(); //sets difficulity
  155.       bhide(); //hides the buttons
  156.       startTime = millis(); //intializes the start Time
  157.     }
  158.   }
  159. }
  160.  
  161. void generate(){ //genrate a random color using the build in random function
  162.   ValueR = random(0,255);  
  163.   ValueG = random(0,255);
  164.   ValueB = random(0,255);
  165.   colorSet = true;
  166. }
  167.  
  168. void bhide(){ //hides all the buttons/title
  169.   cp5.controller("Easy").hide();
  170.   cp5.controller("Normal").hide();
  171.   cp5.controller("Hard").hide();
  172.   cp5.controller("Extreme").hide();
  173.   cp5.controller("Instructions").hide();
  174.   cp5.controller("RGB").hide();
  175. }
  176.  
  177. void draw(){ //main function (loops) when the main menu is loaded it wont do anything
  178.   if(correct.all == true){ // if all the input is correct displays the correct values then a you win message
  179.     fill(255);
  180.     text((int)ValueR+"  "+(int)ValueG+"  "+(int)ValueB, 60 ,70);
  181.     long currentTime = millis();
  182.     if(currentTime-startTime>5000){
  183.       background(255);
  184.       fill(0);
  185.       text("You Win!",100, 220);
  186.     }
  187.   }
  188.   else if(timeOver == true){ //if the user fails to get the right values he/she will get a game over message
  189.     background(255);
  190.     text("Game Over",100, 220);
  191.   }
  192.   else if (colorSet == true){ //when the color is set (after a difficulity is selected)
  193.     //read the values of the potentiometers and map them to the colors range (0-255)
  194.     float In0 = arduino.analogRead(pins[0]);
  195.     InR = map(In0, 0, 1023, 0, 255);
  196.     float In1 = arduino.analogRead(pins[1]);
  197.     InG = map(In1, 0, 1023, 0, 255);
  198.     float In2 = arduino.analogRead(pins[2]);
  199.     InB = map(In2, 0, 1023, 0, 255);
  200.    
  201.     color cur = color(InR,InG,InB); //set the current color to a color data type to be used to display it in hex
  202.    
  203.     background(255); //draw the background
  204.     fill(ValueR, ValueG, ValueB);
  205.     rect(10,10,340,100); //draws the rectangle with color that the user has to guess
  206.     fill(InR,InG,InB);
  207.     rect(10,120,340,200); //draws a rectangle with the color thet the user inputs through the potentiometers
  208.     fill(255);
  209.     text("#"+hex(cur,6),110,230); //gives the hex value of the user inputed color in hex
  210.     fill(InR,0,0);
  211.     rect(10,330,110,100); //draw the red rectanlge for the user input
  212.     fill(255);
  213.     text((int)InR,50,390);//Write the value of the user input for red
  214.     fill(0,InG,0);
  215.     rect(125,330,110,100);//draw the Green rectanlge for the user input
  216.     fill(255);
  217.     text((int)InG,160,390);//Write the value of the user input for green
  218.     fill(0,0,InB);
  219.     rect(240,330,110,100);//draw the blue rectanlge for the user input
  220.     fill(255);
  221.     text((int)InB,280,390);//Write the value of the user input for blue
  222.     fill(0);
  223.     switch(difficulity){ //a switch statement to switch between each difficulity
  224.       case(0):
  225.         text("Easy",145,470); //displays easy instead of timer because the easy mode has no time limit
  226.         check(20); //checks the values with a tolarence of 40 (20 higher and 20 less than the right value)
  227.         break;
  228.       case(1):
  229.         text(counter(90),165,470);//displays a countdown from 90
  230.         check(15); //checks the values with tolarence of 30
  231.         break;
  232.       case(2):
  233.         text(counter(60),165,470);
  234.         check(10);
  235.         break;
  236.       case(3):
  237.         text(counter(30),165,470);
  238.         check(5);
  239.         break;
  240.     }
  241.   }
  242. }
  243.  
  244. void check(int n){ //compares the input values with the random value
  245.   if(abs(ValueR-InR) <= n){//if the values are in range sets the color to correctness to true and plays a tone
  246.     if(correct.R == false){
  247.       correct.R = true;
  248.       beep.trigger();
  249.     }
  250.   }
  251.   else{ //otherwise the color is not correct
  252.     correct.R = false;
  253.   }
  254.   if(abs(ValueG-InG) <= n){
  255.     if(correct.G == false){
  256.     correct.G = true;
  257.     beep.trigger();
  258.     }
  259.   }
  260.   else{
  261.     correct.G = false;
  262.   }
  263.   if(abs(ValueB-InB) <= n){
  264.     if(correct.B == false){
  265.     correct.B = true;
  266.     beep.trigger();
  267.     }
  268.   }
  269.   else{
  270.     correct.B = false;
  271.   }
  272.   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
  273.     correct.all = true;
  274.     startTime = millis();
  275.   }
  276. }
  277.  
  278.  
  279. int counter(int secs){ //a timer function
  280.   long counter;
  281.   long currentTime = millis();
  282.   secs = secs * 1000;
  283.   counter = (startTime+secs) - currentTime;
  284.     if(counter < 0){
  285.       timeOver = true;
  286.       return 0;
  287.     }
  288.     else{return (int)counter/1000;}
  289. }
  290.  
  291. void stop() //terminates the sound when the processing app is closed while pressing a key
  292. { //used to prevent the problem of having the sound on after the app exits
  293.   minim.stop();
  294.   super.stop();
  295. }
Advertisement
Add Comment
Please, Sign In to add comment