【发布时间】:2016-11-24 23:30:36
【问题描述】:
我正在为我的学校制作游戏,但我不能让静态最终 int 得到一个随机数,游戏是小行星,小行星是用多边形制作的,我可以用这个改变多边形的点:
private static final int NUMBER_OF_POINTS = 5;
如果我放了
private static final int NUMBER_OF_POINTS = 3;
它会形成一个三角形,但我希望在游戏开始时它会通过在以下位置生成随机数来生成随机数字:
private static final int NUMBER_OF_POINTS = 3;
这是我做的,但它不起作用:
private static final int NUMBER_OF_POINTS = 3 + (int)(Math.random() * (5 - 3) + 1));
这是完整的代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package game;
import java.awt.Polygon;
import java.util.Random;
public enum AsteroidSize {
/**
* Small Asteroids have a radius of 15, and are worth 150 points.
*/
XtraSmall(5.0, 150),
/**
* Small Asteroids have a radius of 15, and are worth 100 points.
*/
Small(15.0, 100),
/**
* Medium asteroids have a radius of 25, and are worth 50 points.
*/
Medium(25.0, 50),
/**
* Large asteroids have a radius of 40, and are worth 20 points.
*/
Large(40.0, 20),
/**
* XtraLarge asteroids have a radius of 40, and are worth 10 points.
*/
XtraLarge(50.0, 10);
/**
* The number of points on the Asteroid.
*/
private static final int NUMBER_OF_POINTS = 5;
/**
* The polygon for this type of Asteroid.
*/
public final Polygon polygon;
/**
* The radius of this type of Asteroid.
*/
public final double radius;
/**
* The number of points earned for killing this type of Asteroid.
*/
public final int killValue;
/**
* Creates a new type of Asteroid.
* @param radius The radius.
* @param value The kill value.
*/
private AsteroidSize(double radius, int value) {
this.polygon = generatePolygon(radius);
this.radius = radius + 1.0;
this.killValue = value;
}
/**
* Generates a regular polygon of size radius.
* @param radius The radius of the Polygon.
* @return The generated Polygon.
*/
private static Polygon generatePolygon(double radius) {
//Create an array to store the coordinates.
int[] x = new int[NUMBER_OF_POINTS];
int[] y = new int[NUMBER_OF_POINTS];
//Generate the points in the polygon.
double angle = (2 * Math.PI / NUMBER_OF_POINTS);
for(int i = 0; i < NUMBER_OF_POINTS; i++) {
x[i] = (int) (radius * Math.sin(i * angle));
y[i] = (int) (radius * Math.cos(i * angle));
}
//Create a new polygon from the generated points and return it.
return new Polygon(x, y, NUMBER_OF_POINTS);
}
}
【问题讨论】:
-
定义“不起作用”。您预计会发生什么,以及会发生什么?
-
我希望生成的小行星有不同的形状,比如如果我放 3 个点,我会看到一个三角形,或者如果我放 4 个点,我会看到一个矩形,相反,如果我开始,它不会显示数字射击我因为碰撞而得到积分,但数字只是没有出现
-
那么它和随机静态final int没有任何关系吗?你问为什么屏幕上没有出现数字?好吧,您没有发布任何与在屏幕上显示数字相关的代码,因此很难提供帮助。