如何使游戏继续玩,而不仅仅是玩一次

时间:2018-10-07 05:47:08

标签: javascript html p5.js

目前,红色圆圈只能播放一次,游戏是否可以继续播放1分钟,然后它将停止并显示得分?并且当下一个游戏继续进行时,游戏答案将自动更改。

人们可以运行它并查看处理过程是一件很高兴的事。游戏是p5.js。 这是链接:https://editor.p5js.org/mtwhill/sketches/ryMrazX5Q

The picture shows the game interface. They the red ball goes into the red area then it appears, vice versa

var bx;
var by;
var ellSize = 75;
var overBox = false;
var locked = false;
var xOffset = 0.0; 
var yOffset = 0.0; 

function setup() {
  createCanvas(600, 300);
  bx = width/2.0;
  by = height/2.0;
  rectMode(RADIUS);
  strokeWeight(1.5);
}

function draw() { 
  background(230);
  
  // Test if the cursor is over the box 
  if (mouseX > bx-ellSize && mouseX < bx+ellSize && 
      mouseY > by-ellSize && mouseY < by+ellSize) {
    overBox = true;  
    if(!locked) { 
      stroke(255); 
    } 
  } else {
    stroke(0);
    overBox = false;
  }
  
  // Draw the box
	fill(250, 0, 0);
  ellipse(bx, by, ellSize, ellSize);
	rect(50, 50, 80, 80);
	fill(0, 250, 0);
	rect(550, 50, 80, 80);
	fill(0, 0, 250);
	rect(50, 250, 80, 80);
	fill(0, 0, 0);
	rect(550, 250, 80, 80);

}

function mousePressed() {
  if(overBox) { 
    locked = true; 
  } else {
    locked = false;
  }
  xOffset = mouseX-bx; 
  yOffset = mouseY-by; 

}

function mouseDragged() {
  if(locked) {
    bx = mouseX-xOffset; 
    by = mouseY-yOffset; 
  }
}

function mouseReleased() {
  if(bx>120|by >120) {
		bx = 300;
		by = 150;
		 }
	else {
		ellSize = 0;
	}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.js"></script>

1 个答案:

答案 0 :(得分:1)

如果我理解正确,一旦游戏结束,它会暂停一分钟,然后显示分数。完成后,将更改一些答案,然后它将继续运行?如果是这样,您可以执行以下操作:

// you'll need to have something change this to true
// You'll use this variable to rerun the game if the user chooses to do so
let gameOver = false 

function draw(){
  // draw your game's elements
   if (/*game over condition*/) {
     gameOver = true;
     // Once gameOver is true, the elements of your game will
     // no longer be drawn and that is when you'll draw the scoreboard
   }
   if(gameOver){
        // Once gameOver is false again, your game will run normally
        showScores();
      } 
}

ShowScores 是您可以绘制记分板内容和操纵变量的地方

function showScore(){
  while(counter != 60){
    // introduce some delay so your game will still be displayed for 1 minute
    if(frameCount % 60 == 0){
      counter++;
    }
  }

  // you can also manipulate variables that contain the answer here if you want

  // reset the canvas so stuff won't be drawn over other stuff
  // draw our imaginary scoreboard
  background(255,255,255);
  fill(240,240,240);
  rect(50, 50, 80, 80);

  resetGame();
}

resetGame 是您向用户提供重新运行游戏的选项。如果他选择这样做,请将gameOver设置为false,以便重新绘制游戏。

function resetGame(){
 // give the user an option to rerun the game
 // then set gameOver to false so that the game's elements will be
 // draw again instead of the scroreboard.
}

这里是working example:)