我正在学习Java,并且最好的方法是通过手机上的QnA,练习书,游戏编程和应用程序的混合。到目前为止它已经成功但......
在其中一本游戏编程书籍中,我制作了一个创建小行星游戏的java小程序。我正在使用NetBeans,当我运行该文件时,applet窗口打开,空白,并在底部显示Start:Applet未初始化。
在书中,它将此作为初始化事件:
//Primary class for the game
public abstract class Asteroids extends Applet implements Runnable, KeyListener
{
//The Main thread becomes the game loop
Thread gameloop;
//Use this as a double buffer
BufferedImage backbuffer;
//The Main drawing object for the back buffer
Graphics2D g2d;
//toggle for drawing bounding boxes
boolean showBounds = false;
//create the asteroid array
int ASTEROIDS = 20;
Asteroid[] ast = new Asteroid[ASTEROIDS];
//Create the bullet Array
int BULLETS = 10;
Bullet[] bullet = new Bullet[BULLETS];
int currentBullet = 0;
//The Players Ship
Ship ship = new Ship();
//Create the Identity transform(0,0)
AffineTransform identity = new AffineTransform();
//Create a random number generator
Random rand = new Random();
//Applet init event
public void init()
{
//Create the back buffer for smooth graphics
backbuffer = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB);
g2d = backbuffer.createGraphics();
//Set Up the Ship!
ship.setX(320);
ship.setY(240);
//Set the Bullets up!
for (int n = 0; n < BULLETS; n++)
{
bullet[n] = new Bullet();
}
//Creating Asteroids!
for (int n = 0; n < ASTEROIDS; n++)
{
ast[n] = new Asteroid();
ast[n].setRotationVelocity(rand.nextInt(3) + 1);
ast[n].setX((double) rand.nextInt(600) + 20);
ast[n].setY((double) rand.nextInt(440) + 20);
ast[n].setMoveAngle(rand.nextInt(360));
double ang = ast[n].getMoveAngle() - 90;
ast[n].setVelX(calcAngleMoveX(ang));
ast[n].setVelY(calcAngleMoveY(ang));
}
//Start User Input Listener
addKeyListener(this);
这就是它所说的应该启动applet。
我不知道这是否只需要,但还有更多。我已检查,双重检查并三重检查代码,这是正确的。