• 安卓游戏开发_基础环境获取代码


    public class PinBall extends Activity{
        
        private int tableHeight;
        private int tableWidth;
        Random random = new Random();
        
        //挡板大小和坐标
        private final int racket_width = 70;
        private final int racket_height = 20;
        private int racketY;
        private int racketX;
        
        //球体大小,速度,坐标
        private final int ballR = 12;
        private int ballX;
        private int ballY;
        private int Vy = 3;
        //生成一个比率(-0.5~0.5之间),用于产生随机方向
        private double xyRate = random.nextDouble() - 0.5;
        private int Vx = (int) (Vy * 2 * xyRate); 
        
        //游戏进行时标志
        private boolean isLose = false;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            //全屏无标题
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            //设置游戏界面
            final GameView game = new  GameView(this);
            setContentView(game);
            //获取桌面大小
            WindowManager manager = getWindowManager();
            Display display = manager.getDefaultDisplay();
            tableHeight = display.getHeight();
            tableWidth = display.getWidth();
            //根据获取的桌面大小初始化坐标
            initXY();
            
            //重绘
            final Handler handler = new Handler(){
                public void handleMessage(Message message){
                    if(message.what == 0x123){
                        game.invalidate();
                    }
                }
            };
            
            final Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                
                @Override
                public void run() {
                    //左右边框
                    if(ballX < 0 || (ballX + ballR > tableWidth)){
                        Vx = -Vx;
                    }
                    //超过下边界且未被球拍接住
                    if((ballY + ballR > racketY) && (ballX + ballR < racketX || ballX > racketX + racket_width)){
                        timer.cancel();
                        isLose = true;
                    }
                    //被球拍打回或者反弹时到达顶部
                    if(ballY < 0 || (ballY + ballR > racketY && ballX > racketX && ballX < racketX + racket_width )){
                        Vy = -Vy;
                    }
                    ballX += Vx;
                    ballY += Vy;
                    handler.sendEmptyMessage(0x123);
                }
            }, 0, 10);
            
            //给游戏view加监听器
            game.setOnTouchListener(new OnTouchListener() {
                
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    int x = (int) event.getX();
                    int mid = tableWidth/2;
                    switch (x > mid ? 1 : 2) {
                    case 1:
                        if(racketX + racket_width < tableWidth){
                            racketX += 10;    
                        }
                        break;
                    case 2:
                        if(racketX > 0){
                            racketX -= 10;
                        }
                        break;
                    }
                    game.invalidate();
                    return true;
                }
            });
            
        }
        
        private void initXY(){
            racketY = tableHeight - 80;
            racketX = random.nextInt(tableWidth);
            ballX = random.nextInt(tableWidth);
            ballY = random.nextInt(10) + 20;
        }
        
        class GameView extends View{
    
            private static final float TEXT_SIZE = 40;
    
            public GameView(Context context) {
                super(context);
                // TODO Auto-generated constructor stub
                setFocusable(true);
            }
    
            @Override
            protected void onDraw(Canvas canvas) {
                Paint paint = new Paint();
                paint.setStyle(Paint.Style.FILL);
                //游戏结束
                if (isLose) {
                    paint.setTextSize(TEXT_SIZE);
                    paint.setColor(Color.RED);
                    canvas.drawText("Game Over!", tableWidth/2 - paint.getTextSize()*2, tableHeight/2 - paint.getTextSize()/2, paint);
                }
                //游戏继续
                else {
                    paint.setColor(Color.BLUE);
                    canvas.drawCircle(ballX, ballY, ballR, paint);
                    paint.setColor(Color.GREEN);
                    canvas.drawRect(racketX, racketY, racketX + racket_width, racketY + racket_height, paint);
                    
                }
            }
            
            
        }
    }
  • 相关阅读:
    被老板批评“公司离开谁都照转,谁的工作干不好谁走人”,你会立马辞职吗?
    开课啦 dubbo-go 微服务升级实战
    如何通过 Serverless 提高 Java 微服务治理效率?
    Alluxio 助力 Kubernetes,加速云端深度学习
    收藏!这些 IDE 使用技巧,你都知道吗
    基于 Wasm 和 ORAS 简化扩展服务网格功能
    基于 KubeVela 与 Kubernetes 打造“无限能力”的开放 PaaS
    Kubernetes 稳定性保障手册 -- 日志专题
    Kubernetes 稳定性保障手册 -- 极简版
    Serverless 如何在阿里巴巴实现规模化落地?
  • 原文地址:https://www.cnblogs.com/cfas/p/5149881.html
Copyright © 2020-2023  润新知