必威体育Betway必威体育官网
当前位置:首页 > IT技术

弹球游戏的简单实现

时间:2019-07-23 16:40:00来源:IT技术作者:seo实验室小编阅读:72次「手机版」
 

弹球

public class MainActivity extends Activity {
    //随机数。每次进入游戏初始不一样
    Random rand = new Random();
    private double xyRate = rand.nextDouble() - 0.5; //一个-0.5~0.5的比率,控制小球的方向
    //屏幕宽,高
    private int tablewidth;
    private int tableheight;
    //球拍的水平位置,垂直位置,宽度,高度
    private int racketx = rand.nextint(200);//***每次进入游戏得到随机球拍初始位置
    private int rackety ;
    private int RACKET_WIDTH = 200;
    private int RACKET_HEIGHT = 30;
    //球的大小,x和Y坐标
    private int BALL_SIZE = 16;
    private int ballx = rand.nextInt(200) + 20;//***每次进入游戏得到随机小球初始位置
    private int bally = rand.nextInt(10) + 20;//***
    //球的纵向速度,横向速度
    private int yspeed = 40;
    private int xspped = (int) (yspeed*xyRate*2); //***每次进入游戏得到一个随机的横向速度
    //游戏是否结束的标志
    private boolean isLose = false;
    //球拍的旧xy坐标
    int oldx;
    int oldy;

    @Override
    protected void onCreate(Bundle savedinstanceState) {
        super.onCreate(savedInstanceState);
        setcontentView(R.layout.activity_main);
        final GameView gameView = new GameView(this);
        setContentView(gameView);
        //requestWindowFeature(Window.FEATURE_NO_title);//因为已经没有标题了,加上该代码会出错
        getWindow().setFlags(WindowManager.layoutparams.FLAG_FULLSCREEN , WindowManager.LayoutParams.FLAG_FULLSCREEN);


        WindowManager windowManager = getWindowManager();
        display display = windowManager.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);

        tablewidth = metrics.widthPixels;
        tableheight = metrics.heightPixels;
        Log.i("mydate" , tablewidth +"  " + tableheight);
        rackety = tableheight - 80;
        final handler handler = new Handler(){
            @Override
            public void handlemessage(Message msg) {
                if (msg.what == 1){
                    gameView.invalidate();
                }
            }
        };

        gameView.setOnKeyListener(new View.OnKeyListener() { //按键
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                switch (event.getKeyCode()){
                    case KeyEvent.KEYCODE_A:
                        if (racketx > 0) racketx -= 10;
                        break;
                    case KeyEvent.KEYCODE_D:
                        if (racketx < tablewidth - RACKET_WIDTH) racketx += 10;
                        break;
                }
                gameView.invalidate();//控制球拍左右移动

                return true;
            }
        });

        gameView.setOnTouchListener(new View.OnTouchListener() { //触摸
            @Override
            public boolean onTouch(View v, motionevent event) {
                int nowx = (int) event.getX();
                //int nowy = (int) event.getY();
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        oldx = nowx;
                        //oldy = nowy;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (nowx > oldx){ //向右移动
                            racketx += 10;
                        } else if (nowx < oldx) {//向左移动
                            racketx -= 10;
                        }
                        oldx = nowx;
                        break;
                    case MotionEvent.ACTION_UP:
                        oldx = nowx;
                        break;
                }
                gameView.invalidate();
                return true;
            }
        });

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //小球碰到游戏界面左右边框时
                if (ballx <= 0 || ballx >= tablewidth - BALL_SIZE){
                    xspped = -xspped; //横向速度反向
                }
                //如果小球的高度超出了球拍的位置,且横向不在球拍的范围内,则GAME OVER
                if (bally >= rackety - BALL_SIZE && (ballx < racketx || ballx > racketx+RACKET_WIDTH)){
                    timer.cancel();
                    isLose = true; //游戏结束标志
                } //如果小球在拍的范围内或碰到顶部,小球反弹
                else if (bally <= 0 || (bally >= rackety-BALL_SIZE && ballx > racketx && ballx < racketx + RACKET_WIDTH)){
                    yspeed = -yspeed; //纵向速度反向
                }
                //小球坐标不停的改变
                bally += yspeed;
                ballx += xspped;
                handler.sendemptyMessage(1);

            }
        } , 0 , 100); //每0.1秒执行刷新

    }

    class GameView extends View{
        Paint paint = new Paint();

        public GameView(context context) {
            super(context);
            setFocusable(true);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true);
            if (isLose){
                paint.setcolor(Color.RED);
                paint.setTextSize(40);
                canvas.drawtext("GAME OVER" , tablewidth/2-100 , 200 , paint);
            } else {
                paint.setColor(Color.BLACK);
                canvas.drawCircle(ballx , bally , BALL_SIZE , paint); //绘制小球
                canvas.drawRect(racketx , rackety , racketx + RACKET_WIDTH , rackety + RACKET_HEIGHT , paint);//绘制球拍
            }

        }

    }


}

相关阅读

小游戏网站发展的关键:精确定位 不断创新

网站的首页布局很简单,并不像4399,7k7k,那样在首页显示过多的图片和游戏分类。这一点,我反而觉得是一种创新。因为在人们的一般定式中

android游戏开发入门: 贪吃蛇 源代码分析

/d/file/news/20190606/6618313

天猫魔盒体感游戏删除怎么操作?其工作原理是怎样的?

现在已经逐渐进入智能化社会,我们不仅仅习惯了使用智能手机,还开始用起了其他智能设备,比如天猫魔盒,这是一个非常有用的只能音箱,已经

腾讯手游助手如何删除游戏 腾讯手游模拟器卸载游戏教

腾讯手游助手是一款界面美观的安卓模拟器。用户可以在里面下载很多的游戏,腾讯手游助手如何卸载游戏?下面小编带来了腾讯手游模拟器

淘宝玩小游戏赢金币,具体操作步骤解析!

淘宝玩小游戏赢金币可能很多朋友之前都已经玩过了,就和我们平时玩的小游戏一样,只要好的一点,这些游戏的奖励是淘宝淘金币,可以在购买

分享到:

栏目导航

推荐阅读

热门阅读