Java小遊戲項目(一):飛機與子彈的實現

飛機大戰項目

剛學Java時用Frame寫的一個飛機子彈的小遊戲項目,難度不大,學了frame的小夥伴可以試試手
核心包:

package
背景和飛機
在這裏插入圖片描述
backgroundplane
遊戲窗口的實現,包含整個窗體的監聽

package GamePlane;

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 javax.print.attribute.standard.SheetCollate;


public class MyGameFrame extends Frame{
	
	Image planeImg = GameUtil.getImage("Image/plane.png");
	Image bg = GameUtil.getImage("Image/bg.jpg");
	Plane plane = new Plane(planeImg, 250, Constant.GAME_HEIGHT-150);
	Shell[] shell = new Shell[20];
	Explode bao ;
	@Override
	public void paint(Graphics g) {
		g.drawImage(bg, 0, 0, null);
		plane.drawSelf(g);
		
		for(int i = 0;i<shell.length;i++)
		{
			Color   c =  g.getColor();
			shell[i].drawSelf(g);
			boolean boom = shell[i].getRect().intersects(plane.getRect());
			for(int j=0;j<plane.all.size();j++) {
			boolean coll = shell[i].getRect().intersects(plane.all.get(j).getRect());
					if(coll)
					{
						shell[i].live = false;
						plane.all.get(j).islive = false;
					}
			}
			if(boom) {
				plane.live = false;
				for(i = 0;i<shell.length;i++) {
				shell[i].live = false;
				}
				if(bao == null)
				{
					bao = new Explode(plane.x, plane.y);
				}
				bao.draw(g);
			}
			if(!plane.live)
			{
				g.setColor(Color.red);
				Font f = new Font("宋體", Font.BOLD, 50);
				g.setFont(f);
				g.drawString("你輸了哈哈", (int)plane.x,(int) plane.y);
			}
			g.setColor(c);
		}
		
	}
	class PaintThread extends Thread
	{
		@Override
		public void run() {
			while(true)
			{
				repaint();
				try {
					Thread.sleep(40);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
	}
	class   KeyMonitor extends  KeyAdapter  {

		@Override
		public void keyPressed(KeyEvent e) {
			plane.addDection(e);
		}

		@Override
		public void keyReleased(KeyEvent e) {
			plane.minusDection(e);
		}
		
		
	}
	public void lunchFrame() {
		this.setTitle("hello!This is the first Game");
		this.setVisible(true);
		this.setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);
		this.setLocation(100, 100);
		
		
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		new PaintThread().start();	//啓動重畫窗口的線程
		addKeyListener(new KeyMonitor());   //給窗口增加鍵盤的監聽
		for(int i =0;i<shell.length;i++)
		{
			shell[i] = new Shell();
		}
	}
	public static void main(String[] args) {
		MyGameFrame f = new MyGameFrame();
		f.lunchFrame();
	}
	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);
	}
}


飛機的實現(飛機可以發射子彈)
package GamePlane;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;


public class Plane extends GameObject{
	boolean left,right,up,down,space;
	boolean live;
	List<Bullet> all = new ArrayList<Bullet>();
	public Plane (Image img,double x,double y)
	{
		live = true;
		this.image = img;
		this.x = x;
		this.y = y;
		speed = 10;
		this.width = img.getWidth(null);
		this.height = img.getHeight(null);
	}
	@Override
	public void drawSelf(Graphics g) {
		if(live)
		{
			g.drawImage(image, (int)x,(int) y, null);
			oper();
			for(int i = 0;i<all.size();i++)
			{
				new Thread(all.get(i)).start();
				all.get(i).draw(g);
			}
		}
	}
	public void oper() {
		if(left){
			if(x>10)
			{
				x-=speed;
			}
			
		}
		else if (down) {
			if(y<Constant.GAME_HEIGHT-50)
			{
				y+=speed;
			}
		}
		else if (right) {
			if(x<Constant.GAME_WIDTH-30)
			{x+=speed;}
		}
		else if (up) {
			if(y>30) 
			{y-=speed;}
		}
	}

	public void addDection(KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_LEFT:
			left = true;	
			break ;
		case KeyEvent.VK_RIGHT:
			right = true;
			break;
		case KeyEvent.VK_DOWN:
			down = true;
			break;
		case KeyEvent.VK_UP:
			up = true;
			break;
		case KeyEvent.VK_SPACE:
			all.add(new Bullet(x, y));
			break;
		}
	}
	public void minusDection(KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_LEFT:
			left = false;	
			break ;
		case KeyEvent.VK_RIGHT:
			right = false;
			break;
		case KeyEvent.VK_DOWN:
			down = false;
			break;
		case KeyEvent.VK_UP:
			up = false;
			break;
		}
	}
}

子彈的發射
package GamePlane;

import java.awt.Color;
import java.awt.Graphics;


public class Bullet extends GameObject implements Runnable {
	boolean islive;
	public Bullet(double x,double y)
	{
		this.x = x;
		this.y = y;
		speed = 3;
		islive = true;
	}
	public void run() {
		while(true) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public void draw(Graphics g) {
		if(islive) {
		g.setColor(Color.BLUE);
		y-=speed;
		g.fillOval((int)x, (int)y,20, 20);
		}
	}
}

障礙時通過繪製圓形實現
package GamePlane;

import java.awt.Color;
import java.awt.Graphics;

public class Shell extends GameObject{
	double degree;
	boolean live;
	public Shell()
	{
		x = 200;
		y = 200;
		width = 20;
		height = 20;
		speed = 3;
		degree = Math.random()*Math.PI*2;
		live = true;
	}
	@Override
	public void drawSelf(Graphics g) {
		if (live) {
		Color c = g.getColor();
		g.setColor(Color.YELLOW);
		g.fillOval((int)x, (int)y, width, height);
		x += Math.cos(degree)*speed;
		y += Math.sin(degree)*speed;
		if(x<0||x>Constant.GAME_WIDTH-width)
		{
			degree = Math.PI-degree;
		}
		if(y<30||y>Constant.GAME_HEIGHT-height)
		{
			degree = -degree;
		}
		g.setColor(c);
		}
	}

}

最後是爆炸效果的實現

package GamePlane;

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<16;i++)
		{
			imgs[i]=GameUtil.getImage("explode/explode/e" + (i + 1) + ".gif");
			imgs[i].getWidth(null);
		}
	}
	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;
	}
}

接下來上游戲效果圖
game
如果有興趣的小夥伴,也可以親手寫個小遊戲試試,下期更新貪喫蛇的Java項目啦,初學Java的小夥伴,可以關注我哦!下面貼上源碼:
源碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章