当前位置:首页 > 开发 > 编程语言 > Java > 正文

java中简单的指法练习游戏(仅在32位下正常运行)

发表于: 2013-03-28   作者:dcj3sjt126com   来源:转载   浏览:
摘要: import java.awt.*; import java.awt.event.*; import java.awt.font.*; import java.awt.print.*; import javax.swing.*; public class typewrite { public static void main(String[] args) { JFr
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.print.*;

import javax.swing.*;

public class typewrite {

	public static void main(String[] args) {
		JFrame w = new JFrame("typewrite game");
		w.setSize(300, 400);
		w.setFont(new Font("",10,100000));
		w.setBackground(Color.RED);
		MyPanel mp = new MyPanel();
		w.add(mp);
		w.addKeyListener(mp);
		mp.addKeyListener(mp);
		Thread t = new Thread(mp);
		t.start();
		w.setVisible(true);
	}
}

class MyPanel extends JPanel implements Runnable, KeyListener ,MouseListener{

	private static final long serialVersionUID = 1L;
	int x[] = new int[10];
	int y[] = new int[10];
	char c[] = new char[10];
	int sum = 1000;

	public MyPanel() {
		for (int i = 0; i < 10; i++) {
			x[i] = (int) (Math.random() * 260);
			y[i] = (int) (Math.random() * 300);
			c[i] = (char) (Math.random() * 26 + 97);
		}
	}

	public void paint(Graphics g) {
		g.clearRect(0, 0, 300, 400);
		g.setColor(Color.BLACK);

		for (int i = 0; i < 10; i++) {
			g.drawString("" + c[i], x[i], y[i]);
		}

		g.setColor(Color.GREEN);
		g.drawString("score: " + sum, 20, 10);
		if (sum < 0) {
			g.clearRect(0, 0, 300, 400);
			g.drawString("GAME OVER", 100, 200);

		}
	}

	public void run() {
		while (true) {
			for (int i = 0; i < 10; i++) {
				y[i]++;
				if (y[i] > 400) {
					y[i] = 0;
					x[i] = (int) (Math.random() * 260);
					c[i] = (char) (Math.random() * 27 + 97);
					sum -= 100;
				}
			}

			if (sum < 0) 
				return;
			
			try {
				Thread.sleep(40);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			repaint();

		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		char cc = e.getKeyChar();
		int yy = -1;
		int index = -1;

		for (int i = 0; i < 10; i++) {
			if (cc == c[i]) {
				if (y[i] > yy) {
					yy = y[i];
					index = i;
				}
			}
		}

		if(index>-1){
			y[index]=0;
			x[index]=(int)(Math.random()*260);
			c[index]=(char)(Math.random()*26+97);
			sum+=10;
		}else{
			sum-=100;
		}
	}

	@Override
	public void keyReleased(KeyEvent e) { }

	@Override
	public void keyTyped(KeyEvent e) { }

	@Override
	public void mouseClicked(MouseEvent arg0) { }

	@Override
	public void mouseEntered(MouseEvent arg0) { }

	@Override
	public void mouseExited(MouseEvent arg0) { }

	@Override
	public void mousePressed(MouseEvent arg0) { }

	@Override
	public void mouseReleased(MouseEvent arg0) { }

}

 

java中简单的指法练习游戏(仅在32位下正常运行)

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号