android学习中--弹球游戏

在学习图形图像这一块,写一个小游戏,记录学习历程。

规则:小球随机向下运动,遇到左边框、右边框、上边框和“球拍”时反弹,用户可以控制“球拍”的左右,若“球拍”接不到小球,游戏结束。代码如下:

package com.example.admin.imgapp;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    Random random = new Random();

    float bx = 100,by = 100;   //球的坐标
    float b_size = 10;
    double xyRate = random.nextDouble()-0.5;
    int yspeed = 15;
    int xspeed = (int)(yspeed*xyRate*2);



    int rx= random.nextInt(200);//球拍的坐标
    int ry =0;
    float r_width = 90;
    float r_height = 10;

    boolean isOver = false;
    int tableWidth,tableHeight;



    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);  //不要标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏
        final GameView gameView = new GameView(this);
        setContentView(gameView);  //设置页面为游戏界面


        WindowManager wm = getWindowManager();
        Display display = wm.getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        display.getMetrics(dm);
        tableHeight = dm.widthPixels;
        tableWidth = dm.widthPixels;
        ry = tableHeight-20;





        gameView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                switch(event.getKeyCode()){
                    case KeyEvent.KEYCODE_A:
                        if(rx>0)rx-=10;break;
                    case KeyEvent.KEYCODE_D:
                        if(rx=tableWidth-b_size){
                    xspeed = -xspeed;
                }
                //如果没接住
                if(by>=ry-b_size &&(bxrx+r_width)){
                    timer.cancel();
                    isOver = true;
                } //接住了,反弹
                else if(by<=0 ||(by>=ry-b_size && bx>rx && bx<=rx+r_width)){
                    yspeed = -yspeed;
                }

                by+=yspeed;
                bx += xspeed;
                handler.sendEmptyMessage(100);
            }
        },0,200);
    }

    /**
     *游戏类
     */
    class GameView extends View {
        Paint paint = new Paint();
        public GameView(Context context){
            super(context);
            setFocusable(true); //可获得焦点
        }
        public void onDraw(Canvas canvas){
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true);

            if(isOver){
                paint.setColor(Color.RED);
                paint.setTextSize(40);
                canvas.drawText("游戏结束",tableWidth/2-100,200,paint);
            }else{
                paint.setColor(Color.rgb(255,0,0));
                canvas.drawCircle(bx,by,b_size,paint);
                paint.setColor(Color.rgb(80,80,200));
                canvas.drawRect(rx,ry,rx+r_width,ry+r_height,paint);
            }

        }
    }
}


你可能感兴趣的:(android)