📄 hscanvas.java
字号:
// Start a new game
newGame();
// The game is over, so don't update anything
return;
}
// Process user input to move the water layer and animate the player
int keyState = getKeyStates();
int xMove = 0, yMove = 0;
if ((keyState & LEFT_PRESSED) != 0) {
xMove = -4;
playerSprite.setFrame(3);
}
else if ((keyState & RIGHT_PRESSED) != 0) {
xMove = 4;
playerSprite.setFrame(1);
}
if ((keyState & UP_PRESSED) != 0) {
yMove = -4;
playerSprite.setFrame(0);
}
else if ((keyState & DOWN_PRESSED) != 0) {
yMove = 4;
playerSprite.setFrame(2);
}
if (xMove != 0 || yMove != 0) {
layers.setViewWindow(xView + xMove, yView + yMove, getWidth(),
getHeight() - infoBar.getHeight());
playerSprite.move(xMove, yMove);
}
// Check for a collision with the player and the land tiled layer
if (playerSprite.collidesWith(landLayer, true)) {
// Restore the original view window and player sprite positions
layers.setViewWindow(xView, yView, getWidth(),
getHeight() - infoBar.getHeight());
playerSprite.move(-xMove, -yMove);
}
else {
// If there is no collision, commit the changes to the view window position
xView += xMove;
yView += yMove;
}
for (int i = 0; i < 2; i++) {
// Update the pirate and barrel sprites
pirateSprite[i].update();
barrelSprite[i].update();
// Check for a collision with the player and the pirate sprite
if (playerSprite.collidesWith(pirateSprite[i], true)) {
// Play a wave sound for rescuing a pirate
try {
rescuePlayer.start();
}
catch (MediaException me) {
}
// Increase the number of pirates saved
piratesSaved++;
// Randomly place the pirate in a new location
placeSprite(pirateSprite[i], landLayer);
}
// Check for a collision with the player and the barrel sprite
if (playerSprite.collidesWith(barrelSprite[i], true)) {
// Play a tone sound for gaining energy from a barrel
try {
Manager.playTone(ToneControl.C4 + 12, 250, 100);
}
catch (MediaException me) {
}
// Increase the player's energy
energy = Math.min(energy + 5, 45);
// Randomly place the barrel in a new location
placeSprite(barrelSprite[i], landLayer);
}
}
for (int i = 0; i < 5; i++) {
// Update the mine and squid sprites
mineSprite[i].update();
squidSprite[i].update();
// Check for a collision with the player and the mine sprite
if (playerSprite.collidesWith(mineSprite[i], true)) {
// Play a wave sound for hitting a mine
try {
minePlayer.start();
}
catch (MediaException me) {
}
// Decrease the player's energy
energy -= 10;
// Randomly place the mine in a new location
placeSprite(mineSprite[i], landLayer);
}
// Check for a collision with the player and the squid sprite
if (playerSprite.collidesWith(squidSprite[i], true)) {
// Play a tone sound for hitting a squid
try {
Manager.playTone(ToneControl.C4, 250, 100);
}
catch (MediaException me) {
}
// Decrease the player's energy
energy -= 5;
}
}
// Update the enemy ship sprite
enemyShipSprite.update();
// Check for a collision with the player and the enemy ship sprite
if (playerSprite.collidesWith(enemyShipSprite, true)) {
// Play a wave sound for hitting the enemy ship
try {
minePlayer.start();
}
catch (MediaException me) {
}
// Decrease the player's energy
energy -= 10;
}
// Check for a game over
if (energy <= 0) {
// Stop the music
try {
musicPlayer.stop();
}
catch (MediaException me) {
}
// Play a wave sound for the player ship sinking
try {
gameoverPlayer.start();
}
catch (MediaException me) {
}
// Hide the player ship sprite
playerSprite.setVisible(false);
gameOver = true;
}
// Update the animated water tiles
if (++waterDelay > 3) {
if (++waterTile[0] > 3)
waterTile[0] = 1;
waterLayer.setAnimatedTile(-1, waterTile[0]);
if (--waterTile[1] < 1)
waterTile[1] = 3;
waterLayer.setAnimatedTile(-2, waterTile[1]);
waterDelay = 0;
}
}
private void draw(Graphics g) {
// Draw the info bar with energy and pirate's saved
g.drawImage(infoBar, 0, 0, Graphics.TOP | Graphics.LEFT);
g.setColor(0, 0, 0); // black
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
g.drawString("Energy:", 2, 1, Graphics.TOP | Graphics.LEFT);
g.drawString("Pirates saved: " + piratesSaved, 88, 1, Graphics.TOP | Graphics.LEFT);
g.setColor(32, 32, 255); // blue
g.fillRect(40, 3, energy, 12);
// Draw the layers
layers.paint(g, 0, infoBar.getHeight());
if (gameOver) {
// Draw the game over message and score
g.setColor(255, 255, 255); // white
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
g.drawString("GAME OVER", 90, 40, Graphics.TOP | Graphics.HCENTER);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
if (piratesSaved == 0)
g.drawString("You didn't save any pirates.", 90, 70, Graphics.TOP | Graphics.HCENTER);
else if (piratesSaved == 1)
g.drawString("You saved only 1 pirate.", 90, 70, Graphics.TOP | Graphics.HCENTER);
else
g.drawString("You saved " + piratesSaved + " pirates.", 90, 70, Graphics.TOP |
Graphics.HCENTER);
}
// Flush the offscreen graphics buffer
flushGraphics();
}
private void newGame() {
// Initialize the game variables
gameOver = false;
energy = 45;
piratesSaved = 0;
// Show the player ship sprite
playerSprite.setVisible(true);
// Randomly place the player and adjust the view window
placeSprite(playerSprite, landLayer);
xView = playerSprite.getX() - ((getWidth() - playerSprite.getWidth()) / 2);
yView = playerSprite.getY() - ((getHeight() - playerSprite.getHeight()) / 2);
layers.setViewWindow(xView, yView, getWidth(), getHeight() - infoBar.getHeight());
// Start the music (at the beginning)
try {
musicPlayer.setMediaTime(0);
musicPlayer.start();
}
catch (MediaException me) {
}
}
private void placeSprite(Sprite sprite, TiledLayer barrier) {
// Initially try a random position
sprite.setPosition(Math.abs(rand.nextInt() % barrier.getWidth()) -
sprite.getWidth(), Math.abs(rand.nextInt() % barrier.getHeight()) -
sprite.getHeight());
// Reposition until there isn't a collision
while (sprite.collidesWith(barrier, true)) {
sprite.setPosition(Math.abs(rand.nextInt() % barrier.getWidth()) -
sprite.getWidth(), Math.abs(rand.nextInt() % barrier.getHeight()) -
sprite.getHeight());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -