java——菜鸟飞机大战

Julia ·
更新时间:2024-11-10
· 629 次阅读

说是飞机大战倒不如说是飞机躲避炮弹

//飞机游戏主窗口 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.Date; import javax.swing.JFrame; import utils.GameUtil; public class MyGame09 extends Frame{ public static final Thread Thead = null; Image bg=GameUtil.getImage("imgs/background.png"); Image planeImg=GameUtil.getImage("imgs/hero0.png"); Plane plane=new Plane(planeImg,200,500); // Shell shell=new Shell(); Shell [] shells=new Shell[50]; Explode baoz; Date startTime=new Date(); Date endTime; int period ;//游戏持续的时间 @Override public void paint(Graphics g) {//绘图 窗口类的主方法 自动执行 super.paint(g);//调用父类的paint g.drawImage(bg,0,0,null);//画图片 plane.drawSelf(g);//画飞机 Color c=g.getColor(); //画出所有的炮弹 for(int i=0;i<shells.length;i++) { shells[i].draw(g); //飞机和炮弹封装检测 boolean flag=shells[i].getRect().intersects(plane.getRect()); if(flag) { plane.live=false; if(baoz==null) { baoz=new Explode(plane.x,plane.y); endTime=new Date(); period=(int)((endTime.getTime()-startTime.getTime())/1000); } baoz.draw(g); } if(!plane.live) { g.setColor(Color.red); g.setFont(new Font("宋体",Font.BOLD,50)); g.drawString("存活时间为"+period+"秒", 100, 300); } } g.setColor(c); } //帮助我们重复的重画窗口 class PaintThread extends Thread{ public void run() { while(true) { repaint();//外部类方法 重画 try { Thead.sleep(60); }catch(InterruptedException e) { e.printStackTrace(); } } } } //定义键盘监听的内部类 class KeyMonitor extends KeyAdapter{ @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub // System.out.println("按下"+e.getKeyCode()); plane.addDirection(e); } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub // System.out.println("松开"+e.getKeyCode()); plane.minusDirection(e); } } //初始化窗口 public void launchFrame() { this.setTitle("琳同学的飞机大战"); this.setVisible(true); this.setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT); this.setLocationRelativeTo(null); this.setResizable(false); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); new PaintThread().start();//启动重画窗口的线程 addKeyListener(new KeyMonitor());//给窗口增加键盘监听 //初始化50个炮弹 for(int i=0;i<shells.length;i++) { shells[i]=new Shell(); } } public static void main(String []args) { MyGame09 f=new MyGame09(); f.launchFrame(); } private Image offScreenImage = null; public void update(Graphics g) { if(offScreenImage == null) { //这是游戏窗口的宽度和高度 offScreenImage = this.createImage(Constant.GAME_WIDTH, Constant.GAME_HEIGHT); } Graphics gOff = offScreenImage.getGraphics(); paint(gOff); g.drawImage(offScreenImage, 0, 0, null); } } public class Constant { //控制游戏页面的大小 public static final int GAME_WIDTH=500; public static final int GAME_HEIGHT=700; } import java.awt.Graphics; import java.awt.Image; import utils.GameUtil; //爆炸类,在飞机和炮弹碰撞时,一系列图片的显示 public class Explode { double x,y; static Image[] imgs=new Image[4]; static { for(int i=0;i<4;i++) { imgs[i]=GameUtil.getImage("imgs/bom"+(i+1)+".png"); imgs[i].getWidth(null); } } int count; public void draw(Graphics g) { if(count <=3) { g.drawImage(imgs[count],(int)x+20,(int)y+20,null); count++; } } public Explode(double x,double y) { this.x=x; this.y=y; } } import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; //游戏物体的父类 public class GameObject { Image img; public double x; public double y; int speed; int width,height; public void drawSelf(Graphics g) { g.drawImage(img, (int)x,(int)y,null); } public GameObject(Image img, double x, double y) { super(); this.img = img; this.x = x; this.y = y; } public GameObject(Image img, double x, double y, int speed, int width, int height) { super(); this.img = img; this.x = x; this.y = y; this.speed = speed; this.width = width; this.height = height; } public GameObject() {} //返回物体所在的矩形,便于后续的碰撞检测 public Rectangle getRect() { return new Rectangle((int)x,(int)y,width,height); } } import java.awt.Color; import java.awt.Graphics; //炮弹类 public class Shell extends GameObject { double degree; public Shell() { x=200; y=200; width=10; height=10; speed=4; degree=Math.random()*Math.PI*2; } public void draw(Graphics g) { Color c=g.getColor(); g.setColor(Color.YELLOW); g.fillOval((int)x, (int)y, width, height); //炮弹沿着任意角度去飞 x +=speed*Math.cos(degree); y +=speed*Math.sin(degree); if(xConstant.GAME_WIDTH-width-24) { degree=Math.PI-degree; } if(yConstant.GAME_HEIGHT-height) { degree=-degree; } g.setColor(c); } } import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyEvent; public class Plane extends GameObject{ int speed=8; boolean left,up,right,down; boolean live=true; public void drawSelf(Graphics g) { if(live) {g.drawImage(img, (int)x,(int)y,null); if(left) { x-=speed; } if(right) { x+=speed; } if(up) { y-=speed; } if(down) { y+=speed; } }else { } } public Plane(Image img,double x,double y) { this.img=img; this.x=x; this.y=y; this.speed=3; this.width=img.getWidth(null); this.height=img.getHeight(null); } //按下某个键,增加相应的方向 public void addDirection(KeyEvent e) { switch(e.getKeyCode()) { case KeyEvent.VK_LEFT: left=true; break; case KeyEvent.VK_UP: up=true; break; case KeyEvent.VK_RIGHT: right=true; break; case KeyEvent.VK_DOWN: down=true; break; } } //取消相应的键 public void minusDirection(KeyEvent e) { switch(e.getKeyCode()) { case KeyEvent.VK_LEFT: left=false; break; case KeyEvent.VK_UP: up=false; break; case KeyEvent.VK_RIGHT: right=false; break; case KeyEvent.VK_DOWN: down=false; break; } } }

在这里插入图片描述
在这里插入图片描述


作者:琳同学



JAVA 飞机大战 菜鸟

需要 登录 后方可回复, 如果你还没有账号请 注册新账号