比赛结束后如何重新开始? (处理)

时间:2015-10-25 10:14:07

标签: switch-statement processing

我想在Processing中制作这个游戏。

在'Switch'中,同时显示case0,1,2。 我不知道如何编辑它。

在case2(gameover)之后,按“1”键再次启动。 但是我认为当游戏情况发生时会发生情况......

我该如何编辑?

PImage work[] = new PImage[3];
float workSize[] = new float[3];
float workX[] = new float[3];
float workY[] = new float[3];
float workS[] = new float[3];
PImage handA, handB;
PFont font;
int level;
boolean gameover = false;

boolean selected[] = new boolean [3];
int salary = 0;


void setup(){

  size(1000,800);
  background(255);
  imageMode(CENTER);

  for (int i=0; i<3; i++) {

    workX[i] = random(0, width);
    workY[i] = random(0, height);
    selected[i] = false;
    workSize[i] = 120;

  }

  handA = loadImage("handA.png");
  handB = loadImage("handB.png");

  work[0] = loadImage("work0.png");
  work[1] = loadImage("work1.png");
  work[2] = loadImage("work2.png");

  font = createFont("Gulim", 48);
  textFont(font);
  textAlign(CENTER, CENTER);
}



void draw(){

  background(255);

  if (mousePressed) {
    cursor(handB, 0, 0);
  } else {
    cursor(handA, 0, 0);
  }

  switch (level) {
    default: // press'1' to start game
        fill(0);
        text("1을 눌러 일 얻기", width/2, height/2);
        if (key == '1') {
          level = 1;
        }
      break;
    case 1:
        game();
        if (gameover == true) {
          level = 2;
        }      
      break;
    case 2: // press '1' to start again
        fill(0);
        text("퇴직금 : "+ salary + " + (비정규직으로 퇴직금 없음)", width/2, height/2-100); 
        text("일을 못해서 정리해고", width/2, height/2);
        text("1을 눌러 다시 일 얻기", width/2, height/2+100);
        if (key == '1') {
          level = 1;
        }
      break;     
  }
}


void game() {
  for (int i=0; i<3; i++) {

    float clickedDist = dist(workX[i], workY[i], mouseX, mouseY);

    if (clickedDist<workSize[i]/2 && mousePressed) {
      workSize[i] = workSize[i] - 2;
    } else {
      workSize[i] = workSize[i] + 0.7;
    }

    if (workSize[i]<100) {
      workSize[i] = 0;
    }

    if (workSize[i]>400) {
      gameover = true;
    }

    if (workSize[i] == 0 && selected[i] == false) {
      salary = salary + 50;
      selected[i] = true;
      workX[i] = random(0, width);
      workY[i] = random(0, height);
      selected[i] = false;
      workSize[i] = 120;
    }

    if (salary > 150) {
      workS[i] = workSize[i] + 0.5;
      workSize[i] = workS[i];
    }

    if (abs(mouseX-workX[i]) < workSize[i]/2 && abs(mouseY-workY[i]) < workSize[i]/2) {
      workX[i] += random(-5,5);
      workY[i] += random(-5,5);
    }


  image(work[i], workX[i], workY[i], workSize[i], workSize[i]);



  pushMatrix();
    fill(0);
    textSize(48);
    text("봉급 : "+ salary, textWidth("salary"), (textAscent()+textDescent()/2));
  popMatrix();
  }
}

1 个答案:

答案 0 :(得分:0)

您所要做的就是重置存储游戏状态的所有变量,例如level变量。像这样:

void keyPressed(){
   if(gameover && key == '1'){
      gameover = false;
      level = 1;
   }
}