// LibGDX core game class public class MyGame extends Game @Override public void create() setScreen(new GameScreen());
// GameView.java public class GameView extends SurfaceView implements Runnable private Thread gameThread = null; private SurfaceHolder holder; private volatile boolean playing = false; private int screenWidth, screenHeight; private float playerX = 100; private float playerY = 100; private int score = 0; public GameView(Context context) super(context); holder = getHolder(); juegos para android version 4.1.2
private void controlFPS() try Thread.sleep(17); // ~60 FPS catch (InterruptedException e) e.printStackTrace(); // LibGDX core game class public class MyGame
// GameScreen.java public class GameScreen implements Screen private OrthographicCamera camera; private SpriteBatch batch; private Texture playerTexture; private Vector2 playerPosition; private SurfaceHolder holder
private void draw() if (holder.getSurface().isValid()) Canvas canvas = holder.lockCanvas(); Paint paint = new Paint(); // Clear screen canvas.drawColor(Color.BLACK); // Draw player paint.setColor(Color.RED); canvas.drawCircle(playerX, playerY, 30, paint); // Draw score paint.setColor(Color.WHITE); paint.setTextSize(40); canvas.drawText("Score: " + score, 50, 80, paint); holder.unlockCanvasAndPost(canvas);
// Feature: High scores public void saveHighScore(String gameId, int score) int currentHigh = prefs.getInt(gameId + "_highscore", 0); if (score > currentHigh) prefs.edit().putInt(gameId + "_highscore", score).apply();