Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

nested switch, nested if

$
0
0
Hello!
I am on my way to making a game that prompt user to move a particular alphabet letter to a particular position for a particular sound to play and game to start. Until the player drag the right letter to the right position , game won't start. I have succeeded in dragging the right letter at the right position. The challenge is to get the other letter to show on the screen once they are positioned at the right places.....the example I took is the word "CAR". After the player had succeeded in playing the tune, he has to continue the game by dragging C to the the 3rd position, A to the 1st right position and to the 18th position; the order is not mandatory.....Player is able to do that....but the problem is that the program does not display the letters once positioned at the right places....this happens in function  void car(int selected)

Thanks for your help.


  1. import ddf.minim.*;

    Minim minim;
    AudioSample kick;
    AudioSample snare ;


    color backgroundColor = color(0);
    color squareColor = color(100,175,75);
    color squareColorEmpty = color(51);
    color squareColorHighlight = color(204,102,0);

    /* an extra variable is needed to swap the square and the empty field */
    int newEmptyLocation;
    int newEmptyX;
    int newEmptyY;


    //Image and font titles
    PFont font;
    PImage apple;
    PImage bananas;
    PImage boat;
    PImage car;
    PImage football;
    PImage cricket;
    PImage badminton;
    PImage grapes;
    PImage plane;

    SquareButton[] squares = new SquareButton[27];
    SquareEmpty empty;
    //variables
    int level = 1;

    Particle[] z1 = new Particle[900];
    int xPos = 25;
    int yPos = 575;
    int boxx = 500;
    int boxy = 600;

    /* count the number of moves*/
    int totalMoves;
    boolean playing ;
    boolean gameOver = false ;

    int fontY = 100;
    boolean a = false;
    boolean b = false;
    boolean c = false;
    boolean d = false;
    boolean e = false;
    boolean f = false;
    boolean g = false;
    boolean i = false;
    boolean k = false;
    boolean l = false;
    boolean m = false;
    boolean n = false;
    boolean o = false;
    boolean p = false;
    boolean r = false;
    boolean s = false;
    boolean t = false;
     
    void setup() {
      size(1400,500);
      background(backgroundColor);
      frameRate(15);
      minim = new Minim(this);
     
      //  load Banjo.mp3 from theh data folder
      kick = minim.loadSample("Banjo.mp3",512);


      playing = false ;
      gameOver = false;
      totalMoves = 0;
     
      newEmptyLocation = 0;
      newEmptyX = 1350;
      newEmptyY = 450;
     
      for (int i1=0; i1 < z1.length; i1++) {
        z1[i1] = new Particle(boxx, boxy);
      }
      //load images and font
      //font = loadFont("KristenITC-Regular-50.vlw");
      apple = loadImage("apple.jpeg");
      bananas = loadImage("bananas.jpeg");
      boat = loadImage("boat.jpeg");
      car = loadImage("car.jpeg");
      football = loadImage("football.jpeg");
      grapes = loadImage("grapes.jpeg");
      plane = loadImage("plane.jpeg");
      cricket = loadImage("cricket.jpeg");
      badminton = loadImage("badminton.jpeg");
     
     
      /* initialize each square
       * element 'empty' is used for the empty square
      
       */
     
      squares[0] = new SquareButton(1350,450,64);
      squares[1] = new SquareButton(50,350,65);
      squares[2] = new SquareButton(150,350,66);
      squares[3] = new SquareButton(250,350,67);
      squares[4] = new SquareButton(350,350,68);
      squares[5] = new SquareButton(450,350,69);
      squares[6] = new SquareButton(550,350,70);
      squares[7] = new SquareButton(650,350,71);
      squares[8] = new SquareButton(750,350,72);
      squares[9] = new SquareButton(850,350,73);
      squares[10] = new SquareButton(950,350,74);
      squares[11] = new SquareButton(1050,350,75);
      squares[12] = new SquareButton(1150,350,76);
      squares[13] = new SquareButton(1250,350,77);
     
      squares[14] = new SquareButton(50,450,78);
      squares[15] = new SquareButton(150,450,79);
      squares[16] = new SquareButton(250,450,80);
      squares[17] = new SquareButton(350,450,81);
      squares[18] = new SquareButton(450,450,82);
      squares[19] = new SquareButton(550,450,83);
      squares[20] = new SquareButton(650,450,84);
      squares[21] = new SquareButton(750,450,85);
      squares[22] = new SquareButton(850,450,86);
      squares[23] = new SquareButton(950,450,87);
      squares[24] = new SquareButton(1050,450,88);
      squares[25] = new SquareButton(1150,450,89);
      squares[26] = new SquareButton(1250,450,90);
      empty = new SquareEmpty(1350,450,64);
     
      /* draw the squares on the grid */
      empty.display(squareColorEmpty);
      for(int i=1; i<=26; i++) {
       squares[i].display(squareColor);
      }
     /* and shuffle them to start the game */
      shuffleSquares();
     
    }

     
    void draw() {
      //progresses through each level
      /*if (keyPressed == true && key == '2') {
        level = 0;
      }
      if (level == 0)
        startScreen();*/
      if (level == 1)
            car();
     
      
     
    }

    void PlayKick()
    {
      kick.trigger();
    }

    /* ******SHUFFLING****** */
      /* play many random moves to shuffle the game */
      void shuffleSquares(){
      int shuffleMoves = 0;
      int oldSelected = 0;
      int selected = 26;
      while(shuffleMoves<3000) {
        if(selected!=oldSelected) {
          playSquare(selected);
          oldSelected = selected;
          shuffleMoves++;
        }
        selected = int(random(25))+1;
      }
      playing=true;
    }
     void mousePressed() {
          if(gameOver!=true) {
        for(int i=1; i<=26; i++) {
          if(squares[i].selected()==true) {
            playSquare(i);
            car(i);
            break;

          }
        }
      }
    }

    /* playing */
    void playSquare(int selected) {
        switch(squares[selected].location) {
           case 65:
             if(empty.location==66 ||
                empty.location==78) { moveSquare(selected); }
             break;
           case 66:
             if(empty.location==65 ||
                empty.location==67 ||
                empty.location==79) { moveSquare(selected); }
             break;
           case 67:
             if(empty.location==66 ||
                empty.location==68 ||
                empty.location==80) { moveSquare(selected); }
             break;
           case 68:
             if(empty.location==67 ||
                empty.location==69 ||
                empty.location==81) { moveSquare(selected); }
             break;
           case 69:
             if(empty.location==68 ||
                empty.location==70 ||
                empty.location==82) { moveSquare(selected); }
             break;
           case 70:
             if(empty.location==69 ||
                empty.location==71 ||
                empty.location==83) { moveSquare(selected); }
             break;
           case 71:
             if(empty.location==70 ||
                empty.location==72 ||
                empty.location==84) { moveSquare(selected); }
             break;
           case 72:
             if(empty.location==71 ||
                empty.location==73 ||
                empty.location==85) { moveSquare(selected); }
             break;
           case 73:
             if(empty.location==72 ||
                empty.location==74 ||
                empty.location==86) { moveSquare(selected); }
                break;
             case 74:
             if(empty.location==73 ||
                empty.location==75 ||
                empty.location==87) { moveSquare(selected); }
             break;
           case 75:
             if(empty.location==74 ||
                empty.location==76 ||
                empty.location==88) { moveSquare(selected); }
             break;
           case 76:
             if(empty.location==75 ||
                empty.location==77 ||
                empty.location==89) { moveSquare(selected); }
             break;
           case 77:
             if(empty.location==76 ||
                empty.location==90) { moveSquare(selected); }
             break;
           case 78:
             if(empty.location==65 ||
                empty.location==79) { moveSquare(selected); }
             break;
           case 79:
             if(empty.location==66 ||
                empty.location==78 ||
                empty.location==80) { moveSquare(selected); }
             break;
           case 80:
             if(empty.location==67 ||
                empty.location==79 ||
                empty.location==81) { moveSquare(selected); }
             break;
           case 81:
             if(empty.location==68 ||
                empty.location==80 ||
                empty.location==82) { moveSquare(selected); }
             break;
           case 82:
             if(empty.location==69 ||
                empty.location==81 ||
                empty.location==83) { moveSquare(selected); }
                break;
           case 83:
             if(empty.location==70 ||
                empty.location==82 ||
                empty.location==84) { moveSquare(selected); }
             break;
           case 84:
             if(empty.location==71 ||
                empty.location==83 ||
                empty.location==85) { moveSquare(selected); }
             break;
           case 85:
             if(empty.location==72 ||
                empty.location==84 ||
                empty.location==86) { moveSquare(selected); }
             break;
           case 86:
             if(empty.location==73 ||
                empty.location==85 ||
                empty.location==87) { moveSquare(selected); }
             break;
           case 87:
             if(empty.location==74 ||
                empty.location==86 ||
                empty.location==88) { moveSquare(selected); }
             break;
           case 88:
             if(empty.location==75 ||
                empty.location==87 ||
                empty.location==89) { moveSquare(selected); }
             break;
           case 89:
             if(empty.location==76 ||
                empty.location==88 ||
                empty.location==90) { moveSquare(selected); }  
                break;
           case 90:
             if(empty.location==77 ||
                empty.location==89 ||
                empty.location==64) { moveSquare(selected); }    
               
             break;
           case 64:
             if (empty.location==90) {
                moveSquare(selected);
              /* the game can only finish when the piece on field 0 is moved
                 check if the game is solved correctly */
                checkResult();
            }
            break;
        }
        println("selected: "+squares[selected].location +  " /empty.location: "+ empty.location);
    }

    /* move square */
    void moveSquare(int selected) {
             newEmptyLocation = squares[selected].location;
             newEmptyX = squares[selected].x;
             newEmptyY = squares[selected].y;
     
             squares[selected].location = empty.location;
             squares[selected].x = empty.x;
             squares[selected].y = empty.y;
            
             empty.location = newEmptyLocation;
             empty.x = newEmptyX;
             empty.y = newEmptyY;
     
             empty.display(squareColorEmpty);
             squares[selected].display(squareColor);
             if (playing==true) { totalMoves++; }
    }
    void checkResult() {
      gameOver = true;
      int c = 1;
      int d = 65 ;
      while (gameOver==true && c<=26) {
        if (squares[c].location!=d) {
          gameOver=false;
        }
        c++;
        d++;
      }
      if (gameOver==true) {
        empty.display(backgroundColor);
        for(int i=1; i<=26; i++) {
          squares[i].display(squareColorHighlight);
        }
        noStroke();
        fill(0);
        textAlign(LEFT);
       // textFont(loadFont("LucidaSans-Demi-12.vlw"));
        text("Well done!",310,25);
        text(totalMoves+" moves",310,50);
        text("Press a key:",310,225);
        text("[R] restart",310,250);
        text("[Q] quit",310,275);
      }
    }


    /* ***************************************** spelling classes ******************************************** */
     
     void car() {//first picture
                 text("Click S to play the sound",300,300);
              //image(car, 450, 100);
         
     }
     
     
     
     void car( int selected) {
      
               image(car, 450, 100);
             if(squares[selected].location ==83 && squares[selected] == squares[19])
             {
                 PlayKick() ;
                 text("Go on!", 100,100);
                
                
                 switch(squares[selected].location)
                 {
                   case 67 :if(squares[selected] == squares[3]){
                     int myInt = 67 ;
                     char myChar =(char)myInt ;
                     text(myChar,100,fontY);
                     text("Well done!", 150, 50);}
                     break ;
                   case 65: if(squares[selected] == squares[1])
                   {
                    
                     int myInt = 65 ;
                     char myChar =(char)myInt ;
                     text(myChar,110,fontY);
                     text("Well done!", 150, 80);
                   }
                  break;
                 case 82 :
                  if(squares[selected] == squares[18]) {
                   int myInt = 82 ;
                   char myChar =(char)myInt ;
                   text(myChar,120,fontY);
                   text("Well done!", 150, 120);
         // fireworks();
         //fill(0, 0, 0);
       }
       break;
      }
      }
      else
        text("Wrong!", 100,100);

                    
           
            
      
                 /*if(squares[selected].location == 67 && squares[selected] == squares[3]) {
                 int myInt = 67 ;
                 char myChar =(char)myInt ;
                 text(myChar,100,fontY);
           
                text("Well done!", 150, 50);
                 }
                  if(squares[selected].location == 65 && squares[selected] == squares[1]) {
         
                     int myInt = 65 ;
                     char myChar =(char)myInt ;
                     text(myChar,110,fontY);
                     text("Well done!", 150, 80);
          
                     }
                  if(squares[selected].location == 82 && squares[selected] == squares[18]) {
                   int myInt = 82 ;
                   char myChar =(char)myInt ;
                   text(myChar,120,fontY);
                   text("Well done!", 150, 120);
         // fireworks();
         //fill(0, 0, 0);
       }*/
      }
     
     void fireworks() {//method to run the fireworks at the end of the game
     
      if (dist(boxx, boxy, xPos, yPos) < 30) {
        point(boxx, boxy);
        for (int i1=0;i1<z1.length;i1++) {
          z1[i1] = new Particle(500, 500);
        }
      }
     
      for (int i1=0;i1<z1.length;i1++) {
        z1[i1].render();
      }
    }
    class Particle {// class to create the array of ellipses used in the firework class
     
      float xPos=0;
      float yPos=0;
      float a1=0;
      float g=0;
      float m=0;
     
     
      int counter=0;
     
      public Particle(float x, float y) {
        xPos = x;
        yPos = y;
        a1 = random(360);
        m = random(2);
        counter=0;
      }
     
      public void render() {
        counter++;
     
        if (counter > 100) {
          xPos += m*cos(radians(a1));
          yPos += m*sin(radians(a1));
          yPos += g;
          g += 0.1;
        }
        else {
          yPos-=5;
        }
        fill(255, 217, 0, random(75));
        noStroke();
        ellipse(xPos, yPos, random(2, 10), random(2, 10));
      }
    }
    //End of code

     /* ****************************** classes ****************************** */
     
    class Square {
      char letter ; // the number of the square
      int location; // the locations of the square
      int x; // the x position of the square
      int y; // the y position of the square
      int s; // the size of the square
      color c; // the color of the square
      boolean selected;
     
       boolean selected() {
    /* check if the mouse is over the square */
       if (mouseX >= x-(s/2) && mouseX <= x+(s/2) &&
             mouseY >= y-(s/2) && mouseY <= y+(s/2)) {
          return true;
        } else {
          return false;
        }
      }
     
      }
     
    class SquareButton extends Square {
     
      SquareButton (int tempX, int tempY, int tempN) {
        x = tempX;
        y = tempY;
        s = 96;
        c = color(192);
        letter = (char)tempN;
        location = tempN;
      }
     
      void display(int squareColor) {
        /* draw the square */
        int margin = 49;
        rectMode(CENTER);
        noStroke();
        fill(squareColor);
        rect(x,y,s,s);
       
        /* draw the number inside the square */
        noStroke();
        fill(255);
        //textFont(loadFont("TrebuchetMS-Bold-48.vlw"), 48);
        textAlign(CENTER);
        text(letter,x,y+18);
      }
    }
     
    class SquareEmpty extends Square {
     
      SquareEmpty(int tempX, int tempY, int tempN) {
        x = tempX;
        y = tempY;
        s = 96;
        c = color(42);
        letter = (char)tempN;
        location = 64;
      }
      void display(int squareColor) {
        /* draw the empty square */
        rectMode(CENTER);
        noStroke();
        fill(squareColor);
        rect(x,y,s,s);
      }
     
    }






Viewing all articles
Browse latest Browse all 1768

Trending Articles