Advertisement
TeamOneButton

MakesCentsGame.java

Mar 22nd, 2023 (edited)
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.mygdx.makescents;
  2.  
  3. import com.badlogic.gdx.Game;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.graphics.Color;
  6. import com.badlogic.gdx.graphics.Texture;
  7. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  8. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  9. import com.badlogic.gdx.utils.ScreenUtils;
  10.  
  11. public class MakesCentsGame extends Game {
  12.     private SpriteBatch batch;
  13.     private Texture img;
  14.     private BitmapFont font;
  15.     private GameScreen gameScreen;
  16.     private GameTitle gameTitle;
  17.  
  18.     @Override
  19.     public void create() {
  20.         gameTitle = new GameTitle("Makes Cents", 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  21.         gameScreen = new GameScreen(this); // pass the game instance to the GameScreen constructor
  22.  
  23.         // Create a new SpriteBatch and load the image
  24.         batch = new SpriteBatch();
  25.         img = new Texture("pennyfriends.jpg");
  26.  
  27.         // Create a new BitmapFont with white color
  28.         font = new BitmapFont();
  29.         font.setColor(Color.WHITE);
  30.  
  31.         // Set the font's properties
  32.         font.getData().setScale(2f); // Set the font scale
  33.         font.getData().setLineHeight(50f); // Set the line height
  34.  
  35.         System.out.println("Game created");
  36.     }
  37.  
  38.     @Override
  39.     public void render() {
  40.         // Clear the screen
  41.         ScreenUtils.clear(0, 0, 0, 1); // Set the background color to black
  42.  
  43.         // Get the screen width and height
  44.         float screenWidth = Gdx.graphics.getWidth();
  45.         float screenHeight = Gdx.graphics.getHeight();
  46.  
  47.         // Get the image width and height
  48.         float imageWidth = img.getWidth();
  49.         float imageHeight = img.getHeight();
  50.  
  51.         // Calculate the position to center the image
  52.         float x = (screenWidth - imageWidth) / 2;
  53.         float y = (screenHeight - imageHeight) / 2;
  54.  
  55.         // Begin the SpriteBatch and draw the image
  56.         batch.begin();
  57.         batch.draw(img, x, y);
  58.  
  59.         // Draw the text with the white font
  60.         font.draw(batch, "Welcome to Makes Cents!", 100, 200);
  61.  
  62.         // End the SpriteBatch
  63.         batch.end();
  64.  
  65.         if (Gdx.input.justTouched()) {
  66.             System.out.println("Screen changed to GameScreen");
  67.             setScreen(gameScreen); // Call setScreen() with gameScreen instance
  68.         }
  69.     }
  70.  
  71.     @Override
  72.     public void dispose() {
  73.         // Dispose of the SpriteBatch, image, and font
  74.         if (batch != null) {
  75.             batch.dispose();
  76.         }
  77.         if (img != null) {
  78.             img.dispose();
  79.         }
  80.         if (font != null) {
  81.             font.dispose();
  82.         }
  83.  
  84.         System.out.println("Game disposed");
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement