飞机大战 java源代码(14页).doc
《飞机大战 java源代码(14页).doc》由会员分享,可在线阅读,更多相关《飞机大战 java源代码(14页).doc(14页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、-飞机大战 java 源代码-第 14 页package com;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.Rectangle;import java.awt.event.KeyEvent;import java.io.IOException;import javax.imageio.ImageIO;public class Plane Image feijiImage = null;int x = 300;int y = 7
2、00;int lifeCount=5;public Plane() try feijiImage = ImageIO.read(Plane.class.getClassLoader().getResourceAsStream(images/feiji.png); catch (IOException e) e.printStackTrace();public void draw(Graphics g) /画飞机图片g.drawImage(feijiImage, x, y, null);/飞机移动this.move();/ 血条if(lifeCount0)g.setColor(Color.WHI
3、TE);g.fillRect(20, 80, 100, 10);g.setColor(Color.red);g.fillRect(20, 80, (100/5)*lifeCount, 10);g.setColor(Color.blue);g.setFont(new Font(幼圆, Font.BOLD, 30);g.drawString(Score:+Play01.count, 20, 60);public void move()if(isUP & !isDown & !isLeft & !isRight)/上y=y-5;else if(!isUP & isDown & !isLeft & !
4、isRight)/下y=y+5;else if(!isUP & !isDown & isLeft & !isRight)/左x=x-5;else if(!isUP & !isDown & !isLeft & isRight)/右x=x+5;else if(isUP & !isDown & isLeft & !isRight)/左上x=x-5;y=y-5;else if(!isUP & isDown & isLeft & !isRight)/左下x=x-5;y=y+5;else if(isUP & !isDown & !isLeft & isRight)/右上x=x+5;y=y-5;else i
5、f(!isUP & isDown & !isLeft & isRight)/右下x=x+5;y=y+5;boolean isUP = false;boolean isDown = false;boolean isLeft = false;boolean isRight = false;/ 摁下public void keyPressed(KeyEvent e) int keyCode = e.getKeyCode();if (keyCode = KeyEvent.VK_RIGHT | keyCode = KeyEvent.VK_D) isRight=true; else if (keyCode
6、 = KeyEvent.VK_LEFT | keyCode = KeyEvent.VK_A) isLeft=true; else if (keyCode = KeyEvent.VK_UP | keyCode = KeyEvent.VK_W) isUP=true; else if (keyCode = KeyEvent.VK_DOWN | keyCode = KeyEvent.VK_S) isDown=true;/ 放开public void keyReleased(KeyEvent e) int keyCode = e.getKeyCode();if (keyCode = KeyEvent.V
7、K_RIGHT | keyCode = KeyEvent.VK_D) isRight=false; else if (keyCode = KeyEvent.VK_LEFT | keyCode = KeyEvent.VK_A) isLeft=false; else if (keyCode = KeyEvent.VK_UP | keyCode = KeyEvent.VK_W) isUP=false; else if (keyCode = KeyEvent.VK_DOWN | keyCode = KeyEvent.VK_S) isDown=false;public Rectangle getRect
8、angle()return new Rectangle(x,y,feijiImage.getWidth(null),feijiImage.getHeight(null); package com;public class PlaneStatus public static int roleNum = 1;public static int playStatus = 0;/ 0 游戏开始前 1 第一关 -1 游戏结束package com;import java.awt.Graphics;import java.awt.Image;import java.awt.Rectangle;import
9、 java.awt.event.KeyEvent;import java.io.IOException;import javax.imageio.ImageIO;public class Power Image PowerImage = null;int x = 20;int y = 0;public Power() this.x=(int)(Math.random()*540)+20;try PowerImage = ImageIO.read(Gift.class.getClassLoader().getResourceAsStream(images/power.png); catch (I
10、OException e) e.printStackTrace();public void draw(Graphics g) /画血瓶图片g.drawImage(PowerImage, x, y, null);/血瓶移动y+;public Rectangle getRectangle()return new Rectangle(x,y,PowerImage.getWidth(null),PowerImage.getHeight(null); package com;import java.awt.Graphics;import java.awt.Image;import java.awt.Re
11、ctangle;import java.awt.event.KeyEvent;import java.io.IOException;import java.util.ArrayList;import java.util.List;import javax.imageio.ImageIO;/ 第一关public class Play01 static int count=0;Image bgImage = null;/ 战机Plane plane = new Plane();/ 战机子弹List mzds = new ArrayList();/ 敌机List dijis = new ArrayL
12、ist();/ 敌机子弹/List dijizidans = new ArrayList();/ 血瓶List gifts = new ArrayList();List powers = new ArrayList();public Play01() try bgImage = ImageIO.read(Play01.class.getClassLoader().getResourceAsStream(images/bg_01.jpg); catch (IOException e) e.printStackTrace();int bgY1 = 0;int bgY2 = -600;int fir
13、eTime = 0;boolean flag=false;public void draw(Graphics g) / 画背景图片g.drawImage(bgImage, 0, bgY1, null);bgY1 += 5;if (bgY1 = 0) bgY2 = -600;/ 清理战机子弹for (int i = 0; i 800) mzds.remove(i);/ 添加子弹if (isFire1 = true & flag=false) if (fireTime % 8 = 0) mzds.add(new MyZiDan(plane.x + 25, plane.y + 0);fireTime
14、+;if (isFire1= true & flag=true) if (fireTime % 4 = 0) mzds.add(new MyZiDan(plane.x +8, plane.y +0);mzds.add(new MyZiDan(plane.x +52, plane.y +0);fireTime+;/ 画战机子弹for (int i = 0; i mzds.size(); i+) MyZiDan myZidan = mzds.get(i);myZidan.draw(g);/ 清理敌机for (int i = 0; i dijis.size(); i+) Diji dj = diji
15、s.get(i);if (dj.x -100) dijis.remove(i);/ 抽奖:添加敌机if (int) (Math.random() * 20) = 5) dijis.add(new Diji();/ 抽奖:添加血瓶if (int) (Math.random() * 1000) = 5) gifts.add(new Gift();for (int i = 0; i gifts.size(); i+) Gift gift = gifts.get(i);gift.draw(g);/ 抽奖:添加powerif (int) (Math.random() * 1000) = 5) power
16、s.add(new Power();for (int i = 0; i powers.size(); i+) Power power = powers.get(i);power.draw(g);/ 画敌机for (int i = 0; i dijis.size(); i+) Diji dj = dijis.get(i);dj.draw(g);/ 画飞机plane.draw(g);/ 判断战机相撞for (int i = 0; i 0)flag=false; if(plane.lifeCount1) PlaneStatus.playStatus=-1; / 判断战机和血瓶相撞for (int i
17、 = 0; i gifts.size(); i+) / 先得到每一个血瓶Gift gift = gifts.get(i);Rectangle r1 = gift.getRectangle();Rectangle r2 = plane.getRectangle();if (r1.intersects(r2) gifts.remove(i);if (plane.lifeCount 5) plane.lifeCount = plane.lifeCount + 1;/ 判断战机和Power相撞for (int i = 0; i powers.size(); i+) / 先得到每一个powerPower
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 飞机大战 java 源代码14页 飞机 大战 源代码 14
限制150内