如何在Android游戏中检测敌人是玩家正在使用手机中的加速度计

时间:2017-08-22 11:02:27

标签: java android opengl-es 2d-games

如果用户检测到用户停止的墙以及用户获胜的终点线,如何在墙上设置约束。我根据手机的传感器移动创建了播放器移动的位图。我希望以任何形式添加约束,例如玩家不能在屏幕之外。玩家使用加速计传感器正常运转。我想使用内置的原生openGL与android studio一起创建游戏。

代码:

public class Game extends Activity implements SensorEventListener {

    Maze maze;
    private SensorManager sensorManager;
    private Sensor accelerometer;
    public static int sensorX;
    public static int sensorY;

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        Intent intent = getIntent();
        Bundle extras = intent.getExtras();

        // start of sensor
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        accelerometer = 
        sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        lastUpdate = System.currentTimeMillis();
        // end of sensor

        setContentView(gameView);
    }


    // start sensor activity
    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {
        // Not in use
    }

    @Override
    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER ) {

            sensorX -= (int) event.values[0];   // values of x-axis
            sensorY += (int) event.values[1];   // values of y-axis

        }
    }

这是一个GameView代码

public class GameView extends View {

    // maze walls are drawn
    @Override
    protected void onDraw(Canvas canvas) {

        //fill in the background
        canvas.drawRect(0, 0, width, height, background);

        boolean[][] hLines = maze.getHorizontalLines();
        boolean[][] vLines = maze.getVerticalLines();

        //iterate over the boolean arrays to draw walls
        for(int i = 0; i < mazeSizeX; i++) {
            for(int j = 0; j < mazeSizeY; j++){

                float x = j * totalCellWidth;
                float y = i * totalCellHeight;

                if(j < mazeSizeX - 1  && vLines[i][j]) { // draw vertical line

                    canvas.drawLine(x + (cellWidth),     //start X  
                                    y,                   //start Y
                                    x + (cellWidth) ,    //stop X  
                                    y + (cellHeight),    //stop Y
                                    line);
                }

                if(i < mazeSizeY - 1  && hLines[i][j]) { //draw horizontal line

                    canvas.drawLine(x,                          //startX
                                    y + cellHeight,             //startY
                                    x + cellWidth,              //stopX
                                    y + cellHeight,             //stopY
                                    line);
                }

            }
        }

        float ballX;
        float ballY;
        final int width = 10;
        final int height = 10;

        ballX = (Game.sensorX)  + (cellWidth/4);
        ballY = (Game.sensorY)  + (cellWidth/4);

        bitmapBall = BitmapFactory.decodeResource(getResources(), 
        R.drawable.player);    // image of player

        // draw player at X and Y position
        canvas.drawBitmap (bitmapBall,
                           ballX *2 ,          // left of image is drawn
                           ballY * 2 ,         // top of image is drawn
                           paint);

       }
}

0 个答案:

没有答案