Game

edited December 2015 in Library Questions

I am trying to create a puzzle game, I had my code working where there would be two level screens with a set of pieces that I could drag onto a grid, however for some reason the pieces are no longer showing up on my level screen and I can not drag them when they do show up. Any help would be greatly appreciated.

// Global Variables import ddf.minim.*; AudioPlayer player; Minim minim;

PImage imgB1; int value = 0; int clickedCount = 0; int x; int y; boolean shapeSelected;

boolean shapeGenerated = false;

int [] type = new int[100]; int [][] coordinate = new int[100][2];

int selectedSquare = -1; Shape [] square = new Shape [100];

/*setup() sets the size of the window, and holds the backgorund image. */ void setup() { minim = new Minim(this); player = minim.loadFile("Jack's Mannequin - Dark Blue (8 bit).mp3", 2048); player.play();

for (int i = 0; i < 100; i++) { square[i] = new Shape (0, 0, 0); }

size(600, 600); imgB1 = loadImage("puzzle.jpg"); }

// draw() includes all of the different backgrounds that will be used throughtout the code, each of which are defined below. /* draw() is a continuous loop which will continue to redraw each of the backgrounds throughout the code*/ void draw() { if (clickedCount == 0) { mainScreen(); } if (clickedCount == 1) {
ruleScreen(); }

if (clickedCount == 2) { Level1();

if (shapeGenerated == false)

  square[22].numShape = 12;
  square[22].x = 20;
  square[22].y = 100;

  square[23].numShape = 0;
  square[23].x = 110;
  square[23].y = 100;

  square[24].numShape = 0;
  square[24].x = 180;
  square[24].y = 100;

  square[25].numShape = 1;
  square[25].x = 20;
  square[25].y = 190;

  square[26].numShape = 1;
  square[26].x = 65;
  square[26].y = 190;

  square[27].numShape = 1;
  square[27].x = 110;
  square[27].y = 190;

  square[28].numShape = 1;
  square[28].x = 155;
  square[28].y = 190;

  square[29].numShape = 1;
  square[29].x = 200;
  square[29].y = 190;

  square[30].numShape = 13;
  square[30].x = 20;
  square[30].y = 240;

  square[31].numShape = 14;
  square[31].x = 90;
  square[31].y = 240;

  square[32].numShape = 7;
  square[31].x = 140;
  square[31].y = 240;

  square[32].numShape = 7;
  square[32].x = 170;
  square[32].y = 240;

  square[33].numShape = 9;
  square[33].x = 200;
  square[33].y = 240;




//shapeGenerated = true;

}

if (clickedCount == 3) { Level2(); { if (shapeGenerated == false) {

square[0].numShape = 0;
square[0].x = 20;
square[0].y = 100;

square[1].numShape = 0;
square[1].x = 85;
square[1].y = 100;

square[2].numShape = 0;
square[2].x = 150;
square[2].y = 100;

square[3].numShape = 0;
square[3].x = 215;
square[3].y = 100;

square[4].numShape = 1;
square[4].x = 20;
square[4].y = 165;

square[5].numShape = 1;
square[5].x = 73;
square[5].y = 165;

square[6].numShape = 1;
square[6].x = 126;
square[6].y = 165;

square[7].numShape = 1;
square[7].x = 179;
square[7].y = 165;

square[8].numShape = 1;
square[8].x = 232;
square[8].y = 165;

square[9].numShape = 2;
square[9].x = 20;
square[9].y = 210;

square[10].numShape = 3;
square[10].x = 90;
square[10].y = 210;

square[11].numShape = 4;
square[11].x = 180;
square[11].y = 210;

square[12].numShape = 5;
square[12].x = 180;
square[12].y = 235;

square[13].numShape = 6;
square[13].x = 20;
square[13].y = 260;

square[14].numShape = 7;
square[14].x = 50;
square[14].y = 260;

square[15].numShape = 8;
square[15].x = 80;
square[15].y = 260;

square[16].numShape = 9;
square[16].x = 110;
square[16].y = 260;

square[17].numShape = 9;
square[17].x = 140;
square[17].y = 260;

square[18].numShape = 9;
square[18].x = 170;
square[18].y = 260;

square[19].numShape = 10;
square[19].x = 200;
square[19].y = 260;

square[20].numShape = 11;
square[20].x = 110;
square[20].y = 310;

square[21].numShape = 11;
square[21].x = 110;
square[21].y = 340;

}

shapeGenerated = true; }

}

}

//This sets the program up to be able to change backgrounds everytime a key is pressed. void keyPressed() { clickedCount++; }

//This is the code for the mainscreen background void mainScreen() { background(214); image(imgB1, 0, 0);

PFont font; font = createFont("BradleyHandITCTT-Bold", 50); textFont(font); textAlign(CENTER, CENTER); fill(255); text("Puzzle Buzzle", 300, 100); textAlign(CENTER);

font = createFont("BradleyHandITCTT-Bold", 30); textFont(font); textAlign(CENTER, BOTTOM); fill(0, 100, 300); text("press any key to begin", 300, 550); }

//after a key is pressed the program will change to the next page, the rule page, which is setup as follows. void ruleScreen() { background(0, 150, 200); PFont font; font = createFont("BradleyHandITCTT-Bold", 50); textFont(font); font = createFont("BradleyHandITCTT-Bold", 40); textFont(font); fill(255); text("Rules", 300, 90); textAlign(CENTER); font = createFont("BradleyHandITCTT-Bold", 25); textFont(font); fill(255); text("Object: Fill the puzzle board with the given pieces.", 300, 130); textAlign(CENTER); text("Click once to pick up a piece, and click again to", 300, 180); textAlign(CENTER); text("drop the piece.", 300, 210); textAlign(CENTER); text("press any key to continue", 300, 420); textAlign(CENTER); }

//The next screen will be the first level, which is setuip as follows. void Level1() { background(#06C8D1); PFont font; font = createFont("BradleyHandITCTT-Bold", 50); textFont(font); textAlign(CENTER, CENTER); fill(255); text("Level 1", 300, 50);

for (int x = 350; x <= 550; x=x+20) { line(x, 250, x, 450); }

for (int y = 250; y <= 450; y=y+20) { line(350, y, 550, y); }

font = createFont("BradleyHandITCTT-Bold", 30); textFont(font); textAlign(CENTER, BOTTOM); fill(255); text("press any key to skip level", 300, 550); }

//The final screen is the second level, which is setup as follows. void Level2() { background(#06C8D1); PFont font; font = createFont("BradleyHandITCTT-Bold", 50); textFont(font); textAlign(CENTER, CENTER); fill(255); text("Level 2", 300, 50);

for (int x = 350; x <= 550; x=x+20) { line(x, 250, x, 450); }

for (int y = 250; y <= 450; y=y+20) { line(350, y, 550, y); }

class Shape { int numShape = 0; int x = 0; int y = 0; int wSquare = 0; int hSquare = 0;

Shape(int numS, int px, int py) { numShape = numS; x = px; y = py;

if (numShape == 0)
{
  wSquare = 60;
  hSquare = 60;
}

if (numShape == 1)
{
  wSquare = 40;
  hSquare = 40;
}

if (numShape == 2)
{
  wSquare = 60;
  hSquare = 40;
}

if (numShape == 3)
{
  wSquare = 80;
  hSquare = 40;
}

if (numShape == 4)
{
  wSquare = 80;
  hSquare = 20;
}

if (numShape == 5)
{
  wSquare = 60;
  hSquare = 20;
}

if (numShape == 6) 
{
  wSquare = 20;
  hSquare = 100;
}

if (numShape == 7)
{
  wSquare = 20;
  hSquare = 80;
}

if (numShape == 8)
{
  wSquare = 20;
  hSquare = 60;
}

if (numShape == 9)
{
  wSquare = 20;
  hSquare = 40;
}

if (numShape == 10)
{
  wSquare = 20;
  hSquare = 20;
}

if (numShape == 11)
{
  wSquare = 60;
  hSquare = 20;
}

if (numShape == 12)
{
  wSquare = 80;
  hSquare = 80;
}

if (numShape == 13)
{
  wSquare = 60;
  hSquare = 100;
}

if (numShape == 14) 
{
  wSquare = 40;
  hSquare = 80;
}

if (numShape == 15)
{
  wSquare = 100;
  hSquare = 20;
}

}

void drawShape() { if (numShape == 0) { fill(#F59454); rect(x, y, 60, 60); }

if (numShape == 1)
{
  fill(#71F554);
  rect(x, y, 40, 40);
}

if (numShape == 2)
{
  fill(#FA7EF2);
  rect(x, y, 60, 40);
}

if (numShape == 3)
{
  fill(#7EFAF7);
  rect(x, y, 80, 40);
}
if (numShape == 4)

{
  fill(#1D0CEA);
  rect(x, y, 80, 20);
}
if (numShape == 5)
{
  fill(#C10CEA);
  rect(x, y, 60, 20);
}
if (numShape == 6)
{
  fill(#FC0A27);
  rect(x, y, 20, 100);
}
if (numShape == 7)
{
  fill(#3B40FF);
  rect(x, y, 20, 80);
}
if (numShape == 8)
{
  fill(#F2EC31);
  rect(x, y, 20, 60);
}
if (numShape == 9)
{
  fill(#3EA516);
  rect(x, y, 20, 40);
}
if (numShape == 10)
{
  fill(#F76B48);
  rect(x, y, 20, 20);
}
if (numShape == 11)
{
  fill(#F022DB);
  rect(x, y, 60, 20);
}
if (numShape == 12)
{
  fill(#19F7F5);
  rect(x, y, 80, 80);
}
if (numShape == 13)
{
  fill(#F73719);
  rect(x, y, 60, 100);
}
if (numShape == 14)
{
  fill(#F8FF3B);
  rect(x, y, 40, 80);
}
if (numShape == 15)
{
  fill(#FFD071);
  rect(x, y, 100, 20);
}

}

void mousePressed() { for (int i = 0; i < 22; i++) { if (mouseX >= square[i].x && mouseX <= square[i].x + square[i].wSquare) { if (mouseY >= square[i].y && mouseY <= square[i].y + square[i].hSquare) { selectedSquare = i;

    break;
  }
}

} }

void mouseDragged() { if (selectedSquare != -1) { if (mouseX > square[selectedSquare].x + square[selectedSquare].wSquare) { square[selectedSquare].x += 5; } if (mouseX < square[selectedSquare].x) { square[selectedSquare].x -= 5; } if (mouseY > square[selectedSquare].y + square[selectedSquare].hSquare) { square[selectedSquare].y += 5; } if (mouseY < square[selectedSquare].y) { square[selectedSquare].y -= 5; } } } } }

Answers

  • Edit post. Select code. Press Ctrl + o.

  • in Level 1 you need

        for (Shape s1:square) {
          s1.drawShape();
        }
    
  • edited December 2015

    also, mousePressed and mouseDragged must be outside the class.

    I removed image and sound with // to be able to run it

  • // Global Variables 
    
    import ddf.minim.*; 
    AudioPlayer player; 
    Minim minim;
    PImage imgB1; 
    int value = 0; 
    int clickedCount = 0; 
    int x; 
    int y; 
    boolean shapeSelected;
    boolean shapeGenerated = false;
    int [] type = new int[100]; 
    int [][] coordinate = new int[100][2];
    int selectedSquare = -1; 
    
    Shape [] square = new Shape [100];
    
    /*setup() sets the size of the window, and holds the backgorund image. */
    
    void setup() {
      size(600, 600); 
    
      minim = new Minim(this); 
      //player = minim.loadFile("Jack's Mannequin - Dark Blue (8 bit).mp3", 2048); 
      //player.play();
      for (int i = 0; i < 100; i++) { 
        square[i] = new Shape (0, 0, 0);
      }
    
      imgB1 = loadImage("puzzle.jpg");
    }
    
    // draw() includes all of the different backgrounds that will be used throughtout the code, each of which are defined below.
    /* draw() is a continuous loop which will continue to redraw each of the backgrounds throughout the code*/
    void draw() { 
      if (clickedCount == 0) { 
        mainScreen();
      } 
      if (clickedCount == 1) {
        ruleScreen();
      }
      if (clickedCount == 2) { 
        Level1();
        for (Shape s1:square) {
          s1.drawShape();
        }
        if (shapeGenerated == false) {
    
          square[22].numShape = 12;
          square[22].x = 20;
          square[22].y = 100;
    
          square[23].numShape = 0;
          square[23].x = 110;
          square[23].y = 100;
    
          square[24].numShape = 0;
          square[24].x = 180;
          square[24].y = 100;
    
          square[25].numShape = 1;
          square[25].x = 20;
          square[25].y = 190;
    
          square[26].numShape = 1;
          square[26].x = 65;
          square[26].y = 190;
    
          square[27].numShape = 1;
          square[27].x = 110;
          square[27].y = 190;
    
          square[28].numShape = 1;
          square[28].x = 155;
          square[28].y = 190;
    
          square[29].numShape = 1;
          square[29].x = 200;
          square[29].y = 190;
    
          square[30].numShape = 13;
          square[30].x = 20;
          square[30].y = 240;
    
          square[31].numShape = 14;
          square[31].x = 90;
          square[31].y = 240;
    
          square[32].numShape = 7;
          square[31].x = 140;
          square[31].y = 240;
    
          square[32].numShape = 7;
          square[32].x = 170;
          square[32].y = 240;
    
          square[33].numShape = 9;
          square[33].x = 200;
          square[33].y = 240;
        }
    
        shapeGenerated = true;
      }
    
      if (clickedCount == 3) { 
        Level2(); 
        { 
          if (shapeGenerated == false) {
            square[0].numShape = 0;
            square[0].x = 20;
            square[0].y = 100;
    
            square[1].numShape = 0;
            square[1].x = 85;
            square[1].y = 100;
    
            square[2].numShape = 0;
            square[2].x = 150;
            square[2].y = 100;
    
            square[3].numShape = 0;
            square[3].x = 215;
            square[3].y = 100;
    
            square[4].numShape = 1;
            square[4].x = 20;
            square[4].y = 165;
    
            square[5].numShape = 1;
            square[5].x = 73;
            square[5].y = 165;
    
            square[6].numShape = 1;
            square[6].x = 126;
            square[6].y = 165;
    
            square[7].numShape = 1;
            square[7].x = 179;
            square[7].y = 165;
    
            square[8].numShape = 1;
            square[8].x = 232;
            square[8].y = 165;
    
            square[9].numShape = 2;
            square[9].x = 20;
            square[9].y = 210;
    
            square[10].numShape = 3;
            square[10].x = 90;
            square[10].y = 210;
    
            square[11].numShape = 4;
            square[11].x = 180;
            square[11].y = 210;
    
            square[12].numShape = 5;
            square[12].x = 180;
            square[12].y = 235;
    
            square[13].numShape = 6;
            square[13].x = 20;
            square[13].y = 260;
    
            square[14].numShape = 7;
            square[14].x = 50;
            square[14].y = 260;
    
            square[15].numShape = 8;
            square[15].x = 80;
            square[15].y = 260;
    
            square[16].numShape = 9;
            square[16].x = 110;
            square[16].y = 260;
    
            square[17].numShape = 9;
            square[17].x = 140;
            square[17].y = 260;
    
            square[18].numShape = 9;
            square[18].x = 170;
            square[18].y = 260;
    
            square[19].numShape = 10;
            square[19].x = 200;
            square[19].y = 260;
    
            square[20].numShape = 11;
            square[20].x = 110;
            square[20].y = 310;
    
            square[21].numShape = 11;
            square[21].x = 110;
            square[21].y = 340;
          }
          shapeGenerated = true;
        }
      }
    }
    //This sets the program up to be able to change backgrounds everytime a key is pressed. 
    
    void keyPressed() { 
      clickedCount++;
    }
    
    //This is the code for the mainscreen background 
    
    void mainScreen() { 
      background(214); 
      //image(imgB1, 0, 0);
      PFont font; 
      font = createFont("BradleyHandITCTT-Bold", 50); 
      textFont(font); 
      textAlign(CENTER, CENTER); 
      fill(255); 
      text("Puzzle Buzzle", 300, 100); 
      textAlign(CENTER);
      font = createFont("BradleyHandITCTT-Bold", 30); 
      textFont(font); 
      textAlign(CENTER, BOTTOM); 
      fill(0, 100, 300); 
      text("press any key to begin", 300, 550);
    }
    //after a key is pressed the program will change to the next page, the rule page, which is setup as follows. 
    void ruleScreen() { 
      background(0, 150, 200); 
      PFont font; 
      font = createFont("BradleyHandITCTT-Bold", 50); 
      textFont(font); 
      font = createFont("BradleyHandITCTT-Bold", 40); 
      textFont(font); 
      fill(255); 
      text("Rules", 300, 90); 
      textAlign(CENTER); 
      font = createFont("BradleyHandITCTT-Bold", 25); 
      textFont(font); 
      fill(255); 
      text("Object: Fill the puzzle board with the given pieces.", 300, 130); 
      textAlign(CENTER); 
      text("Click once to pick up a piece, and click again to", 300, 180); 
      textAlign(CENTER); 
      text("drop the piece.", 300, 210); 
      textAlign(CENTER); 
      text("press any key to continue", 300, 420); 
      textAlign(CENTER);
    }
    //The next screen will be the first level, which is setuip as follows. 
    void Level1() { 
      background(#06C8D1); 
      PFont font; 
      font = createFont("BradleyHandITCTT-Bold", 50); 
      textFont(font); 
      textAlign(CENTER, CENTER); 
      fill(255); 
      text("Level 1", 300, 50);
      for (int x = 350; x <= 550; x=x+20) { 
        line(x, 250, x, 450);
      }
      for (int y = 250; y <= 450; y=y+20) { 
        line(350, y, 550, y);
      }
      font = createFont("BradleyHandITCTT-Bold", 30); 
      textFont(font); 
      textAlign(CENTER, BOTTOM); 
      fill(255); 
      text("press any key to skip level", 300, 550);
    }
    //The final screen is the second level, which is setup as follows.
    void Level2() { 
      background(#06C8D1); 
      PFont font; 
      font = createFont("BradleyHandITCTT-Bold", 50); 
      textFont(font); 
      textAlign(CENTER, CENTER); 
      fill(255); 
      text("Level 2", 300, 50);
      for (int x = 350; x <= 550; x=x+20) { 
        line(x, 250, x, 450);
      }
      for (int y = 250; y <= 450; y=y+20) { 
        line(350, y, 550, y);
      }
    }
    
    
    class Shape { 
      int numShape = 0; 
      int x = 0; 
      int y = 0; 
      int wSquare = 0; 
      int hSquare = 0;
      Shape(int numS, int px, int py) { 
        numShape = numS; 
        x = px; 
        y = py;
        if (numShape == 0)
        {
          wSquare = 60;
          hSquare = 60;
        }
    
        if (numShape == 1)
        {
          wSquare = 40;
          hSquare = 40;
        }
    
        if (numShape == 2)
        {
          wSquare = 60;
          hSquare = 40;
        }
    
        if (numShape == 3)
        {
          wSquare = 80;
          hSquare = 40;
        }
    
        if (numShape == 4)
        {
          wSquare = 80;
          hSquare = 20;
        }
    
        if (numShape == 5)
        {
          wSquare = 60;
          hSquare = 20;
        }
    
        if (numShape == 6) 
        {
          wSquare = 20;
          hSquare = 100;
        }
    
        if (numShape == 7)
        {
          wSquare = 20;
          hSquare = 80;
        }
    
        if (numShape == 8)
        {
          wSquare = 20;
          hSquare = 60;
        }
    
        if (numShape == 9)
        {
          wSquare = 20;
          hSquare = 40;
        }
    
        if (numShape == 10)
        {
          wSquare = 20;
          hSquare = 20;
        }
    
        if (numShape == 11)
        {
          wSquare = 60;
          hSquare = 20;
        }
    
        if (numShape == 12)
        {
          wSquare = 80;
          hSquare = 80;
        }
    
        if (numShape == 13)
        {
          wSquare = 60;
          hSquare = 100;
        }
    
        if (numShape == 14) 
        {
          wSquare = 40;
          hSquare = 80;
        }
    
        if (numShape == 15)
        {
          wSquare = 100;
          hSquare = 20;
        }
      }
      void drawShape() { 
        if (numShape == 0) { 
          fill(#F59454); 
          rect(x, y, 60, 60);
        }
        if (numShape == 1)
        {
          fill(#71F554);
          rect(x, y, 40, 40);
        }
    
        if (numShape == 2)
        {
          fill(#FA7EF2);
          rect(x, y, 60, 40);
        }
    
        if (numShape == 3)
        {
          fill(#7EFAF7);
          rect(x, y, 80, 40);
        }
        if (numShape == 4)
    
        {
          fill(#1D0CEA);
          rect(x, y, 80, 20);
        }
        if (numShape == 5)
        {
          fill(#C10CEA);
          rect(x, y, 60, 20);
        }
        if (numShape == 6)
        {
          fill(#FC0A27);
          rect(x, y, 20, 100);
        }
        if (numShape == 7)
        {
          fill(#3B40FF);
          rect(x, y, 20, 80);
        }
        if (numShape == 8)
        {
          fill(#F2EC31);
          rect(x, y, 20, 60);
        }
        if (numShape == 9)
        {
          fill(#3EA516);
          rect(x, y, 20, 40);
        }
        if (numShape == 10)
        {
          fill(#F76B48);
          rect(x, y, 20, 20);
        }
        if (numShape == 11)
        {
          fill(#F022DB);
          rect(x, y, 60, 20);
        }
        if (numShape == 12)
        {
          fill(#19F7F5);
          rect(x, y, 80, 80);
        }
        if (numShape == 13)
        {
          fill(#F73719);
          rect(x, y, 60, 100);
        }
        if (numShape == 14)
        {
          fill(#F8FF3B);
          rect(x, y, 40, 80);
        }
        if (numShape == 15)
        {
          fill(#FFD071);
          rect(x, y, 100, 20);
        }
      }
    }
    void mousePressed() { 
      for (int i = 0; i < 22; i++) { 
        if (mouseX >= square[i].x && mouseX <= square[i].x + square[i].wSquare) { 
          if (mouseY >= square[i].y && mouseY <= square[i].y + square[i].hSquare) { 
            selectedSquare = i;
            break;
          }
        }
      }
    }
    void mouseDragged() { 
      if (selectedSquare != -1) { 
        if (mouseX > square[selectedSquare].x + square[selectedSquare].wSquare) { 
          square[selectedSquare].x += 5;
        } 
        if (mouseX < square[selectedSquare].x) { 
          square[selectedSquare].x -= 5;
        } 
        if (mouseY > square[selectedSquare].y + square[selectedSquare].hSquare) { 
          square[selectedSquare].y += 5;
        } 
        if (mouseY < square[selectedSquare].y) { 
          square[selectedSquare].y -= 5;
        }
      }
    }
    
  • edited December 2015

    also in draw

    if (clickedCount == 2) { 
        Level1();
    

    you forgot the {..........} after the if (shapeGenerated == false) {

Sign In or Register to comment.