0

这段代码有一个大问题。我正在使用 Java 进行处理。

我创建了一个游戏,用户必须引导角色远离物体。

所有对象、健康系统和评分系统都基于mills()。

游戏结束后,我们需要重置 millis(),重置对象、分数和健康系统。

我已经从朋友那里搜索了实施的建议,并且之前在这里提出了问题,但建议略有不同。我假设这是我看不到的东西。

非常感谢您对此提供的帮助,我仅在不得已时才使用此站点,而不仅仅是在我感到懒惰时。

//these are used to set the times which the games increases difficulty 
//int timeDelay = 30000; 

int delayOne = 2000;
int delayTwo = 5000;
int delayThree = 80000;
int delayFour = 90000;
int display = 2000;

//for collisions
float[] xpos = new float[6];
float[] ypos = new float[6];


//timer counts how many millis() each game lasts for 
int timeStamp = 5000;
int timer;
int timer2 = millis() - timer;
//always at zero 


//outputting score at the end of a game
int score;
int start;



//trying to get lives working
boolean lost = false;

//variable to store & output gale force when giving score 
int Gale = 0;

//Changing length rectangle
float rX = 350.0;

float x1 = 20;
float y1 = 20;
float w1 = 100;
float h1 = 30;

//DECLARE OBJECTS JELLY CLASS
Jelly myObject;
Jelly myObject1;
Jelly myObject2;
Jelly myObject3;

//GENTLEMAN CLASS
gentleMan mygentleMan;

//LOLLY CLASS
Lolly myCow;
Lolly myCow1;

//PImages
PImage loader;
PImage bg;
PImage uh;
PImage bg1;
PImage bolt;
PImage over;


void setup()
{
            bg=loadImage("backy1.png");
            bg1 = loadImage("backy.png");
            over = loadImage("over.png");


             PFont L = loadFont("Lobster1.3-48.vlw");
            textFont( L, 16);

            size(400, 600);
            smooth();

           //begin = millis();

            imageMode(CENTER);

            //INITIALISE
            myObject = new Jelly(320, 500);
            myObject1 = new Jelly(150, 200);
           // myObject2 = new Jelly(550, 500);
            //myObject3 = new Jelly(300, 100);

            mygentleMan = new gentleMan(200, 300);
            //myObject.run();
            //myObject1.run();
            //myObject2.run();

            myCow = new Lolly(400, 250);
            myCow1 = new Lolly(150, 350);

            timer = millis();
}

void draw()
{


     start = 0;

    //because we have image mode set to center for collisions 
    //we have to divide the height & width of the screen by 2 for hte image to fit
    image(bg, 200, 300);

     if (millis() >= start + delayOne)
     {

      image(bg1, 200, 300);
     }
      //CALL FUNCTIONALITY
      myObject.run();
      myObject.put_in_array(0);
      myObject1.run(); // this one is going top to bottom
      myObject1.put_in_array(1);
     // myObject2.run();
      //myObject2.put_in_array(2);
     // myObject3.run();
      //myObject3.put_in_array(3);

      myCow.run();
      myCow.put_in_array(4);
      myCow1.run();
      myCow1.put_in_array(5);


      mygentleMan.run();  

      //health bar
      fill(161, 221, 16);
      noStroke();
      rect(10, 24, rX, 10);


      if(rX <= 100)
      {
        fill(221, 59, 16);
        rect(10, 24, rX, 10);
      }

      else

      if(rX <= 200)
      {
        fill(221, 137, 16);
        rect(10, 24, rX, 10);
      }


      if(rX == 5.0)
      {
         lost = true;
         noLoop();
         // lives = lives - 1;
         image(over, width/2, height/3);
         fill(255);
         text("Your Score Is: " + timer, width/2.7, height/2);
         text("Gale Force Is; " + Gale, width/2.7, height/1.8);
         score = timer;
      }

    //For Loop detecting collisions between mygentleMan & objects
      for (int i=0; i < 6; i++) {

        if (xpos[i] > 150 && xpos[i] < 250 && ypos[i] > (mygentleMan.y-58) && ypos[i] < (mygentleMan.y+58))
        {
         // text("collision", 200, 300);
          bolt = loadImage("bolt.png");
          image(bolt, xpos[i], ypos[i]);

          rX = rX - 1;

        }

        //outputting score on screen @ at all times
        fill(255);
        text("Score: " + timer, 320, 20);
      } 


    //timer which will be score counter essentially
      timer = millis();
      //text(timer, 20, 20);

    //moving the man up the screen if button is pressed, if not he levitates downward

      if (keyPressed)
      {
        mygentleMan.y -= mygentleMan.moveY;

        mygentleMan.moveY += 0.4;
      }
      else
      {
        mygentleMan.y += mygentleMan.moveY;

        mygentleMan.moveY += 0.2;
      }

      fill(255);
      text("Health", 20, 20);

  if(mousePressed)
  {
  if(timer2 > timeStamp)
  {
  println("tit");
  mygentleMan.y = height/2;
  loop();
  } 
  }

    }

//class for first objects that move into the screen

class Jelly
{
                    //GLOBAL VARIABLES
                    float x = 0;
                    float y = 0;
                    float speedX = 1.8;
                    float speedY = 1.8;
                    float speedX2 = 2.1;
                    float speedY2 = 2.1;
                    float speedX3 = 2.2;
                    float speedY3 = 2.2;
                    PImage jelly = loadImage("jelly.png");
                    PImage hat = loadImage("hat.png");
                    PImage gale = loadImage("g1.png");
                    PImage force = loadImage("force.png");
                    PImage news = loadImage("news.png");



                    //CONSTRUCTOR
                    Jelly(float _x, float _y)
                    {
                      x = _x;
                      y = _y;
                    }


                    //FUNCTIONS
                    void run()
                    {
                      display();
                      move();
                      bounce();
                      image(force, 330, 550);



                      if (millis() >= start + delayOne)
                      {
                        display();
                        moveFast();
                        bounceFast();
                        image(gale, 280, 560);
                        Gale = 1;

                        if (start + delayOne + display >= millis())
                        {
                        image(news, 200, 300);
                        }
                      }

                      if (millis() >= start +delayTwo)
                      {
                        display();
                        moveFaster();
                        bounceFaster();
                        image(gale, 310, 560);
                        Gale = 2;

                         if (start + delayTwo + display >= millis())
                        {
                        image(news, 200, 300);
                        }


                      }
                      }

                      void bounce()
                      {
                        if ( x > width) 
                        {
                          speedX = speedX * -1; //multiply by -1 to make it bounce
                        }

                        if ( x < 0)
                        {
                          speedX = speedX * -1;
                        }

                        if ( y > height)
                        {
                          speedY = speedY * -1;
                        }

                        if ( y < 0)
                        {
                          speedY = speedY * -1;
                        }
                      }

                      void bounceFast()
                      {
                        if ( x > width) 
                        {
                          speedX2 = speedX2 * -1; //multiply by -1 to make it bounce
                        }

                        if ( x < 0)
                        {
                          speedX2 = speedX2 * -1;
                        }

                        if ( y > height)
                        {
                          speedY2 = speedY2 * -1;
                        }

                        if ( y < 0)
                        {
                          speedY2 = speedY2 * -1;
                        }
                      }

                      void bounceFaster()
                      {
                        if ( x > width) 
                        {
                          speedX3 = speedX3 * -1; //multiply by -1 to make it bounce
                        }

                        if ( x < 0)
                        {
                          speedX3 = speedX3 * -1;
                        }

                        if ( y > height)
                        {
                          speedY3 = speedY3 * -1;
                        }

                        if ( y < 0)
                        {
                          speedY3 = speedY3 * -1;
                        }
                      }



                      void move()
                      {
                        x = x + speedX;
                        y = y + speedY;
                      }

                      void moveFast()
                      {
                        x = x + speedX2;
                        y = y + speedY2;
                      }

                      void moveFaster()
                      {
                        x = x + speedX3;
                        y = y + speedY3;
                      }

                      void put_in_array(int a)
                      {

                        xpos[a] = x;
                        ypos[a] = y;
                      }



                      void display()
                      {
                        image(hat, x, y);
                      }
}


//class for gentleman that floats 

class gentleMan
{
  //GLOBAL VARIABLES
  float y = 400;
  float x = 400;
  float moveY;
  //PImage umbrella;
  PImage umbrella = loadImage("dafuq.png");
  PImage over = loadImage("over.png");



  //CONSTRCUTOR --- PIECES OF INFO PROVDE TO BUILD CLASS -- INTIIALIZE VARIBALE
  gentleMan(float _x, float _y)
  {
    y = _y;
    x = _x;
    moveY = 2;
  }

  //FUNCTIONS
  void run()
  {
    display();
    keyReleased();
    bounce();
    // collision();
  }


  void display() 
  {

    image(umbrella, x, y);
  }


  void keyReleased()
  {

    mygentleMan.moveY    = 4;
  }

  void bounce()
  {
    if ( y < 0)
    {
      y = 0;
    }  

    if (y > height)
  {
     //score = millis();
     lost = true;
     noLoop();
    // lives = lives - 1;
     image(over, width/2, height/3);
     text("Your Score Is: " + timer, width/2.7, height/2);
     text("Gale Force Is; " + Gale, width/2.7, height/1.8);

    }

  }
}

class Lolly
{
  //GLOBAL VARIABLES
  float x = 0;
  float y = 0;
  float speedX = 2;
  float speedY = 2;
  float speedX1 = 2.1;
  float speedY1 = 2.1;
  float speedX2 = 2.3;
  float speedY2 = 2.3;
  PImage cow = loadImage("cow.png");



  //CONSTRUCTOR
  Lolly(float _x, float _y)
  {
    x = _x;
    y = _y;
  }


  //FUNCTIONS
  void run()
  {


    // display();
    //move();
    //bounce();

    if (millis() >= start + delayThree)
    {
      display();
      moveFast();
      bounceFast();
    }

    if (millis() >= start +delayFour)
    {
      display();
      moveFaster();
      bounceFaster();
    }
  }

  void put_in_array(int a)
  {

    xpos[a] = x;
    ypos[a] = y;
  }




  void bounce()
  {
    if ( x > width) 
    {
      speedX = speedX * -1; //multiply by -1 to make it bounce
    }

    if ( x < 0)
    {
      speedX = speedX * -1;
    }

    if ( y > height)
    {
      speedY = speedY * -1;
    }

    if ( y < 0)
    {
      speedY = speedY * -1;
    }
  }

  void bounceFast()
  {
    if ( x > width) 
    {
      speedX1 = speedX1 * -1; //multiply by -1 to make it bounce
    }

    if ( x < 0)
    {
      speedX1 = speedX1 * -1;
    }

    if ( y > height)
    {
      speedY1 = speedY1 * -1;
    }

    if ( y < 0)
    {
      speedY1 = speedY1 * -1;
    }
  }

  void bounceFaster()
  {
    if ( x > width) 
    {
      speedX2 = speedX2 * -1; //multiply by -1 to make it bounce
    }

    if ( x < 0)
    {
      speedX2 = speedX2 * -1;
    }

    if ( y > height)
    {
      speedY2 = speedY2 * -1;
    }

    if ( y < 0)
    {
      speedY2 = speedY2 * -1;
    }
  }



  void move()
  {
    x = x + speedX;
    y = y + speedY;
  }

  void moveFast()
  {
    x = x + speedX1;
    y = y + speedY1;
  }

  void moveFaster()
  {
    x = x + speedX2;
    y = y + speedY2;
  }


  void display()
  {
    image(cow, x, y);
  }
}//end of cow class


   void mousePressed()
   {


   }
4

2 回答 2

2

Your question is not at all clear, but it sounds like you should be able to wrap System.currentTimeMillis() in an object that maintains a starting point and returns the offset. Something like

public class Millis
{
    long start;
    public Millis() { this.reset(); }
    public void reset() { this.start = System.currentTimeMillis(); }
    public long getMillis() { return System.currentTimeMillis() - start; }
}

You create an instance of this class at startup

Millis timer = new Millis();

then call reset() to set it back to zero at the beginning of each game. Everywhere in your code you currently have millis() you would have timer.getMillis()

于 2013-05-22T00:23:22.087 回答
1

使用自定义计时器类

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;
    }
   }




 }//end class

然后打电话

 firstTimer = new Timer(50000);

然后在抽签中检查

if (firstTimer.isFinished()) {
    //do sopemthing
    //then restart timer
    firstTimer.start();
  }
  else
  {
   // do soemthing else for the rest of the time....
  }
于 2013-05-27T15:48:57.190 回答