首页 » 技术分享 » Java_85_开发体验_飞机游戏

Java_85_开发体验_飞机游戏

 

建立窗口

package com.nuipc.game;
/**
 * 飞机游戏的主窗口
 * @author pmc
 *
 */
import javax.swing.JFrame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//import java.awt.*;
public class MyGameFrame extends JFrame {
	/**
	 * 初始化窗口
	 */
	public void launchFrame(){
		this.setTitle("pmc_作品");//窗口标题
		this.setVisible(true);//窗口可见
		this.setSize(500,500);
		this.setLocation(300,300);
		/**
		 * 窗口关闭
		 */
		this.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					System.exit(0);
				}
		});
	}
	public static void main(String[] args){
		MyGameFrame f=new MyGameFrame();
		f.launchFrame();
	}
}

图形绘制_改变颜色_改变字体_加载图像对象

package com.nuipc.game;
/**
 * 飞机游戏的主窗口
 * @author pmc
 *
 */
import javax.swing.JFrame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//import java.awt.*;
public class MyGameFrame2 extends JFrame {
	Image ball=GameUtil.getImage("images/ball.png");
	/**
	 * paint自动被调用,g相当于一只画笔
	 */
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		Color c=g.getColor();
		Font f=g.getFont();
		g.setColor(Color.BLUE);
		g.drawLine(100, 100, 300, 300);
		g.drawRect(100, 100, 300, 300);
		g.drawOval(100, 100, 300, 300);
		g.fillRect(100, 100, 40, 40);
		g.setColor(Color.red);
		g.setFont(new Font("宋体",Font.BOLD,50));
		g.drawString("张三", 200, 200);
		g.drawImage(ball, 250, 250, null);
		g.setColor(c);
		g.setFont(f);
	}
	
	/**
	 * 初始化窗口
	 */
	public void launchFrame(){
		this.setTitle("pmc_作品");//窗口标题
		this.setVisible(true);//窗口可见
		this.setSize(500,500);
		this.setLocation(0,0);
		/**
		 * 窗口关闭
		 */
		this.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					System.exit(0);
				}
		});
	}
	public static void main(String[] args){
		MyGameFrame2 f=new MyGameFrame2();
		f.launchFrame();
	}
}

辅助类

package com.nuipc.game;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
public class GameUtil {
	/**
	 * 工具类构造器私有化
	 */
	private GameUtil(){
		
	}
	/**
	 * 返回指定路径文件的图片对象
	 * @param path
	 * @return
	 */
	public static Image getImage(String path){
		BufferedImage bi=null;
		try{
			URL u=GameUtil.class.getClassLoader().getResource(path);
			bi=ImageIO.read(u);
		}catch(IOException e){
			e.printStackTrace();
		}
		return bi;
	}
}

多线程和内部类实现动画效果

package com.nuipc.game;
/**
 * 飞机游戏的主窗口
 * @author pmc
 *
 */
import javax.swing.JFrame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//import java.awt.*;
public class MyGameFrame3 extends JFrame {
	Image bg=GameUtil.getImage("images/bg.jpg");
	Image plane=GameUtil.getImage("images/plane.png");
	/**
	 * paint自动被调用,g相当于一只画笔
	 */
	int planeX=250,planetY=250;
	@Override
	public void paint(Graphics g) {
//		super.paint(g);
		/**
		 * 有层次关系,先画背景,再画飞机
		 */
		g.drawImage(bg, 0, 0, null);
		g.drawImage(plane, planeX, planetY, null);		
		planeX++;
	}
	/**
	 * 反复重画窗口
	 * @author pmc
	 *
	 */
	class PaintThread extends Thread{
		public void run(){
			while(true){
				System.out.println("重画窗口");
				repaint();
				try {
					Thread.sleep(40);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 初始化窗口
	 */
	public void launchFrame(){
		this.setTitle("pmc_作品");//窗口标题
		this.setVisible(true);//窗口可见
		this.setSize(500,500);
		this.setLocation(0,0);
		/**
		 * 窗口关闭
		 */
		this.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					System.exit(0);
				}
		});
		
		new PaintThread().start();
	}
	public static void main(String[] args){
		MyGameFrame3 f=new MyGameFrame3();
		f.launchFrame();
	}
}

解决java.awt.Frame类,闪烁现象,使用“双缓冲技术”

package com.nuipc.game;
/**
 * 飞机游戏的主窗口
 * @author pmc
 *
 */
import java.awt.Frame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//import java.awt.*;
public class MyGameFrame4 extends Frame {
	/**
	 * 使用awt.Frame会闪烁
	 * 双缓冲技术
	 * 1.在内存中创建与画布一致的缓冲区
	 * 2.在缓冲区画图
	 * 3.将缓冲区位图拷贝到当前画布上
	 * 4.释放内存缓冲区
	 * 双缓冲即在内存中创建一个与屏幕绘图区域一致的对象,先将图形绘制到内存中的这个对象上,在一次性将这个对象上的图形拷贝到屏幕上,这样能大大加快绘图的速度。
	 */
	private Image offScreenImage=null;
	public void update(Graphics g){
		if(offScreenImage==null){
			offScreenImage=this.createImage(500,500);//这是游戏窗口的宽度和高度
		}
		Graphics gOff=offScreenImage.getGraphics();
		paint(gOff);
		g.drawImage(offScreenImage, 0, 0, null);
	}
	/**
	 * 
	 */
	Image bg=GameUtil.getImage("images/bg.jpg");
	Image plane=GameUtil.getImage("images/plane.png");
	/**
	 * paint自动被调用,g相当于一只画笔
	 */
	int planeX=250,planetY=250;
	@Override
	public void paint(Graphics g) {
//		super.paint(g);
		/**
		 * 有层次关系,先画背景,再画飞机
		 */
		g.drawImage(bg, 0, 0, null);
		g.drawImage(plane, planeX, planetY, null);		
		planeX++;
	}
	/**
	 * 反复重画窗口
	 * @author pmc
	 *
	 */
	class PaintThread extends Thread{
		public void run(){
			while(true){
				System.out.println("重画窗口");
				repaint();
				try {
					Thread.sleep(40);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 初始化窗口
	 */
	public void launchFrame(){
		this.setTitle("pmc_作品");//窗口标题
		this.setVisible(true);//窗口可见
		this.setSize(500,500);
		this.setLocation(0,0);
		/**
		 * 窗口关闭
		 */
		this.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					System.exit(0);
				}
		});
		
		new PaintThread().start();
	}

	public static void main(String[] args){
		MyGameFrame4 f=new MyGameFrame4();
		f.launchFrame();
	}
}

游戏物体根类的实现

package com.nuipc.game;

import java.awt.Graphics;
import java.awt.Image;

/**
 * 物体类的实现
 * @author pmc
 *
 */
public class GameObject {
	 Image img;
	 double x,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, 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(Image img, double x, double y) {
		super();
		this.img = img;
		this.x = x;
		this.y = y;
	}
	
	public GameObject(){
	}
}

面向对象思想重构非机类设计

package com.nuipc.game;

import java.awt.Graphics;
import java.awt.Image;

public class Plane extends GameObject{
	public void drawSelf(Graphics g){
		g.drawImage(img, (int)x, (int)y, null);
		x++;
	}
	public Plane(Image img,double x,double y){
		this.img=img;
		this.x=x;
		this.y=y;
	}
}
package com.nuipc.game;

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

/**
 * 物体类的实现
 * @author pmc
 *
 */
public class GameObject {
	 Image img;
	 double x,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, 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(Image img, double x, double y) {
		super();
		this.img = img;
		this.x = x;
		this.y = y;
	}
	
	public GameObject(){
	}
	/**
	 * 返回物体所在的矩形。便于后续的碰撞检测
	 * @return
	 */
	public Rectangle getRect(){
		return new Rectangle((int)x,(int)y,width,height);
	}
}
package com.nuipc.game;
/**
 * 飞机游戏的主窗口
 * @author pmc
 *
 */
import javax.swing.JFrame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//import java.awt.*;
public class MyGameFrame5 extends JFrame {
	Image bg=GameUtil.getImage("images/bg.jpg");
	Image planeimg=GameUtil.getImage("images/plane.png");
	
	Plane plane=new Plane(planeimg,250,250);
	Plane plane2=new Plane(planeimg,250,200);
	Plane plane3=new Plane(planeimg,250,150);
	
	/**
	 * paint自动被调用,g相当于一只画笔
	 */
	@Override
	public void paint(Graphics g) {
//		super.paint(g);
		/**
		 * 有层次关系,先画背景,再画飞机
		 */
		g.drawImage(bg, 0, 0, null);
		plane.drawSelf(g);//画飞机
		plane2.drawSelf(g);//画飞机
		plane3.drawSelf(g);//画飞机
	}
	/**
	 * 反复重画窗口
	 * @author pmc
	 *
	 */
	class PaintThread extends Thread{
		public void run(){
			while(true){
				System.out.println("重画窗口");
				repaint();
				try {
					Thread.sleep(40);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 初始化窗口
	 */
	public void launchFrame(){
		this.setTitle("pmc_作品");//窗口标题
		this.setVisible(true);//窗口可见
		this.setSize(500,500);
		this.setLocation(0,0);
		/**
		 * 窗口关闭
		 */
		this.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					System.exit(0);
				}
		});
		
		new PaintThread().start();
	}
	public static void main(String[] args){
		MyGameFrame5 f=new MyGameFrame5();
		f.launchFrame();
	}
}

键盘控制原理

package com.nuipc.game;
/**
 * 飞机游戏的主窗口
 * @author pmc
 *
 */
import javax.swing.JFrame;

import java.awt.Color;
import java.awt.Font;
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.awt.*;
public class MyGameFrame6 extends JFrame {
	Image bg=GameUtil.getImage("images/bg.jpg");
	Image planeimg=GameUtil.getImage("images/plane.png");
	
	Plane plane=new Plane(planeimg,250,250);
	
	/**
	 * paint自动被调用,g相当于一只画笔
	 */
	@Override
	public void paint(Graphics g) {
//		super.paint(g);
		/**
		 * 有层次关系,先画背景,再画飞机
		 */
		g.drawImage(bg, 0, 0, null);
		plane.drawSelf(g);//画飞机
	}
	/**
	 * 反复重画窗口
	 * @author pmc
	 *
	 */
	class PaintThread extends Thread{
		public void run(){
			while(true){
//				System.out.println("重画窗口");
				repaint();
				try {
					Thread.sleep(40);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	class KeyMonitor extends KeyAdapter{

		@Override
		public void keyPressed(KeyEvent e) {
			System.out.println("按下键盘:"+e.getKeyCode());
		}

		@Override
		public void keyReleased(KeyEvent e) {
			System.out.println("释放键盘:"+e.getKeyCode());
		}
			
	}
	/**
	 * 初始化窗口
	 */
	public void launchFrame(){
		this.setTitle("pmc_作品");//窗口标题
		this.setVisible(true);//窗口可见
		this.setSize(500,500);
		this.setLocation(0,0);
		/**
		 * 窗口关闭
		 */
		this.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					System.exit(0);
				}
		});
		
		new PaintThread().start();//启动重画窗口的线程
		
		addKeyListener(new KeyMonitor());//给窗口增加键盘的监听
	
	}
	public static void main(String[] args){
		MyGameFrame6 f=new MyGameFrame6();
		f.launchFrame();
	}
}

飞机类的键盘控制代码

package com.nuipc.game;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;

public class Plane extends GameObject{
	int speed=3;
	boolean left,up,right,down;
	public void drawSelf(Graphics g){
		g.drawImage(img, (int)x, (int)y, null);
//		x++;
		if(left){
			x-=speed;
		}
		if(right){
			x+=speed;
		}
		if(up){
			y-=speed;
		}
		if(down){
			y+=speed;
		}
	}
	public Plane(Image img,double x,double y){
		this.img=img;
		this.x=x;
		this.y=y;
	}
	/**
	 * 增加方向
	 * @param e
	 */
	public void addDirection(KeyEvent e){
		switch(e.getKeyCode()){
//			case 37:
			case KeyEvent.VK_LEFT:
			{
				this.left=true;
				break;
			}
//			case 38:
			case KeyEvent.VK_UP:
			{
				this.up=true;
				break;
			}
//			case 39:
			case KeyEvent.VK_RIGHT:
			{
				this.right=true;
				break;
			}
//			case 40:
			case KeyEvent.VK_DOWN:
			{
				this.down=true;
				break;
			}
			default:
			{
				break;
			}
		}
	}
	/**
	 * 取消方向
	 * @param e
	 */
	public void minusDirection(KeyEvent e){
		switch(e.getKeyCode()){
//			case 37:
			case KeyEvent.VK_LEFT:
			{
				this.left=false;
				break;
			}
//			case 38:
			case KeyEvent.VK_UP:
			{
				this.up=false;
				break;
			}
//			case 39:
			case KeyEvent.VK_RIGHT:
			{
				this.right=false;
				break;
			}
//			case 40:
			case KeyEvent.VK_DOWN:
			{
				this.down=false;
				break;
			}
			default:
			{
				break;
			}
		}
	}
}
package com.nuipc.game;
/**
 * 飞机游戏的主窗口
 * @author pmc
 *
 */
import javax.swing.JFrame;

import java.awt.Color;
import java.awt.Font;
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.awt.*;
public class MyGameFrame7 extends JFrame {
	Image bg=GameUtil.getImage("images/bg.jpg");
	Image planeimg=GameUtil.getImage("images/plane.png");
	
	Plane plane=new Plane(planeimg,250,250);
	
	/**
	 * paint自动被调用,g相当于一只画笔
	 */
	@Override
	public void paint(Graphics g) {
//		super.paint(g);
		/**
		 * 有层次关系,先画背景,再画飞机
		 */
		g.drawImage(bg, 0, 0, null);
		plane.drawSelf(g);//画飞机
	}
	/**
	 * 反复重画窗口
	 * @author pmc
	 *
	 */
	class PaintThread extends Thread{
		public void run(){
			while(true){
//				System.out.println("重画窗口");
				repaint();
				try {
					Thread.sleep(40);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	class KeyMonitor extends KeyAdapter{

		@Override
		public void keyPressed(KeyEvent e) {
//			System.out.println("按下键盘:"+e.getKeyCode());
			plane.addDirection(e);
		}

		@Override
		public void keyReleased(KeyEvent e) {
//			System.out.println("释放键盘:"+e.getKeyCode());
			plane.minusDirection(e);
		}
			
	}
	/**
	 * 初始化窗口
	 */
	public void launchFrame(){
		this.setTitle("pmc_作品");//窗口标题
		this.setVisible(true);//窗口可见
		this.setSize(500,500);
		this.setLocation(0,0);
		/**
		 * 窗口关闭
		 */
		this.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					System.exit(0);
				}
		});
		
		new PaintThread().start();//启动重画窗口的线程
		
		addKeyListener(new KeyMonitor());//给窗口增加键盘的监听
	
	}
	public static void main(String[] args){
		MyGameFrame7 f=new MyGameFrame7();
		f.launchFrame();
	}
}

炮弹类设计

package com.nuipc.game;

public class Constant {
	public static final int GAME_WIDHT=500;
	public static final int GAME_HEIGHT=500;
}
package com.nuipc.game;

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=3;
		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(x<0||x>Constant.GAME_WIDHT-width){
			degree=Math.PI-degree;
		}
		if(y<30||y>Constant.GAME_HEIGHT-height){
			degree=-degree;
		}
		g.setColor(c);
	}
}
package com.nuipc.game;
/**
 * 飞机游戏的主窗口
 * @author pmc
 *
 */
import javax.swing.JFrame;

import java.awt.Color;
import java.awt.Font;
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.awt.*;
public class MyGameFrame8 extends JFrame {
	Image bg=GameUtil.getImage("images/bg.jpg");
	Image planeimg=GameUtil.getImage("images/plane.png");
	
	Plane plane=new Plane(planeimg,250,250);
	Shell shell=new Shell();
	/**
	 * paint自动被调用,g相当于一只画笔
	 */
	@Override
	public void paint(Graphics g) {
//		super.paint(g);
		/**
		 * 有层次关系,先画背景,再画飞机
		 */
		g.drawImage(bg, 0, 0, null);
		plane.drawSelf(g);//画飞机
		shell.draw(g);
	}
	/**
	 * 反复重画窗口
	 * @author pmc
	 *
	 */
	class PaintThread extends Thread{
		public void run(){
			while(true){
//				System.out.println("重画窗口");
				repaint();
				try {
					Thread.sleep(40);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	class KeyMonitor extends KeyAdapter{

		@Override
		public void keyPressed(KeyEvent e) {
//			System.out.println("按下键盘:"+e.getKeyCode());
			plane.addDirection(e);
		}

		@Override
		public void keyReleased(KeyEvent e) {
//			System.out.println("释放键盘:"+e.getKeyCode());
			plane.minusDirection(e);
		}
			
	}
	/**
	 * 初始化窗口
	 */
	public void launchFrame(){
		this.setTitle("pmc_作品");//窗口标题
		this.setVisible(true);//窗口可见
		this.setSize(Constant.GAME_WIDHT,Constant.GAME_HEIGHT);
		this.setLocation(0,0);
		/**
		 * 窗口关闭
		 */
		this.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					System.exit(0);
				}
		});
		
		new PaintThread().start();//启动重画窗口的线程
		
		addKeyListener(new KeyMonitor());//给窗口增加键盘的监听
	
	}
	public static void main(String[] args){
		MyGameFrame8 f=new MyGameFrame8();
		f.launchFrame();
	}
}

容器或数组产生多发炮弹

package com.nuipc.game;

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=3;
		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(x<0||x>Constant.GAME_WIDHT-width){
			degree=Math.PI-degree;
		}
		if(y<30||y>Constant.GAME_HEIGHT-height){
			degree=-degree;
		}
		g.setColor(c);
	}
}
package com.nuipc.game;
/**
 * 飞机游戏的主窗口
 * @author pmc
 *
 */
import javax.swing.JFrame;

import java.awt.Color;
import java.awt.Font;
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.awt.*;
public class MyGameFrame9 extends JFrame {
	Image bg=GameUtil.getImage("images/bg.jpg");
	Image planeimg=GameUtil.getImage("images/plane.png");
	
	Plane plane=new Plane(planeimg,250,250);
	Shell shell=new Shell();
	Shell[] shells=new Shell[50];
	/**
	 * paint自动被调用,g相当于一只画笔
	 */
	@Override
	public void paint(Graphics g) {
//		super.paint(g);
		/**
		 * 有层次关系,先画背景,再画飞机
		 */
		g.drawImage(bg, 0, 0, null);
		plane.drawSelf(g);//画飞机
//		shell.draw(g);
		for(int i=0;i<shells.length;i++){
			shells[i].draw(g);
		}
	}
	/**
	 * 反复重画窗口
	 * @author pmc
	 *
	 */
	class PaintThread extends Thread{
		public void run(){
			while(true){
//				System.out.println("重画窗口");
				repaint();
				try {
					Thread.sleep(40);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	class KeyMonitor extends KeyAdapter{

		@Override
		public void keyPressed(KeyEvent e) {
//			System.out.println("按下键盘:"+e.getKeyCode());
			plane.addDirection(e);
		}

		@Override
		public void keyReleased(KeyEvent e) {
//			System.out.println("释放键盘:"+e.getKeyCode());
			plane.minusDirection(e);
		}
			
	}
	/**
	 * 初始化窗口
	 */
	public void launchFrame(){
		this.setTitle("pmc_作品");//窗口标题
		this.setVisible(true);//窗口可见
		this.setSize(Constant.GAME_WIDHT,Constant.GAME_HEIGHT);
		this.setLocation(0,0);
		/**
		 * 窗口关闭
		 */
		this.addWindowListener(new WindowAdapter(){
				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){
		MyGameFrame9 f=new MyGameFrame9();
		f.launchFrame();
	}
}

Frame双缓冲解决闪烁

package com.nuipc.game;
/**
 * 飞机游戏的主窗口
 * @author pmc
 *
 */
import javax.swing.JFrame;

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.awt.*;
public class MyGameFrame9 extends Frame {
	Image bg=GameUtil.getImage("images/bg.jpg");
	Image planeimg=GameUtil.getImage("images/plane.png");
	
	Plane plane=new Plane(planeimg,250,250);
	Shell shell=new Shell();
	Shell[] shells=new Shell[50];
	/**
	 * paint自动被调用,g相当于一只画笔
	 */
	@Override
	public void paint(Graphics g) {
//		super.paint(g);
		/**
		 * 有层次关系,先画背景,再画飞机
		 */
		g.drawImage(bg, 0, 0, null);
		plane.drawSelf(g);//画飞机
//		shell.draw(g);
		for(int i=0;i<shells.length;i++){
			shells[i].draw(g);
		}
	}
	/**
	 * 反复重画窗口
	 * @author pmc
	 *
	 */
	class PaintThread extends Thread{
		public void run(){
			while(true){
//				System.out.println("重画窗口");
				repaint();
				try {
					Thread.sleep(40);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	class KeyMonitor extends KeyAdapter{

		@Override
		public void keyPressed(KeyEvent e) {
//			System.out.println("按下键盘:"+e.getKeyCode());
			plane.addDirection(e);
		}

		@Override
		public void keyReleased(KeyEvent e) {
//			System.out.println("释放键盘:"+e.getKeyCode());
			plane.minusDirection(e);
		}
			
	}
	/**
	 * 初始化窗口
	 */
	public void launchFrame(){
		this.setTitle("pmc_作品");//窗口标题
		this.setVisible(true);//窗口可见
		this.setSize(Constant.GAME_WIDHT,Constant.GAME_HEIGHT);
		this.setLocation(0,0);
		/**
		 * 窗口关闭
		 */
		this.addWindowListener(new WindowAdapter(){
				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){
		MyGameFrame9 f=new MyGameFrame9();
		f.launchFrame();
	}
	
	private Image offScreenImage=null;
	public void update(Graphics g){
		if(offScreenImage==null){
			offScreenImage=this.createImage(Constant.GAME_WIDHT,Constant.GAME_HEIGHT);
		}
		Graphics gOff=offScreenImage.getGraphics();
		paint(gOff);
		g.drawImage(offScreenImage, 0, 0, null);
	}
}

矩形检测原理

package com.nuipc.game;

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

/**
 * 物体类的实现
 * @author pmc
 *
 */
public class GameObject {
	 Image img;
	 double x,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, 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(Image img, double x, double y) {
		super();
		this.img = img;
		this.x = x;
		this.y = y;
	}
	
	public GameObject(){
	}
	/**
	 * 返回物体所在的矩形。便于后续的碰撞检测
	 * @return
	 */
	public Rectangle getRect(){
		return new Rectangle((int)x,(int)y,width,height);
	}
}
package com.nuipc.game;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;

public class Plane extends GameObject{
	int speed=3;
	boolean left,up,right,down;
	boolean live=true;
	public void drawSelf(Graphics g){
		if(live){
			g.drawImage(img, (int)x, (int)y, null);
//			x++;
			if(left){
				x-=speed;
			}
			if(right){
				x+=speed;
			}
			if(up){
				y-=speed;
			}
			if(down){
				y+=speed;
			}
		}else{
			System.out.println("死亡");
		}	
	}
		
	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);
	}
	/**
	 * 增加方向
	 * @param e
	 */
	public void addDirection(KeyEvent e){
		switch(e.getKeyCode()){
//			case 37:
			case KeyEvent.VK_LEFT:
			{
				this.left=true;
				break;
			}
//			case 38:
			case KeyEvent.VK_UP:
			{
				this.up=true;
				break;
			}
//			case 39:
			case KeyEvent.VK_RIGHT:
			{
				this.right=true;
				break;
			}
//			case 40:
			case KeyEvent.VK_DOWN:
			{
				this.down=true;
				break;
			}
			default:
			{
				break;
			}
		}
	}
	/**
	 * 取消方向
	 * @param e
	 */
	public void minusDirection(KeyEvent e){
		switch(e.getKeyCode()){
//			case 37:
			case KeyEvent.VK_LEFT:
			{
				this.left=false;
				break;
			}
//			case 38:
			case KeyEvent.VK_UP:
			{
				this.up=false;
				break;
			}
//			case 39:
			case KeyEvent.VK_RIGHT:
			{
				this.right=false;
				break;
			}
//			case 40:
			case KeyEvent.VK_DOWN:
			{
				this.down=false;
				break;
			}
			default:
			{
				break;
			}
		}
	}
}
package com.nuipc.game;
/**
 * 飞机游戏的主窗口
 * @author pmc
 *
 */
import javax.swing.JFrame;

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.awt.*;
public class MyGameFrame9 extends Frame {
	Image bg=GameUtil.getImage("images/bg.jpg");
	Image planeimg=GameUtil.getImage("images/plane.png");
	
	Plane plane=new Plane(planeimg,250,250);
	Shell shell=new Shell();
	Shell[] shells=new Shell[50];
	/**
	 * paint自动被调用,g相当于一只画笔
	 */
	@Override
	public void paint(Graphics g) {
//		super.paint(g);
		/**
		 * 有层次关系,先画背景,再画飞机
		 */
		g.drawImage(bg, 0, 0, null);
		plane.drawSelf(g);//画飞机
//		shell.draw(g);
		//画炮弹
		for(int i=0;i<shells.length;i++){
			shells[i].draw(g);
			boolean peng=shells[i].getRect().intersects(plane.getRect());
			if(peng){
				System.out.println("碰撞");
				plane.live=false;
			}
		}
	}
	/**
	 * 反复重画窗口
	 * @author pmc
	 *
	 */
	class PaintThread extends Thread{
		public void run(){
			while(true){
//				System.out.println("重画窗口");
				repaint();
				try {
					Thread.sleep(40);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	class KeyMonitor extends KeyAdapter{

		@Override
		public void keyPressed(KeyEvent e) {
//			System.out.println("按下键盘:"+e.getKeyCode());
			plane.addDirection(e);
		}

		@Override
		public void keyReleased(KeyEvent e) {
//			System.out.println("释放键盘:"+e.getKeyCode());
			plane.minusDirection(e);
		}
			
	}
	/**
	 * 初始化窗口
	 */
	public void launchFrame(){
		this.setTitle("pmc_作品");//窗口标题
		this.setVisible(true);//窗口可见
		this.setSize(Constant.GAME_WIDHT,Constant.GAME_HEIGHT);
		this.setLocation(0,0);
		/**
		 * 窗口关闭
		 */
		this.addWindowListener(new WindowAdapter(){
				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){
		MyGameFrame9 f=new MyGameFrame9();
		f.launchFrame();
	}
	
	private Image offScreenImage=null;
	public void update(Graphics g){
		if(offScreenImage==null){
			offScreenImage=this.createImage(Constant.GAME_WIDHT,Constant.GAME_HEIGHT);
		}
		Graphics gOff=offScreenImage.getGraphics();
		paint(gOff);
		g.drawImage(offScreenImage, 0, 0, null);
	}
}

爆炸类

package com.nuipc.game;

import java.awt.Graphics;
import java.awt.Image;

public class Explode {
	double x,y;
	static Image[] imgs=new Image[16];
	static{
		for(int i=0;i<imgs.length;i++){
			imgs[i]=GameUtil.getImage("images/explode/e"+(i+1)+".gif");
			imgs[i].getWidth(null);
		}
	}
	/**
	 * count 默认0
	 */
	int count;
	public void draw(Graphics g){
		if(count<=15){
			g.drawImage(imgs[count], (int)x, (int)y, null);
			count++;
		}
	}
	public Explode(double x,double y){
		this.x=x;
		this.y=y;
	}
	
}
package com.nuipc.game;
/**
 * 飞机游戏的主窗口
 * @author pmc
 *
 */
import javax.swing.JFrame;

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.awt.*;
public class MyGameFrame9 extends Frame {
	Image bg=GameUtil.getImage("images/bg.jpg");
	Image planeimg=GameUtil.getImage("images/plane.png");
	
	Plane plane=new Plane(planeimg,Constant.PL_X,Constant.PL_Y);
	
	Shell shell=new Shell();
	Shell[] shells=new Shell[50];
	Explode bao;
	/**
	 * paint自动被调用,g相当于一只画笔
	 */
	@Override
	public void paint(Graphics g) {
//		super.paint(g);
		/**
		 * 有层次关系,先画背景,再画飞机
		 */
		g.drawImage(bg, 0, 0, null);
		plane.drawSelf(g);//画飞机
//		shell.draw(g);
		//画炮弹
		for(int i=0;i<shells.length;i++){
			shells[i].draw(g);
			boolean peng=shells[i].getRect().intersects(plane.getRect());
			if(peng){
				System.out.println("碰撞");
				plane.live=false;
				if(bao==null){
					bao=new Explode(plane.x,plane.y);	
				}
				bao.draw(g);
			}
		}
	}
	/**
	 * 反复重画窗口
	 * @author pmc
	 *
	 */
	class PaintThread extends Thread{
		public void run(){
			while(true){
//				System.out.println("重画窗口");
				repaint();
				try {
					Thread.sleep(40);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	class KeyMonitor extends KeyAdapter{

		@Override
		public void keyPressed(KeyEvent e) {
//			System.out.println("按下键盘:"+e.getKeyCode());
			plane.addDirection(e);
		}

		@Override
		public void keyReleased(KeyEvent e) {
//			System.out.println("释放键盘:"+e.getKeyCode());
			plane.minusDirection(e);
		}
			
	}
	/**
	 * 初始化窗口
	 */
	public void launchFrame(){
		this.setTitle("pmc_作品");//窗口标题
		this.setVisible(true);//窗口可见
		this.setSize(Constant.GAME_WIDHT,Constant.GAME_HEIGHT);
		this.setLocation(0,0);
		/**
		 * 窗口关闭
		 */
		this.addWindowListener(new WindowAdapter(){
				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){
		MyGameFrame9 f=new MyGameFrame9();
		f.launchFrame();
	}
	
	private Image offScreenImage=null;
	public void update(Graphics g){
		if(offScreenImage==null){
			offScreenImage=this.createImage(Constant.GAME_WIDHT,Constant.GAME_HEIGHT);
		}
		Graphics gOff=offScreenImage.getGraphics();
		paint(gOff);
		g.drawImage(offScreenImage, 0, 0, null);
	}
}

飞机死亡和计时功能

package com.nuipc.game;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;

public class Plane extends GameObject{
	int speed=3;
	boolean left,up,right,down;
	boolean live=true;
	public void drawSelf(Graphics g){
		if(live){
			g.drawImage(img, (int)x, (int)y, null);
//			x++;
			if(left){
				x-=speed;
			}
			if(right){
				x+=speed;
			}
			if(up){
				y-=speed;
			}
			if(down){
				y+=speed;
			}
		}else{
//			System.out.println("死亡");
		}	
	}
		
	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);
	}
	/**
	 * 增加方向
	 * @param e
	 */
	public void addDirection(KeyEvent e){
		switch(e.getKeyCode()){
//			case 37:
			case KeyEvent.VK_LEFT:
			{
				this.left=true;
				break;
			}
//			case 38:
			case KeyEvent.VK_UP:
			{
				this.up=true;
				break;
			}
//			case 39:
			case KeyEvent.VK_RIGHT:
			{
				this.right=true;
				break;
			}
//			case 40:
			case KeyEvent.VK_DOWN:
			{
				this.down=true;
				break;
			}
			default:
			{
				break;
			}
		}
	}
	/**
	 * 取消方向
	 * @param e
	 */
	public void minusDirection(KeyEvent e){
		switch(e.getKeyCode()){
//			case 37:
			case KeyEvent.VK_LEFT:
			{
				this.left=false;
				break;
			}
//			case 38:
			case KeyEvent.VK_UP:
			{
				this.up=false;
				break;
			}
//			case 39:
			case KeyEvent.VK_RIGHT:
			{
				this.right=false;
				break;
			}
//			case 40:
			case KeyEvent.VK_DOWN:
			{
				this.down=false;
				break;
			}
			default:
			{
				break;
			}
		}
	}
}
package com.nuipc.game;
/**
 * 飞机游戏的主窗口
 * @author pmc
 *
 */
import javax.swing.JFrame;

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 java.awt.*;
public class MyGameFrame10 extends Frame {
	Image bg=GameUtil.getImage("images/bg.jpg");
	Image planeimg=GameUtil.getImage("images/plane.png");
	
	Plane plane=new Plane(planeimg,Constant.PL_X,Constant.PL_Y);
	Shell shell=new Shell();
	Shell[] shells=new Shell[50];
	Explode bao;
	Date starTime=new Date();//游戏开始时间
	Date endTime;//游戏结束时间
	int gameTime;//游戏时间计算
	/**
	 * paint自动被调用,g相当于一只画笔
	 */
	@Override
	public void paint(Graphics g) {
//		super.paint(g);
		Color c=g.getColor();
		/**
		 * 有层次关系,先画背景,再画飞机
		 */
		g.drawImage(bg, 0, 0, null);
		plane.drawSelf(g);//画飞机
//		shell.draw(g);
		//画炮弹
		for(int i=0;i<shells.length;i++){
			shells[i].draw(g);
			boolean peng=shells[i].getRect().intersects(plane.getRect());
			if(peng){
//				System.out.println("碰撞");
				plane.live=false;
				if(bao==null){
					bao=new Explode(plane.x,plane.y);	
					endTime=new Date();
					gameTime=((int)endTime.getTime()-(int)starTime.getTime())/1000;
				}
				bao.draw(g);
			}
			if(!plane.live){
				g.setColor(Color.RED);
				Font f=new Font("宋体",Font.BOLD,50);
				g.setFont(f);
				g.drawString("游戏时间:"+gameTime+"秒", (int)plane.x, (int)plane.y);
			}
			
		}
		g.setColor(c);
	}
	/**
	 * 反复重画窗口
	 * @author pmc
	 *
	 */
	class PaintThread extends Thread{
		public void run(){
			while(true){
//				System.out.println("重画窗口");
				repaint();
				try {
					Thread.sleep(40);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	class KeyMonitor extends KeyAdapter{

		@Override
		public void keyPressed(KeyEvent e) {
//			System.out.println("按下键盘:"+e.getKeyCode());
			plane.addDirection(e);
		}

		@Override
		public void keyReleased(KeyEvent e) {
//			System.out.println("释放键盘:"+e.getKeyCode());
			plane.minusDirection(e);
		}
			
	}
	/**
	 * 初始化窗口
	 */
	public void launchFrame(){
		this.setTitle("pmc_作品");//窗口标题
		this.setVisible(true);//窗口可见
		this.setSize(Constant.GAME_WIDHT,Constant.GAME_HEIGHT);
		this.setLocation(0,0);
		/**
		 * 窗口关闭
		 */
		this.addWindowListener(new WindowAdapter(){
				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){
		MyGameFrame10 f=new MyGameFrame10();
		f.launchFrame();
	}
	
	private Image offScreenImage=null;
	public void update(Graphics g){
		if(offScreenImage==null){
			offScreenImage=this.createImage(Constant.GAME_WIDHT,Constant.GAME_HEIGHT);
		}
		Graphics gOff=offScreenImage.getGraphics();
		paint(gOff);
		g.drawImage(offScreenImage, 0, 0, null);
	}
}

 

转载自原文链接, 如需删除请联系管理员。

原文链接:Java_85_开发体验_飞机游戏,转载请注明来源!

0