java - 如何让我的倒计时器显示分钟*和*秒,而不仅仅是秒?

标签 java processing

我创建了一个开放式处理游戏,您(作为 bat 侠角色)必须在 3 分钟的时间限制内捕获尽可能多的 clown 。唯一我只能让倒计时器以秒为单位显示(180 秒倒计时)。

我希望它能以分钟:秒的形式显示,但是我从不同论坛尝试过的所有内容都变得不稳定,并且没有以我想要的格式倒计时。看看我的代码,您有什么建议吗?

/**** Game Screen Reference ****/

// 0: Initial Screen
// 1: Game Screen
// 2: Game-over Screen 


/********* VARIABLES *********/


int gameScreen = 0;

//loads music for background
import processing.sound.*;
SoundFile file;
String audioName = "dark_knight_rises.wav";
String path;

//loads gif library for background
import gifAnimation.*;
Gif myAnimation;

// Pixel images
PImage batman;       //Batman character
PImage joker;        //Joker character
PImage gameOverImage;     //Game Over image

//Character positions, distance and size

int batmanX = 100;                   //batman X position on screen
int batmanY = 100;                   //batman Y position on screen
float jokerX = random(1000);         //randomize joker X position on screen
float jokerY = random(750);          // randomize joker Y position on screen
int batman1Size = 50;                //for batman distance
int joker2Size = 50;                 //for joker distance
int width = 100;                     //width for characters
int height = 100;                    // height for characters

// declaration for high score
int score = 0;            

// declaration for time countdown
int begin; 
int duration = 180;
int time = 180;


/********* SETUP BLOCK *********/

void setup() {
  size(1067, 800);

  //plays background music
  path = sketchPath(audioName);
  file = new SoundFile(this, path);
  file.play();

  //loads background and images
  myAnimation = new Gif(this, "background.gif");
  myAnimation.play();
  batman = loadImage("pixelbatman.png");
  joker = loadImage("pixeljoker.png");
  gameOverImage = loadImage("game_over.jpg");



}

void restart() {    //restarts global variables for game

 gameScreen = 0; 
 file.play();
 batmanX = 100;     //batman X position on screen
 batmanY = 100;     //batman Y position on screen
 jokerX = random(1000);    //randomize joker X position on screen
 jokerY = random(750);     //randomize joker Y position on screen
 batman1Size = 50;         //for batman distance
 joker2Size = 50;          //for joker distance
 time = 180;                //setting time      
 duration = 180;            //reset duration
 score= 0;                 //reset score



 }

/********* DRAW BLOCK *********/

  void draw() {
  // Display the contents of the current screen
  if (gameScreen == 0) { 
    initScreen();
  } else if (gameScreen == 1) { 
    gameScreen();
  } else if (gameScreen == 2) { 
    gameOverScreen();
  }
}


/********* SCREEN CONTENTS *********/

void initScreen() {
  background(#282e3c);
  textAlign(CENTER);
  fill(#ffffff);
  textSize(70);
  text("Capture the Joker!", width*5.5, height*3);
  textSize(20);
  text("Joker got his hands on a clone machine!", width*5.5, height*4);
  text("Use your arrow keys to help Batman round up all of the clones before they take over Gotham!", width*5.5, height*4.25);
  textSize(15); 
  text("Click to start", width*5.5, height*5);
}

void gameScreen() {
 drawBackground();
 drawCharacters();
 manageInput();
 collisions();
}

void gameOverScreen() {
  background(#282e3c); 
  textAlign(CENTER);
  fill(236, 240, 241);
  textSize(12);
  text("Your Score", width*5.5, height*3);
  textSize(130);
  text(score, width*5.5, height*4.5);
  textSize(15);
  text("Click to Restart", width*5.5, height*5);
}

/********* INPUTS *********/

public void mousePressed() {
  if (gameScreen==0) {  // if we are on the initial screen when clicked, start the game 
    startGame();
    begin = millis();  //begins countdown timer
  }
  if (gameScreen==2) {  // if we are on the game over screen when clicked, restart the game 
    restart();
  }
}


/********* OTHER FUNCTIONS *********/

// This method sets the necessery variables to start the game  
void startGame() {
  gameScreen=1;
}
void gameOver() {
  gameScreen=2;
}




void drawBackground() {
  image(myAnimation, 0, 0);   //lays down gif background 

  //display the score
  textSize(20);
  fill(#ffffff);
  text("Jokers caught:", 885, 30);
  text(score, 1025, 30);

  //display the countdown timer
  if (time > 0){  
    time = duration - (millis() - begin)/1000;
    textSize(20);
    text("Countdown:", 880, 60);
    text(time, 1025, 60);
  }

  //once countdown timer hits zero, goes to game over screen 
  if (time <= 0) { 
    gameOver();
    file.stop();      //end music playing
  }
}

void drawCharacters() {
  image(joker, jokerX, jokerY, width, height);      //joker character
  image(batman, batmanX, batmanY, width, height);   //batman character

}

void manageInput() {      //Batman character movement
  if (keyPressed) {
    if (key == CODED) {
      if (keyCode == UP) batmanY--;
      if (keyCode == RIGHT) batmanX++;
      if (keyCode == DOWN) batmanY++;
      if (keyCode == LEFT) batmanX--;
    }
  }  
}

void collisions() {
  if (dist(batmanX, batmanY, jokerX, jokerY) < (batman1Size + joker2Size)/10 ) { //if batman and joker collide - joker disappears and teleports elsewhere
    jokerX = (float)random(1000);  //randomized Joker's X coordinates
    jokerY = (float)random(750);  //randomized Joker's Y coordinates
    score += 1; //increase the score by 1 when batman captures a joker
  }
}

最佳答案

当然,一分钟有 60 秒。所以分钟可以通过(积分除法:

int min = time / 60;

秒是time除以60的余数。余数可以通过取模运算符(%)得到:

int sec = time % 60;

使用nf()将整数值转换为带有前导零的字符串:

int min = time / 60;
int sec = time % 60;
text(nf(min, 1) + ":" + nf(sec, 2), 1025, 60);

关于java - 如何让我的倒计时器显示分钟*和*秒,而不仅仅是秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58206391/

相关文章:

java - 正确的 cookie 管理

java - Gradle下载源失败

java - 算法将值分成组以使具有相同数量值的组相等

java - 测试类组织

python - Python 和处理计算之间的区别

java - 颜色混合RGB模型: Transparency

java - 使用 Google 以及 Spring Security 和 Spring Social 登录

opencv - 在处理和 OpenCV 中使用 Minoru 3d 网络摄像头进行立体视觉

java - 随机 2D 城市景观生成器,如何随机生成?

javascript - 如何在 p5.js 中管理多测验游戏的多个屏幕?