Guest User

Untitled

a guest
Mar 4th, 2023
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. public class MyGame extends SurfaceView implements SurfaceHolder.Callback {
  2. private GameThread thread;
  3. private Paint paint;
  4. private float playerX, playerY;
  5.  
  6. public MyGame(Context context) {
  7. super(context);
  8.  
  9. // Initialize the game thread and paint object
  10. thread = new GameThread(getHolder(), this);
  11. paint = new Paint();
  12.  
  13. // Set the player's initial position
  14. playerX = getWidth() / 2f;
  15. playerY = getHeight() - 100f;
  16.  
  17. // Add this class as a surface holder callback
  18. getHolder().addCallback(this);
  19. }
  20.  
  21. @Override
  22. public void surfaceCreated(SurfaceHolder holder) {
  23. // Start the game thread when the surface is created
  24. thread.setRunning(true);
  25. thread.start();
  26. }
  27.  
  28. @Override
  29. public void surfaceDestroyed(SurfaceHolder holder) {
  30. // Stop the game thread when the surface is destroyed
  31. boolean retry = true;
  32. while (retry) {
  33. try {
  34. thread.setRunning(false);
  35. thread.join();
  36. retry = false;
  37. } catch (InterruptedException e) {
  38. // Do nothing
  39. }
  40. }
  41. }
  42.  
  43. @Override
  44. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  45. // Reset the player's position when the surface size changes
  46. playerX = width / 2f;
  47. playerY = height - 100f;
  48. }
  49.  
  50. @Override
  51. public void draw(Canvas canvas) {
  52. super.draw(canvas);
  53.  
  54. // Draw the player's ship onto the canvas
  55. Path shipPath = new Path();
  56. shipPath.moveTo(playerX - 50f, playerY + 50f);
  57. shipPath.lineTo(playerX, playerY - 50f);
  58. shipPath.lineTo(playerX + 50f, playerY + 50f);
  59. shipPath.close();
  60. canvas.drawPath(shipPath, paint);
  61. }
  62.  
  63. @Override
  64. public boolean onTouchEvent(MotionEvent event) {
  65. // Move the player's ship to the touch location
  66. playerX = event.getX();
  67. playerY = event.getY();
  68. return true;
  69. }
  70.  
  71. private class GameThread extends Thread {
  72. private SurfaceHolder holder;
  73. private MyGame game;
  74. private boolean running;
  75.  
  76. public GameThread(SurfaceHolder holder, MyGame game) {
  77. this.holder = holder;
  78. this.game = game;
  79. running = false;
  80. }
  81.  
  82. public void setRunning(boolean running) {
  83. this.running = running;
  84. }
  85.  
  86. @Override
  87. public void run() {
  88. while (running) {
  89. // Update the game logic
  90. // ...
  91.  
  92. // Draw the game onto the canvas
  93. Canvas canvas = null;
  94. try {
  95. canvas = holder.lockCanvas();
  96. synchronized (holder) {
  97. game.draw(canvas);
  98. }
  99. } finally {
  100. if (canvas != null) {
  101. holder.unlockCanvasAndPost(canvas);
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment