import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
import java.io.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.rms.*;
public class SOCanvas extends GameCanvas implements Runnable {
private Random rand;
private Display display;
private boolean sleeping;
private long frameDelay;
private LayerManager layers;
private Image splash;
private Image background;
private Image smallCar;
private MovingSprite playerSprite;
private MovingSprite[] blobboSprite = new MovingSprite[3];
private MovingSprite[] jellySprite = new MovingSprite[3];
private MovingSprite[] timmySprite = new MovingSprite[3];
private MovingSprite[] missileSprite = new MovingSprite[10];
private Sprite[] explosionSprite = new Sprite[3];
private Player musicPlayer;
private Player explosionPlayer;
private Player gameoverPlayer;
private boolean gameOver;
private int score, carsLeft;
private int[] hiScores = new int[5];
public SOCanvas(Display d) {
super(true);
display = d;
// Initialize the random number generator
rand = new Random();
// Set the frame rate (30 fps)
frameDelay = 33;
}
public void start() {
// Set the canvas as the current screen
display.setCurrent(this);
// Read the high scores
readHiScores();
// Initialize the background and small car images
try {
splash = Image.createImage("/Splash.png");
background = Image.createImage("/StarryNight.png");
smallCar = Image.createImage("/SmallCar.png");
}
catch (IOException e) {
System.err.println("Failed loading images!");
}
// Initialize the sprites
try {
// Create the player car sprite
playerSprite = new MovingSprite(Image.createImage("/Car.png"), 0, 0,
MovingSprite.BA_STOP, this);
playerSprite.setVisible(false);
int sequence5[] = { 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 3, 3, 2, 2, 1, 1 };
int sequence3[] = { 0, 0, 0, 1, 1, 1, 2, 2, 2, 1, 1, 1 };
for (int i = 0; i < 3; i++) {
// Create the Blobbo alien sprites
blobboSprite[i] = new MovingSprite(Image.createImage("/Blobbo.png"), 20, 21, 3, 2,
MovingSprite.BA_BOUNCE, this);
blobboSprite[i].setFrameSequence(sequence5);
blobboSprite[i].setVisible(false);
// Create the Jelly alien sprites
jellySprite[i] = new MovingSprite(Image.createImage("/Jelly.png"), 21, 21, 1, 4,
MovingSprite.BA_BOUNCE, this);
jellySprite[i].setFrameSequence(sequence3);
jellySprite[i].setVisible(false);
// Create the Timmy alien sprites
timmySprite[i] = new MovingSprite(Image.createImage("/Timmy.png"), 21, 11, 5, 0,
MovingSprite.BA_WRAP, this);
timmySprite[i].setFrameSequence(sequence3);
timmySprite[i].setVisible(false);
// Create the explosion sprites
explosionSprite[i] = new Sprite(Image.createImage("/Explosion.png"), 21, 21);
explosionSprite[i].setVisible(false);
}
// Create the missile sprites
for (int i = 0; i < 10; i++) {
missileSprite[i] = new MovingSprite(Image.createImage("/Missiles.png"), 11, 11, 0, 0,
MovingSprite.BA_HIDE, this);
missileSprite[i].setVisible(false);
}
}
catch (IOException e) {
System.err.println("Failed loading images!");
}
// Create the layer manager and add the sprites to it
layers = new LayerManager();
layers.append(playerSprite);
for (int i = 0; i < 3; i++) {
layers.append(blobboSprite[i]);
layers.append(jellySprite[i]);
layers.append(timmySprite[i]);
layers.append(explosionSprite[i]);
}
for (int i = 0; i < 10; i++) {
layers.append(missileSprite[i]);
}
// Initialize the music and wave players
try {
InputStream is = getClass().getResourceAsStream("Music.mid");
musicPlayer = Manager.createPlayer(is, "audio/midi");
musicPlayer.prefetch();
musicPlayer.setLoopCount(-1);
is = getClass().getResourceAsStream("Explosion.wav");
explosionPlayer = Manager.createPlayer(is, "audio/X-wav");
explosionPlayer.prefetch();
is = getClass().getResourceAsStream("GameOver.wav");
gameoverPlayer = Manager.createPlayer(is, "audio/X-wav");
gameoverPlayer.prefetch();
}
catch (IOException ioe) {
}
catch (MediaException me) {
}
// Show the splash screen by indicating a game over
gameOver = true;
// Start the animation thread
sleeping = false;
Thread t = new Thread(this);
t.start();
}
public void stop() {
// Close the music and wave players
musicPlayer.close();
explosionPlayer.close();
gameoverPlayer.close();
// Stop the animation
sleeping = true;
// Write the high scores
writeHiScores();
}
public void run() {
Graphics g = getGraphics();
// The main game loop
while (!sleeping) {
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
newGame();
// The game is over, so don't update anything
return;
}
// Process user input to move the car and fire missiles
int keyState = getKeyStates();
if ((keyState & LEFT_PRESSED) != 0) {
playerSprite.setXSpeed(-2);
}
else if ((keyState & RIGHT_PRESSED) != 0) {
playerSprite.setXSpeed(4);
}
if ((keyState & FIRE_PRESSED) != 0) {
// Play a fire sound
try {
Manager.playTone(ToneControl.C4 + 12, 10, 100);
}
catch (Exception e) {
}
addMissile(playerSprite);
}
playerSprite.update();
// Update the alien and explosion sprites
for (int i = 0; i < 3; i++) {
if (blobboSprite[i].isVisible()) {
blobboSprite[i].update();
blobboSprite[i].nextFrame();
}
if (jellySprite[i].isVisible()) {
jellySprite[i].update();
jellySprite[i].nextFrame();
}
if (timmySprite[i].isVisible()) {
timmySprite[i].update();
timmySprite[i].nextFrame();
}
if (explosionSprite[i].isVisible()) {
if (explosionSprite[i].getFrame() < 3)
explosionSprite[i].nextFrame();
else
explosionSprite[i].setVisible(false);
}
}
// Check for collisions and update the missile sprites
for (int i = 0; i < 10; i++) {
if (missileSprite[i].isVisible()) {
// Is the missile a player missile?
if (missileSprite[i].getFrame() == 0) {
for (int j = 0; j < 3; j++) {
// Did the missile hit a Blobbo alien?
if (blobboSprite[j].isVisible())
if (missileSprite[i].collidesWith(blobboSprite[j], false)) {
// Play a destruction sound
try {
Manager.playTone(ToneControl.C4 - 6, 100, 100);
}
catch (Exception e) {
}
// Create an explosion
addExplosion(blobboSprite[j]);
// Hide the sprites and update the score
blobboSprite[j].setVisible(false);
missileSprite[i].setVisible(false);
score += 10;
break;
}
// Did the missile hit a
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
《J2ME手机游戏编程入门》一书的配套源码----【下载不扣分,回帖加1分,欢迎下载,童叟无欺】 (388个子文件)
HSCanvas.java.bak 18KB
WCanvas.java.bak 8KB
build.bat 644B
build.bat 644B
build.bat 642B
build.bat 640B
build.bat 640B
build.bat 640B
build.bat 640B
build.bat 640B
build.bat 640B
build.bat 640B
build.bat 640B
build.bat 640B
build.bat 636B
build.bat 636B
build.bat 630B
build.bat 630B
build.bat 630B
run.bat 124B
run.bat 124B
run.bat 122B
run.bat 121B
run.bat 121B
run.bat 121B
run.bat 120B
run.bat 120B
run.bat 120B
run.bat 120B
run.bat 120B
run.bat 120B
run.bat 117B
run.bat 116B
run.bat 111B
run.bat 111B
run.bat 110B
HSCanvas.class 15KB
HSCanvas.class 15KB
HSCanvas.class 14KB
HSCanvas.class 14KB
SOCanvas.class 11KB
SOCanvas.class 9KB
SOCanvas.class 9KB
SOCanvas.class 8KB
WCanvas.class 7KB
WCanvas.class 7KB
C4Canvas.class 6KB
HCanvas.class 6KB
C4Canvas.class 5KB
HCanvas.class 5KB
WCanvas.class 5KB
UFOCanvas.class 5KB
HCanvas.class 5KB
WCanvas.class 5KB
HCanvas.class 4KB
UFOCanvas.class 4KB
UFOCanvas.class 3KB
UFOCanvas.class 3KB
LHCanvas.class 3KB
LHCanvas.class 3KB
UFOCanvas.class 2KB
UFOCanvas.class 2KB
SSCanvas.class 2KB
MovingSprite.class 2KB
MovingSprite.class 2KB
LighthouseMIDlet.class 2KB
Connect4MIDlet.class 2KB
SSCanvas.class 2KB
C4Server.class 2KB
LHServer.class 2KB
C4Client.class 2KB
LHClient.class 2KB
LighthouseMIDlet.class 2KB
Connect4MIDlet.class 2KB
MovingSprite.class 2KB
MovingSprite.class 2KB
C4Server.class 2KB
SCanvas.class 2KB
SCanvas.class 2KB
LHServer.class 2KB
C4State.class 2KB
C4Client.class 2KB
LHClient.class 2KB
SCanvas.class 2KB
SCanvas.class 2KB
ChaseSprite.class 2KB
ChaseSprite.class 1KB
SkeletonMIDlet.class 1KB
SCCanvas.class 1KB
SkeletonMIDlet.class 1KB
HighSeasMIDlet.class 1KB
SpaceOutMIDlet.class 1KB
HighSeasMIDlet.class 1KB
SpaceOutMIDlet.class 1KB
WandererMIDlet.class 1KB
WandererMIDlet.class 1KB
UFOMIDlet.class 1KB
UFOMIDlet.class 1KB
UFOMIDlet.class 1KB
HenwayMIDlet.class 1KB
共 388 条
- 1
- 2
- 3
- 4
资源评论
- mi_LOVE_su2012-06-05还不错,但是后面有些就没源码
- 打不死的迪仔2013-08-29还不错,后面的代码咋就没啦
Star_of_Java
- 粉丝: 13
- 资源: 137
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功