Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
- }
- // 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement