Java小游戏--舒尔特方格

这篇博客详细介绍了如何使用Java开发一款舒尔特方格小游戏,包括游戏的逻辑设计、页面展示以及程序的主入口。通过阅读,读者可以了解到如何结合Java的图形界面和逻辑控制来创建互动的小游戏。
摘要由CSDN通过智能技术生成

一、逻辑部分

public class GridCalculator {

	private int row = 0;
	private int column = 0;
	private int nextScore = 0;
	private long startTime = 0;
	private int costTime = 0;
	private boolean isTrainning = false;
	private boolean isFinished = false;
	
	private Timer timer;

	GridCalculator(int rows, int columns) {
		this.row = rows;
		this.column = columns;
		
	}

	public List<Integer> generateSequence() {

		ArrayList<Integer> list = new ArrayList<Integer>();

		for (int i = 1; i <= row * column; i++) {

			list.add(i);
		}

		Collections.shuffle(list);
		return list;
	}

	public void startTrain() {
		this.costTime = 0;
		this.startTime = new Date().getTime();
		this.isTrainning = true;
		this.isFinished = false;
		this.nextScore = 1;
		
		this.timer = new  Timer();
		this.timer.schedule(new  TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				costTime = (int) ((new Date().getTime() - startTime) / 1000);
			}
		}, 0, 500);
	}

	public void stopTrain() {
		this.isTrainning = false;
		//this.costTime = (int) ((new Date().getTime() - this.startTime) / 1000);
		this.timer.cancel();
	}

	public boolean trySocre(int next) {
		boolean success = false;

		if (isTrainning && next == this.nextScore) {
			if (next == this.row * this.column) {
				// finish
				//this.costTime = (int) ((new Date().getTime() - this.startTime) / 1000);
				this.isFinished = true;
				this.isTrainning = false;
				this.timer.cancel();
			}

			this.nextScore++;
			success = true;
		}

		return success;
	}

	public boolean isFinishTrain() {
		return this.isFinished;
	}

	public int getCostTime() {
		if (isFinished || isTrainning) {
			return this.costTime;
		} else {
			//return Integer.MAX_VALUE;
			return 0;
		}
	}
}


二、页面部分

public class GridPage extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Map<JButton, Integer> gridButtonMap;
	private List<Integer> list;
	private GridCalculator calculator;

	private JButton btStart;
	private JButton btStop;
	private JLabel lbInfo;
	private JLabel lbUsedTime;
	private List<JButton> btList;

	private Timer trainTimer;
	private Color defaultColor;
	
	private void initGridButton(List<JButton> btList) {
		this.calculator = new GridCalculator((int) Math.sqrt(btList.size()),
				(int) Math.sqrt(btList.size()));
		this.list =
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值