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

📄 space.java

📁 坦克小战网络版
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Space extends Applet implements KeyListener, ActionListener{
	private surface Surface;	private T3D P1, P2;
	private Image offScreenImage;
	private Ticker t;
	private ammunition[] Ammo;
	private explosion[] Explosion;

	private Button one, two, three;
	private boolean start;

	public void init(){
		one = new Button("f(x,y) = 0");
		two = new Button("f(x,y) = sin(sqrt(x^2+y^2))");
		three = new Button("f(x,y) = x^2*y^2*exp(-x^2-y^2)");
		add(one);
		add(two);
		add(three);
		one.addActionListener(this);
		two.addActionListener(this);
		three.addActionListener(this);
	}


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

	public void actionPerformed(ActionEvent e){
		if (e.getSource() == one)
			startGame(1);
		if (e.getSource() == two)
			startGame(2);
		if (e.getSource() == three)
			startGame(3);

		if(start){
			moveRealTimeObjects();
			repaint();
		}
	}

	public void removeButton(){
		remove(one);
		remove(two);
		remove(three);
	}

	public void moveRealTimeObjects(){
		P1.move();
		P2.move();
		for(int i = 0; i < Ammo.length; i++){
			if(Ammo[i] != null){
				Ammo[i].fly();
				if(Ammo[i].Crashed(P1, P2)){
					for(int j = 0; j< Explosion.length; j++){
						if(Explosion[j] == null){
							Explosion[j] = Ammo[i].explode();
							break;
						}
					}
					Ammo[i] = null;
				}
			}
		}
		for(int i = 0; i < Explosion.length; i++){
			if(Explosion[i] != null){
				Explosion[i].changeShape(P1, P2);
				if(Explosion[i].over())
					Explosion[i] = null;
			}
		}
	}

	public void startGame(int index){
		Surface = new surface(index);
		removeButton();
		P1 = new T3D(Surface, -100, 100, 21, 31, 7, Color.red);
		P2 = new T3D(Surface, 400, 100, 21, 31, 7, Color.blue);
		Ammo = new ammunition[10];
		Explosion = new explosion[10];

		addKeyListener(this);
		requestFocus();

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

		start = true;
	}

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

		Surface.draw(g);

		if(P1.body.centre.b >= P2.body.centre.b){
			P1.draw(g);
			P2.draw(g);
		} else{
			P2.draw(g);
			P1.draw(g);
		}

		for(int i = 0; i < Ammo.length; i++){
			if(Ammo[i] != null)
				Ammo[i].draw(g);
		}
	}

	public void drawStatusBar(Graphics g){
		Font titleFont = new Font("Century Gothic", Font.BOLD, 14);
		g.setFont(titleFont);
		g.setColor(Color.red);
		g.drawString("Health: " + P1.health + "%", 10, 20);
		g.drawString("Kills: " + P1.kills, 10, 36);

		g.setColor(Color.blue);
		g.drawString("Health: " + P2.health + "%", 700, 20);
		g.drawString("Kills: " + P2.kills, 700, 36);
	}


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

	public void keyPressed(KeyEvent e){
		control1P(e, true, false);
		control2P(e, true, false);
	}


	public void keyReleased(KeyEvent e){
		control1P(e, false ,true);
		control2P(e, false ,true);
	}



	public void keyTyped(KeyEvent e){}

	public void control1P(KeyEvent e, boolean keyPress, boolean keyRelease){
		if(keyPress){
			if(e.getKeyCode() == KeyEvent.VK_LEFT){
				P1.rotateRight = false;
				P1.rotateLeft = true;
			}
			if(e.getKeyCode() == KeyEvent.VK_RIGHT){
				P1.rotateRight = true;
				P1.rotateLeft = false;
			}
			if(e.getKeyCode() == KeyEvent.VK_UP){
				P1.moveUp = true;
				P1.moveDown = false;
			}
			if(e.getKeyCode() == KeyEvent.VK_DOWN){
				P1.moveDown = true;
				P1.moveUp = false;
			}


			if(e.getKeyChar() == 'o'){
				P1.aimRight = false;
				P1.aimLeft = true;
			}
			if(e.getKeyChar() == 'p'){
				P1.aimRight = true;
				P1.aimLeft = false;
			}

			if(e.getKeyChar() == 'k'){
				P1.aimDown = false;
				P1.aimUp = true;
			}
			if(e.getKeyChar() == 'l'){
				P1.aimDown = true;
				P1.aimUp = false;
			}

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

		if(keyRelease){
			if(e.getKeyCode() == KeyEvent.VK_LEFT)
				P1.rotateLeft = false;
			if(e.getKeyCode() == KeyEvent.VK_RIGHT)
				P1.rotateRight = false;
			if(e.getKeyCode() == KeyEvent.VK_UP)
				P1.moveUp = false;
			if(e.getKeyCode() == KeyEvent.VK_DOWN)
				P1.moveDown = false;

			if(e.getKeyChar() == 'o')
				P1.aimLeft = false;
			if(e.getKeyChar() == 'p')
				P1.aimRight = false;
			if(e.getKeyChar() == 'k')
				P1.aimUp = false;
			if(e.getKeyChar() == 'l')
				P1.aimDown = false;
		}
	}
	public void control2P(KeyEvent e, boolean keyPress, boolean keyRelease){
		if(keyPress){
			if(e.getKeyChar() == 'a'){
				P2.rotateRight = false;
				P2.rotateLeft = true;
			}
			if(e.getKeyChar() == 'd'){
				P2.rotateRight = true;
				P2.rotateLeft = false;
			}
			if(e.getKeyChar() == 'w'){
				P2.moveUp = true;
				P2.moveDown = false;
			}
			if(e.getKeyChar() == 's'){
				P2.moveDown = true;
				P2.moveUp = false;
			}

			if(e.getKeyChar() == 'r'){
				P2.aimRight = false;
				P2.aimLeft = true;
			}
			if(e.getKeyChar() == 't'){
				P2.aimRight = true;
				P2.aimLeft = false;
			}

			if(e.getKeyChar() == 'f'){
				P2.aimDown = false;
				P2.aimUp = true;
			}
			if(e.getKeyChar() == 'g'){
				P2.aimDown = true;
				P2.aimUp = false;
			}

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

		if(keyRelease){
			if(e.getKeyChar() == 'a')
				P2.rotateLeft = false;
			if(e.getKeyChar() == 'd')
				P2.rotateRight = false;
			if(e.getKeyChar() == 'w')
				P2.moveUp = false;
			if(e.getKeyChar() == 's')
				P2.moveDown = false;

			if(e.getKeyChar() == 'r')
				P2.aimLeft = false;
			if(e.getKeyChar() == 't')
				P2.aimRight = false;
			if(e.getKeyChar() == 'f')
				P2.aimUp = false;
			if(e.getKeyChar() == 'g')
				P2.aimDown = false;
		}
	}
}

⌨️ 快捷键说明

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