用多边形开发Java游戏:小行星游戏实现指南
立即解锁
发布时间: 2025-08-26 00:32:32 阅读量: 4 订阅数: 9 


Android游戏开发:从零开始构建经典射击游戏
### 用多边形开发 Java 游戏:小行星游戏实现指南
#### 1. 为小行星游戏创建多边形类
标准的 J2SE API 中有很实用的 `Polygon` 类,可用于开发小行星游戏。遗憾的是,Google 未将 `java.awt` 中的这些实用类纳入 Android API。以下是修改后的 `Polygon` 类,它是 Java SE `Polygon` 类的精简版本,适用于 Android 和小行星游戏。
##### 1.1 `Polygon` 类的主要方法
- **构造函数**:默认构造函数创建一个四边形。也可通过提供代表顶点 X 和 Y 坐标的两个数组以及边数来定义一个 N 边形。
- **calculateBounds(int xpoints[], int ypoints[], int npoints)**:根据一组 X 和 Y 坐标以及点数,计算多边形的边界。
- **addPoint(int x, int y)**:根据给定的 X 和 Y 坐标,为多边形添加一个点。
- **updateBounds(int x, int y)**:更新多边形的边界,使其包含新点 (X, Y)。
- **getBoundingBox()**:返回多边形的矩形边界。
- **contains(int x, int y)**:检查多边形是否包含给定的点 (X, Y)。
- **float[] getPoints()**:返回多边形顶点的 X 和 Y 坐标。
```java
package ch04.common;
public class Polygon {
public int npoints;
public int[] ypoints;
public int[] xpoints;
protected Rectangle bounds;
public Polygon() {
xpoints = new int[4];
ypoints = new int[4];
}
public Polygon(int xpoints[], int ypoints[], int npoints) {
if (npoints > xpoints.length || npoints > ypoints.length) {
throw new IndexOutOfBoundsException(
"npoints > xpoints.length || npoints > ypoints.length");
}
this.npoints = npoints;
this.xpoints = new int[npoints];
this.ypoints = new int[npoints];
System.arraycopy(xpoints, 0, this.xpoints, 0, npoints);
System.arraycopy(ypoints, 0, this.ypoints, 0, npoints);
}
void calculateBounds(int xpoints[], int ypoints[], int npoints) {
int boundsMinX = Integer.MAX_VALUE;
int boundsMinY = Integer.MAX_VALUE;
int boundsMaxX = Integer.MIN_VALUE;
int boundsMaxY = Integer.MIN_VALUE;
for (int i = 0; i < npoints; i++) {
int x = xpoints[i];
boundsMinX = Math.min(boundsMinX, x);
boundsMaxX = Math.max(boundsMaxX, x);
int y = ypoints[i];
boundsMinY = Math.min(boundsMinY, y);
boundsMaxY = Math.max(boundsMaxY, y);
}
bounds = new Rectangle(boundsMinX, boundsMinY, boundsMaxX
- boundsMinX, boundsMaxY - boundsMinY);
}
public void reset() {
npoints = 0;
bounds = null;
}
public void addPoint(int x, int y) {
if (npoints == xpoints.length) {
int tmp[];
tmp = new int[npoints * 2];
System.arraycopy(xpoints, 0, tmp, 0, npoints);
xpoints = tmp;
tmp = new int[npoints * 2];
System.arraycopy(ypoints, 0, tmp, 0, npoints);
ypoints = tmp;
}
xpoints[npoints] = x;
ypoints[npoints] = y;
npoints++;
if (bounds != null) {
updateBounds(x, y);
}
}
void updateBounds(int x, int y) {
if (x < bounds.x) {
bounds.width = bounds.width + (bounds.x - x);
bounds.x = x;
} else {
bounds.width = Math.max(bounds.width, x - bounds.x);
}
if (y < bounds.y) {
bounds.height = bounds.height + (bounds.y - y);
bounds.y = y;
} else {
bounds.height = Math.max(bounds.height, y - bounds.y);
}
}
public Rectangle getBoundingBox() {
if (npoints == 0) {
return new Rectangle();
}
if (bounds == null) {
calculateBounds(xpoints, ypoints, npoints);
}
return bounds;
}
public boolean contains(int x, int y) {
if (npoints <= 2 || !getBoundingBox().contains(x, y)) {
return false;
}
int hits = 0;
int lastx = xpoints[npoints - 1];
int lasty = ypoints[npoints - 1];
int curx, cury;
for (int i = 0; i < npoints; lastx = curx, lasty = cury, i++) {
curx = xpoints[i];
cury = ypoints[i];
if (cury == lasty) {
continue;
}
int leftx;
if (curx < lastx) {
if (x >= lastx) {
continue;
}
leftx = curx;
} else {
if (x >= curx) {
continue;
}
leftx = lastx;
}
double test1, test2;
if (cury < lasty) {
if (y < cury || y >= lasty) {
continue;
}
if (x < leftx) {
hits++;
continue;
}
test1 = x - curx;
test2 = y - cury;
} else {
if (y < lasty || y >= cury) {
continue;
}
if (x < leftx) {
hits++;
continue;
}
test1 = x - lastx;
test2 = y - lasty;
}
if (test1 < (test2 / (lasty - cury) * (lastx - curx))) {
hits++;
}
}
return ((hits & 1) != 0);
}
@Override
public String toString() {
if (npoints == 0)
return null;
String s = "";
for (int i = 0; i < xpoints.length; i++) {
s += "(" + xpoints[i] + "," + ypoints[i] + ") ";
}
return s;
}
public float[] getPoints() {
int size = npoints * 4;
float[] points = new float[size];
int j = 1;
if (size == 0 || xpoints == null || ypoints == null)
return null;
points[0] = xpoints[0];
points[1] = ypoints[0];
for (int i = 2; i < points.length - 2; i += 4) {
points[i] = xpoints[j];
points[i + 1] = ypoints[j];
points[i + 2] = xpoints[j];
points[i + 3] = ypoints[j];
j++;
}
poi
```
0
0
复制全文
相关推荐










