读书人

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

发布时间: 2012-09-01 09:33:02 作者: rapoo

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

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();//添加随机数产生器,用于控制敌方的AIImage 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




读书人网 >编程

热点推荐