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

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

截图:


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,thread,游戏,C++,c)