import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

/**打字游戏框架类
 * @author Mr.xiao
 *
 */
public class TypeFrame extends Frame {

	public long start,end;//游戏开始与刷新时的时间,用于统计打字速度
	public int typecount=0;//打字个数
	public Image paintplace = null;  //绘制其他图形的底图
	public static final int GAME_HEIGHT = 666;// 游戏高度
	public static final int GAME_WIDTH = 956; // 游戏宽度
	public ArrayList<Emery> apples = new ArrayList<>();  //放置所有存活的苹果的容器
	public static int score = 0;    //分数
	public Image bg = Toolkit.getDefaultToolkit().getImage("bg.jpg");//背景图片
	
	TypeFrame tf=this;  //得到当前对象以便后面使用
	
	public TypeFrame() {
		setTitle("拯救苹果");
		start=System.currentTimeMillis();  //获取系统当前的毫秒
		setVisible(true);
		setBounds(200, 20, GAME_WIDTH, GAME_HEIGHT);//设置界面

		addWindowListener(new WindowAdapter() {  //设置关闭监听
			public void windowClosing(WindowEvent e) {
				dispose();
			}
		});

		addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent e) {
				
				for (int i = 0; i < apples.size(); ) {
					if (e.getKeyChar() == apples.get(i).c) {//如果与输入的字母一致,则移除苹果并加10分
						apples.get(i).alive = false;
						apples.remove(i);
						score+=10;
						typecount++;
						apples.add(new Emery(tf));
					}
					i++;
				}
			}
		});

		for (int i = 0; i < 10; i++) {
			apples.add(new Emery(this));
		}
		new Thread(new PaintThread()).start();
	}

	public void update(Graphics g) {
		if (paintplace == null) {
			paintplace = createImage(GAME_WIDTH, GAME_HEIGHT);
		}
		Graphics gback = paintplace.getGraphics(); // 得到背景图片画笔
		Color c = gback.getColor();
		gback.setColor(Color.gray);
		gback.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
		gback.setColor(c);
		
		gback.drawImage(bg, 0, 0, null);  //绘制背景图片
		gback.setColor(Color.black);
		gback.setFont(new Font("Dialog", 5, 25));
		gback.drawString("Score:"+score, 30, 70);//绘制分数
		end=System.currentTimeMillis();   //获取系统此刻的毫秒数
		gback.drawString("打字速度:"+(typecount*60000/(end-start))+"字/分", 30, 100);
		paint(gback);

		g.drawImage(paintplace, 0, 0, null);    //绘制画板

	}

	public void paint(Graphics g) {
		for (int i = 0; i < apples.size(); ) {
			if(apples.get(i).y<=GAME_HEIGHT+20){   //如果苹果还在界内,则正常绘制
			apples.get(i).draw(g);
			i++;
			}
			else{       //否则移除出容器,扣5分,并且加入新的苹果
				apples.get(i).alive = false;
				apples.remove(i);
				score-=5;
				apples.add(new Emery(tf));
			}
		}

	}

	class PaintThread implements Runnable {

		@Override
		public void run() {
			Emery emery = null;
			while (true) {           //控制苹果坐标改变
				for (int i = 0; i < apples.size(); i++) {
					emery = apples.get(i);
					emery.y += emery.YSPEED;
					if (emery.turnleft) {
						emery.x -= emery.XSPEED;
						if (Math.abs((emery.center - emery.x)) > 20) {
							emery.turnleft = false;
						}
					} else {
						emery.x += emery.XSPEED;
						if (Math.abs((emery.center - emery.x)) > 20) {
							emery.turnleft = true;
						}
					}
				}
				repaint();   //重绘整个界面
				try {
					Thread.sleep(40);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}
	
	public static void main(String[] args) {
		TypeFrame tf = new TypeFrame();
	}
}
最近下载更多
942395729  LV1 2023年6月30日
655982592  LV2 2023年5月11日
liuhao475230970  LV1 2022年12月4日
Cokkey  LV1 2022年6月5日
闫小玥  LV8 2021年12月22日
hrbylw  LV8 2021年9月16日
pipichao  LV6 2021年6月20日
1972195660  LV1 2021年6月7日
Czhiyi  LV6 2021年4月12日
asfaij  LV1 2020年12月22日
最近浏览更多
陈小灏  LV14 1月9日
wangjialiang1  LV17 2023年8月24日
22111303117  LV1 2023年8月21日
你们的代码都是我的了  LV16 2023年8月21日
zz1230012300  LV11 2023年7月20日
942395729  LV1 2023年6月30日
2385649653  LV7 2023年6月25日
655982592  LV2 2023年5月11日
liuhao475230970  LV1 2022年12月4日
六十四易生 2022年11月16日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友