📄 hcanvas.java
字号:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
import java.io.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
public class HCanvas extends GameCanvas implements Runnable {
private Display display;
private boolean sleeping;
private long frameDelay;
private boolean inputDelay;
private Random rand;
private Image background;
private Sprite sunSprite;
private Sprite houyiSprite;
private Sprite rowsSprite;
private int score;
private Player musicPlayer;
private Player celebratePlayer;
private Player gameoverPlayer;
private boolean gameOver;
private int numLives;
private int bSpeed;
private int sunSpeed;
HCanvas(Display d) {
super(true);
display = d;
// Set the frame rate (30 fps)
frameDelay = 33;
// Clear the input delay
inputDelay = false;
}
public void start() {
// Set the canvas as the current screen
display.setCurrent(this);
// Initialize the random number generator
// Initialize the game variables
score = 0;
gameOver=false;
numLives=12;
inputDelay=false;
// Initialize the background image and chicken and car sprites
try {
background = Image.createImage("/Highway.png");
sunSprite = new Sprite(Image.createImage("/Sun.png"));
sunSprite.setPosition(160, 160);
houyiSprite = new Sprite(Image.createImage("/houyi3.png"));
houyiSprite.setPosition(0, 3);
rowsSprite=new Sprite(Image.createImage("/Row.png"));
rowsSprite.setPosition(6,20);
}
catch (IOException e) {
System.err.println("Failed loading images!");
}
// Initialize the music and wave players
try {
InputStream is = getClass().getResourceAsStream("Music.mid");
musicPlayer = Manager.createPlayer(is, "audio/midi");
musicPlayer.prefetch();
is = getClass().getResourceAsStream("Celebrate.wav");
celebratePlayer = Manager.createPlayer(is, "audio/X-wav");
celebratePlayer.prefetch();
is = getClass().getResourceAsStream("GameOver.wav");
gameoverPlayer = Manager.createPlayer(is, "audio/X-wav");
gameoverPlayer.prefetch();
}
catch (IOException ioe) {
}
catch (MediaException me) {
}
// Start playing the music indefinitely
try {
musicPlayer.setLoopCount(-1);
musicPlayer.start();
}
catch (MediaException me) {
}
// Start the animation thread
sleeping = false;
Thread t = new Thread(this);
t.start();
}
public void stop() {
// Close the music and wave players
musicPlayer.close();
celebratePlayer.close();
gameoverPlayer.close();
// Stop the animation
sleeping = true;
}
public void run() {
Graphics g = getGraphics();
// The main game loop
while (!sleeping) {
bSpeed=5;
if(sunSprite.getY()<-4)
sunSprite.setPosition(160, 160);
else
sunSprite.move(0,-bSpeed);
update();
draw(g);
try {
Thread.sleep(frameDelay);
}
catch (InterruptedException ie) {}
}
}
private void update() {
// Check to see whether the game is being restarted
if (gameOver) {
int keyState = getKeyStates();
if ((keyState & FIRE_PRESSED) != 0) {
// Start a new game
try {
musicPlayer.setMediaTime(0);
musicPlayer.start();
}
catch (MediaException me) {
}
sunSprite.setPosition(160, 160);
gameOver = false;
score = 0;
numLives=12;
inputDelay=false;
}
// The game is over, so don't update anything
return;
}
// Process user input to move the chicken
if (!inputDelay) {
int keyState = getKeyStates();
if ((keyState & RIGHT_PRESSED) != 0)
{ --numLives;
inputDelay = true;}
}
if(inputDelay)
rowsSprite.move(9, 0);
if (rowsSprite.collidesWith(sunSprite, true))
{
// Play a sound for making it safely across
try {
celebratePlayer.start();
}
catch (MediaException me) {}
// Reset the chicken position and increment the score
sunSprite.setPosition(160, 160);
rowsSprite.setPosition(6,20);
score ++;
inputDelay=false;
}
else{ if(rowsSprite.getX()>getWidth())
{ inputDelay=false;
rowsSprite.setPosition(6,20);
}
}
// Check for a game over
if (numLives == 0) {
// Stop the music and play a game over sound
try {
musicPlayer.stop();
gameoverPlayer.start();
}
catch (MediaException me) {
}
gameOver = true;
}
// No need to continue updating the car sprites
return;
}
private void draw(Graphics g) {
// Draw the highway background
g.drawImage(background, 0, 0, Graphics.TOP | Graphics.LEFT);
// Draw the chicken sprite
houyiSprite.paint(g);
sunSprite.paint(g);
rowsSprite.paint(g);
if (gameOver) {
// Draw the game over message and score
g.setColor(255, 255, 255); // white
g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE));
g.drawString("GAME OVER", 90, 40, Graphics.TOP | Graphics.HCENTER);
g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
g.drawString("You scored " + score + " points.", 90, 70, Graphics.TOP |Graphics.HCENTER);
}
// Flush the offscreen graphics buffer
flushGraphics();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -