贴一个简单的击键类Java小游戏

系统 2194 0
游戏内容:单词由窗口上部落下,玩家在单词掉落到窗口底部前,输入该单词,输入正确后,单词消失。这只是个简单的示例,没有做成绩统计等相关功能。

截图:


    
package fun.dwords;
import ...;
public class DWords extends JFrame {
	String[] words = { "good", "slow", "goods", "goodbye", "bye", "byte",
			"bits", "sister", "monkey", "word", "world", "left", "leave" };
	JPanel panel;
	Image img = new ImageIcon("background/jessica_alba.jpg").getImage();

	public DWords() {
		panel = new JPanel() {
			@Override
			public void paint(Graphics g) {
				g.drawImage(img, 0, 0, null);
				super.paintChildren(g);
			}

		};
		add(panel);
		panel.setLayout(null);

		JPanel btnPanel = new JPanel();
		add(btnPanel, BorderLayout.SOUTH);
		JButton btnStart = new JButton("Start");
		btnPanel.add(btnStart);
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Thread thd = new Thread() {
					public void run() {
						downWords();
					}
				};
				thd.start();
				DWords.this.requestFocus();
			}
		});

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(500, 400);
		setTitle("word  word  down...");
		setVisible(true);
		this.requestFocus();
	}

	public void resetWords() {
		Component[] lb = panel.getComponents();
		for (int i = 0; i < lb.length; i++) {
			WordLabel wl = (WordLabel) lb[i];
			wl.reset();
		}
	}

	public void downWords() {
		Random rand = new Random(System.currentTimeMillis());

		for (int i = 0; i < words.length; i++) {
			WordLabel wl = new WordLabel(words[i]);
			wl.setForeground(new Color(255, 0, 0));
			panel.add(wl);
			wl.setBounds(rand.nextInt(panel.getWidth() - 50), 0, 120, 25);
			panel.validate();

			addKeyListener(wl);
			Thread thd = new Thread(wl);
			thd.start();

			try {
				Thread.sleep(2000);
			} catch (InterruptedException ex) {
			}
		}
	}

	static String fmtHeader = "<html><span style=\"color:red\">";
	static String fmtMiddle = "</span><span style=\"color:black;\">";
	static String fmtTail = "</span></html>";

	private class WordLabel extends JLabel implements Runnable, KeyListener {
		volatile boolean removed = false;
		volatile String fmtWord;
		String word;
		int idx = 0;

		public WordLabel(String word) {
			super(fmtHeader + fmtMiddle + word + fmtTail);
			this.fmtWord = fmtHeader + fmtMiddle + word + fmtTail;
			this.word = word;
		}

		public void run() {
			int y = getY();
			while (y < getParent().getHeight()) {
				y += 2;

				setText(fmtWord);
				setLocation(getX(), y);

				if (removed) {
					break;
				}
				try {
					Thread.sleep(150);
				} catch (InterruptedException ex) {

				}
			}
			JPanel panel = (JPanel) getParent();
			panel.remove(this);
			DWords.this.removeKeyListener(this);
			panel.validate();
			panel.repaint();
		}

		@Override
		public void keyPressed(KeyEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void keyReleased(KeyEvent e) {
		}

		@Override
		public void keyTyped(KeyEvent e) {
			char c = e.getKeyChar();
			if (c == word.charAt(idx)) {
				idx++;
				if (idx == word.length()) {
					removed = true;
					DWords.this.resetWords();
				}
			} else {
				idx = 0;
			}

			if (idx > 0) {
				fmtWord = fmtHeader + word.substring(0, idx) + fmtMiddle
						+ word.substring(idx) + fmtTail;
			} else {
				fmtWord = fmtHeader + fmtMiddle + word.substring(idx) + fmtTail;
			}

		}

		public void reset() {
			idx = 0;
			fmtWord = fmtHeader + fmtMiddle + word.substring(idx) + fmtTail;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final DWords dw = new DWords();
		new Thread() {
			public void run() {
				dw.downWords();
			}
		}.start();
	}

}

  

贴一个简单的击键类Java小游戏


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论