Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. Maze maze;
  2. private SensorManager sensorManager;
  3. private Sensor accelerometer;
  4. public static int sensorX;
  5. public static int sensorY;
  6.  
  7. public void onCreate(Bundle bundle) {
  8. super.onCreate(bundle);
  9.  
  10. Intent intent = getIntent();
  11. Bundle extras = intent.getExtras();
  12.  
  13. // start of sensor
  14. sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  15. accelerometer =
  16. sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  17. lastUpdate = System.currentTimeMillis();
  18. // end of sensor
  19.  
  20. setContentView(gameView);
  21. }
  22.  
  23.  
  24. // start sensor activity
  25. @Override
  26. public void onAccuracyChanged(Sensor sensor, int i) {
  27. // Not in use
  28. }
  29.  
  30. @Override
  31. public void onSensorChanged(SensorEvent event) {
  32.  
  33. if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER ) {
  34.  
  35. sensorX -= (int) event.values[0]; // values of x-axis
  36. sensorY += (int) event.values[1]; // values of y-axis
  37.  
  38. }
  39. }
  40.  
  41. // maze walls are drawn
  42. @Override
  43. protected void onDraw(Canvas canvas) {
  44.  
  45. //fill in the background
  46. canvas.drawRect(0, 0, width, height, background);
  47.  
  48. boolean[][] hLines = maze.getHorizontalLines();
  49. boolean[][] vLines = maze.getVerticalLines();
  50.  
  51. //iterate over the boolean arrays to draw walls
  52. for(int i = 0; i < mazeSizeX; i++) {
  53. for(int j = 0; j < mazeSizeY; j++){
  54.  
  55. float x = j * totalCellWidth;
  56. float y = i * totalCellHeight;
  57.  
  58. if(j < mazeSizeX - 1 && vLines[i][j]) { // draw vertical line
  59.  
  60. canvas.drawLine(x + (cellWidth), //start X
  61. y, //start Y
  62. x + (cellWidth) , //stop X
  63. y + (cellHeight), //stop Y
  64. line);
  65. }
  66.  
  67. if(i < mazeSizeY - 1 && hLines[i][j]) { //draw horizontal line
  68.  
  69. canvas.drawLine(x, //startX
  70. y + cellHeight, //startY
  71. x + cellWidth, //stopX
  72. y + cellHeight, //stopY
  73. line);
  74. }
  75.  
  76. }
  77. }
  78.  
  79. float ballX;
  80. float ballY;
  81. final int width = 10;
  82. final int height = 10;
  83.  
  84. ballX = (Game.sensorX) + (cellWidth/4);
  85. ballY = (Game.sensorY) + (cellWidth/4);
  86.  
  87. bitmapBall = BitmapFactory.decodeResource(getResources(),
  88. R.drawable.player); // image of player
  89.  
  90. // draw player at X and Y position
  91. canvas.drawBitmap (bitmapBall,
  92. ballX *2 , // left of image is drawn
  93. ballY * 2 , // top of image is drawn
  94. paint);
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement