Randomize image appearance.

edited December 2013 in Questions about Code

As an assignement for school I have to create a simply game with dissapearing circles. When a circle's size equals 0, the game displays a gameover screen (loaded from void gameOver). The function void gameOver() consists of a reset of the score and an svg that's being loaded on the center of the screen.

However I made 5 different svgs I would want to include in the gameOver screen. I want to display only one of course but each time gameOver happens it should be a randomly chosen svg instead of just using shape(filename,etc).

I hope it's clear what I want. I've tried playing around with random(); but it didn't really help. I hope someone can help me.

Full paste of the code here:

PImage bg;
PShape beachball;
PShape folder;
PShape crash;
PShape crash1;
PShape crash2;
PShape crash3;
PShape crash4;
float angle;

void setup() {
  size(1024, 768);
  bg = loadImage("bg.jpg");
  beachball = loadShape("beachball.svg");
  folder = loadShape("folder.svg");
  crash = loadShape("crash.svg");
  crash1 = loadShape("crash1.svg");
  crash2 = loadShape("crash2.svg");
  crash3 = loadShape("crash3.svg");
  crash4 = loadShape("crash4.svg");
}

float size = 100;
float wx = random(size, width-size);
float wy = random(size, height-size);
float afstand = dist(mouseX, mouseY, wx, wy);
int score = 0;

void draw() {
  println(score);
  if (size > 0) {
    imageMode(CORNER);
    image(bg, 0, 0);
    shapeMode(CENTER);
    pushMatrix();
    translate(wx, wy);
    rotate(angle);
    shape(beachball, 0, 0, size, size);
    angle += 0.1;
    popMatrix();
    size--;
  }
  if (size == 0) {
    gameover();
  }
  scoreCounter();
}

void keyPressed() {
  switch(key) {
  case 'p':
    noLoop();
    break;
  case 'r':
    loop();
    break;
  }
}

void mouseClicked() {
  float afstand = dist(mouseX, mouseY, wx, wy);
  if (afstand<=size/2) {
    circleGen();
    score++;
  }
}

void scoreCounter() {
  switch(score) {
  case 2:
    shape(folder, 895, 95);
    break;
  case 4:
    shape(folder, 895, 223);
    break;
  case 6:
    shape(folder, 895, 350);
    break;
  case 8:
    shape(folder, 895, 478);
    break;
  case 10:
    shape(folder, 895, 606);
    break;
  }
}

void circleGen() {
  size = 120; 
  wx = random(size, width-size);
  wy = random(size, height-size);
}

void gameover() {
  background(255);
  score = 0;
  shapeMode(CENTER);
  pushMatrix();
  translate(width/2, height/2);
  scale(2.0);
  shape(crash4, 0, 0);
  popMatrix();
  if(keyPressed){
    key = ' ';
    size = 100;
  }
}

If you also want to see the 5 different svg's which has to be chosen of, I've uploaded them to Behance: behance.net/gallery/Beachball-Bch-Game/12723209

Tagged:

Answers

  • Answer ✓

    use an array rather than crash, crash1, crash2 etc

    this lets you reference the items using an index - crash[0], crash[1] etc - one thing, many indexes, easier to handle than many things.

    then you can use crash[int(random(crash.length))]; // random crash

  • So instead of

    crash = loadShape("crash.svg");
    crash1 = loadShape("crash1.svg");
    crash2 = loadShape("crash2.svg");
    crash3 = loadShape("crash3.svg");
    crash4 = loadShape("crash4.svg");
    

    I should use:

    crash = new loadShape[5];
        array[0] = "crash.svg";
        array[1] = "crash1.svg";
        array[2] = "crash2.svg";
        array[3] = "crash3.svg";
        array[4] = "crash4.svg";
    

    And then instead of

    shape(crash1,0,0);

    use:

    crash[int(random(crash.length))];

    ?

    The teacher only just yesterday learned us about arrays, I get the basic idea of what it does, but he only showed examples with int's.

  • Answer ✓
    // definition (at top of file)
    PShape[] crash = new PImage[5];
    
    
    // loading (in setup)
    crash[0] = loadShape("crash.svg");
    crash[1] = loadShape("crash1.svg");
    // etc
    
    
    // drawing (in gameover())
    shape(crash[int(random(crash.length))], 0, 0); // draw random shape
    

    all untested 8)

  • (if you'd named your images better you can use a for loop to load them. but given you've only 5 images it's probably not worth it

    for (int i = 0 ; i < 1000 ; i++) {
      crash[i] = loadShape("crash" + nf(i, 3) + ".svg"); // crash000.svg to crash999.svg
    }
    

    )

  • Ok so using that code snippet it worked but now when gameOver() appears the images keep scrolling. I need to get only 1 image when gameOver(), then when the game is restarted and again gameOver(), i need to get a different one.

  • But thanks for helping me, so grateful :)

  • ah, ok, it's still looping despite being gameover and it's choosing a different image every time it's drawn.

    probably easiest is setting noLoop(); in gameover(). but then how to restart?

    or add a global variable at the top, shapeIndex, say, set it to int(random(crash.length)) once per game and then in gameover() use

    shape(crash[shapeIndex], 0, 0);

Sign In or Register to comment.