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

📄 stars3d.java

📁 空战游戏,来自java2 编程第五版源码
💻 JAVA
字号:
import java.applet.*;
import java.awt.*;

class Stars3D
{
	static final int STAR_MAX = 50;
	static final int STAR_SPEED = 6;
	
	static final Color Col1 = new Color(96,96,96);
	static final Color Col2 = new Color(160,160,160);
	static final Color Col3 = new Color(255,255,255);
	
	Vector3D Vertices[];
	Point2D  Points[];

	double XPos = 0;
	double YPos = 0;
	double ZPos = 100;
	
	int XRot = 0;
	int YRot = 0;
	int ZRot = 0;

	double XScale = 1;
	double YScale = 1;
	double ZScale = 1;

	double XShear = 0;
	double YShear = 0;

	public Stars3D()
	{
		Vertices = new Vector3D[STAR_MAX];
		Points = new Point2D[STAR_MAX];
		
		for (int i=0; i<STAR_MAX; i++)
		{
			Points[i]   = new Point2D();
			Vertices[i] = new Vector3D();
			
			Vertices[i].x = 200 * Math.random() * GetRandomSign();
			Vertices[i].y = 200 * Math.random() * GetRandomSign();
			Vertices[i].z = (1000 * Math.random()) + 200;
		}
	}

	private double GetRandomSign()
	{
		double Sign = 1;

		for (int i=0; i<(int)(200 * Math.random()); i++)
			Sign *= -1;
		
		return Sign;
	}
	
	public void Draw(Graphics g)
	{
		Matrix3D MyMatrix = new Matrix3D();
		
		MyMatrix.RotateZ(ZRot);
		MyMatrix.RotateX(XRot);
		MyMatrix.RotateY(YRot);
		MyMatrix.Scale(XScale,YScale,ZScale);
		MyMatrix.Shear(XShear,YShear);
		MyMatrix.Translate(XPos,YPos,ZPos);
		
		for (int i=0; i<STAR_MAX; i++)
		{
			MyMatrix.Transform(Vertices[i]);
			Points[i] = Point2D.Projection(Vertices[i]);			
		}
		
		for (int i=0; i<STAR_MAX; i++)
		{
			if (Vertices[i].z < 300)
				g.setColor(Col3);
			else
			if (Vertices[i].z < 600)
				g.setColor(Col2);
			else
				g.setColor(Col1);
			g.drawLine(Points[i].x,Points[i].y,Points[i].x,Points[i].y);
		}
	}
	
	public void Move()
	{
		for (int i=0; i<STAR_MAX; i++)
		{
			Vertices[i].z -= STAR_SPEED;
			if (Vertices[i].z <= 0)
				Vertices[i].z += 1000;
		}
	}
}

⌨️ 快捷键说明

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