Guest User

chatgpt game example

a guest
Mar 4th, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. public class MyGame extends SurfaceView implements SurfaceHolder.Callback {
  2.     private GameThread thread;
  3.     private Paint paint;
  4.  
  5.     public MyGame(Context context) {
  6.         super(context);
  7.  
  8.         // Initialize the game thread and paint object
  9.         thread = new GameThread(getHolder(), this);
  10.         paint = new Paint();
  11.  
  12.         // Add this class as a surface holder callback
  13.         getHolder().addCallback(this);
  14.     }
  15.  
  16.     @Override
  17.     public void surfaceCreated(SurfaceHolder holder) {
  18.         // Start the game thread when the surface is created
  19.         thread.setRunning(true);
  20.         thread.start();
  21.     }
  22.  
  23.     @Override
  24.     public void surfaceDestroyed(SurfaceHolder holder) {
  25.         // Stop the game thread when the surface is destroyed
  26.         boolean retry = true;
  27.         while (retry) {
  28.             try {
  29.                 thread.setRunning(false);
  30.                 thread.join();
  31.                 retry = false;
  32.             } catch (InterruptedException e) {
  33.                 // Do nothing
  34.             }
  35.         }
  36.     }
  37.  
  38.     @Override
  39.     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  40.         // Do nothing
  41.     }
  42.  
  43.     @Override
  44.     public void draw(Canvas canvas) {
  45.         super.draw(canvas);
  46.  
  47.         // Draw a circle onto the canvas
  48.         canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, 100f, paint);
  49.     }
  50.  
  51.     private class GameThread extends Thread {
  52.         private SurfaceHolder holder;
  53.         private MyGame game;
  54.         private boolean running;
  55.  
  56.         public GameThread(SurfaceHolder holder, MyGame game) {
  57.             this.holder = holder;
  58.             this.game = game;
  59.             running = false;
  60.         }
  61.  
  62.         public void setRunning(boolean running) {
  63.             this.running = running;
  64.         }
  65.  
  66.         @Override
  67.         public void run() {
  68.             while (running) {
  69.                 // Update the game logic
  70.                 // ...
  71.  
  72.                 // Draw the game onto the canvas
  73.                 Canvas canvas = null;
  74.                 try {
  75.                     canvas = holder.lockCanvas();
  76.                     synchronized (holder) {
  77.                         game.draw(canvas);
  78.                     }
  79.                 } finally {
  80.                     if (canvas != null) {
  81.                         holder.unlockCanvasAndPost(canvas);
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.     }
  87. }
  88.  
Add Comment
Please, Sign In to add comment