Java实现的一个打飞机的小游戏详解编程语言

Bullet.java       

package GameSubstance; 
 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.Toolkit; 
 
import Tools.GameBox; 
import View.StartFrame; 
 
public class Bullet{ 
	public static final int BULLET_SPEED=15; 
	private int x,y; 
	private boolean live=true; 
	Image bullet=GameBox.bullet; 
	public Bullet(int x,int y){ 
		this.x=x; 
		this.y=y; 
	} 
	public void draw(Graphics g){ 
		if(!live){ 
			return; 
		} 
		g.drawImage(bullet,x,y,GameBox.BULLET_WIDTH,GameBox.BULLET_HEIGHT,null); 
		y=y-BULLET_SPEED; 
		if(y<-30){ 
			live=false; 
		} 
	} 
	public boolean isLive() { 
		return live; 
	} 
	public void setLive(boolean live) { 
		this.live = live; 
	} 
	public int getX() { 
		return x; 
	} 
	public int getY() { 
		return y; 
	} 
} 

EnemyBullet.java        

package GameSubstance; 
 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.Toolkit; 
import Tools.GameBox; 
public class EnemyBullet { 
	private int x,y; 
	private int bulletspeed=15; 
	private boolean live=true; 
 
	Image enemyplanebullet=GameBox.enemybullet; 
	 
	public EnemyBullet(int x,int y){ 
		this.x=x; 
		this.y=y; 
	} 
	public void draw(Graphics g){ 
		if(!live){ 
			return; 
		} 
		y=y+bulletspeed; 
		g.drawImage(enemyplanebullet,x,y,GameBox.ENEMYBULLET_WIDTH,GameBox.ENEMYBULLET_HEIGHT,null); 
		if(y>GameBox.GAME_HEIGHT+GameBox.ENEMYBULLET_HEIGHT){ 
			live=false; 
		} 
	} 
	public boolean isLive() { 
		return live; 
	} 
	public void setLive(boolean live) { 
		this.live = live; 
	} 
	public int getX() { 
		return x; 
	} 
	public int getY() { 
		return y; 
	} 
	 
} 

 EnemyPlane.java       

package GameSubstance; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.Toolkit; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 
 
import Tools.GameBox; 
import View.StartFrame; 
 
public class EnemyPlane { 
	StartFrame sf; 
	private int xpeed=8; 
	private int yspeed=5; 
	private boolean live=true; 
	private boolean fired=true; 
	private int x,y=0; 
	private static int i=0; 
	private String dir; 
	Image enemy; 
	Random rand=new Random(); 
	public EnemyPlane(StartFrame sf){ 
		this.sf=sf; 
		if(i==0){ 
			enemy=GameBox.enemy1; 
			i=1; 
		}else{ 
			enemy=GameBox.enemy2; 
			i=0; 
		} 
		x=rand.nextInt(GameBox.GAME_WIDTH); 
		if(x%2==0){ 
			dir="left"; 
		}else{ 
			dir="right"; 
		} 
		if(x<0){ 
			x=0; 
		} 
		if(x+GameBox.ENEMY_WIDTH>GameBox.GAME_WIDTH){ 
			x=GameBox.GAME_WIDTH-GameBox.ENEMY_WIDTH; 
		} 
	} 
	public void draw(Graphics g){ 
		 
		if(live){ 
			g.drawImage(enemy,x,y,GameBox.ENEMY_WIDTH,GameBox.ENEMY_HEIGHT,null); 
			move(); 
		} 
		 
		 
	} 
	private void move(){ 
		y=y+xpeed; 
		int i=rand.nextInt(5)+2; 
		if(dir=="left"){ 
			x=x-i; 
		}else{ 
			x=x+i; 
		} 
		if(y>GameBox.GAME_HEIGHT+GameBox.ENEMY_HEIGHT){ 
			live=false; 
		} 
		if(x<0){ 
			x=0; 
			dir="right"; 
		} 
		if(x+GameBox.ENEMY_WIDTH>GameBox.GAME_WIDTH){ 
			x=GameBox.GAME_WIDTH-GameBox.ENEMY_WIDTH; 
			dir="left"; 
		} 
		 
	} 
	public int getX() { 
		return x; 
	} 
	public int getY() { 
		return y; 
	} 
	public boolean isLive() { 
		return live; 
	} 
	public void setLive(boolean live) { 
		this.live = live; 
	} 
	public int getXpeed() { 
		return xpeed; 
	} 
	public void setXpeed(int xpeed) { 
		this.xpeed = xpeed; 
	} 
	public int getYspeed() { 
		return yspeed; 
	} 
	public void setYspeed(int yspeed) { 
		this.yspeed = yspeed; 
	} 
	public boolean isFired() { 
		return fired; 
	} 
	public void setFired(boolean fired) { 
		this.fired = fired; 
	} 
} 

 Explode.java ~ 724B  

package GameSubstance; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.Toolkit; 
import Tools.GameBox; 
public class Explode { 
	private int x,y; 
	private boolean live=true; 
	private int count; 
	Image Explode1=GameBox.Explode1; 
	public Explode(int x,int y){ 
		this.x=x; 
		this.y=y; 
	} 
	public void draw(Graphics g){ 
		if(!live){ 
			return; 
		} 
		g.drawImage(Explode1,x,y,GameBox.EXPLODE1_WIDTH,GameBox.EXPLODE1_HEIGHT,null); 
		if(count==8){ 
			live=false; 
		} 
	} 
	public boolean isLive() { 
		return live; 
	} 
	public void setLive(boolean live) { 
		this.live = live; 
	} 
	public int getCount() { 
		return count; 
	} 
	public void setCount(int count) { 
		this.count = count; 
	} 
} 

[文件] MyPlane.java ~ 2KB         

package GameSubstance; 
import Tools.GameBox; 
import View.StartFrame; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.Toolkit; 
import java.awt.event.KeyEvent; 
import javax.swing.ImageIcon; 
public class MyPlane { 
	private int x,y; 
	private static Image plane; 
	private Bullet  bullet; 
	private boolean dl=false,du=false,dr=false,dd=false; 
	public static final int MYPLANE_XSPEED=10; 
	public static final int MYPLANE_YSPEED=10; 
	private boolean live=true; 
	public MyPlane(int x,int y){ 
		this.x=x; 
		this.y=y; 
	} 
	public void draw(Graphics g){ 
		if(!live){ 
			return; 
		} 
		g.drawImage(GameBox.plane,x,y,GameBox.PLANE_WIDTH,GameBox.PLANE_HEIGHT,null); 
		move(); 
	} 
	 
	public void keyPressed(KeyEvent e) { 
		int key=e.getKeyCode(); 
		switch(key){ 
		case KeyEvent.VK_UP: 
			du=true; 
			break; 
		case KeyEvent.VK_DOWN: 
			dd=true; 
			break; 
		case KeyEvent.VK_LEFT: 
			dl=true; 
			break; 
		case KeyEvent.VK_RIGHT: 
			dr=true; 
			break; 
		} 
	} 
	public void keyReleased(KeyEvent e) { 
		int key=e.getKeyCode(); 
		switch(key){ 
		case KeyEvent.VK_UP: 
			du=false; 
			break; 
		case KeyEvent.VK_DOWN: 
			dd=false; 
			break; 
		case KeyEvent.VK_LEFT: 
			dl=false; 
			break; 
		case KeyEvent.VK_RIGHT: 
			dr=false; 
		} 
	} 
	private void move(){ 
		if(du&&!dl&&!dd&&!dr){ 
			y=y-MYPLANE_YSPEED;//上 
		}else if(dd&&!du&&!dl&&!dr){ 
			y=y+MYPLANE_YSPEED;//下 
		}else if(!dd&&!du&&dl&&!dr){ 
			x=x-MYPLANE_XSPEED;//左 
		}else if(!dd&&!du&&!dl&&dr){ 
			x=x+MYPLANE_XSPEED;//右 
		}else if(!dd&&du&&dl&&!dr){ 
			y=y-MYPLANE_YSPEED;//左上 
			x=x-MYPLANE_XSPEED; 
		}else if(du&&!dl&&!dd&&dr){ 
			y=y-MYPLANE_YSPEED;//右上 
			x=x+MYPLANE_XSPEED; 
		}else if(dd&&!du&&dl&&!dr){ 
			x=x-MYPLANE_XSPEED;//左下 
			y=y+MYPLANE_YSPEED; 
		}else if(dd&&!du&&!dl&&dr){ 
			x=x+MYPLANE_XSPEED;//右下 
			y=y+MYPLANE_YSPEED; 
		} 
		if(x+GameBox.PLANE_WIDTH>GameBox.GAME_WIDTH){ 
			x=GameBox.GAME_WIDTH-GameBox.PLANE_WIDTH; 
		} 
		if(x<0){ 
			x=0; 
		} 
		if(y+GameBox.PLANE_HEIGHT*2>GameBox.GAME_HEIGHT){ 
			y=GameBox.GAME_HEIGHT-GameBox.PLANE_HEIGHT*2; 
		} 
		if(y<30){ 
			y=30; 
		} 
	} 
	public int getX() { 
		return x; 
	} 
	public int getY() { 
		return y; 
	} 
	public boolean isLive() { 
		return live; 
	} 
	public void setLive(boolean live) { 
		this.live = live; 
	} 
} 

 GameBox.java  

package Tools; 
import java.awt.Font; 
import java.awt.Image; 
import java.awt.Toolkit; 
import java.util.ArrayList; 
import java.util.List; 
 
import GameSubstance.EnemyBullet; 
import GameSubstance.Explode; 
public class GameBox { 
	public static int GAME_WIDTH=800; 
	public static int GAME_HEIGHT=600; 
	public static Font f1=new Font("ºÚÌå",3,20); 
	public static Toolkit tk = Toolkit.getDefaultToolkit(); 
	public static Image backgroundimage=tk.createImage("images//±³¾°.jpg"); 
	public static Image plane=tk.createImage("images//ÎÒ·½·É»ú.gif"); 
	public static int PLANE_WIDTH=30; 
	public static int PLANE_HEIGHT=30; 
	public static Image Explode1=tk.createImage("images/±¬Õ¨.gif"); 
	public static int EXPLODE1_WIDTH=30; 
	public static int EXPLODE1_HEIGHT=30; 
	public static Image bullet=tk.createImage("images//×Óµ¯.gif"); 
	public static int BULLET_WIDTH=18; 
	public static int BULLET_HEIGHT=35; 
	public static Image enemy1=tk.createImage("images//µÐ»ú1.gif"); 
	public static Image enemy2=tk.createImage("images//µÐ»ú2.gif"); 
	public static int ENEMY_WIDTH=30; 
	public static int ENEMY_HEIGHT=30; 
	public static Image enemybullet=tk.createImage("images//·ßÅ­µÄ×Óµ¯.gif"); 
	public static int ENEMYBULLET_WIDTH=15; 
	public static int ENEMYBULLET_HEIGHT=30; 
} 

 StartFrame.java  

package View; 
import GameSubstance.*; 
import Tools.*; 
import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.Toolkit; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 
import java.awt.image.ImageObserver; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 
 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
public class StartFrame extends JFrame implements Runnable{ 
	//游戏组件 
	Image bk=GameBox.backgroundimage; 
	MyPlane myplane; 
	List<EnemyPlane> enemyplanelist=new ArrayList<EnemyPlane>(); 
	private int count=0; 
	private float sourse=0; 
	List<Bullet> bulletlist=new ArrayList<Bullet>(); 
	List<Explode> explodelist=new ArrayList<Explode>(); 
	List<EnemyBullet> enemybulletlist=new ArrayList<EnemyBullet>(); 
	JPanel jp; 
	public static void main(String[] args) { 
		new Thread(new StartFrame()).start(); 
 
	} 
	public StartFrame(){ 
		this.setSize(GameBox.GAME_WIDTH,GameBox.GAME_HEIGHT); 
		//窗口居中 
		int width=Toolkit.getDefaultToolkit().getScreenSize().width; 
		int height=Toolkit.getDefaultToolkit().getScreenSize().height; 
		setLocation(width/2-GameBox.GAME_WIDTH/2, height/2-GameBox.GAME_HEIGHT/2); 
		setResizable(false); 
		myplane=new MyPlane(GameBox.GAME_WIDTH/2,GameBox.GAME_HEIGHT-GameBox.PLANE_HEIGHT*2); 
		new Thread(new AddEnemy()).start(); 
		this.addKeyListener(new KeyMonitor()); 
		this.add(new GameJPanel()); 
		setTitle("打飞机小游戏"); 
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE); 
		setVisible(true); 
	} 
 
	@Override 
	public void run() { 
		while(true){ 
			repaint(); 
			Random rand=new Random(); 
			try { 
				Thread.sleep(50); 
			} catch (InterruptedException e) { 
				e.printStackTrace(); 
			} 
		} 
	} 
 
	private class KeyMonitor extends KeyAdapter { 
 
		public void keyReleased(KeyEvent e) { 
			if(e.getKeyCode()==KeyEvent.VK_F2){ 
				myplane.setLive(true); 
			} 
			myplane.keyReleased(e); 
		} 
 
		public void keyPressed(KeyEvent e) { 
			myplane.keyPressed(e); 
		} 
 
	} 
 
	private class AddEnemy implements Runnable{ 
		@Override 
		public void run() { 
			while(true){ 
				try { 
					Thread.sleep(500); 
					addEnemyPlane(); 
				} catch (InterruptedException e) { 
					e.printStackTrace(); 
				} 
			} 
		} 
 
		private void addEnemyPlane(){ 
			EnemyPlane enemyplane=new EnemyPlane(StartFrame.this); 
			enemybulletlist.add(new EnemyBullet(enemyplane.getX(),enemyplane.getY())); 
			enemyplanelist.add(enemyplane); 
		} 
	} 
 
 
	private class GameJPanel extends JPanel{ 
 
		public void paint(Graphics g){ 
			super.paint(g); 
			g.drawImage(bk,0,0,GameBox.GAME_WIDTH,GameBox.GAME_HEIGHT,null); 
			myplane.draw(g); 
			if (count==0){ 
				if(myplane.isLive()){ 
					createBullet(); 
				}else{ 
					for(int i=0;i<bulletlist.size();i++){ 
						bulletlist.remove(i); 
					} 
				} 
			} 
			count++; 
			if(count==2){ 
				count=0; 
			} 
			for(int i=0;i<bulletlist.size();i++){ 
				 
				Bullet bullet=bulletlist.get(i); 
				bullet.draw(g); 
				clearBullet(); 
			} 
			for(int i=0;i<enemyplanelist.size();i++){ 
				EnemyPlane enemyplane=enemyplanelist.get(i); 
				enemyplane.draw(g); 
				clearEnemyplane(); 
				Random rand=new Random(); 
				if(rand.nextInt(100)>93&&enemyplane.isLive()){ 
					enemybulletlist.add(new EnemyBullet(enemyplane.getX(),enemyplane.getY())); 
				} 
				//如果敌机面对我方飞机时发射一枚子弹 
				if(enemyplane.getX()>=myplane.getX()&&enemyplane.getX()<=myplane.getX()+GameBox.PLANE_WIDTH&&enemyplane.isFired()&&enemyplane.isLive()){ 
					enemybulletlist.add(new EnemyBullet(enemyplane.getX(),enemyplane.getY())); 
					enemyplane.setFired(false); 
				} 
				 
			} 
			for(int i=0;i<enemybulletlist.size();i++){ 
				EnemyBullet eb=enemybulletlist.get(i); 
				eb.draw(g); 
				clearEnemyBullet(); 
			} 
			 
			hitPlane();//攻击敌机 
			impactMyplane();//敌机与我方飞机发生碰撞 
			if(enemybulletlist.size()>=1)hitMyplane();// 
			for(int i=0;i<explodelist.size();i++){ 
				Explode exp=explodelist.get(i); 
				exp.draw(g); 
				exp.setCount(exp.getCount()+1); 
				clearExplode(); 
			} 
			g.setColor(Color.RED); 
		//	g.setFont(GameBox.f1);//设置字体之后加载窗口会卡2秒钟 
			if(sourse!=0){ 
				g.drawString("您的得分:"+(int)sourse+"0", 50, 50); 
			}else{ 
				g.drawString("您的得分:0", 50, 50); 
			} 
 
		} 
		 
		private void clearEnemyBullet() { 
			for(int i=0;i<enemybulletlist.size();i++){ 
				if(!(enemybulletlist.get(i).isLive())){ 
					enemybulletlist.remove(i); 
				} 
			} 
		} 
		private void createBullet(){ 
			Bullet bullet=new Bullet((myplane.getX()+5),(myplane.getY()-30)); 
			bulletlist.add(bullet); 
		} 
		private void clearBullet(){ 
			for(int i=0;i<bulletlist.size();i++){ 
				if(!(bulletlist.get(i).isLive())){ 
					bulletlist.remove(i); 
				} 
			} 
		} 
		private void clearEnemyplane() { 
			for(int i=0;i<enemyplanelist.size();i++){ 
				if(!(enemyplanelist.get(i).isLive())){ 
					enemyplanelist.remove(i); 
				} 
			} 
		} 
		private void clearExplode() { 
			for(int i=0;i<explodelist.size();i++){ 
				if(!(explodelist.get(i).isLive())){ 
					explodelist.remove(i); 
				} 
			} 
		} 
		private void hitPlane(){ 
			int bx,ex,by,ey; 
			for(int i=0;i<bulletlist.size();i++){ 
				Bullet bullet=bulletlist.get(i); 
				bx=bullet.getX(); 
				by=bullet.getY(); 
				for(int j=0;j<enemyplanelist.size();j++){ 
					EnemyPlane enemyplane=enemyplanelist.get(j); 
					ex=enemyplane.getX(); 
					ey=enemyplane.getY(); 
					if(bx>=ex&&bx<=ex+GameBox.ENEMY_WIDTH&&by>=ey&&by<=ey+GameBox.ENEMY_HEIGHT&&enemyplane.isLive()&&bullet.isLive()){ 
						bullet.setLive(false); 
						enemyplane.setLive(false); 
						explodelist.add(new Explode(bx,by)); 
						sourse=sourse+1.168320194f;//防止CE恶意修改分数(我的QQ号码) 
					} 
				} 
			} 
		} 
		private void impactMyplane(){ 
			int mx,my,ex,ey; 
			mx=myplane.getX(); 
			my=myplane.getY(); 
			for(int i=0;i<enemyplanelist.size();i++){ 
				EnemyPlane enemyplane=enemyplanelist.get(i); 
				ex=enemyplane.getX(); 
				ey=enemyplane.getY(); 
				if(mx>=ex&&mx<=ex+GameBox.ENEMY_WIDTH&&my>=ey&&my<=ey+GameBox.ENEMY_HEIGHT&&myplane.isLive()&&enemyplane.isLive()){ 
					myplane.setLive(false); 
					enemyplane.setLive(false); 
					explodelist.add(new Explode(mx,my)); 
					explodelist.add(new Explode(ex,ey)); 
				} 
			} 
		} 
		private void hitMyplane() { 
			int mx,my,ex,ey; 
			mx=myplane.getX(); 
			my=myplane.getY(); 
			for(int i=0;i<enemyplanelist.size();i++){ 
				EnemyBullet eb=enemybulletlist.get(i); 
				ex=eb.getX(); 
				ey=eb.getY(); 
				if(ex+GameBox.ENEMYBULLET_WIDTH>=mx&&ex+GameBox.ENEMYBULLET_WIDTH<=mx+GameBox.PLANE_WIDTH&&ey+GameBox.ENEMYBULLET_HEIGHT>=my&&ey+GameBox.ENEMYBULLET_HEIGHT<=my+GameBox.PLANE_HEIGHT&&myplane.isLive()&&eb.isLive()){ 
					myplane.setLive(false); 
					eb.setLive(false); 
					explodelist.add(new Explode(mx,my)); 
				} 
			} 
		} 
	} 
} 
 

爆炸.gif    

Java实现的一个打飞机的小游戏详解编程语言

背景.jpg    

Java实现的一个打飞机的小游戏详解编程语言

[图片] 敌机1.gif    

Java实现的一个打飞机的小游戏详解编程语言

[图片] 敌机2.gif    

Java实现的一个打飞机的小游戏详解编程语言

[图片] 愤怒的子弹.gif    

Java实现的一个打飞机的小游戏详解编程语言

[图片] 子弹.gif    

Java实现的一个打飞机的小游戏详解编程语言

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10126.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论