Java 如何使倒计时计时器显示分*秒,而不仅仅是秒?
我创建了一个开放式处理游戏,在这个游戏中,你(作为蝙蝠侠角色)必须在3分钟的时间限制内捕捉尽可能多的小丑。唯一的事情,我只能让我的倒计时计时器显示秒(180秒倒计时) 我希望它能在几分钟:几秒钟内显示出来,但我在不同论坛上尝试过的每件事都会变得不可靠,而且不会以我想要的格式倒计时。看看我的代码,你有什么建议吗Java 如何使倒计时计时器显示分*秒,而不仅仅是秒?,java,processing,Java,Processing,我创建了一个开放式处理游戏,在这个游戏中,你(作为蝙蝠侠角色)必须在3分钟的时间限制内捕捉尽可能多的小丑。唯一的事情,我只能让我的倒计时计时器显示秒(180秒倒计时) 我希望它能在几分钟:几秒钟内显示出来,但我在不同论坛上尝试过的每件事都会变得不可靠,而且不会以我想要的格式倒计时。看看我的代码,你有什么建议吗 /**** Game Screen Reference ****/ // 0: Initial Screen // 1: Game Screen // 2: Game-over Scre
/**** 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
}
}
/****游戏屏幕参考****/
//0:初始屏幕
//1:游戏屏幕
//2:屏幕上的游戏
/*********变数*********/
int gameScreen=0;
//加载背景音乐
进口加工。声音。*;
声音文件;
String audioName=“黑暗骑士升起.wav”;
字符串路径;
//加载背景的gif库
输入动画*;
Gif动画;
//像素图像
皮迈奇·蝙蝠侠//蝙蝠侠角色
皮美杰小丑//小丑角色
PImage配子图像//图像游戏
//字符位置、距离和大小
int-batmanX=100//蝙蝠侠X在屏幕上的位置
int-batmanY=100//蝙蝠侠在屏幕上的Y位置
float jokerX=随机(1000)//随机化小丑X在屏幕上的位置
浮动玩笑=随机(750);//随机化小丑在屏幕上的Y位置
int蝙蝠侠1Size=50//蝙蝠侠距离
int joker2Size=50//小丑距离
整数宽度=100//字符宽度
整数高度=100;//字符高度
//高分宣言
智力得分=0;
//宣布倒数计时
int开始;
int持续时间=180;
整数时间=180;
/*********设置块*********/
无效设置(){
大小(1067800);
//播放背景音乐
路径=草图路径(audioName);
文件=新声音文件(此,路径);
file.play();
//加载背景和图像
myAnimation=newgif(这是“background.Gif”);
myAnimation.play();
蝙蝠侠=loadImage(“pixelbatman.png”);
joker=loadImage(“pixeljoker.png”);
gameOverImage=loadImage(“game_over.jpg”);
}
void restart(){//重新启动游戏的全局变量
游戏屏幕=0;
file.play();
蝙蝠侠X=100;//蝙蝠侠X在屏幕上的位置
蝙蝠侠=100;//蝙蝠侠在屏幕上的Y位置
jokerX=random(1000);//随机化jokerX在屏幕上的位置
jokerY=random(750);//随机选择小丑在屏幕上的Y位置
蝙蝠侠1Size=50;//蝙蝠侠距离
joker2Size=50;//用于joker距离
时间=180;//设置时间
持续时间=180;//重置持续时间
分数=0;//重置分数
}
/*********绘图块*********/
作废提款(){
//显示当前屏幕的内容
如果(游戏屏幕==0){
initScreen();
}如果(游戏屏幕==1){
游戏屏幕();
}如果(游戏屏幕==2){
游戏屏幕();
}
}
/*********屏幕内容*********/
void initScreen(){
背景(#282e3c);
文本对齐(中心);
填充(#ffffff);
文本大小(70);
文字(“捕捉小丑!”,宽*5.5,高*3);
文本大小(20);
文字(“小丑在克隆机上得到了手!”,宽*5.5,高*4);
文本(“使用你的箭头键帮助蝙蝠侠在克隆人接管哥谭市之前将其全部围捕!”,宽*5.5,高*4.25);
文本大小(15);
文本(“点击开始”,宽度*5.5,高度*5);
}
void gameScreen(){
牵引杆接地();
drawCharacters();
manageInput();
碰撞();
}
void gameOverScreen(){
背景(#282e3c);
文本对齐(中心);
填充(236240241);
文本大小(12);
文本(“你的分数”,宽*5.5,高*3);
文本大小(130);
文字(分数、宽度*5.5、高度*4.5);
文本大小(15);
文本(“单击以重新启动”,宽度*5.5,高度*5);
}
/*********投入*********/
公共空间鼠标垫(){
如果(gameScreen==0){//如果单击时我们在初始屏幕上,则启动游戏
startGame();
begin=millis();//开始倒计时
}
如果(gameScreen==2){//如果单击时我们在屏幕上方的游戏上,请重新启动游戏
重启();
}
}
/*********其他职能*********/
//此方法设置启动游戏所需的变量
void startGame(){
游戏屏幕=1;
}
void gameOver(){
游戏屏幕=2;
}
无效退税地(){
图像(myAnimation,0,0);//放置gif背景
//显示分数
文本大小(20);
填充(#ffffff);
文字(“捉住小丑:”,885,30);
文本(分数1025,30);
//显示倒计时
如果(时间>0){
时间=持续时间-(毫秒()-开始)/1000;
文本大小(20);
文本(“倒计时:”、880、60);
文本(时间1025,60);
}
//一旦倒数计时器达到零,进入屏幕上的游戏
当然,如果(时间一分钟有60秒,那么可以通过(整数除法:
int min=time/60;
秒是时间除以60的余数。余数可以通过模运算符(%
)获得:
int-sec=时间%60;
用于将整数值转换为带前导零的字符串:
int min=time/60;
整数秒=时间%60;
文本(nf(最小值,1)+“:”+nf(第2节),1025,60);