打砖块的程序无法显示

w051914026 2008-03-30 11:07:14
程序无错误提示,就是不能在模拟器上显示,还请各位指点指点

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 java.io.*;
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;
}
}

...全文
45 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
w051914026 2008-03-31
  • 打赏
  • 举报
回复
是哦 我又放这个低级错误了 谢谢
贝壳鱼 2008-03-31
  • 打赏
  • 举报
回复
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();
}
}


********************************************

别的好像没什么问题, 不过你按键处理写的好怪异, 你是想让得到的game action == Canvas.LEFT 吗?

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧