在processing3中实现游戏菜单

qlvxas9a  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(280)

我正在为我的大学课程做一个小游戏,却被困在菜单的制作上。我想解决这个问题已经有一段时间了。代码如下:在尝试摆弄loop和noloop()函数之前,以及在我将箭头位置从void设置移到void键按下之前,我的游戏已经成功了。现在程序在运行draw之前冻结。我只想在按下键之前显示菜单背景,在本例中是s代表开始。如果有人能想出一些方向,那将大有帮助。提前谢谢。

int totalArrows = 6;
arrowclass[] theArrows = new arrowclass[totalArrows];
PImage[] imgList = new PImage[4];
PImage bg;
PImage life;
PImage menu;
int score;
int loose = 3;

void setup() {
  size(400, 600);

  bg = loadImage("bck.jpg");
  life = loadImage("life.png");
  menu = loadImage("menu.jpg");
  background(menu);

  // loading the arrow imgs
  for (int i= 0; i<4; i++) {
    println(i+".png");
    imgList[i] = loadImage(i+".png");
  }

 noLoop();

  }

void draw() {
   textSize(32);
  text(score,30,100);

  // active area
  fill(255,31,31);
  rect(0,500,width,100);

  //lifetrack
  if (loose==3){
  image(life,10,10);
  image(life,45,10);
  image(life,80,10);
  }
   if (loose==2){
  image(life,10,10);
  image(life,45,10);
  }
   if (loose==1){
  image(life,10,10);
  }

 // calling class action, movement
 for (int i = 0; i<totalArrows; i++) {
   theArrows[i].run();

if (theArrows[i].y>475 && theArrows[i].y<600) {
     theArrows[i].inactive = true;
}
if(theArrows[i].did == false && theArrows[i].y>598) {
 theArrows[i].lost = true; 
}
if(theArrows[i].lost && theArrows[i].did == false && theArrows[i].wrongclick == false){
 loose--;
 theArrows[i].lost = false;
}
}

if (loose == 0){
  textSize(32);
  text("gameover",width/2,height/2);
  for(int i=0; i<totalArrows;i++){

  }
}

}   
void keyPressed() {
  if (key == 'p'){
  // placing tha arrows. (i*105 = positioning) THIS PART IS AN ISSUE FOR SURE. SEE ARROW CLASS 
   for (int i= 0; i<totalArrows; i++) {
    theArrows[i] = new arrowclass(-i*105);
  }  
  loop();
}
}
    void keyReleased() {

 for (int i= 0; i<totalArrows; i++) { 
   if (theArrows[i].inactive && theArrows[i].did == false) {
    if (keyCode == UP && theArrows[i].id == 3){
      score++;
      theArrows[i].did = true;
    }
    if (keyCode != UP && theArrows[i].id == 3){
      loose--;
      theArrows[i].wrongclick = true;

    }
    if (keyCode == DOWN && theArrows[i].id == 0){
      score++;
      theArrows[i].did = true;
    }
    if (keyCode != DOWN && theArrows[i].id == 0){
      loose--;
      theArrows[i].wrongclick = true;

    }
    if (keyCode == RIGHT && theArrows[i].id == 2){
      score++;
      theArrows[i].did = true;
    }
    if (keyCode != RIGHT && theArrows[i].id == 2){
      loose--;
      theArrows[i].wrongclick = true;
    }
    if (keyCode == LEFT && theArrows[i].id == 1){
      score++;
      theArrows[i].did = true;
    }
    if (keyCode != LEFT && theArrows[i].id == 1){
      loose--;
      theArrows[i].wrongclick = true;
    }
   }
 } 
 }

箭头类:

class arrowclass{
  int y;
  int id;
  boolean did;
  boolean inactive;
  boolean lost;
  boolean wrongclick;

  // proprieties of the class
  arrowclass(int initY){
    y = initY;
    id = int(random(4));
    did = false;
    inactive = false;
    lost = false;
    wrongclick = false;

  }

  //actions 

  void run(){
   image(imgList[id],width/2,y);

  // score goes up, speed goes up
   y += 2+score; 

  // reset arrow to default 
   if (y>600)  {
      y = -50;
      id = int(random(4));
      inactive = false;
      did = false;
      lost = false;
      wrongclick = false;

   }
  }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题