自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(0)
  • 资源 (1)
  • 收藏
  • 关注

空空如也

飞机大战游戏

这是我们集体编写的一个小小的单机游戏! package com.tarena.fight.game; import java.awt.Color; import java.awt.Graphics; import java.awt.MediaTracker; import java.awt.Toolkit; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.io.File; import javax.imageio.ImageIO; import javax.swing.JPanel; import com.tarena.fight.model.GameBean; /** 主面板 * @author tang MouseMotionListener 用于接收组件上的鼠标移动事件的侦听器接口。(对于单击和其他鼠标事件,请使用 * MouseListener。) 旨在处理鼠标移动事件的类要么实现此接口(及其包含的所有方法), 要么扩展抽象 * MouseMotionAdapter 类(仅重写有用的方法)。 MouseListener * 用于接收组件上“感兴趣”的鼠标事件(按下、释放、单击、进入或离开)的侦听器接口。 (要跟踪鼠标移动和鼠标拖动,请使用 * MouseMotionListener。) Runnable 为希望在活动时执行代码的对象提供一个公共协议 */ public class MainPanel extends JPanel implements Runnable, MouseMotionListener,MouseListener { GameFrame a; GameBean gb=new GameBean(); MainPanel(GameFrame a) { this.a = a; installPic();//加载图片 this.addMouseMotionListener(this); this.addMouseListener(this); gb.Game_star = new Thread(this); gb.y = -(5 * a.y); gb.hero_x = a.x / 2 - 50; gb.hero_y = a.y - 150; gb.hero_oldx = gb.hero_x; gb.hero_oldy = gb.hero_y; gb.hero_hp = -1; gb.seq = 0; gb.dir = 0; gb.Sum = 0; gb.E_num = 0; gb.E_max = 9; gb.E_vel = 7; gb.E_mov = 90; gb.E_hit = 97; gb.isfire = false; } /** * 绘图部分 */ public void paint(Graphics g) { /** * 绘制图片属性 */ g.drawImage(gb.back, 0, gb.y, a.x, a.y * 6, this); g.setColor(Color.RED); g.setFont(gb.S); g.drawString("生", a.x - 23, 40); g.drawString("命", a.x - 23, 55); g.fillRect(a.x - 20, 65, 10, gb.hero_hp); g.setColor(Color.BLACK); g.drawRect(a.x - 20, 64, 10, 200); if (gb.seq == 0){ g.setColor(Color.RED); } if (gb.seq == 1){ g.setColor(Color.BLACK); } /** * 显示信息(设置字体) */ g.setFont(gb.S); g.drawString("积分:", a.x - 80, 25); g.drawString(String.valueOf(gb.Sum), a.x - 40, 25); g.setFont(gb.f); // 如果游戏尚未开始显示如下信息 if (gb.hero_hp == -1) { gameNotStart(g); }else if (gb.hero_hp < 0) { // 如果游戏结束显示如下信息 gameStart(g); } // 显示信息 showManager1(g); showManager2(g); showManager3(g); showManager4(g); showManager5(g); showManager6(g); chulidijizidan(g);// 处理敌机子弹s zidanchuli(g);// 处理子弹s chulibaozha(g);// 处理爆炸s dijichuli(g);// 处理敌机s // 处理hero g.clipRect(gb.hero_x, gb.hero_y, 70, 70); g.drawImage(gb.hero, (gb.hero_x - gb.seq * 70), gb.hero_y, 140, 70, this); /********************************************************************************************/ } /** * 加載圖片 */ public void installPic(){ gb.tracker = new MediaTracker(this); try{ gb.back = ImageIO.read(new File("./image/back.jpg")); gb.tracker.addImage(gb.back, 0); gb.hero = ImageIO.read(new File("./image/hero.gif")); gb.tracker.addImage(gb.hero, 1); gb.fire = ImageIO.read(new File("./image/fire.gif")); gb.tracker.addImage(gb.fire,2); gb.fire_1= ImageIO.read(new File("./image/fire_1.gif")); gb.tracker.addImage(gb.fire_1,3); gb.bomb_0 = ImageIO.read(new File("./image/bomb_0.gif")); gb.tracker.addImage(gb.bomb_0, 4); gb.bomb_1 = ImageIO.read(new File("./image/bomb_1.gif")); gb.tracker.addImage(gb.bomb_1, 5); gb.bomb_2 = ImageIO.read(new File("./image/bomb_2.gif")); gb.tracker.addImage(gb.bomb_2, 6); gb.bomb_3 = ImageIO.read(new File("./image/bomb_3.gif")); gb.tracker.addImage(gb.bomb_3, 7); gb.el_0_l = ImageIO.read(new File("./image/el_0_l.png")); gb.tracker.addImage(gb.el_0_l,8); gb.el_0_r = ImageIO.read(new File("./image/el_0_r.png")); gb.tracker.addImage(gb.el_0_r,9); gb.el_0 = ImageIO.read(new File("./image/el_0.png")); gb.tracker.addImage(gb.el_0,10); gb.el_bb_0= ImageIO.read(new File("./image/el_bb_0.gif")); gb.tracker.addImage(gb.el_bb_0,11); gb.el_bb = ImageIO.read(new File("./image/el_bb.gif")); gb.tracker.addImage(gb.el_bb,12); }catch(Exception e){} } /** * 游戏未开始显示信息 * @param g */ public void gameNotStart(Graphics g){ g.setFont(gb.f); g.setColor(Color.RED); g.drawString("你有信心超过1000分吗?", a.x / 2 - a.x / 3 + 60, a.y / 2 - a.y / 16); g.setFont(gb.SS); g.setColor(Color.BLUE); g.drawString("让我们按下鼠标键开战吧!Raedy Go!", a.x / 2 - a.x / 4 + 35, a.y / 2 + a.y / 8); g.dispose(); try { this.gb.Game_star.sleep(50); } catch (Exception e) { } ; this.gb.Game_star = null; } /** * 游戏开始显示信息 */ public void gameStart(Graphics g){ g.setFont(gb.ff); g.setColor(Color.RED); g.drawString("GAME", a.x / 2 - a.x / 4 + 35, a.y / 2 - a.y / 16); g.drawString("OVER", a.x / 2 - a.x / 4 + 35, a.y / 2 + a.y / 16); g.setFont(gb.SS); g.setColor(Color.BLUE); g.drawString("让我们按下鼠标键复活吧!", a.x / 2 - a.x / 4 + 35, a.y / 2 + a.y / 8); g.dispose(); try { this.gb.Game_star.sleep(50); } catch (Exception e) { } ; this.gb.Game_star = null; } /** * 显示信息1 */ public void showManager1(Graphics g){ if (Math.abs(gb.y) > (a.y) * 5 - 50) { g.drawString("英雄们,让我们为荣誉而战吧!", a.x / 2 - a.x / 4, a.y / 2 - a.y / 8); } } /** * 显示信息2 */ public void showManager2(Graphics g){ if (Math.abs(gb.y) > (a.y) * 4 && Math.abs(gb.y) < (a.y) * 4 + 150) { gb.E_max = 13; gb.E_vel = 7; gb.E_mov = 90; gb.E_hit = 95; g.drawString("第一关即将开始", a.x / 2 - a.x / 4, a.y / 2 - a.y / 8); } } /** * 显示信息3 */ public void showManager3(Graphics g){ if (Math.abs(gb.y) > (a.y) * 3 && Math.abs(gb.y) < (a.y) * 3 + 150) { gb.E_max = 16; gb.E_vel = 9; gb.E_mov = 90; gb.E_hit = 93; g.drawString("第一关即将开始", a.x / 2 - a.x / 4, a.y / 2 - a.y / 8); } } /** * 显示信息4 */ public void showManager4(Graphics g){ if (Math.abs(gb.y) > (a.y) * 2 && Math.abs(gb.y) < (a.y) * 2 + 150) { gb.E_max = 29; gb.E_vel = 11; gb.E_mov = 90; gb.E_hit = 91; g.drawString("第三关即将开始", a.x / 2 - a.x / 4, a.y / 2 - a.y / 8); } } /** * 显示信息5 */ public void showManager5(Graphics g){ if (Math.abs(gb.y) > (a.y) && Math.abs(gb.y) < (a.y) + 150) { gb.E_max = 32; gb.E_vel = 13; gb.E_mov = 90; gb.E_hit = 89; g.drawString("最后关头了,祝你好运!", a.x / 2 - a.x / 3, a.y / 2 - a.y / 16); g.drawString("让我们冲向殿堂吧!", a.x / 2 - a.x / 4, a.y / 2 - a.y / 8); } } /** * 显示信息6 */ public void showManager6(Graphics g){ if (gb.y == 0) { gb.E_max = 40; gb.E_vel = 18; gb.E_mov = 90; gb.E_hit = 86; g.drawString("向上帝祈祷吧!", a.x / 2 - a.x / 4, a.y / 2 - a.y / 8); } } /** * 敌机子弹处理 */ public void chulidijizidan(Graphics g){ for (int i = 0; i < gb.EBv.size(); i++) { EnemyBullet ebs; ebs = (EnemyBullet) gb.EBv.elementAt(i); if (ebs.y > -50) { if (ebs.cont < 6) g.drawImage(gb.el_bb, ebs.x, ebs.y, 20, 20, this); if (ebs.cont >= 6) g.drawImage(gb.el_bb_0, ebs.x, ebs.y, 20, 20, this); } else { } } } /** * 子弹处理 */ public void zidanchuli(Graphics g){ for (int i = 0; i < gb.BBv.size(); i++) { MyBullet bs; bs = (MyBullet) gb.BBv.elementAt(i); if (bs.y > -50) { if (bs.cont < 3) g.drawImage(gb.fire_1, bs.x, bs.y, 50, 50, this); if (bs.cont >= 3) g.drawImage(gb.fire, bs.x, bs.y, 50, 50, this); } else { } } } /** * 爆炸处理 */ public void chulibaozha(Graphics g){ for (int i = 0; i < gb.BOMBv.size(); i++) { Bomb bombs; bombs = (Bomb) gb.BOMBv.elementAt(i); if (bombs.islive) { if (bombs.cont < 10) g.drawImage(gb.bomb_0, bombs.x, bombs.y, 50, 50, this); if (bombs.cont > 10 && bombs.cont < 20) g.drawImage(gb.bomb_1, bombs.x, bombs.y, 50, 50, this); if (bombs.cont > 20 && bombs.cont < 40) g.drawImage(gb.bomb_2, bombs.x, bombs.y, 50, 50, this); if (bombs.cont > 40) g.drawImage(gb.bomb_3, bombs.x, bombs.y, 50, 50, this); } else { gb.BOMBv.remove(i); } } } /** * 敌机处理 */ public void dijichuli(Graphics g){ for (int i = 0; i < gb.EEv.size(); i++) { Enemy es; es = (Enemy) gb.EEv.elementAt(i); if (es.y < (a.y - 50)) { if (es.islive && !es.toleft && !es.toright) { g.drawImage(gb.el_0, es.x, es.y, 50, 50, this); } else if (es.toleft) { g.drawImage(gb.el_0_l, es.x, es.y, 50, 50, this); } else if (es.toright) { g.drawImage(gb.el_0_r, es.x, es.y, 50, 50, this); } } else { es.islive = false; } } } // 主线程 @Override public void run() { int cont = 0;// 定义更换hero动画的频率 while (gb.tracker.checkAll(true) == false) { try { Thread.sleep(300); } catch (Exception e) { } } while (gb.Game_star != null) { // 处理敌机s是否撞击hero if (gb.EEv.size() == 0) { gb.E_num = 0; } // 添加敌机数组 addDiji(cont); // 处理子弹s是否击中 zidanshifoujizhong(); // 处理爆炸s baozhachuli(); // 处理敌机子弹是否击中 dijizidanjizhongfou(); changeScore(cont++);// 定义更换hero动画的频率 repaint(); try { Thread.sleep(20); } catch (Exception E) { } changeY();//改变y坐标值 } } /** * 敌机添加 */ public void addDiji(int cont){ if (cont != 0) { // 移动条件下增加难度 if (gb.E_num <= gb.E_max)// 满足条件则增加敌机 { Enemy es; es = new Enemy((int) (Math.random() * (a.x - 50)), gb.E_vel, gb.E_mov, gb.E_hit); gb.EEv.addElement(es); gb.E_num++; } //敌机数组处理 dijichuli(); } } /** * 敌机处理 */ public void dijichuli(){ for (int i = 0; i < gb.EEv.size(); i++) { Enemy es; es = (Enemy) gb.EEv.elementAt(i); if (es.y < (a.y - 50) && es.islive) { es.hit(gb.hero_x, gb.hero_y);// 监测碰撞 if (!es.islive) { Bomb bombs; bombs = new Bomb(es.x, es.y); gb.BOMBv.add(bombs); System.out.println("YOU LOST"); gb.hero_hp -= 10; } // 移动未撞到的敌机 moveDiji(es,i); } else { gb.EEv.remove(i); }// 消除无效敌机 } } /** * 移动未撞到的敌机 */ public void moveDiji(Enemy es,int i){ if (es.islive){ es.move(gb.hero_x); es.tofire(); if (es.tofire() && i % 4 == 0 && es.y < (a.y - 100)) { EnemyBullet ebs; ebs = new EnemyBullet(es.x, es.y, gb.hero_x, gb.hero_y); gb.EBv.addElement(ebs); } } } /** * 处理子弹是否击中 */ public void zidanshifoujizhong(){ for (int i = 0; i < gb.BBv.size(); i++) { MyBullet bs; bs = (MyBullet) gb.BBv.elementAt(i); //子弹存货 zidancunhuo(bs,i); } } /** * 子弹存货 */ public void zidancunhuo(MyBullet bs,int i){ if (bs.y > -50 && bs.islive) // 子弹存活 { for (int j = 0; j < gb.EEv.size(); j++) { Enemy es; es = (Enemy) gb.EEv.elementAt(j); dijicunhuo(es,bs);//敌机存货 } if (bs.islive) { bs.move(); }// 未击中的子弹继续移动 } else { gb.BBv.remove(i); }// 清除击中的子弹 } /** * 敌机存货 * @param es * @param bs */ public void dijicunhuo(Enemy es,MyBullet bs){ if (es.islive && bs.islive)// 敌机存活 { es.hit(bs.x, bs.y); bs.hit(es.x, es.y);// 监测碰撞 if (!es.islive)// 添加爆炸 { gb.Sum++; Bomb bombs; bombs = new Bomb(es.x, es.y); gb.BOMBv.add(bombs); } } } /** * 爆炸处理 */ public void baozhachuli(){ for (int i = 0; i < gb.BOMBv.size(); i++) { Bomb bombs; bombs = (Bomb) gb.BOMBv.elementAt(i); if (bombs.islive) { bombs.grow(); } else { gb.BOMBv.remove(i); } } } /** * 处理敌机子弹是否击中 */ public void dijizidanjizhongfou(){ for (int i = 0; i < gb.EBv.size(); i++) { EnemyBullet ebs; ebs = (EnemyBullet) gb.EBv.elementAt(i); ebs.hit(gb.hero_x, gb.hero_y); // 消除无效子弹 if (ebs.islive && ebs.y > -10 && ebs.y < a.y - 10 && ebs.x > 0 && ebs.x < a.x - 10) { ebs.move(); } else if (!ebs.islive) { System.out.println("hit you"); gb.EBv.remove(i); gb.hero_hp -= 10; } else { gb.EBv.remove(i); } } } /** * 定义更换hero动画的频率 */ public void changeScore(int cont){ if (cont % 3 == 0) { if (gb.isfire) { BBmove(gb.BBx, gb.BBy); } if (gb.seq == 0) { gb.seq = 1; } else { gb.seq = 0; } cont = 0; } } /** * 改变y坐标 */ public void changeY(){ if (gb.y == 0) { gb.y = 0; } else { gb.y++; } } public void BBmove(int x, int y) { MyBullet bs; bs = new MyBullet(x, y); // 保证不出届 if (gb.BBx >= (a.x - 75)) { gb.BBx = a.x - 75; } if (gb.BBy >= (a.y - 150)) { gb.BBy = a.y - 150; } gb.BBv.add(bs); } @Override public void mouseDragged(MouseEvent e) { gb.isfire = true; gb.BBx = gb.hero_x + 10; gb.BBy = gb.hero_y; gb.hero_x = e.getX() - 35; gb.hero_y = e.getY() - 35; // 保证不出届 if (gb.BBx >= (a.x - 75)) { gb.BBx = a.x - 75; } if (gb.BBy >= (a.y - 150)) { gb.BBy = a.y - 150; } if (gb.BBx < 25) { gb.BBx = 25; } if (gb.BBy < 25) { gb.BBy = 25; } // 限速 if ((gb.hero_x - gb.hero_oldx) > 15) { gb.hero_x = gb.hero_oldx + 15; } ; if ((gb.hero_x - gb.hero_oldx) < -15) { gb.hero_x = gb.hero_oldx - 15; } ; if ((gb.hero_y - gb.hero_oldy) > 15) { gb.hero_y = gb.hero_oldy + 15; } ; if ((gb.hero_y - gb.hero_oldy) < -15) { gb.hero_y = gb.hero_oldy - 15; } ; // 保证不出届 if (gb.hero_x >= (a.x - 70)) { gb.hero_x = a.x - 70; } if (gb.hero_y >= (a.y - 120)) { gb.hero_y = a.y - 120; } if (gb.hero_x < 0) { gb.hero_x = 0; } if (gb.hero_y < 0) { gb.hero_y = 0; } gb.hero_oldx = gb.hero_x; gb.hero_oldy = gb.hero_y; } @Override public void mouseMoved(MouseEvent e) { gb.hero_x = e.getX() - 35; gb.hero_y = e.getY() - 35; // 保证不出届 if (gb.hero_x >= (a.x - 70)) { gb.hero_x = a.x - 70; } if (gb.hero_y >= (a.y - 120)) { gb.hero_y = a.y - 120; } if (gb.hero_x < 0) { gb.hero_x = 0; } if (gb.hero_y < 0) { gb.hero_y = 0; } // 限速 if ((gb.hero_x - gb.hero_oldx) > 15) { gb.hero_x = gb.hero_oldx + 15; } ; if ((gb.hero_x - gb.hero_oldx) < -15) { gb.hero_x = gb.hero_oldx - 15; } ; if ((gb.hero_y - gb.hero_oldy) > 15) { gb.hero_y = gb.hero_oldy + 15; } ; if ((gb.hero_y - gb.hero_oldy) < -15) { gb.hero_y = gb.hero_oldy - 15; } ; gb.hero_oldx = gb.hero_x; gb.hero_oldy = gb.hero_y; } @Override public void mousePressed(MouseEvent e) { // 如果GAMEOVER可以重新开始 if (gb.hero_hp < 0) { if (this.gb.Game_star != null) { this.gb.Game_star.stop(); this.gb.Game_star = null; } this.gb.Game_star = new Thread(this); gb.Game_star.start(); gb.Game_star.setPriority(7); gb.y = -(5 * a.y); gb.hero_x = a.x / 2 - 35; gb.hero_y = a.y - 85; gb.hero_oldx = gb.hero_x; gb.hero_oldy = gb.hero_y; gb.hero_hp = 200; gb.seq = 0; gb.dir = 0; gb.Sum = 0; gb.E_num = 0; gb.E_max = 9; gb.E_vel = 7; gb.E_mov = 90; gb.E_hit = 97; gb.isfire = false; gb.EEv.removeAllElements(); gb.BBv.removeAllElements(); gb.EBv.removeAllElements(); gb.BOMBv.removeAllElements(); }else { gb.isfire = true; gb.BBx = gb.hero_x + 10; gb.BBy = gb.hero_y; // 保证不出届 if (gb.BBx >= (a.x - 75)) { gb.BBx = a.x - 75; } if (gb.BBy >= (a.y - 150)) { gb.BBy = a.y - 150; } if (gb.BBx < 25) { gb.BBx = 25; } if (gb.BBy < 25) { gb.BBy = 25; } } } @Override public void mouseReleased(MouseEvent e) { gb.isfire = false; } @Override public void mouseEntered(MouseEvent e) {} @Override public void mouseExited(MouseEvent e) {} @Override public void mouseClicked(MouseEvent e) {} }

2014-04-01

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除