雷电 射击游戏

重点
1, 处理发射的子弹
当按下空格键时,将激光弹同时加到容器和数组列表中。 创建线程和永久循环后, 通过for循环语句和setBounds方法让太空飞船向上射击激光弹。

代码还不能运行, 仅供参考
package chapter4;

import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;
import java.applet.AudioClip;
import java.net.MalformedURLException;
import java.net.*;

public class SpaceDestroyers extends JFrame implements

MouseMotionListener, KeyListener {
	Container cont;
	// 游戏等级
	int currentLevel = 1;
	// 外星人数量
	int numOfEnemies = 1;
	// 背景图片background
	JLabel backgrounD = new JLabel(new ImageIcon

	("background.PNG"));
	// 封面图片
	JLabel front = new JLabel(new ImageIcon("front.png"));
	// 飞机发射图片
	ImageIcon shipBullet = new ImageIcon("shipBullets.PNG");
	ImageIcon enemyBullet = new ImageIcon("enemyBullets.PNG");
	// 能量包
	JLabel powerup = new JLabel(new ImageIcon("powerup.PNG"));
	// 飞机
	ImageIcon shipImg = new ImageIcon("ship.PNG");
	ImageIcon shipHit = new ImageIcon("shipHit.PNG");
	// 敌人
	ImageIcon enemyImg = new ImageIcon("enemy.PNG");
	ImageIcon enemyImg1 = new ImageIcon("ufoImg.png");
	ImageIcon enemyImg2 = new ImageIcon("bigenemy1.png");
	ImageIcon enemyImg3 = new ImageIcon("bigenemy.png");
	ImageIcon enemyHit = new ImageIcon("enemyHit.PNG");
	// 能量加强包能量释放
	JLabel powerAttack = new JLabel(new ImageIcon

	("powerAttack.PNG"));
	// 医疗包
	JLabel healthPack = new JLabel(new ImageIcon("health.PNG"));
	// 游戏得分
	int score = 0;
	// 显示分数标签
	JLabel scoreDisplay = new JLabel();
	// 健康指数
	int health = 200;
	// 最后得分
	int finalScore = 0;
	// 能量加强包是否释放能量
	boolean useAttack = false;
	JLabel ship = new JLabel(shipImg);
	// 飞机发射的子弹数组
	ArrayList playerBullets = new ArrayList();
	// 敌人发射弹的数组
	ArrayList enemyBullets = new ArrayList();
	// 敌人的数组
	ArrayList enemies = new ArrayList();

	// 背景音乐
	// AudioClip sound = loadSound("beijing.mp3");

	public SpaceDestroyers() {
		super("太空保卫战");
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(500, 610);
		cont = getContentPane();
		cont.setLayout(null);
		front.setBounds(-10, -20, 500, 610);
		backgrounD.setBounds(0, 0, 600, 600);
		ship.setBounds(225, 500, 50, 50);
		cont.add(front);
		title();
		cont.remove(front);
		// cont.setBackground(Color.BLACK);
		cont.add(ship);
		scoreDisplay.setBounds(10, 10, 400, 30);
		scoreDisplay.setFont(new Font("Time New Roman",

		Font.BOLD, 15));
		scoreDisplay.setForeground(Color.WHITE);
		cont.add(scoreDisplay);
		cont.add(backgrounD);
		addMouseMotionListener(this);
		addKeyListener(this);
		populateEnemies();
		Play play = new Play();
		play.start();
		// backgrounD.setBounds(0, 0, 600, 600);
		// cont.add(backgrounD);
		setContentPane(cont);

	}

	// public void paint(Graphics g)
	// {
	// try
	// {
	// g.setColor(Color.WHITE);
	// String name = "--太空保卫战背景--";
	// Font f = new Font(name, Font.BOLD, 20);
	// String xinxi=g.setFont(f);
	// JLabel information = new JLabel(xinxi);
	// cont.add(information);
	// information.setBounds(-60, -300, 600, 200);
	// do
	// {
	// information.setBounds(information.getX(),information.getY() + 1, 600,
	// 200);
	// Thread.sleep(5);
	// } while (information.getY() < 700);
	// }
	// catch (Exception e) { }
	// }
	public void title() {
		try {
			JLabel title = new JLabel(new ImageIcon

			("title.PNG"));
			cont.add(title);
			title.setBounds(-60, -300, 600, 200);
			do {
				title.setBounds(title.getX(),

				title.getY() + 1, 600, 200);
				Thread.sleep(10);
			} while (title.getY() < 560);
			// repaint();
		} catch (Exception e) {
		}
	}

	// 程序自动生成敌人
	public void populateEnemies() {
		for (int i = 0; i <= numOfEnemies; i++) {
			if (i % 2 == 0) {
				JLabel tempEnemy = new JLabel

				(enemyImg);
				int randLocation = (int) (Math.random()

				* 500);
				enemies.add(tempEnemy);
				cont.add((JLabel) (enemies.get(i)));
				tempEnemy.setBounds(randLocation, 10,

				30, 30);
				cont.setComponentZOrder(((JLabel)

				(enemies.get(i))), 0);
			} else if (i % 4 == 0) {
				JLabel tempEnemy2 = new JLabel

				(enemyImg2);
				int randLocation2 = (int) (Math.random

				() * 500);
				enemies.add(tempEnemy2);
				cont.add((JLabel) (enemies.get(i)));
				tempEnemy2.setBounds(randLocation2,

				10, 30, 30);
				cont.setComponentZOrder(((JLabel)

				(enemies.get(i))), 0);
			} else if (i % 3 == 0) {
				JLabel tempEnemy3 = new JLabel

				(enemyImg3);
				int randLocation3 = (int) (Math.random

				() * 500);
				enemies.add(tempEnemy3);
				cont.add((JLabel) (enemies.get(i)));
				tempEnemy3.setBounds(randLocation3,

				10, 30, 30);
				cont.setComponentZOrder(((JLabel)

				(enemies.get(i))), 0);
			} else {
				JLabel tempEnemy1 = new JLabel

				(enemyImg1);
				int randLocation1 = (int) (Math.random

				() * 500);
				enemies.add(tempEnemy1);
				cont.add((JLabel) (enemies.get(i)));
				tempEnemy1.setBounds(randLocation1,

				10, 30, 30);
				cont.setComponentZOrder(((JLabel)

				(enemies.get(i))), 0);
			}
		}
	}

	public class Play extends Thread {
		// 游戏结束
		public void stopp() {

		}

		public void run() {
			while (true) {
				try {
					for (int i = 0; i <

					enemies.size(); i++) {
						JLabel tempEnemy =

						(JLabel) (enemies.get(i));
						int distance = (int)

						(Math.random() * 2);
						tempEnemy.setBounds

						(tempEnemy.getX(), tempEnemy.getY() + distance, 30, 30);
						// 能量包是否释放能量
						if (useAttack) {
							// 释放的能量是否击中敌机
							if

							(powerAttack.getBounds().intersects(
									tempEnemy.getBounds())) {

								cont.remove(tempEnemy);
								i--;

								numOfEnemies--;
								score

								+= 15;
							}
						}
						if

						(tempEnemy.getBounds().intersects(ship.getBounds())) {
							health--;
							cont.remove

							(tempEnemy);
						}
						if (tempEnemy.getY() >

						550) {

							tempEnemy.setBounds(tempEnemy.getX(), 10, 30, 30);
						}
						int fire = (int)

						(Math.random() * 2500);
						if (fire <= 1) {
							JLabel

							tempBullet = new JLabel(enemyBullet);

							// tempBullet.setBounds(ship.getX() + 5,
							// ship.getY()+30, 10, 20);

							tempBullet.setBounds(tempEnemy.getX() + 5,
									tempEnemy.getY() + 30, 10,

									20);

							enemyBullets.add(tempBullet);
							cont.add

							((JLabel) (enemyBullets.get(enemyBullets.size() - 1)));

							cont.setComponentZOrder((JLabel) (enemyBullets
									.get(enemyBullets.size()

									- 1)), 0);
						}
					}
					// 是否歼灭了敌人
					boolean breakAll = false;
					for (int

					i = 0; i < playerBullets.size(); i++) {
						JLabel temp =

						((JLabel) (playerBullets.get(i)));
						temp.setBounds

						(temp.getX(), temp.getY() - 8, 10, 20);
						if (temp.getY() < 0) {
							cont.remove

							(temp);

							playerBullets.remove(i);// shipBullet
							i--;
						}
						for (int j = 0; j <

						enemies.size(); j++) {
							JLabel

							tempEnemy = (JLabel) (enemies.get(j));
							if

							(temp.getBounds().intersects(tempEnemy.getBounds())) {
								score

								+= 1000;

								tempEnemy.setIcon(enemyHit);

								Thread.sleep(100);

								enemies.remove(j);

								cont.remove(tempEnemy);

								numOfEnemies--;
								if

								(numOfEnemies <= 0) {

									currentLevel++;// 升级

									if (currentLevel % 3 == 0)

									{

										cont.add(powerup);

										int randLoc = (int) (Math.random() * 450);

										powerup.setBounds(randLoc, 0, 30, 30);

									}

									if (currentLevel % 5 == 0)

									{

										cont.add(healthPack);

										int randLoc = (int) (Math.random() * 450);

										healthPack
												.setBounds(randLoc, 0, 30, 30);

									}

									// numOfEnemies = currentLevel *
									// currentLevel;//敌机数量翻倍

									numOfEnemies = (int) (Math.random() * 5) + 1;// 随机产生敌机数量

									populateEnemies();

									breakAll = true;

									break;
								}
							}
						}
						if (breakAll) {
							break;
						}
					}
					if (useAttack) {
						powerAttack.setBounds

						(0, powerAttack.getY() - 1, 500, 10);
						if (powerAttack.getY()

						< 0) {
							cont.remove(powerAttack);
							useAttack = false;
							currentLevel++;
							// numOfEnemies= currentLevel *
							// currentLevel;//敌机数量翻倍
							numOfEnemies = (int) (Math.random() * 5) + 1;// 随机产生敌机数量

							populateEnemies();
						}
					}
					// 如果游戏级数为3的倍数,就显示能量包为
					if (currentLevel % 3 == 0) {
						powerup.setBounds

						(powerup.getX(), powerup.getY() + 1, 30, 30);
						if (powerup.getBounds

						().intersects(ship.getBounds())) {
							useAttack =

							true;
							cont.add

							(powerAttack);

							powerAttack.setBounds(0, ship.getY(), 500, 10);
							cont.remove

							(powerup);

							powerup.setBounds(-200, -200, 30, 30);
						}
					}
					if (currentLevel % 5 == 0) {
						healthPack.setBounds

						(healthPack.getX(), healthPack.getX() + 1, 30, 30);
						if

						(healthPack.getBounds().intersects(ship.getBounds())) {
							health += 50;
							score += 100;
							cont.remove

							(healthPack);

							healthPack.setBounds(-100, -100, 30, 30);
						}
					}
					// 敌人是否击中飞机
					breakAll = false;
					for (int i = 0; i <

					enemyBullets.size(); i++) {
						JLabel temp =

						((JLabel) (enemyBullets.get(i)));
						temp.setBounds

						(temp.getX(), temp.getY() + 2, 10, 20);
						if (temp.getY() > 550) {
							cont.remove

							(temp);

							enemyBullets.remove(i);
							i--;
						}
						if (ship.getBounds

						().intersects(temp.getBounds())) {
							ship.setIcon

							(shipHit);
							Thread.sleep

							(100);
							ship.setIcon

							(shipImg);
							score -= 100;
							health -= 50;
							cont.remove

							(temp);

							enemyBullets.remove(i);

							numOfEnemies--;

							if (health <=

							0) {
								// 游戏结束了

								// stopp();

								// break;
							}

							if

							(numOfEnemies <= 0) {

								currentLevel++;

								// numOfEnemies = currentLevel * currentLevel;

								numOfEnemies = (int) (Math.random() * 5) + 1;// 随机产生敌机数量

								populateEnemies();

								breakAll = true;
								break;
							}
						}
						if (breakAll) {
							break;
						}
					}
					scoreDisplay.setText("等级: "

					+ currentLevel + "                得分: " + score);
					cont.repaint();
					Thread.sleep(10);
				} catch (Exception e) {
					System.out.println

					(e.getMessage());
				}
			}
		}
	}

	public void mouseMoved(MouseEvent event) {
		// ship.setBounds(event.getX() - 25, event.getY() - 40, 50, 50);
	}

	public void mouseDragged(MouseEvent event) {
		// new SpaceDestroyers();
	}

	// 使用空格键发射弹

	public void keyPressed(KeyEvent event) {
		if (event.getKeyChar() == ' ') {
			JLabel tempBullet = new JLabel(shipBullet);
			tempBullet.setBounds(ship.getX() + 20,

			ship.getY() - 20, 10, 20);
			playerBullets.add(tempBullet);
			cont.add((JLabel) (playerBullets.get

			(playerBullets.size() - 1)), 0);
			score -= 2;
		}
		if (event.getKeyCode() == KeyEvent.VK_UP)// 上
		{
			if (ship.getY() <= 0) {
				ship.setBounds(ship.getX(), 0, 50,

				50);
			} else {
				ship.setBounds(ship.getX(), ship.getY

				() - 20, 50, 50);
			}

		}
		if (event.getKeyCode() == KeyEvent.VK_DOWN)// 下
		{
			if (ship.getY() >= 520) {
				ship.setBounds(ship.getX(), 520, 50,

				50);
			} else {
				ship.setBounds(ship.getX(), ship.getY

				() + 20, 50, 50);
			}

		}
		if (event.getKeyCode() == KeyEvent.VK_LEFT)// 左
		{
			if (ship.getX() <= -5) {
				ship.setBounds(-5, ship.getY(), 50,

				50);
			} else {
				ship.setBounds(ship.getX() - 20,

				ship.getY(), 50, 50);
			}

		}
		if (event.getKeyCode() == KeyEvent.VK_RIGHT)// 右
		{
			if (ship.getX() >= 450)
				ship.setBounds(450, ship.getY(), 50,

				50);
			else
				ship.setBounds(ship.getX() + 20,

				ship.getY(), 50, 50);
		}
	}

	public void keyReleased(KeyEvent event) {
	}

	public void keyTyped(KeyEvent event) {
	}

	public static void main(String[] args) {

		try {
			URL cb;
			File f = new File("D:\\软件\\Java\\eclipse 安装\\phone\\beijing.wav");
			cb = f.toURL();
			AudioClip aau;
			aau = Applet.newAudioClip(cb);
			// aau.play();
			aau.loop();
			new SpaceDestroyers();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

	}
}




你可能感兴趣的:(thread,游戏,swing,医疗,音乐)