javaのミニゲームの開発、完全版(初心者は頑張っています)

13792 ワード

会社で勉强していますが、仕事によって小さいゲームを书きましたが、最终的に実现したいのは容认できません。机能も完全に私の要求を満たしていません。
package comp.example.amu;
public class Scrore Thread extens Thread{
GameView gameView;//GameView    
int highest=60;  //   ,       

public boolean flag=true;  //          

public ScoreThread(GameView gameView) {    //   
    this.gameView=gameView;
    // TODO Auto-generated constructor stub
}

public void run() {

    while(flag){
        //gameView.score++;//    

        if(gameView.score==highest){  //            
            gameView.status=3;   //    
            //gameView.scoreThread.stop();
            gameView.scoreThread.flag=false;   //  TimeThread  
            gameView.coinThread.flag=false;   //  coinThread  

        }

        try {
            Thread.sleep(1000);   //  1S
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
)
/**
  • */package comp.example.amu;
  • import java.security.PublicKey;import java.util.Timer;import java.util.TimeTask;
    import android.annotations.Suppress Lint;import android.co nt.context;import android.graphics.Bitmap;import android.graphics.Bitmap Factory;import android.graphics.C.anvas;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceView;
    public class GameView extens SurfaceView implements SurfaceHolder.Callback{
     public static int run_flag = 0;   //        ,  0   
    
    /*               */
    MainActivity mainActivity;  //activity    
    
    ScoreThread scoreThread;     //TimeThread   
    CoinGoThread   coinThread; //CoinGoThread   
    
    int screenWidth=480;   //   
    int screenHeight=700;//   
    int carWidth=40;  //          
    int carHeight=10;//          
    int bottomSpance=20;//    
    //int coinSize=16;
    int carSpan=5; //    
    int coinSpan=8; //    
    int coinSize=16;//     
    
    int hintWidth=100;//    
    int hintHeight=20;//    
    
    /*            0->    ,1->   ,2->    ,3->    */
    int status=0;  
    int score=0;//  
    int coinx;//   X  
    int coiny;//   Y  
    
    int carx;//         X  
    int cary;//         Y  
    
    int richManX;//   X  
    int richManY; //   Y  
    int richManSpan=5;  //       
    
    int richManWidth=20;  //     
    int richManHeight=20;  //     
    
    int scoreWith=32;//       
    
    Bitmap [] iscore=new Bitmap[10]; //        
    Bitmap icoin;//       
    Bitmap icar;//     
    Bitmap iRichMan;  //     
    Bitmap ibegin; //       
    Bitmap igameover;//         
    Bitmap iwin;//         
    Bitmap iexit;//         
    Bitmap ireplay;//         
    GameViewDrawThread gameViewDrawThread;   //    ,        
    
    int direction=0;
    
    private int backSize=16;
    
    private Bitmap iback;
    
    private int x;
    
    public GameView(MainActivity mainActivity) {
        super(mainActivity);
        getHolder().addCallback(this);  //    
        this.mainActivity=mainActivity;
        initBitmap();  //         
        gameViewDrawThread=new GameViewDrawThread(this);//        
    
    }
    
    public void initBitmap() {//          
    
            /* score       ===========  */
        iscore[0] = BitmapFactory.decodeResource(getResources(), R.drawable.d0);
        iscore[1] = BitmapFactory.decodeResource(getResources(), R.drawable.d1);
        iscore[2] = BitmapFactory.decodeResource(getResources(), R.drawable.d2);
        iscore[3] = BitmapFactory.decodeResource(getResources(), R.drawable.d3);
        iscore[4] = BitmapFactory.decodeResource(getResources(), R.drawable.d4);
        iscore[5] = BitmapFactory.decodeResource(getResources(), R.drawable.d5);
        iscore[6] = BitmapFactory.decodeResource(getResources(), R.drawable.d6);
        iscore[7] = BitmapFactory.decodeResource(getResources(), R.drawable.d7);
        iscore[8] = BitmapFactory.decodeResource(getResources(), R.drawable.d8);
        iscore[9] = BitmapFactory.decodeResource(getResources(), R.drawable.d9);
        /* score       ===========  */
        iback = BitmapFactory.decodeResource(getResources(), R.drawable.back);
        /*     ,    ,  ,                ===========  */
        ibegin = BitmapFactory.decodeResource(getResources(), R.drawable.begin);
        igameover = BitmapFactory.decodeResource(getResources(), R.drawable.gameover);
        iwin = BitmapFactory.decodeResource(getResources(), R.drawable.win);
        iexit = BitmapFactory.decodeResource(getResources(), R.drawable.exit);
        ireplay = BitmapFactory.decodeResource(getResources(), R.drawable.replay); 
        /*     ,    ,  ,                ===========  */
    
               //         
        icoin=BitmapFactory.decodeResource(getResources(), R.drawable.coin);
              //         
        icar=BitmapFactory.decodeResource(getResources(), R.drawable.car);
    
                //         
    
        /*  drawable               ,    ,  R.java        id,       ,*/
        iRichMan=BitmapFactory.decodeResource(getResources(), R.drawable.richman);
    
        initCoinAndCar();  //              
    
    }
    
    public void initCoinAndCar() {//              
    
        coiny= 10;  /*screenHeight-bottomSpance-carHeight-coinSize;*/   //     y  
        //   x   10~screenWith      
        coinx=screenWidth/2-carWidth/2;   //(int) ((screenWidth-10)*Math.random())+10;  
    
        carx=screenWidth/2-carWidth/2;  //     x  
        cary=screenHeight-bottomSpance-carHeight;//     y  
    
        richManX=screenWidth/2-carWidth/2-richManWidth-coinSize/2;  //   X      
        richManY=10-richManHeight/2;  //   Y  
    
    }
    /*replay()                  */
    public void replay() {
        if(status==2 || status==3){
            coinSpan=8;  //        
            //2->    ,3->    */
            initCoinAndCar();//              
            //score=0;  //    ,                   
            run_flag=0;  //
    
        }
    
    }
    
    public void Automatic_replay() {  //              ,      ,
                                        //         
    
    /*            ,             */
        if(score>=2)
            coinSpan=10;
        if(score>=4)
            coinSpan=20;
        if(score>=6)
            coinSpan=30;
        if(score>=8)            
            coinSpan=40;
    
        if(score>=10)
            coinSpan=45;
        if(score>=15)
            coinSpan=50;
        if(score>=20)
            coinSpan=60;
    
        //coiny= 10;  /*screenHeight-bottomSpance-carHeight-coinSize;*/   //     y  
        //   x   10~screenWith      
        int random=(int) ((screenWidth-10)*Math.random())+10;
    
        while(random>460 ||random<20){
            random=(int) ((screenWidth-10)*Math.random())+10; 
        }
    
        //richManX=random-richManWidth-coinSize/2;
    
        richManY=10-richManHeight/2;
        coiny= 20;
        //coinx=random;
        coinx=coinThread.father.richManX+15;
    
        carx=x;///screenWidth/2-carWidth/2;  //     x  
        cary=screenHeight-bottomSpance-carHeight;//     y  
    
            //initCoinAndCar();//              
            coinThread.flag=true;  
            run_flag=0;
            status=0;    //
            status=1;
    
    }
    
    /*         */
    
     @SuppressLint("WrongCall") public void  doDraw(final Canvas canvas) {
        //1.    
        super.onDraw(canvas);
        //   
        int cols=screenWidth/backSize+((screenWidth%backSize==0)?0:1);//  
        int rows=screenHeight/backSize+((screenHeight%backSize==0)?0:1);//  
        for(int i=0;i    ,1->   ,2->    ,3->    */
        if(status==0){  //        ,      
            canvas.drawBitmap(ibegin, screenWidth/2-hintWidth/2, 
                    screenHeight/2-hintHeight/2, null); 
        }
    
        //      
        if(status==2){    //        ,      
            canvas.drawBitmap(igameover,screenWidth/2-hintWidth/2,
                screenHeight/2-hintHeight/2, null);
    
        }  
    
        //      
        if(status==3){  //        ,      
            canvas.drawBitmap(iwin,screenWidth/2-hintWidth/2,
                screenHeight/2-hintHeight/2, null);
    
        }  
    
        //      ,    
        canvas.drawBitmap(iexit,screenWidth-50,screenHeight/2,null);        
        //                  
        if(status==2||status==3){//                  
            canvas.drawBitmap(ireplay ,10,screenHeight/2,null);         
        }   
    }
    
    @Override
    //           
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
         x = (int) event.getX();   //      x  
        int y = (int) event.getY();   //      y  
    
        if(xscreenWidth-50  && 
                yscreenHeight/2){ //      
            System.exit(0);  //    
        }
    
        if(status==0){   //        ,        ,     
            status=1;   //        
            scoreThread=new ScoreThread(this);  //        
            coinThread=new CoinGoThread(this);  //    CoinGoThread  
            scoreThread.start();   //    
            coinThread.start();
    
        }
    
        else if(status==1){  //        
            /*         */
            switch (event.getAction()) {
    
            /*   */
            case MotionEvent.ACTION_DOWN  :
    
                carx=x;
    
                break;
            /*    */
            case MotionEvent.ACTION_MOVE  :
    
                carx=x;
    
                break;  
    
                /*    */
            case MotionEvent.ACTION_UP  :
    
                carx=x;
    
                break;
    
            default:
                break;
            }
            //carx=x;  //       
    
        }
        else if(status==2 || status==3){  //            
            if(x<45 && x>0 && yscreenHeight/2){//      
                status=0;
                score=0;  //                        
                replay();  //            
    
            }
    
        }
    
        return super.onTouchEvent(event);
    }
    
    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
    
    public GameView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }
    
    @Override     //     Surface                    
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
    
    }
    
    @Override  // Surface    ,             
    public void surfaceCreated(SurfaceHolder arg0) {  //          
    
        this.gameViewDrawThread.flag=true;
        gameViewDrawThread.start();
        // TODO Auto-generated method stub
    
    }
    
    @Override  // Surface                       
    public void surfaceDestroyed(SurfaceHolder arg0) {  //         
        boolean retry=true;
        this.gameViewDrawThread.flag=false;
        while(retry){
            try {
                gameViewDrawThread.join();
                retry=false;
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
        // TODO Auto-generated method stub
    
    }
    )
    package comp.example.amu;
    import android.graphics.C.anvas;import android.view.SurfaceHolder;
    public class GameView DrawThread extends Thread{bolean flagag=true;intsleepepSpan=100;GameView gameView;SurfaceHorfaceHolder;publicGameView DrawThread(GameView gameVieew=100;GamemememememeViewawawawawawawawawawawaw=ble=blaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaward)))));while(this.flags){c=null;try{/キャンバス全体をロックし、メモリの要求が高い場合、パラメータはnull c=this.surfaceHolder.lockCanvas(null)ではないことを提案します。
                synchronized (this.surfaceHolder) {
                    gameView.doDraw(c);//  
                }
            } finally {
                if (c != null) {
                    //    
                    this.surfaceHolder.unlockCanvasAndPost(c);
                }
            }
            try{
                Thread.sleep(sleepSpan);//       
            }
            catch(Exception e){
                e.printStackTrace();//      
            }
        }
    }
    }(資質代行
    package comp.example.amu;
    import android.os.Bundle;import android.ap.Activity;import android.view.Menu;import android.view.Window;import android.view.Window Manager;import android.widget.Imaget View;
    public class MainActivity extends Activity{
    GameView gameView; //GameView    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        //requestWindowFeature(Window.FEATURE_NO_TITLE);  ///      
       // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                            //WindowManager.LayoutParams.FLAG_FULLSCREEN);//    
        gameView=new GameView(this);    //  GameView
        setContentView(gameView);
    
        WindowManager wm = this.getWindowManager();
    
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight(); 
    
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    )
    package comp.example.amu;
    public class Scrore Thread extens Thread{
    GameView gameView;//GameView    
    int highest=60;  //   ,       
    
    public boolean flag=true;  //          
    
    public ScoreThread(GameView gameView) {    //   
        this.gameView=gameView;
        // TODO Auto-generated constructor stub
    }
    
    public void run() {
    
        while(flag){
            //gameView.score++;//    
    
            if(gameView.score==highest){  //            
                gameView.status=3;   //    
                //gameView.scoreThread.stop();
                gameView.scoreThread.flag=false;   //  TimeThread  
                gameView.coinThread.flag=false;   //  coinThread  
    
            }
    
            try {
                Thread.sleep(1000);   //  1S
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
    }
    )
    作者:ハハハ 
    ソース:慕課網
    本文はオリジナルで慕授業ネットで発表します。転載は出所を明記してください。ご協力ありがとうございます。