⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 start.java

📁 用java实现的射击小游戏
💻 JAVA
字号:
/*
 * Designed by Sweet
 * This is a small game that like the Family Computer(FC),the name
 * is Bee.
 * This program is just for studing.The who is intrested in this can
 * reprogram it.
 * 
 * 
 * 
*/
package sweet.game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Start extends JPanel implements KeyListener {
   /**
	 * 
	 */
	
/**
	 * 
	 */
	private static final long serialVersionUID = -7405396420983281957L;
	//准备加载图片
	private BufferedImage planeimg;
	private BufferedImage enemyimg;
	private BufferedImage bgimg;
	
	//	飞机初始位置
	
	private int planex=100;
	private int planey=300;
	//飞机速度
	private int speed=10;
	//子弹速度
	private int bullet=5;//子弹的速度
	private int bulletx=-10;
	private int bullety=305;
	//蜜蜂初始位置
	private int[] beex=new int[30];
	private int[] beey=new int[30];
	private int[] beespeed=new int[30];//每个小蜜蜂的移动速度
	private int all=30;//统计小蜜蜂个数
	private int[] bee=new int[30];//判断小蜜蜂是否死亡,不是为0,是为1
	//小蜜蜂的子弹
	private int[] beebulletx=new int[30];
	private int[] beebullety=new int[30];
	private int beebullet=1;//蜜蜂子弹速度 这个不要改动 否则失去游戏乐趣(飞机很难被打中)

    //	任务调用(多线程)
	Timer ti=new Timer();
	

	
	
	public Start() {
	
		super();
		try{
			//加载游戏图片
			 planeimg=ImageIO.read(new File("1942.png"));
			 enemyimg=ImageIO.read(new File("beegame.png"));
		     bgimg=ImageIO.read(new File("space.png"));
			
		}catch(Exception e){
			
		}
		setBackground(Color.BLACK);
		//蜜蜂移动
		BeeMove bm=new BeeMove();
		ti.schedule(bm, 0, 25);
		//碰撞判断
	//	Shot_Die sd=new Shot_Die();
	//	ti.schedule(sd, 0, 33);
		BeeShot bs=new BeeShot();
		ti.schedule(bs, 0, 300);
		BeeBulletMove bbm=new BeeBulletMove();
		ti.schedule(bbm, 0, 15);
	//	BeeShot_Die bsd=new BeeShot_Die();
	//	ti.schedule(bsd, 0, 22);
		Random rand=new Random();
		 for(int i=0;i<30;i++){
				bee[i]=0;
				beex[i]=40+rand.nextInt(421);
				beey[i]=30+rand.nextInt(171);
				beespeed[i]=1+rand.nextInt(3);
				beebulletx[i]=-10;
				beebullety[i]=beey[i]+9;

				}
			
		
	}
	//绘制游戏背景
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		g.drawImage(bgimg, 0, 0, null);
	}
	//游戏初始绘图
	public void paint(Graphics g){
		try{

		super.paint(g);
		//g.drawImage(bgimg, 0, 0, null);
		g.drawImage(planeimg, planex,planey, null);
		
		for(int i=0;i<30;i++){
			if(bee[i]==1){
				continue;
			}
				g.drawImage(enemyimg, beex[i], beey[i], null);
				g.setColor(Color.WHITE);
				g.drawLine(beebulletx[i], beebullety[i], beebulletx[i], beebullety[i]+10);
			
			}
		g.setColor(Color.WHITE);
		g.drawLine(bulletx, bullety, bulletx, bullety+10);
		
		}catch(Exception e){
			e.printStackTrace();
			
		}
		
	}
	//按键的判断
	public void keyPressed(KeyEvent e){
		if(planex>=20){
		if(e.getKeyCode()==KeyEvent.VK_LEFT){
			
			planex=planex-speed;
			repaint();
			
		}
		}
		if(planex<=460){
		if(e.getKeyCode()==KeyEvent.VK_RIGHT){
			
			planex=planex+speed;
			repaint();
			
		}
		}
		
		//射击键
		if(e.getKeyCode()==KeyEvent.VK_SPACE){
			if(bulletx==-10){//判断子弹位置,确保一次发出一颗子弹
			bulletx=planex+14;//固定子弹位置为飞机中间
			Bullet bu=new Bullet();
			ti.schedule(bu, 0, 18);
			}
		}
	}
	public void keyTyped(KeyEvent e){}
	public void keyReleased(KeyEvent e){}
     //子弹
	class Bullet extends TimerTask{
		
		public void run(){
			
			bullety=bullety-bullet;
			repaint();
			//子弹到屏幕之外后取消,子弹回到初始位置
			if(bullety<=(-11)){
				bullety=305;
				bulletx=-10;
				cancel();
				
				
			}
			
			
		}
	}
	//小蜜蜂的移动
	class BeeMove extends TimerTask{
		public void run(){
			for(int i=0;i<30;i++){
				if(bee[i]==0){
			if(beex[i]>=480){//判断蜜蜂移动方向
				beespeed[i]=-(beespeed[i]);
			}else if(beex[i]<=30){
				beespeed[i]=-(beespeed[i]);
			}
			
			beex[i]=beex[i]+beespeed[i];
			repaint();
			////////////BeeBulletMove
		/*    beebullety[i]=beebullety[i]+beebullet;
			repaint();
			if(beebullety[i]>=400){
				beebullety[i]=beey[i];
				beebulletx[i]=-10;
				//cancel();
			}*/
			
			//////////BeeShot
			
			/*Random rand=new Random();
			if(rand.nextInt(101)<5){//小蜜蜂的发弹率,不过好像没什么作用?
				if(beebulletx[i]==-10){
					beebulletx[i]=beex[i]+15;
					//BeeBulletMove bbm=new BeeBulletMove();
					//ti.schedule(bbm, 0, 33);
				}
			}*/
			///////////////Shot_Die
			
		if((bullety>=beey[i])&&(bullety<=(beey[i]+17))&&(bulletx>=beex[i])&&(bulletx<=beex[i]+25)){
			all--;
			bee[i]=1;
			bullety=-15;
			bulletx=-10;
			repaint();
			}
		///////////////////BeeShot_Die
		
		if((beebullety[i]==planey)&&(beebulletx[i]>=planex)&&(beebulletx[i]<=planex+30)){
			planex=-100;
			planey=500;
			repaint();
			}
	}
			}
	if(planey==500){
		JOptionPane.showMessageDialog(null, "你被击落", "失败", 1);
		cancel();
		System.exit(0);
	}
	///////////////////////////
			
	
			if(all==0){//如果所有的小蜜蜂被击中,则胜利
				JOptionPane.showMessageDialog(null, "你是男人中的男人", "胜利", 1);
				cancel();
				System.exit(0);
			}
			
		}
		
	}
	//玩家射击碰撞判断
	/*class Shot_Die extends TimerTask{
		public void run(){
			for(int i=0;i<30;i++){
				if(bee[i]==1){
					continue;
				}
			if((bullety>=beey[i])&&(bullety<=(beey[i]+17))&&(bulletx>=beex[i])&&(bulletx<=beex[i]+25)){
				all--;
				bee[i]=1;
				bullety=-15;
				bulletx=-10;
				repaint();
				}
		}
			if(all==0){//如果小蜜蜂死完,则销毁该线程
				cancel();
			}
		}
	}*/
	class BeeBulletMove extends TimerTask{
		public void run(){
			for(int i=0;i<30;i++){
				if(bee[i]==0){
			beebullety[i]=beebullety[i]+beebullet;
			repaint();
			if(beebullety[i]>=400){
				beebullety[i]=beey[i];
				beebulletx[i]=-10;
				//cancel();
			}
			}
			}
			if(all==0){
				cancel();
			}
		}
		
	}
  class BeeShot extends TimerTask{
		
		
		public void run(){
			for(int i=0;i<30;i++){
				if(bee[i]==0){
				if(bee[i]==1){
					continue;
				}
				//Random rand=new Random();
			//	if(rand.nextInt(101)<5){//小蜜蜂的发弹率,不过好像没什么作用?
					if(beebulletx[i]==-10){
						beebulletx[i]=beex[i]+15;
						//BeeBulletMove bbm=new BeeBulletMove();
						//ti.schedule(bbm, 0, 33);
					//}
				}
				
			}
			}
			if(all==0){
				cancel();
			}
		}
	}
	/*class BeeShot_Die extends TimerTask{
		public void run(){
			for(int i=0;i<30;i++){
				if(bee[i]==1){
					continue;
				}
				if((beebullety[i]>=planey)&&(beebullety[i]<=planey+19)&&(beebulletx[i]>=planex)&&(beebulletx[i]<=planex+30)){
					planex=-100;
					planey=500;
					repaint();
					}
			}
			if(planey==500){
				JOptionPane.showMessageDialog(null, "你被击落", "失败", 1);
				cancel();
			}
		}
	}*/

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -