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

📄 tank3d.java

📁 坦克大战游戏源代码
💻 JAVA
字号:
// made by Pan Hu, 2004.6.1

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class tank3D extends Applet implements KeyListener, ActionListener{
	private Ticker t;
	private Image offScreenImage;

	private surface Surface;
	private ssmLauncher[] SSMlauncher;
	private railLauncher[] RAILlauncher;
	private rocket[] Rocket;
	private rail[] Rail;
	private ammunition[] Ammo;
	private explosion[] Explosion;

	private int x_shift, y_shift;

	private tank Tank;

	private enemy[] enemys;
	private map Map;

	private boolean act;

	public void init(){
		act = true;

		Surface = new surface(3, x_shift, y_shift);
		Map = new map(enemys);

		addKeyListener(this);
		requestFocus();


		Tank = new tank(Surface, 0, 0, new vector(-1,0));

		SSMlauncher = new ssmLauncher[4];
		SSMlauncher[0] = new ssmLauncher(Surface, 50, 50, new vector(1, 0.5),x_shift, y_shift);
		SSMlauncher[1] = new ssmLauncher(Surface, 150, 150, new vector(1, 0.5),x_shift, y_shift);
		SSMlauncher[2] = new ssmLauncher(Surface, 50, -50, new vector(1, 0.5),x_shift, y_shift);
		SSMlauncher[3] = new ssmLauncher(Surface, 250, 350, new vector(1, 0.5),x_shift, y_shift);

		RAILlauncher = new railLauncher[4];
		RAILlauncher[0] = new railLauncher(Surface, 250, 250, new vector(1, 0.5),x_shift, y_shift);
		RAILlauncher[1] = new railLauncher(Surface, -50, -50, new vector(1, 0.5),x_shift, y_shift);
		RAILlauncher[2] = new railLauncher(Surface, -250, 250, new vector(1, 0.5),x_shift, y_shift);
		RAILlauncher[3] = new railLauncher(Surface, -350, 350, new vector(1, 0.5),x_shift, y_shift);


		Rocket = new rocket[8];
		Rail = new rail[4];
		Ammo = new ammunition[10];
		Explosion = new explosion[10];


		t = new Ticker(30);
		t.addActionListener(this);
		t.start();

	}

	public void destory(){
		removeKeyListener(this);
		t = null;
	}

	public void actionPerformed(ActionEvent e){

		Tank.move(enemys);
		x_shift = (int)Tank.body.x; y_shift = (int)Tank.body.y;

		if(Tank.moveUp || Tank.moveDown)
			Surface.upDate(x_shift, y_shift);
		ammoAction();
		rocketAction();
		railAction();
		explosionAction();
		ssmTankAction();
		railTankAction();

		createMoveObjectsArray();
		Map.update(enemys);
		repaint();
	}


	public void paint(Graphics g){
		for(int i = 0; i < Explosion.length; i++){
			if(Explosion[i] !=null)
				Explosion[i].draw(g);
		}

		Surface.draw(g);
		drawMoveObjects(g);
		Map.draw(g);
	}

	public void drawMoveObjects(Graphics g){
		for(int i = 0; i < enemys.length; i++){
			enemys[i].draw(g);
		}
		for(int i = 0; i < Rocket.length; i++){
			if(Rocket[i] != null)
				Rocket[i].draw(g);
		}
		for(int i = 0; i < Ammo.length; i++){
			if(Ammo[i] != null)
				Ammo[i].draw(g);
		}
		for(int i = 0; i < Rail.length; i++){
			if(Rail[i] != null)
				Rail[i].draw(g);
		}
	}

	public void createMoveObjectsArray(){
		enemys = new enemy[1 + SSMlauncher.length  + RAILlauncher.length /* +......*/];
		enemys[0] = Tank;
		int currentIndex = 1;
		for(int i = 0; i < SSMlauncher.length; i++, currentIndex++)
			enemys[currentIndex] = SSMlauncher[i];
		for(int i = 0; i < RAILlauncher.length; i++, currentIndex++)
			enemys[currentIndex] = RAILlauncher[i];
		decideWhichToDrawFirst();
	}

	public void decideWhichToDrawFirst(){
		enemy temp;
		int j  = 1;
		while(j<enemys.length){
			for(int i = 0; i <enemys.length - j; i++){
				if(enemys[i].position() > enemys[i+1].position()){
					temp = enemys[i+1];
					enemys[i+1] = enemys[i];
					enemys[i] = temp;
				}
			}
			j++;
		}
	}

	public void deleteNull(String s){
		int currentIndex = 0;
		if(s.equals("ssmTank")){
			for(int i = 0; i < SSMlauncher.length; i++, currentIndex++){
				if(SSMlauncher[i] == null)
					currentIndex--;
				else
					SSMlauncher[currentIndex] = SSMlauncher[i];
			}
			ssmLauncher[] temp = new ssmLauncher[currentIndex];
			for(int i = 0; i < currentIndex; i++)
				temp[i] = SSMlauncher[i];
			SSMlauncher = temp;
		}

		currentIndex = 0;
		if(s.equals("railTank")){
			for(int i = 0; i < RAILlauncher.length; i++, currentIndex++){
				if(RAILlauncher[i] == null)
					currentIndex--;
				else
					RAILlauncher[currentIndex] = RAILlauncher[i];
			}
			railLauncher[] temp = new railLauncher[currentIndex];
			for(int i = 0; i < currentIndex; i++)
				temp[i] = RAILlauncher[i];
			RAILlauncher = temp;
		}
	}

	public void update(Graphics g) {
		Graphics offScreenGraphics;
		if (offScreenImage == null) {
		offScreenImage = createImage(640, 480);
		}
		offScreenGraphics = offScreenImage.getGraphics();
		offScreenGraphics.setColor(Color.white);
		offScreenGraphics.fillRect(0, 0, 640, 480);
		paint(offScreenGraphics);
		g.drawImage(offScreenImage, 0, 0, this);
	}

	public void keyPressed(KeyEvent e){
		if(e.getKeyCode() == KeyEvent.VK_LEFT){
			Tank.turnRight = false;
			Tank.turnLeft = true;
		}
		if(e.getKeyCode() == KeyEvent.VK_RIGHT){
			Tank.turnRight = true;
			Tank.turnLeft = false;
		}
		if(e.getKeyCode() == KeyEvent.VK_UP){
			Tank.moveUp = true;
			Tank.moveDown = false;
		}
		if(e.getKeyCode() == KeyEvent.VK_DOWN){
			Tank.moveDown = true;
			Tank.moveUp = false;
		}

		if(e.getKeyChar() == 'o'){
			Tank.aimRight = false;
			Tank.aimLeft = true;
		}
		if(e.getKeyChar() == 'p'){
			Tank.aimRight = true;
			Tank.aimLeft = false;
		}
		if(e.getKeyChar() == 'k'){
			Tank.aimDown = false;
			Tank.aimUp = true;
		}
		if(e.getKeyChar() == 'l'){
			Tank.aimDown = true;
			Tank.aimUp = false;
		}

		if(e.getKeyChar() == 'j' && Tank.fireInterval == 0){
			for(int i = 0; i < Ammo.length; i++){
				if(Ammo[i] == null){
					Ammo[i] = Tank.fireAmmo();
					break;
				}
			}
		}

			if(e.getKeyChar() == 's'){
				t.setDelay(30);
			}
			if(e.getKeyChar() == 'd'){
				t.setDelay(35);
			}

		//=======================================debug============================

	}

	public void keyReleased(KeyEvent e){
		if(e.getKeyCode() == KeyEvent.VK_LEFT)
			Tank.turnLeft = false;
		if(e.getKeyCode() == KeyEvent.VK_RIGHT)
			Tank.turnRight = false;
		if(e.getKeyCode() == KeyEvent.VK_UP)
			Tank.moveUp = false;
		if(e.getKeyCode() == KeyEvent.VK_DOWN)
			Tank.moveDown = false;
		if(e.getKeyChar() == 'o')
			Tank.aimLeft = false;
		if(e.getKeyChar() == 'p')
			Tank.aimRight = false;
		if(e.getKeyChar() == 'k')
			Tank.aimUp = false;
		if(e.getKeyChar() == 'l')
			Tank.aimDown = false;


	}

	public void keyTyped(KeyEvent e){}

	public void ammoAction(){
		for(int i = 0; i < Ammo.length; i++){
			if(Ammo[i] != null){
				Ammo[i].fly(x_shift,y_shift);
				if(Ammo[i].Crashed(enemys)){
					for(int j = 0; j< Explosion.length; j++){
						if(Explosion[j] == null){
							Explosion[j] = Ammo[i].explode();
							break;
						}
					}
					Ammo[i] = null;
				}
			}
		}
	}

	public void rocketAction(){
		for(int i = 0; i < Rocket.length; i++){
			if(Rocket[i] != null){
				Rocket[i].fly(Tank.centre());
				if(Rocket[i].crashed && Rocket[i].disapearTime == 0){
					for(int j = 0; j < Explosion.length; j++){
						if(Explosion[j] == null){
							Explosion[j] = Rocket[i].explode();
							break;
						}
					}
				}
				if(Rocket[i].disapearTime > 28)
					Rocket[i] =null;
			}
		}
	}

	public void railAction(){
		for(int i = 0; i < Rail.length; i++){
			if(Rail[i] != null){
				Rail[i].changeShape(x_shift, y_shift);
				if(Rail[i].time > 45)
					Rail[i] = null;
			}
		}
	}

	public void explosionAction(){
		for(int i = 0; i < Explosion.length; i++){
			if(Explosion[i] != null){
				Explosion[i].changeShape(x_shift, y_shift);
				if(Explosion[i].over())
					Explosion[i] = null;
			}
		}
	}

	public void ssmTankAction(){
		for(int i = 0; i <SSMlauncher.length; i++){
			if(SSMlauncher[i] != null){
				SSMlauncher[i].move(x_shift, y_shift, Explosion, enemys, Rail);
				if(SSMlauncher[i].canFire()){
					for(int j = 0; j < Rocket.length; j++){
						if(Rocket[j] == null){
							Rocket[j] = SSMlauncher[i].fireRocket();
							break;
						}
					}
				}
				if(SSMlauncher[i].health <= 0){
					for(int j = 0; j< Explosion.length; j++){
						if(Explosion[j] == null){
							Explosion[j] = SSMlauncher[i].explode();
							break;
						}
					}
					SSMlauncher[i] = null;
					deleteNull("ssmTank");
				}
			}
		}
	}
	public void railTankAction(){
		for(int i = 0; i <RAILlauncher.length; i++){
			if(RAILlauncher[i] != null){
				RAILlauncher[i].move(x_shift, y_shift, Explosion, enemys, Rail);
				if(RAILlauncher[i].canFire()){
					for(int j = 0; j < Rail.length; j++){
						if(Rail[j] == null){
							Rail[j] = RAILlauncher[i].shoot();
							break;
						}
					}
				}

				if(RAILlauncher[i].health <= 0){
					for(int j = 0; j< Explosion.length; j++){
						if(Explosion[j] == null){
							Explosion[j] = RAILlauncher[i].explode();
							break;
						}
					}
					RAILlauncher[i] = null;
					deleteNull("railTank");
				}
			}
		}
	}

}

⌨️ 快捷键说明

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