Java写的小游戏,怎么添加倒计时功能?
用JAVA写的小游戏,打算在按游戏开始的同时,启动倒计时?然后计时一定时间,例如5分钟,然后时间一到,游戏结束。应该怎么做?最好还有一个暂停功能,能够暂时停止倒计时!会的...
用JAVA写的小游戏,打算在按游戏开始的同时,启动倒计时?然后计时一定时间,例如5分钟,然后时间一到,游戏结束。应该怎么做?最好还有一个暂停功能,能够暂时停止倒计时!会的高手帮帮忙!谢谢(游戏是用Frame做的,希望在窗口能看到倒计时)
展开
展开全部
很久以前写过这么个:
//TimerLabel.java
package timerlabel;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.lang.Thread.State;
/**
* 计时标签
* @author Jeky
*/
public class TimerLabel extends JLabel {
private int maxTime;
private int count;
private static final int SECOND = 1000;
private static final int FONT_SIZE = 36;
private Thread thread;
private boolean pause;
private boolean start;
/**
* 新建一个计时标签
* @param maxTime 倒计时起始时间
*/
public TimerLabel(int maxTime) {
this.setHorizontalAlignment(JLabel.CENTER);
this.setFont(new Font("Times New Roman", Font.BOLD, FONT_SIZE));
this.pause = false;
setMaxTime(maxTime);
}
/**
* 修改倒计时起始时间
* @param maxTime 新的起始时间
*/
public void setMaxTime(int maxTime) {
if (this.start) {
return;
}
this.maxTime = maxTime;
this.count = maxTime;
initText();
this.thread = new Thread(new Runnable() {
@Override
public void run() {
while (count != 0) {
try {
if (!start) {
count = 0;
initText();
break;
}
if (!pause) {
Thread.sleep(SECOND);
count--;
initText();
}
} catch (InterruptedException ex) {
pause = true;
}
}
done();
}
});
this.start = false;
}
/**
* 倒计时完成后调用此方法
*/
protected void done() {
JOptionPane.showMessageDialog(this, "Time up!");
}
/**
* 标签字符由此方法设置
*/
protected void initText() {
String min = String.valueOf(count / 60);
String sec = String.valueOf(count % 60);
while (min.length() < 2) {
min = "0" + min;
}
while (sec.length() < 2) {
sec = "0" + sec;
}
this.setText(min + ":" + sec);
}
/**
* 暂停
*/
public void pause() {
if (start) {
thread.interrupt();
}
}
/**
* 检测标签倒计时是否开始
* @return 如果开始返回true
*/
public boolean isStart() {
return start;
}
/**
* 得到倒计时起始时间
* @return 倒计时起始时间
*/
public int getMaxTime() {
return maxTime;
}
/**
* 检测标签倒计时是否暂停
* @return 倒计时暂停返回true
*/
public boolean isPause() {
return pause;
}
/**
* 从暂停中恢复计时
*/
public void continueDo() {
if (this.pause) {
this.pause = false;
}
}
/**
* 取消计时
*/
public void stop() {
if (start) {
start = false;
}
}
/**
* 开始计时
*/
public void start() {
if (thread.getState().equals(State.NEW)) {
start = true;
thread.start();
} else if (thread.getState().equals(State.TERMINATED)) {
setMaxTime(maxTime);
start = true;
thread.start();
}
}
}
//演示程序 Test.java
package timerlabel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* @author Jeky
*/
public class Test extends JFrame {
private TimerLabel label;
private static final int GAP = 10;
private JButton runButton;
private JButton pauseButton;
private JButton setButton;
private JButton stopButton;
private JTextField time;
public Test() {
label = new TimerLabel(10);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, GAP, GAP));
runButton = new JButton("Start");
runButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.start();
}
});
panel.add(runButton);
pauseButton = new JButton("Pause");
pauseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (label.isStart()) {
if (!label.isPause()) {
label.pause();
pauseButton.setText("Continue");
} else {
label.continueDo();
pauseButton.setText("Pause");
}
}
}
});
panel.add(pauseButton);
time = new JTextField(10);
panel.add(time);
setButton = new JButton("setMaxTime");
setButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setMaxTime(Integer.parseInt(time.getText()));
}
});
panel.add(setButton);
stopButton = new JButton("Stop");
stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.stop();
}
});
panel.add(stopButton);
this.getContentPane().add(label, BorderLayout.CENTER);
this.getContentPane().add(panel, BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test();
}
}
试试吧
//TimerLabel.java
package timerlabel;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.lang.Thread.State;
/**
* 计时标签
* @author Jeky
*/
public class TimerLabel extends JLabel {
private int maxTime;
private int count;
private static final int SECOND = 1000;
private static final int FONT_SIZE = 36;
private Thread thread;
private boolean pause;
private boolean start;
/**
* 新建一个计时标签
* @param maxTime 倒计时起始时间
*/
public TimerLabel(int maxTime) {
this.setHorizontalAlignment(JLabel.CENTER);
this.setFont(new Font("Times New Roman", Font.BOLD, FONT_SIZE));
this.pause = false;
setMaxTime(maxTime);
}
/**
* 修改倒计时起始时间
* @param maxTime 新的起始时间
*/
public void setMaxTime(int maxTime) {
if (this.start) {
return;
}
this.maxTime = maxTime;
this.count = maxTime;
initText();
this.thread = new Thread(new Runnable() {
@Override
public void run() {
while (count != 0) {
try {
if (!start) {
count = 0;
initText();
break;
}
if (!pause) {
Thread.sleep(SECOND);
count--;
initText();
}
} catch (InterruptedException ex) {
pause = true;
}
}
done();
}
});
this.start = false;
}
/**
* 倒计时完成后调用此方法
*/
protected void done() {
JOptionPane.showMessageDialog(this, "Time up!");
}
/**
* 标签字符由此方法设置
*/
protected void initText() {
String min = String.valueOf(count / 60);
String sec = String.valueOf(count % 60);
while (min.length() < 2) {
min = "0" + min;
}
while (sec.length() < 2) {
sec = "0" + sec;
}
this.setText(min + ":" + sec);
}
/**
* 暂停
*/
public void pause() {
if (start) {
thread.interrupt();
}
}
/**
* 检测标签倒计时是否开始
* @return 如果开始返回true
*/
public boolean isStart() {
return start;
}
/**
* 得到倒计时起始时间
* @return 倒计时起始时间
*/
public int getMaxTime() {
return maxTime;
}
/**
* 检测标签倒计时是否暂停
* @return 倒计时暂停返回true
*/
public boolean isPause() {
return pause;
}
/**
* 从暂停中恢复计时
*/
public void continueDo() {
if (this.pause) {
this.pause = false;
}
}
/**
* 取消计时
*/
public void stop() {
if (start) {
start = false;
}
}
/**
* 开始计时
*/
public void start() {
if (thread.getState().equals(State.NEW)) {
start = true;
thread.start();
} else if (thread.getState().equals(State.TERMINATED)) {
setMaxTime(maxTime);
start = true;
thread.start();
}
}
}
//演示程序 Test.java
package timerlabel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* @author Jeky
*/
public class Test extends JFrame {
private TimerLabel label;
private static final int GAP = 10;
private JButton runButton;
private JButton pauseButton;
private JButton setButton;
private JButton stopButton;
private JTextField time;
public Test() {
label = new TimerLabel(10);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, GAP, GAP));
runButton = new JButton("Start");
runButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.start();
}
});
panel.add(runButton);
pauseButton = new JButton("Pause");
pauseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (label.isStart()) {
if (!label.isPause()) {
label.pause();
pauseButton.setText("Continue");
} else {
label.continueDo();
pauseButton.setText("Pause");
}
}
}
});
panel.add(pauseButton);
time = new JTextField(10);
panel.add(time);
setButton = new JButton("setMaxTime");
setButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setMaxTime(Integer.parseInt(time.getText()));
}
});
panel.add(setButton);
stopButton = new JButton("Stop");
stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.stop();
}
});
panel.add(stopButton);
this.getContentPane().add(label, BorderLayout.CENTER);
this.getContentPane().add(panel, BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(500, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test();
}
}
试试吧
展开全部
这个时间要是不要很精确的话 你就可以自己写个函数啊 这个函数自减呗 这样不就可以表示时间了 如果aip里面有这样的方法的话 那就更好了!!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我的大概想法仅供参考
一个runnable子类
void run()
{
for(int i=0;i<100;i++)
{
Thread.sleep(100);//睡100毫秒
synchronized(synobject)//synobject ,isstop是全局的
{
if(isstop)
wait();
}
}
}
一个runnable子类
void run()
{
for(int i=0;i<100;i++)
{
Thread.sleep(100);//睡100毫秒
synchronized(synobject)//synobject ,isstop是全局的
{
if(isstop)
wait();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
程序中有帧速率的计算吧
就是每一帧消耗的时间
用这个时间能做么
就是每一帧消耗的时间
用这个时间能做么
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class TimerTest extends javax.swing.JFrame {
Timer timer;
int start = 0;
/** Creates new form TimerTest */
public TimerTest() {
initComponents();
timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.setRepeats(true);
int temp = Calendar.getInstance().get(Calendar.SECOND);
jLabel1.setText((temp - start) + "");
if (temp - start == 5) {
timer.stop();
JOptionPane.showMessageDialog(null, "时间到");
}
}
});
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("jLabel1");
jLabel1.setName("jLabel1"); // NOI18N
jButton1.setText("Start");
jButton1.setName("jButton1"); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("End");
jButton2.setName("jButton2"); // NOI18N
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(62, 62, 62)
.addComponent(jLabel1)
.addContainerGap(65, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
.addComponent(jButton2)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
start = Calendar.getInstance().get(Calendar.SECOND);
timer.start();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
timer.stop();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TimerTest().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
我这里为了让你看到效果所以用的秒,你要用分就换
start = Calendar.getInstance().get(Calendar.MINUTE);另外我说一下,你不能自己计时间,时间都从系统时间拿然后和初始时间比较就可以了,定时器本身是个多线程,如果当时cpu负荷高,你可以想象线程是不会严格按照你设定时间运行的,所以如果你设定的1秒时间,但是运行却延迟半秒,整个线程运行下来你会发现你的时间会和实际时间误差很大。另外在定时器使用上请用swing这个包的定时器,这个定时器开销小,而且可以取消和重新运行,还可以设置是否重复做一件事情。如果是一些周期行的事情请用这个类。
楼上几位都是线程形式,其实这个应该是用定时器比较合理。 我的界面是用Netbeans拖出来的。
Timer timer;
int start = 0;
/** Creates new form TimerTest */
public TimerTest() {
initComponents();
timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.setRepeats(true);
int temp = Calendar.getInstance().get(Calendar.SECOND);
jLabel1.setText((temp - start) + "");
if (temp - start == 5) {
timer.stop();
JOptionPane.showMessageDialog(null, "时间到");
}
}
});
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("jLabel1");
jLabel1.setName("jLabel1"); // NOI18N
jButton1.setText("Start");
jButton1.setName("jButton1"); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("End");
jButton2.setName("jButton2"); // NOI18N
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(62, 62, 62)
.addComponent(jLabel1)
.addContainerGap(65, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
.addComponent(jButton2)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
start = Calendar.getInstance().get(Calendar.SECOND);
timer.start();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
timer.stop();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TimerTest().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
我这里为了让你看到效果所以用的秒,你要用分就换
start = Calendar.getInstance().get(Calendar.MINUTE);另外我说一下,你不能自己计时间,时间都从系统时间拿然后和初始时间比较就可以了,定时器本身是个多线程,如果当时cpu负荷高,你可以想象线程是不会严格按照你设定时间运行的,所以如果你设定的1秒时间,但是运行却延迟半秒,整个线程运行下来你会发现你的时间会和实际时间误差很大。另外在定时器使用上请用swing这个包的定时器,这个定时器开销小,而且可以取消和重新运行,还可以设置是否重复做一件事情。如果是一些周期行的事情请用这个类。
楼上几位都是线程形式,其实这个应该是用定时器比较合理。 我的界面是用Netbeans拖出来的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询