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

📄 tank.java

📁 一个基于JAVA的小游戏
💻 JAVA
字号:
package com.bjsxt.tank.core;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Random;




public abstract class Tank extends GameObject{
	protected int offx;
	protected int offy;
	int offxPT,offyPT=HEIGHT/2;
	int SPEED = 5;
	public static int WIDTH=50;
	public static int HEIGHT=50;
	protected boolean good = true;
	Random r = new Random();
	protected int oldx;
	protected int oldy;
	Rectangle rect = new Rectangle(x,y,Tank.WIDTH,Tank.HEIGHT);
	static int count;
	public Tank(int x, int y,boolean good) {
		this.x = x;
		this.y = y;
		this.good = good;
		count++;
	}
	public abstract void draw (Graphics g);
	protected void drawPT(Graphics g) {
		g.setColor(Color.black);
		g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH/2+offxPT, y+Tank.HEIGHT/2+offyPT);
	}
	protected void autoMoveAndFire() {
		if(!good){
			if(r.nextInt(10)==9){
				offx = SPEED *(r.nextInt(3)-1);
			}
			if(r.nextInt(10)==9){
				offy = SPEED *(r.nextInt(3)-1);
			}
			if(r.nextInt(15)==13){
				fire();
			}
			
		}
	}
	protected void checkBounds() {
		if (x<-Tank.WIDTH) {
			x = TankClient.GAME_WIDTH;
		}
		if(x>TankClient.GAME_WIDTH){
			x = -Tank.WIDTH;
		}
		
		if (y<-Tank.HEIGHT) {
			y = TankClient.GAME_HEIGHT;
		}
		if(y>TankClient.GAME_HEIGHT){
			y = -Tank.HEIGHT;
		}
	}
	public void keyPressed(int key){
		switch (key) {
		case KeyEvent.VK_LEFT:
			offx = -SPEED;
			break;
		case KeyEvent.VK_UP:
			offy = -SPEED;
			break;
		case KeyEvent.VK_RIGHT:
			offx = SPEED;
			break;
		case KeyEvent.VK_DOWN:
			offy = SPEED;
			break;

		default:
			break;
		}
		
	}
	private void fire() {
		
		int x = this.x + Tank.WIDTH/2 - Missile.WIDTH/2;
		int y = this.y +Tank.HEIGHT/2 - Missile.HEIGHT/2;
		
		GameMediator.getInstance().addGameObject(GameFactoryMgr.getFactory().createMissile(x,y,offxPT,offyPT,this.good));
		
	}
	public void keyReleased(int key) {
		switch (key) {
		case KeyEvent.VK_CONTROL:
			fire();
			break;

		case KeyEvent.VK_LEFT:
			offx = 0;
			break;
		case KeyEvent.VK_UP:
			offy = 0;
			break;
		case KeyEvent.VK_RIGHT:
			offx = 0;
			break;
		case KeyEvent.VK_DOWN:
			offy = 0;
			break;

		default:
			break;
		}

		
	}
	protected void setOffPT() {
		if(offx==0 && offy==0){
			return;
		}
		
		
//		if(offx==0){
//			offxPT=0;
//		}else if(offx>0){
//			offxPT = Tank.WIDTH/2;
//		}else if(offx<0){
//			offxPT = -Tank.WIDTH/2;
//		}
		offxPT = offx/SPEED * Tank.WIDTH/2;
//		if(offy==0){
//			offyPT=0;
//		}else if(offy>0){
//			offyPT = Tank.HEIGHT/2;
//		}else if(offy<0){
//			offyPT = -Tank.HEIGHT/2;
//		}
		offyPT = offy/SPEED * Tank.HEIGHT/2;
		
	}
	public Rectangle getRect(){
		rect.x = this.x;
		rect.y = this.y;
		return this.rect;
	}
	public void die() {
		setLive(false);
		GameMediator.getInstance().removeGameObject(this);
		count--;
	}
	public void collideWalls(List<Wall> walls) {
		for (int i = 0; i < walls.size(); i++) {
			Wall w = walls.get(i);
			collideWall(w);
		}
		
	}
	boolean collideWall(Wall w) {
		if(this.getRect().intersects(w.getRect())){
			goBack();
			return true;
		}
		return false;
		
	}
	private void goBack() {
		this.x = this.oldx;
		this.y = this.oldy;
	}
	public void collideTanks(List<Tank> tanks) {
		for (int i = 0; i < tanks.size(); i++) {
			Tank t = tanks.get(i);
			collideTank(t);
		}
		
	}
	public boolean collideTank(Tank t) {
		if(this==t){
			return false;
		}
		
		if(this.getRect().intersects(t.getRect())){
			this.goBack();
			t.goBack();
			return true;
		}
		return false;
		
	}
}

⌨️ 快捷键说明

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