处理数学游戏不会随着用户输入正确答案而更新

问题描述 投票:0回答:1

我试图在处理过程中设置一个简单的数学游戏,因此当用户输入问题的答案时,一个块向前移动,但是一旦玩家输入正确答案,公式就不会改变,块也不会移动。

这是我当前的代码:

PFont f;

void setup() 
{
  size (800,800);
  f = createFont("Arial",16);// Loads the font and size of the output window.
  noLoop(); // Only repeats the "draw" field once.
}

String typing = "";//This variable stores the written text
String saved = "";//This variable stores where the answer is written to and checked. 
int firstNum = (int)(random(12)); // A random number is picked between 1 and 12 for the formula. 
int secondNum = (int)(random(12)); // A random number is picked between 1 and 12 for the formula. 
int eqAnswer = firstNum * secondNum; // The equation answer is calculated and stored in this int. 
 String formula = " What is " +firstNum + " times " +secondNum; // Displays the formula onscreen for the user.
  String answer = str(eqAnswer); // Converts the int eqAnswer to a string, so that the answer to the formula is equal to firstNum * secondNum


void draw() {
  background(255);
  int indent = 25;
  fill(#00FAF8);
 
  rect(squarePX, squarePY, 40,40);
  textFont(f);
  fill(0);
  loop();
  text("This is a simple math game \n Answer the question below and bring the square to the finish line! ", indent, 40);
  text("Input: " + typing,indent,190);
  text("Saved text: " + saved,indent,230); //Draws instructions.
  text(formula, 150,125,200,200);//Draws the formula the player needs to answer.
  fill(#FA0043);
  rect(600, 690, 10, 60);// Draws and fills the finish line.
}

int playerSteps = 0; // Variable that determines the players location/ how many questions they have gotten correct. 
int squarePX = 100 + (playerSteps * 100);// Variable that determines the squares x coord.
int squarePY = 700; // Variable that determines the squares y coord.



void keyPressed() {
 
  // If the return key is pressed, save the String and clear it.
  if (key == '\n' ) {
      if (saved.equals(answer)==true){
      playerSteps++;//Increments the players' amount of steps by one
      redraw(); // Redraws the square in hopefully the correct position.
      secondNum = int(random(12)); //
      firstNum = int(random(12));  // Resets the formula
      println(playerSteps); // Prints the counter to the console for testing purposes.
      println(squarePX);
      println(firstNum);
      println(secondNum); // Prints the formula nums for testing purposes.
      }
    saved = typing;
    typing = ""; // Clears the input area for more numbers to be inputted.
  } else {
    // Otherwise, concatenate the String
    // Each character typed by the user is added to the end of the String variable.
    typing = typing + key;  // Input code referenced from Daniel Shiffmans Learning Processing: The Nature of Code.
  }
}

我的想法是让绘图功能运行一次,然后一旦玩家输入正确的数字作为答案,它的块的 x 坐标就会增加然后重新绘制,但 x 坐标不会受到影响并且游戏的文本不会重绘。

我觉得我需要更改代码的位置,但我不确定代码需要移动到哪个位置。

java string processing
1个回答
0
投票

以下是对您的源代码的重新安排,以允许游戏进行。为了允许 keyPressed() 和 draw() 进行更新,删除了所有“loop()”和“noLoop()”调用。正如发布的那样,您的代码没有评估答案是否正确,这就是事情没有按照您预期的方式发展的原因。向 keyPressed() 添加了更多信息,以便它可以进行适当的评估。您在添加注释方面做得很好,这样我们就知道代码的关键行是做什么的。

PFont f;

String typing = "";//This variable stores the written text
String saved = "";//This variable stores where the answer is written to and checked.
int firstNum = 0;
int secondNum = 0;
int playerSteps = 0; // Variable that determines the players location/ how many questions they have gotten correct.
int squarePX = 100;
int squarePY = 400; // Variable that determines the squares y coord.
int indent = 25;

void setup() {
  size (800, 600);
  f = createFont("Arial", 16);// Loads the font and size of the output window.
}

void draw() {
  background(255);
  fill(#00FAF8);
  rect(squarePX, squarePY, 40, 40);
  textFont(f);
  fill(0);
  textFont(f);
  text("This is a simple math game \n Answer the question below and bring the square to the finish line! ", indent, 40);
  text("Input: " + typing, indent, 190);
  text("Saved text: " + saved, indent, 230); //Draws instructions.
  String formula = " What is " +firstNum + " times " +secondNum; // Displays the formula onscreen for the user.
  text(formula, 150, 125, 200, 200);//Draws the formula the player needs to answer.
  fill(#FA0043);
  rect(600, 390, 10, 60);// Draws and fills the finish line.
}

void keyPressed() {
  // If the return key is pressed, save the String and clear it.
  if (key == '\n' ) {
    saved = typing;
    int eqAnswer = firstNum * secondNum; 
    println("correct answer = ", eqAnswer);
    String answer = str(eqAnswer);
    println("user answer = ",saved);
    println("return key hit : answer = " + saved);
    if (saved.equals(answer)==true) {
      playerSteps++;//Increments the players' amount of steps by one
      squarePX += 50; // For every correct answer xcoordinate of square is advanced +50
      secondNum = int(random(12)); //
      firstNum = int(random(12));  // Resets the formula
      println("playerSteps = ",playerSteps); // Prints the counter to the console for testing purposes.
      println("squarePX = ",squarePX);
      println("first Num = ",firstNum);
      println("secondNum = ",secondNum); // Prints the formula nums for testing purposes.
    }    
    typing = ""; // Clears the input area for more numbers to be inputted.
  } else {
    // Otherwise, concatenate the String
    // Each character typed by the user is added to the end of the String variable.
    typing = typing + key;  // Input code referenced from Daniel Shiffmans Learning Processing: The Nature of Code.
  }
}

© www.soinside.com 2019 - 2024. All rights reserved.