打砖块游戏问题
今天写了个打砖块游戏的程序,编译能通过,但不能在模拟器显示,请大家指点一二import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
public class BlockBall extends MIDlet {
private Display display;
private GameCanvas canvas;
public BlockBall() {
display=Display.getDisplay(this);
canvas=new GameCanvas();
}
protected void startApp(){
display.setCurrent(canvas);
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg0){
}
}
import *;
import javax.microedition.lcdui.*;
public class GameCanvas extends Canvas implements Runnable{
private int state;
private final static int active=1;
private final static int clear=2;
private final static int over=3;
private final int Block_v=10;
private final int Block_h=2;
private final int Block_width=this.getWidth()/Block_h;
private final int Block_height=Block_width/2;
private boolean block[][]=new boolean[Block_h][Block_v];
private int blockCount=Block_h*Block_v;
private final int Ball_width=5;
private final int Ball_height=5;
private int Ball_speed=6;
private int Ballx=0;
private int Bally=getHeight()-Ball_height;
private final int Bar_width=12;
private final int Bar_height=4;
private int Bar_speed=0;
private int Barx=0;
private int Bary=getHeight()-Bar_height;
private Image Block=null;
private Image Bar=null;
private Image Ball=null;
public GameCanvas() {
new Thread(this).start();
for(int i=0;i<Block_h;i++){
for(int j=0;j<Block_v;j++)
block[i][j]=true;
}
try{
Block=Image.createImage("/block.png");
Ball=Image.createImage("/ball.png");
Bar=Image.createImage("/bar.png");
}
catch(IOException e){
e.printStackTrace();
}
state=active;
}
public void paint(Graphics g){
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
for(int i=0;i<Block_h;i++){
for(int j=0;j<Block_v;j++){
while(block[i][j])
g.drawImage(Block,i*Block_width,(j+1)*Block_height,Graphics.TOP|Graphics.LEFT);
}
}
g.drawImage(Ball,Ballx,Bally,Graphics.TOP|Graphics.LEFT);
g.drawImage(Bar,Barx,Bary,Graphics.TOP|Graphics.LEFT);
}
public void run(){
moveBall();
moveBar();
repaint();
try{
Thread.sleep(100);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
public void moveBall(){
Ballx+=Ball_speed;
Bally+=Ball_speed;
if(Ballx<0){
Ballx=0;
Ball_speed*=-1;
}
if(Ballx>getWidth()-Ball_width){
Ballx=getWidth()-Ball_width;
Ball_speed*=-1;
}
if(Bally<0){
Bally=0;
Ball_speed*=-1;
}
if(Bally>getHeight()-Ball_height)
state=over;
if(Bally+Ball_height>Bary&&Ballx+Ball_width>Barx&&Ballx<Barx+Bar_width){
Bally=getHeight()-Bar_height;
Bally*=-1;
if(Bar_speed<0)
Ball_speed-=2;
else if(Bar_speed>0)
Ball_speed+=2;
}
for(int i=0;i<Block_h;i++){
for(int j=0;j<Block_v;j++){
if(block[i][j]){
if(Ballx+Ball_width>i*Block_width&&Ballx<(i+1)*Block_width){
if(Bally+Ball_height>(i+1)*Block_height&&Bally<(i+2)*Block_height){
block[i][j]=false;
Bally*=-1;
blockCount--;
if(blockCount==0)
state=clear;
}
}
}
}
}
repaint();
}
public void moveBar(){
Barx+=Bar_speed;
if(Barx<0)
Barx=0;
else if(Barx>getWidth()-Bar_width)
Barx=getWidth()-Bar_width;
}
public void keyPressed(int key){
if(state==active){
if(getGameAction(key)==Graphics.LEFT)
Bar_speed=-5;
else if(getGameAction(key)==Graphics.RIGHT)
Bar_speed=5;
repaint();
}
}
public void keyReleased(int key){
Bar_speed=0;
}
}