📄 ship.java
字号:
import net.jscience.math.kvm.MathFP;
import javax.microedition.lcdui.*;
import java.io.IOException;
/**
* Class to handle the drawing and movement of a little spaceship. Image is
* loaded in a Sprite from the ship.png file.
* @author Martin J. Wells
*/
public class Ship
{
private static int SHIP_FRAME_WIDTH = 16; // size of the ship in pixels
private static int SHIP_FRAME_HEIGHT = 16;
// Some pre-calculated values to speed things up.
public static final int FP_PI2 = MathFP.mul(MathFP.PI, MathFP.toFP(2));
public static final int FP_DEGREES_PER_RAD = MathFP.div(MathFP.toFP(360),
FP_PI2);
public static final int FP_22P5 = MathFP.toFP("22.5");
private static ImageSet shipImageSet; // the one and only imageset
private int xFP; // x position (as a MathFP)
private int yFP; // y position (as a MathFP)
private int direction; // current direction in degrees
private int xVelFP; // x velocity (as a MathFP)
private int yVelFP; // y velocity (as a MathFP)
private int maxVelFP = MathFP.toFP("2");
private int thrustFP = MathFP.toFP("0.2");
private Sprite shipSprite;
private int worldWidth; // size of the world (so we can wrap)
private int worldHeight;
private GameScreen gameScreen;
/**
* Constructs a ship starting at a given location.
* @param x The starting x position.
* @param y The starting y position.
* @param gs The GameScreen this Ship belongs to (used to call back for
* collision detection)
*/
public Ship(int x, int y, GameScreen gs)
{
gameScreen = gs;
// Load up the images for the Ship. Note that shipImageSet is a static
// so this code will only be run once (after that shipImageSet will not
// be null.
if (shipImageSet == null)
{
// Set the width and height of the world (we only do this once as
// well).
worldWidth = gs.getWidth();
worldHeight = gs.getHeight();
try
{
Image[] frames = ImageSet.extractFrames(Image.createImage("/ship.png"),
0, 0, 4, 4,
SHIP_FRAME_WIDTH,
SHIP_FRAME_HEIGHT);
shipImageSet = new ImageSet(1);
shipImageSet.addState(frames, 0);
}
catch (IOException ioe)
{
System.out.println("unable to load image");
}
}
// Create a Sprite for this instance of the Ship.
shipSprite = new Sprite(shipImageSet, 0, 0);
// Set the starting position.
xFP = MathFP.toFP(x);
yFP = MathFP.toFP(y);
// Set a random direction.
direction = Tools.getRand(0, 359) / 23 * 23;
}
/**
* Calculates the correct frame based on the Ship's direction and then draws
* that frame in on the screen at the Ship's current position.
* @param graphics The graphics context upon which to draw.
*/
public void render(Graphics graphics)
{
// Calculate the frame by dividing the direction by 22.5 degrees (360
// degrees divided by 16 frames equals 22.5).
int frame = MathFP.toInt(MathFP.div(MathFP.toFP(direction), FP_22P5));
shipSprite.setFrame(frame);
shipSprite.draw(graphics, MathFP.toInt(xFP), MathFP.toInt(yFP));
}
/**
* Movement for the Ship. Collision detection is done by asking the
* GameScreen class through the checkCollision method. This is done because
* the GameScreen is the one with the array of Ships.
*/
public void cycle()
{
// move the ship according to its current direction (in radians)
int dirRadians = MathFP.div(MathFP.toFP(direction), FP_DEGREES_PER_RAD);
int xAccFP = MathFP.mul(thrustFP, MathFP.cos(dirRadians));
int yAccFP = MathFP.mul(thrustFP, MathFP.sin(dirRadians));
xVelFP = MathFP.add(xVelFP, xAccFP);
yVelFP = MathFP.add(yVelFP, yAccFP);
// Cap the velocity to a controllable level
if (xVelFP > maxVelFP)
xVelFP = maxVelFP;
else if (xVelFP < -maxVelFP) xVelFP = -maxVelFP;
if (yVelFP > maxVelFP)
yVelFP = maxVelFP;
else if (yVelFP < -maxVelFP) yVelFP = -maxVelFP;
int lastXFP = xFP;
xFP = MathFP.add(xFP, xVelFP);
// Test for collisions, after x movement.
if (gameScreen.checkCollision(this))
{
xVelFP = MathFP.mul(xVelFP, MathFP.toFP("-1"));
xFP = MathFP.add(lastXFP, xVelFP);
}
int lastYFP = yFP;
yFP = MathFP.add(yFP, yVelFP);
// Test for collisions, after y movement.
if (gameScreen.checkCollision(this))
{
yVelFP = MathFP.mul(yVelFP, MathFP.toFP("-1"));
yFP = MathFP.add(lastYFP, yVelFP);
}
// Check the position and wrap around if we have to.
if (MathFP.toInt(xFP) < 0) xFP = MathFP.toFP(worldWidth - 1);
if (MathFP.toInt(xFP) > worldWidth) xFP = MathFP.toFP(0);
if (MathFP.toInt(yFP) < 0) yFP = MathFP.toFP(worldHeight - 1);
if (MathFP.toInt(yFP) > worldHeight) yFP = MathFP.toFP(0);
}
/**
* @return The x position of the Ship.
*/
public int getX()
{
return MathFP.toInt(xFP);
}
/**
* @return The y position of the Ship.
*/
public int getY()
{
return MathFP.toInt(yFP);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -