【问题标题】:Adding A Timer with Score添加带分数的计时器
【发布时间】:2013-05-09 00:19:46
【问题描述】:

我目前正在开发一款带有 Processing 和 Fiducials 的游戏。基本上这个游戏包括询问与某些国家有关的问题。每个问题的每个 ANS 都是一个国家/地区。例如比萨斜塔和 ASN 意大利。用户必须出示代表意大利国旗的基准

我让倒计时计时器开始运行,但我无法在新关卡开始时将其重置。我们在一个开关中有每个级别。

非常感谢大家的帮助!!

代码如下:

        import ddf.minim.*;
    import TUIO.*;
    TuioProcessing tuioClient;

    HashMap<Integer, String> symbolWordMap=new HashMap<Integer, String>();

    //The below variables are used to show the background image
    PImage bg;
    PImage start;
    PImage q1;
    PImage q2;
    PImage correct;
    PImage wrong;

    //The following are the variables used fro playing the sounds
    AudioPlayer player;
    AudioPlayer playerfail;
    Minim minim;

    int level = 0;

    int objSize=50;

    //This variable will be reduced everytime the player gets the answer wrong
    int lives = 5;

    //This variable will keep track of the score of the player
    int score = 0;

    //Declaring the font variable so as to display text on the screen
    PFont font;

    //Declaring the variable to be used for the timer
    //Timer t;


    //Below is the timer that will be used  
    void time(){
      int c;
      int csec;
      int climit = 20; //defined for a 20 second countdown

      c = climit*1000 - millis();
      csec = (c/(1000));

      if (csec > 0){
        text("TIME: "+csec+" secs",1800,50);
      } else {
        background(bg);
        text("TIME: 0 secs",1800,50);
        text("Time is up!",950,488);
      }
    }



    void setup()
    {
      symbolWordMap.put(0, "Start");  
      symbolWordMap.put(1, "Italy");
      symbolWordMap.put(2, "France");
      symbolWordMap.put(3, "Spain");
      symbolWordMap.put(4, "UK");
      symbolWordMap.put(5, "USA");
      symbolWordMap.put(6, "Malta");
      symbolWordMap.put(7, "Australia");
      symbolWordMap.put(8, "Germany");

      size(1920,976);

      //This is an instance of Minim
      minim = new Minim(this);

      //Load the applause mp3 file
      player = minim.loadFile("applause.mp3",2048);
      //Load the fail mp3 file
      playerfail = minim.loadFile("fail.mp3",2048);

      //Creating an instance of the Timer class
      //t = new Timer(20000);

      //savedTime = millis();

      //The below statement will load the image to be placed as a background from the Data folder. This image must be the same size as the window in which the program will run
      bg = loadImage("map.png");
      //Load the image for correct answer
      correct = loadImage("correct.png");
      //Load image for the incorrect answer
      wrong = loadImage("wrong.jpg");
      //Load the image for Question 1
      q1 = loadImage("Question1.png");
      //Load the image for Question 2
      q2 = loadImage("question2.png");
      //Load the image for the welcome page
      start = loadImage("Intro.png");

      //Creating the font of text to be displayed on screen;
      font = createFont("Arial",24,true);

      rectMode(CENTER);
      textAlign(CENTER, CENTER);

      // an instance of the TuioProcessing
      // since we ad "this" class as an argument the TuioClient expects
      // an implementation of the TUIO callback methods 
      tuioClient  = new TuioProcessing(this);
    }

    void draw()
    {

      //The following command will present the background
      background(bg);

      //Specifying the font to be used for text that is displayed on screen
      textFont(font,24);
      //Specifying the colour of the text that will be displayed on screen
      fill(0);

      if (level>0){
        text("SCORE: "+score,100,50);
        //text("Lives: "+lives,100,100);
      }

      //Creating the different levels
      switch(level){
        case 0:
        //Welcome Screen

        // get all the tuio objects detected by reactivision
        Vector<TuioObject> tuioObjectList =tuioClient.getTuioObjects();

        image(start,550,160);
        // Process each fiducial in turn
        for (int i=0;i<tuioObjectList.size();i++) {
          TuioObject tobj = tuioObjectList.get(i);

          pushMatrix();
          // Move origin to location of TUI object and rotate
          translate(tobj.getScreenX(width), tobj.getScreenY(height));
          rotate(tobj.getAngle());

          // draw the box
          fill(255);
          rect(0, 0, objSize, objSize);

          // Write the text inside the box 
          fill(0);

          int id = tobj.getSymbolID();
          String txt;
          if (symbolWordMap.containsKey(id)) {
            // if ID is in symbolWordMap, then look it up to find word
            txt = symbolWordMap.get(id);
          } 
          else { // otherwise, we'll just display the id number with a dot after
            txt = id+".";
          }
          text(txt, 0, 0);
          popMatrix();

          if (id == 0){
           level = 1;
           delay(2000);
          }
        }   

    break;

    case 1:
    //Level 1

        //Start timer
        //time();
        //noLoop();

        // get all the tuio objects detected by reactivision
        Vector<TuioObject> tuioObjectList1 =tuioClient.getTuioObjects();

        image(q1,550,160);
        // Process each fiducial in turn
        for (int i=0;i<tuioObjectList1.size();i++) {
          TuioObject tobj = tuioObjectList1.get(i);

          pushMatrix();
          // Move origin to location of TUI object and rotate
          translate(tobj.getScreenX(width), tobj.getScreenY(height));
          rotate(tobj.getAngle());

          // draw the box
          fill(255);
          rect(0, 0, objSize, objSize);

          // Write the text inside the box 
          fill(0);

          int id = tobj.getSymbolID();
          String txt;
          if (symbolWordMap.containsKey(id)) {
            // if ID is in symbolWordMap, then look it up to find word
            txt = symbolWordMap.get(id);
          } 
          else { // otherwise, we'll just display the id number with a dot after
            txt = id+".";
          }
          text(txt, 0, 0);
          popMatrix();

          if (id == 1){
            //Displays the text Correct on screen
            //text("Correct",500,500);
            score=score+10;
            background(bg);
            image(correct,800,300);
            player.play();
            player.rewind();
            //Time delay before advancing to next level
            delay(5000);            
            //Advance to level 2
            level++;
          } else {
            //Display the text Wrong to the left of the screen
            //text("Wrong",500,500);
            lives=lives-1;
            background(bg);
            image(wrong,700,300);
            playerfail.play();
            playerfail.rewind();
          }
        }   

    break;

    case 2:
    //Level 2

        //Start timer
        //time();
        //noLoop();


        // get all the tuio objects detected by reactivision
        Vector<TuioObject> tuioObjectList2 =tuioClient.getTuioObjects();

        //Load image for question 2
        image(q2,550,160);
        // Process each fiducial in turn
        for (int i=0;i<tuioObjectList2.size();i++) {
          TuioObject tobj = tuioObjectList2.get(i);

          pushMatrix();
          // Move origin to location of TUI object and rotate
          translate(tobj.getScreenX(width), tobj.getScreenY(height));
          rotate(tobj.getAngle());

          // draw the box
          fill(255);
          rect(0, 0, objSize, objSize);

          // Write the text inside the box 
          fill(0);

          int id = tobj.getSymbolID();
          String txt;
          if (symbolWordMap.containsKey(id)) {
            // if ID is in symbolWordMap, then look it up to find word
            txt = symbolWordMap.get(id);
          } 
          else { // otherwise, we'll just display the id number with a dot after
            txt = id+".";
          }
          text(txt, 0, 0);
          popMatrix();

           if (id == 8){
           //Displays the text Correct on screen
           //text("Correct",500,500);
           score=score+10;
           background(bg);
           image(correct,900,500);
           player.play();
           player.rewind();
           //Time delay before advancing to next level
           delay(5000);            
           //Advance to level 2
           level++;
         } else {
           //Display the text Wrong to the left of the screen
           //text("Wrong",500,500);
           lives=lives-1;
           background(bg);
           image(wrong,700,300);
           playerfail.play();
           playerfail.rewind();
         }
       }  

    break;

    case 3:
    //Level 3
    break;

    case 4:
    //Level 4
    break;

    case 5:
    //Level 5
    break;

  }
  //End of switch
}


void stopSound()
{
  player.close();
  playerfail.close();
  minim.stop();

  super.stop();
}

【问题讨论】:

    标签: timer processing counter


    【解决方案1】:

    您的计时器的实现表明它是一个忙碌的计时器。这意味着它取决于被多次调用并检查它是否比其他时间晚。繁忙计时器的简单实现如下:

    while(getTime() < someTime) { do nothing; }
    

    这种类型的定时器不好。您应该尝试生成一个线程并使用睡眠。这有点像 javascript 事件处理程序。操作系统会在请求时唤醒线程,然后您可以提醒程序计时器已启动。如果您在此期间不需要做任何事情,您可以轻松地睡觉,但在我看来,您在跟踪时间时正在做某事。

    PS:根据您的“导入”,您似乎正在使用 Java...。这个对吗?如果是这样,我可以为您确定一些参考资料。

    【讨论】:

    • @ChrisCM 我想我会向your question 澄清;仅供参考,我使用了“你的价值观”。是什么让你认为我使用了其他任何东西对我来说是个谜。 (我告诉你我使用了什么,以及如何使用。)祝你有美好的一天。
    【解决方案2】:

    我不完全了解计时器在您的程序中发生的位置。我看到一个计时器类:

    //Declaring the variable to be used for the timer
    //Timer t;
    

    但我没有在您的代码中看到正在使用的类。只有一个时间函数:

    //Below is the timer that will be used  
    void time(){
      int c;
      int csec;
      int climit = 20; //defined for a 20 second countdown
    
      c = climit*1000 - millis();
      csec = (c/(1000));
    
      if (csec > 0){
        text("TIME: "+csec+" secs",1800,50);
      } else {
        background(bg);
        text("TIME: 0 secs",1800,50);
        text("Time is up!",950,488);
      }
    }
    

    我建议不要在处理草图中使用delay(),而是在您希望在一定时间后发生不同事件时使用不同的计时器。

    我发现 Daniel Shiffman 的示例非常简单,有助于理解如何在 Processing 中实现计时器。 Here 是一个面向对象的计时器,在这种情况下对您更有帮助。以下是链接中的 Timer 类:

    class Timer {
    
      int savedTime; // When Timer started
      int totalTime; // How long Timer should last
    
      Timer(int tempTotalTime) {
        totalTime = tempTotalTime;
      }
    
      // Starting the timer
      void start() {
        // When the timer starts it stores the current time in milliseconds.
        savedTime = millis(); 
      }
    
      // The function isFinished() returns true if 5,000 ms have passed. 
      // The work of the timer is farmed out to this method.
      boolean isFinished() { 
        // Check how much time has passed
        int passedTime = millis()- savedTime;
        if (passedTime > totalTime) {
          return true;
        } else {
          return false;
        }
      }
    }
    

    用户如何在关卡之间切换?是否有按键或鼠标按下会触发下一个级别?还是完全基于计时器?如果它基于按键,您可以合并以下代码:

    int level;
    Timer levelTimer;
    
    void setup() {
      levelTimer = new Timer(5000);
    }
    
    void draw() {
      println(levelTimer.isFinished());
    }
    
    void keyPressed() {
      switch(key) {
      case '1':
        level = 1;
        levelTimer.start();
        break;
      case '2':
        level = 2;
        levelTimer.start();
        break;
      case '3':
        level = 3;
        levelTimer.start();
        break;
      }
    }
    

    这样,每次用户进入他们选择的关卡时,您都会启动关卡计时器。只是一个想法。

    【讨论】:

    猜你喜欢
    • 2023-04-05
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 2016-02-12
    • 1970-01-01
    • 2017-05-04
    相关资源
    最近更新 更多