| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1273 人关注过本帖
标题:打砖块游戏问题
取消只看楼主 加入收藏
wangch364200
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2006-12-12
收藏
 问题点数:0 回复次数:1 
打砖块游戏问题
今天写了个打砖块游戏的程序,编译能通过,但不能在模拟器显示,请大家指点一二

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;
    }
}
搜索更多相关主题的帖子: 砖块游戏 void protected canvas display 
2008-03-30 23:39
wangch364200
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2006-12-12
收藏
得分:0 
不好意思 打扰大家了 这个问题解决了 这个代码错误挺多的 别看了
2008-03-31 14:14
快速回复:打砖块游戏问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034426 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved