`
webcode
  • 浏览: 5940470 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

java课程设计源码(游戏:急速生存)

 
阅读更多
package cn.edu.ahu.RapidSurvial;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.List;

/**
 * 类
 * @author Your风之恋(AHU - java - 课程设计)
 *
 */

public class Bomb {
	public static final int BWIDTH = 20;	//宽度
	public static final int BHEIGHT = 5;	//高度
	public static final int BXSPEED = 10;	//x方向上的速度
	public static final int BYSPEED = 10;	//y方向上的速度
	
	int x;									//的左上角 x点的位置
	int y;									//的左上角 y点的位置
	int w;									//的宽度
	int h;									//的高度
	
	RapidSurvialManager rsm;				//持有RapidSurvialManager的引用
	Fighter.Direction dir;					//的方向
	boolean isLive = true;					//是否有效
	boolean isEnemy;						//区分敌我的量
	public static int sid = 0;				//记录战果
	
	private static Toolkit tk = 
		Toolkit.getDefaultToolkit();
	private static Image[] bombImage = null;
	static {
		bombImage = new Image[] {
				tk.getImage(Bomb.class.getClassLoader().getResource("images/Bomb_LTR.png")),
				tk.getImage(Bomb.class.getClassLoader().getResource("images/Bomb_RTL.png"))	
		};
	}
	
	//构造方法
	public Bomb(int x, int y) {
		this.x = x;
		this.y = y;
		this.w = BWIDTH;
		this.h = BHEIGHT;
	}
	
	//构造方法
	public Bomb(int x, int y, RapidSurvialManager rsm) {
		this(x, y);
		this.rsm = rsm;
	}
	
	//构造方法
	public Bomb(int x, int y, RapidSurvialManager rsm, Fighter.Direction dir, boolean isEnemy) {
		this(x, y, rsm);
		this.dir = dir;
		this.isEnemy = isEnemy;
	}
	
	//画出自己的方法
	public void draw(Graphics g) {
		if(!isLive) {
			rsm.bombs.remove(this);
			return;
		}
		if(!isEnemy) {
			g.drawImage(bombImage[0], x, y, null);
			
		} else {
	
			g.drawImage(bombImage[1], x, y, null);
		}
		
		setPostion();
	}
	
	//根据方向计算下一重画的位置
	private void setPostion() {
		switch(dir) {
		case LTR:
			x += BXSPEED;
			break;
		case RTL:
			x -= BXSPEED;
			break;
		}
		
		//出界处理
		if(x < 0 || y < 0 ||
				x > RapidSurvialManager.MAINWIDTH ||
				y > RapidSurvialManager.MAINHEIGHT) {
			
			isLive = false;
		}
		
	}
	
	//返回自己的大小
	public Rectangle getRect() {
		return new Rectangle(x, y, w, h);
	}
	
	//此方法用于与敌机一个子弹的碰撞检测
	public boolean hitBomb(Bomb b) {
		if(this.isLive 
				&& this.getRect().intersects(b.getRect()) 
					&& b.isLive 
						&& b.isEnemy != this.isEnemy) {
			
			Explode e = new Explode(x + BWIDTH, y + BHEIGHT, rsm);
			rsm.explodes.add(e);
			


			this.isLive = false;
			b.isLive = false;
			return true;
			
		}
		
		return false;
	}
	
	//此方法用于与敌机一群子弹的碰撞检测
	public boolean hitBombs(List<Bomb> bombs) {
		for(int i = 0; i < bombs.size(); i++) {
			if(hitBomb(bombs.get(i))){
				return true;
			}
		}
		return false;
	}
	
	public boolean hitFighter(Fighter f) {
		if(this.isLive 
				&& this.getRect().intersects(f.getRect()) 
					&& f.isLive 
						&& f.isEnemy != this.isEnemy) {
			
			Explode e = new Explode(x + BWIDTH, y + BHEIGHT, rsm);
			rsm.explodes.add(e);
			
			if(!f.isEnemy) {
				f.setLifeValue(f.getLifeValue() - 1);
				if(f.getLifeValue() <= 0) {
					f.isLive = false;
				}
			} else {
				f.isLive = false;
				sid ++;
			}

			this.isLive = false;
			return true;
			
		}
		
		return false;
	}
	
	//此方法用于与一群战机的碰撞检测
	public boolean hitFighters(List<Fighter> enemys) {
		for(int i = 0; i < enemys.size(); i++) {
			if(hitFighter(enemys.get(i))){
				return true;
			}
		}
		return false;
	}
	
	
}
package cn.edu.ahu.RapidSurvial;

import java.awt.Color;
import java.awt.Graphics;

/**
 * 爆炸类
 * @author Your风之恋(AHU - java - 课程设计)
 *
 */
public class Explode {
	
	int x; 											//产生爆炸的x点坐标
	int y;											//产生爆炸的y点坐标
	RapidSurvialManager rsm;						//持有RapidSurvialManager的引用
	boolean isLive = true;							//是否有效,是:true,否:false
	int [] diameter = {4, 7, 12, 23, 34, 12, 6};    //模拟爆炸产生的半径
	int step = 0;									//爆炸的步数
	
	//构造函数
	public Explode(int x, int y, RapidSurvialManager rsm) {
		this.x = x;
		this.y = y;
		this.rsm = rsm;
	}
	
	//画出自己的方法
	public void draw(Graphics g) {
		if(!isLive) {
			rsm.explodes.remove(this);
			return;
		}
		Color c = g.getColor();
		g.setColor(Color.ORANGE);
		
		if(step == diameter.length) {
			isLive = false;
			step = 0;
			return;
		}
		g.fillOval(x, y, diameter[step], diameter[step]);
		g.setColor(c);
		
		step ++;
	}
	
}
 

package cn.edu.ahu.RapidSurvial;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;

public class Fighter {

	public static final int FWIDTH = 70;	//战机宽度
	public static final int FHEIGHT = 10;	//战机高度
	public static final int FXSPEED = 4;	//战机在x方向上的速度
	public static final int FYSPEED = 4;	//战机在y方向上的速度
	
	int x;									//战机在x方向上的位置
	int y;									//战机在y方向上的位置
	int w;									//战机的宽度
	int h;									//战机的高度
	
	Direction dir;							//方向
	RapidSurvialManager rsm;				//持有RapidSurvialManager的引用
	private boolean isUp = false;			//键盘↑ 是否被按下,初始化为false
	private boolean isDown = false;			//键盘↓ 是否被按下,初始化为false
	private boolean isRight = false;		//键盘→ 是否被按下,初始化为false
	private boolean isLeft = false;			//键盘← 是否被按下,初始化为false
	enum Direction {LTR, RTL};          	//两个方向,LTR:从左向右,RTL:从右向左
	boolean isEnemy;						//区分敌我的量,是敌人:true,否则为false
	boolean isLive = true;					//判读是否存活,活着:true,否则为false
	private int lifeValue = 10;				//我方战机的生命值
	int speed;								//产生一个速度值
	BloodBar bb = new BloodBar();			//可视的血量
	static int isRelive = 0;				//是否复活
	private int superStarCounts = 1;		//初始大决数
	private static Toolkit tk = 
		Toolkit.getDefaultToolkit();
	private static Image[] fighterImage = null;
	static {
		fighterImage = new Image[] {
				tk.getImage(Fighter.class.getClassLoader().getResource("images/EnemysFighter.png")),
				tk.getImage(Fighter.class.getClassLoader().getResource("images/MyFighter_LTR.png"))	
		};
	}
	
	//构造函数
	Fighter(int x , int y,boolean isEnemy) {
		this.x = x;
		this.y = y;
		this.w = FWIDTH;
		this.h = FHEIGHT;
		this.isEnemy = isEnemy;
	}
	
	//构造函数
	Fighter(int x, int y, boolean isEnemy, RapidSurvialManager rsm) {
		this(x, y, isEnemy);
		this.rsm = rsm;
		this.dir = Direction.LTR;
	}
	
	//构造函数
	Fighter(int x, int y,boolean isEnemy, RapidSurvialManager rsm, Direction dir, int speed) {
		this(x, y, isEnemy, rsm);
		this.dir = dir;
		this.speed = speed;
	}
	
	//设置lifeValue值
	public void setLifeValue(int lifeValue) {
		this.lifeValue = lifeValue;
	}
	
	//得到lifeValue值
	public int getLifeValue() {
		return lifeValue;
	}

	//设置superStarCounts值
	public void setSuperStarCounts(int superStarCounts) {
		this.superStarCounts = superStarCounts;
	}

	//得到superStarCounts值
	public int getSuperStarCounts() {
		return superStarCounts;
	}

	//用此方画出战机
	public void draw(Graphics g) {
		
		
		if(!isLive) {
			if(isEnemy) {
				rsm.enemys.remove(this);
			}
			return;
		}
		
		if(isEnemy) {	
			g.drawImage(fighterImage[0], x, y, null);
			
			go();
		} else {		
			g.drawImage(fighterImage[1], x, y, null);

			setPostion();

			bb.draw(g);
		}
			
	}
	
	//让敌军动起来的方法
	public void go() {
		switch(dir) {
		case LTR:
			x += speed;
			break;
		case RTL:
			x -= speed;
			break;
		}
	}
	
	//对按键被按下经行处理
	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		switch(key) {
			case KeyEvent.VK_UP:
				isUp = true;
				break;
			case KeyEvent.VK_RIGHT:
				isRight = true;
				break;
			case KeyEvent.VK_DOWN:
				isDown = true;
				break;
			case KeyEvent.VK_LEFT:
				isLeft = true;
				break;
		}
		
		setPostion();
	}
	
	//对按键被释放经行处理
	public void keyReleased(KeyEvent e) {
		int key = e.getKeyCode();
		switch(key) {
			case KeyEvent.VK_UP:
				isUp = false;
				break;
			case KeyEvent.VK_RIGHT:
				isRight = false;
				break;
			case KeyEvent.VK_DOWN:
				isDown = false;
				break;
			case KeyEvent.VK_LEFT:
				isLeft = false;
				break;
			case KeyEvent.VK_Q:
				fire();
				break;
			case KeyEvent.VK_F2:
				if(!isLive) {
					isLive = true;
					lifeValue = 10;
					isRelive ++;
				}
					
				break;
			case KeyEvent.VK_W:
				if(getSuperStarCounts() == 0) {
					return;
				}
				if(!isLive) {
					return;
				}
				setSuperStarCounts(getSuperStarCounts() - 1);
				superFire();
				break;
		}
	}
	

	
	//根据按键的组合确定下一次的位置
	private void setPostion() {

		if(isUp && !isRight && !isDown && !isLeft) {
			y -= FYSPEED;
		}
		if(!isUp && isRight && !isDown && !isLeft) {
			x += FXSPEED;
		}
		if(!isUp && !isRight && isDown && !isLeft) {
			y += FYSPEED;
		}
		if(!isUp && !isRight && !isDown && isLeft) {
			x -= FXSPEED;
		}
		if(isUp && isRight && !isDown && !isLeft) {
			x += FXSPEED;
			y -= FYSPEED;
		}
		if(isUp && !isRight && !isDown && isLeft) {
			y -= FYSPEED;
			x -= FXSPEED;
		}
		if(!isUp && isRight && isDown && !isLeft) {
			x += FXSPEED;
			y += FYSPEED;
		}
		if(!isUp && !isRight && isDown && isLeft) {
			x -= FXSPEED;
			y += FYSPEED;		
		}
		
		//对战机的出界处理
		if(x <= 0) {
			x = 0;
		}
		if(y < 45) {
			y = 45;
		}
		if(x + Fighter.FWIDTH > RapidSurvialManager.MAINWIDTH) {
			x = RapidSurvialManager.MAINWIDTH - Fighter.FWIDTH;
		}
		if(y + Fighter.FHEIGHT + 52> RapidSurvialManager.MAINHEIGHT) {
			y = RapidSurvialManager.MAINHEIGHT - Fighter.FHEIGHT - 52;
		}
			
	}
	
	//战机的开火处理
	public Bomb fire() {	
		if(!isLive) {
			return null;
		}	
		int x, y;
		if(!isEnemy) {
			x = this.x + Fighter.FWIDTH;
			y = this.y + Fighter.FHEIGHT / 2 - Bomb.BHEIGHT / 2 + 22;
		} else {
			x = this.x;
			y = this.y + Fighter.FHEIGHT / 2 - Bomb.BHEIGHT / 2 + 22;
		}	
		Bomb b = new Bomb(x, y, rsm, dir, isEnemy);
		rsm.bombs.add(b);
		return b;
 	}
	
	//放大决的方法
	public SuperLine superFire() {
		if(!isLive) {
			return null;
		}
		
		SuperLine s = new SuperLine(x, rsm, dir);
		rsm.superLines.add(s);
		return s;
	}
		
	//得到自己的大小,用于碰撞检测
	public Rectangle getRect() {
		return new Rectangle(x, y, w, h*4);
	}
	
	//血块类
	private class BloodBar {
		public void draw(Graphics g) {
			Color c = g.getColor();
			if(lifeValue <= 5) {
				g.setColor(Color.RED);
				g.drawRect(x+20, y-20, w * 2 / 3, 4);
				
				int w = FWIDTH * lifeValue / 10;
				g.fillRect(x+20, y-20, w * 2 / 3, 4);
				g.drawString(""+ lifeValue, x, y-13);
			} else {
				g.setColor(Color.GREEN);
				g.drawRect(x+20, y-20, w * 2 / 3, 4);
				
				int w = FWIDTH * lifeValue / 10;
				g.fillRect(x+20, y-20, w * 2 / 3, 4);
				g.drawString(""+ lifeValue, x, y-13);
				
				g.setColor(c);
			}
		}
	}
	
	//吃SuperStar的方法
	public boolean eat(SuperStar ss) {
		if(this.isLive 
				&& ss.isLive
					&& this.getRect().intersects(ss.getRect())) {
			ss.isLive = false;
			setSuperStarCounts(getSuperStarCounts() + 1);
			return true;
		}
		return false;
	}
	
	public int getScore() {
		return Bomb.sid - isRelive * 50;
	}
	
}

package cn.edu.ahu.RapidSurvial;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;
/**
 * 游戏中的大决类
 * @author Your风之恋(AHU - java - 课程设计)
 *
 */

public class SuperLine {
	public static final int SXSPEED = 10;    	//大决的移动速度
	public static final int LWIDTH = 10;		//大决的显示宽度
	
	int x;										//大决开始x坐标
	int y;										//大决开始y坐标
	Fighter.Direction dir;						//方向
	RapidSurvialManager rsm;					//持有RapidSurvialManager的引用
	boolean isLive = true;						//是否有效,是:true,否:false
	
	//构造函数
	SuperLine(int x, RapidSurvialManager rsm) {
		this.x = x;
		this.y = LWIDTH;
		this.rsm = rsm;
	}
	
	//构造函数
	SuperLine(int x, RapidSurvialManager rsm, Fighter.Direction dir) {
		this(x, rsm);
		this.dir = dir;
	}
	
	//画方法
	public void draw(Graphics g) {
		if(!isLive) {
			rsm.superLines.remove(this);
			return;
		}
		
		Color c = g.getColor();
		g.setColor(Color.PINK);
		g.fillRect(x, 0, LWIDTH, RapidSurvialManager.MAINHEIGHT);
		g.setColor(c);
		
		go();
	}
	
	//移动自己的方法
	public void go() {
		switch(dir) {
		case LTR:
			x += SXSPEED;
			break;
		case RTL:
			x -= SXSPEED;
			break;
		}
		
		if(x >= RapidSurvialManager.MAINWIDTH) {
			isLive = false;
		}
	}
	
	//拿到自己的大小,为碰撞检测服务
	public Rectangle getRect() {
		return new Rectangle(x, 0, y,RapidSurvialManager.MAINHEIGHT);
	}
	
	//与一个战机经行碰撞检测
	public boolean hitFighter(Fighter f) {
		if(this.isLive 
				&& this.getRect().intersects(f.getRect())
				  	&& f.isLive) {
			Explode e = new Explode(f.x, f.y,rsm);
			rsm.explodes.add(e);
			
			f.isLive = false;
			Bomb.sid ++;
			return true;
		}
		return false;
	}
	
	//与一群战机经行碰撞检测
	public boolean hitFighters(List<Fighter> enemys) {
		for(int i = 0; i < enemys.size(); i++) {
			if(this.hitFighter(enemys.get(i))) {
				return true;
			}
		}
		return false;
	}
}

package cn.edu.ahu.RapidSurvial;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;

/**
 * 超星类
 * @author Your风之恋(AHU - java - 课程设计)
 *
 */
public class SuperStar {
	public static final int SSWIDTH = 20;     	//超星的宽度
	public static final int SSHEIGHT = 20;		//超星的高度
	
	int x;										//超星初始化x点坐标
	int y;										//超星初始化y点坐标
	int w;										//超星的宽度
	int h;										//超星的高度
	int speed;									//超星的移动速度
	boolean isLive = true;						//是否有效,是:true,否:false
	RapidSurvialManager rsm;					//持有RapidSurvialManager的引用
	Fighter.Direction dir;						//方向
	
	private static Toolkit tk = 
		Toolkit.getDefaultToolkit();
	private static Image[] superStarImage = null;
	static {
		superStarImage = new Image[] {
				tk.getImage(SuperStar.class.getClassLoader().getResource("images/SuperStar_RTL.png")),
		};
	}
	
	//构造方法
	public SuperStar(int x, int y, RapidSurvialManager rsm, Fighter.Direction dir,int speed) {
		this.x = x;
		this.y = y;
		this.speed = speed;
		this.w = SSWIDTH;
		this.h = SSHEIGHT;
		this.rsm = rsm;
		this.dir = dir;
	}
	
	//画方法
	public void draw(Graphics g) {
		if(!isLive) {
			rsm.superStars.remove(this);
			return;
		}
		
		g.drawImage(superStarImage[0], x, y, null);
		
		go();
	}
	
	//移动自己的方法
	public void go() {
		switch(dir) {
		case LTR:
			x += speed;
			break;
		case RTL:
			x -= speed;
			break;
		}
		
		if(x <= 0) {
			isLive = false;
		}
	}

	//得到自己的大小
	public Rectangle getRect() {
		return new Rectangle(x, y, w, h);
	}

}

package cn.edu.ahu.RapidSurvial;

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

import cn.edu.ahu.RapidSurvial.Fighter.Direction;

/**
 * 游戏的主类,用于管理其他图形元素
 * @author Your风之恋(AHU - java - 课程设计)
 *
 */

public class RapidSurvialManager extends Frame{
	private static final long serialVersionUID = 1L;
	
	public static final int MAINWIDTH = 1200;		//主窗口的宽度
	public static final int MAINHEIGHT = 600;		//主窗口的高度
	public static final int MAINXPOINT = 30; 		//主窗口初始化x点的位置
	public static final int MAINYPOINT = 50; 		//主窗口初始化y点的位置
	public static Random r= new Random();			//添加随机数产生器,用于控制敌方的AI
	Image bufferImage = null;						//双缓冲的缓冲图像,用双缓冲取消屏幕闪烁
	public static int time = 0;
	Fighter myFighter = new Fighter(50, 300, false, this); 		//创建主战机
	List<Bomb> bombs = new ArrayList<Bomb>();					//炮弹集合
	List<Fighter> enemys = new ArrayList<Fighter>();			//敌机集合
	List<Explode> explodes = new ArrayList<Explode>();			//爆炸集合
	List<SuperLine> superLines = new ArrayList<SuperLine>();	//大决集合
	List<SuperStar> superStars = new ArrayList<SuperStar>();	//超星集合
	
	
	//主线程入口
	public  static void main(String[] args) {
		RapidSurvialManager rsm = new RapidSurvialManager();
		rsm.launchGameFrame();
	}
	
	//重写paint()方法
	public void paint(Graphics g) {
		//显示一些基本信息,便于判断集合中的元素是否被删除

		Color c = g.getColor();
		g.setColor(Color.ORANGE);
		
		Font font = g.getFont();
		g.setFont(new Font("SansSerif", Font.ROMAN_BASELINE, 15));
		g.drawString("游戏时间: " + showTime(), 25, 50);
		g.drawString("消灭敌机: " + Bomb.sid, 25, 70);
		g.drawString("剩余绝招: " + myFighter.getSuperStarCounts(), 150, 50);
		g.drawString("游戏得分: " + myFighter.getScore(),150, 70);
		
		g.setFont(font);
		g.setColor(Color.RED);
		g.drawLine(MAINXPOINT + MAINWIDTH * 4 / 5, MAINYPOINT - 25,
				   MAINXPOINT + MAINWIDTH * 4 / 5, MAINYPOINT + MAINHEIGHT);
		g.setColor(c);
		//画出主战机
		myFighter.draw(g);

		for(int i = 0; i < superStars.size(); i++) {
			SuperStar ss = superStars.get(i);
			myFighter.eat(ss);
			ss.draw(g);
		}
		
		//判断如果超星数少于0,则添加
		if (superStars.size() <= 0) {
			for(int i = 0; i < r.nextInt(2); i++) {
				boolean flag1 = true;
				boolean flag2 = true;
				int x = 0, y = 0;
				while(flag1 || flag2) {
					int t1 = r.nextInt(3000);
					int t2 = r.nextInt(1000);
					if(t1 <= 1100 && t1 >= 1000) {
						x = t1;
						flag1 = false;
					} 
					if(t2 <= 550 && t2 >= 30) {
						y = t2;
						flag2 = false;
					}
				}
				if(r.nextInt(500) > 495) {
					superStars.add(new SuperStar(x, y, this, Direction.RTL, r.nextInt(6) + 4));
				}
			}
		}
		
		//画出大决
		for(int i = 0; i < superLines.size(); i++) {
			SuperLine s = superLines.get(i);
			s.hitFighters(enemys);
			//s.hitBombs(bombs);
			s.draw(g);
		}
		
		//如果敌机数量少于3,则继续添加
		if(enemys.size() <= 2) {
			for(int i = 0; i < r.nextInt(10); i++) {
				boolean flag1 = true;
				boolean flag2 = true;
				int x = 0, y = 0;
				while(flag1 || flag2) {
					int t1 = r.nextInt(3000);
					int t2 = r.nextInt(1000);
					if(t1 <= 1100 && t1 >= 1000) {
						x = t1;
						flag1 = false;
					} 
					if(t2 <= 550 && t2 >= 30) {
						y = t2;
						flag2 = false;
					}
				}		
				
				enemys.add(new Fighter(x, y, true, this, Direction.RTL,r.nextInt(6) + 4));
			}
		}
		
		//画出
		for(int i = 0; i < bombs.size(); i++) {
			Bomb b = bombs.get(i);
			
			b.hitFighters(enemys);
			b.hitFighter(myFighter);
			b.hitBombs(bombs);
			b.draw(g);
		}
		
		//画出敌机
		for(int i = 0; i < enemys.size(); i++) {
			Fighter f = enemys.get(i);
			
			if(f.x <= 0) {
				f.isLive = false;
			}
			
			if(f.isEnemy) {
				if((r.nextInt(50)) > 48) {
					f.fire();
				}
			}
			f.draw(g);
		}
		
		//画出爆炸
		for(int i = 0; i < explodes.size(); i++) {
			Explode e = explodes.get(i);
			e.draw(g);
		}
	}
	
	//统一的主界面的调用函数
	public void launchGameFrame() {
		this.setBounds(MAINXPOINT, MAINYPOINT, MAINWIDTH, MAINHEIGHT);
		this.setBackground(Color.BLACK);
		this.setLayout(null);
		//关闭窗口的处理
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				setVisible(false);
				System.exit(0);
			}
		});
		
		//按键处理
		this.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				myFighter.keyPressed(e);
			}
			
			public void keyReleased(KeyEvent e) {
				myFighter.keyReleased(e);
			}
			
		});
		
		//在窗口显示前开始线程重画
		new Thread(new PaintThread()).start();
			
		this.setResizable(false);
		this.setVisible(true);
	}
	
	//重写update()方法,实现双缓冲取消图像闪烁的情况
	public void update(Graphics g) {
		if(bufferImage == null) {
			bufferImage  = 
				this.createImage(MAINWIDTH, MAINHEIGHT);
		}
		
		Graphics gBuffer = bufferImage.getGraphics();
		Color c = gBuffer.getColor();
		gBuffer.setColor(Color.BLACK);
		gBuffer.fillRect(0, 0, MAINWIDTH, MAINHEIGHT);
		gBuffer.setColor(c);
		
		paint(gBuffer);
		g.drawImage(bufferImage, 0, 0,null);
	}
	
    //绘图线程---用于不断的重绘
	class PaintThread implements Runnable {
		int step = 0;
		public void run() {
			while(true) {
				repaint();
				try {
					Thread.sleep(20);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				step ++;
				
				if(step % 50 == 0) {
					time ++;
					step = 0;
				}
				
			}
		}
	}
	
	public String showTime() {
		int hour = time / 3600;
		int minite = (time - hour*3600) / 60;
		int second = (time - hour*3600) % 60;
		return hour + ":" + minite + ":" + second;
	}
}


工程源码+ 文档+可执行文件:http://download.csdn.net/detail/haifengzhilian/4494594




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics