Skip to content

2D Gaming/Collisions in Java Files#1

Open
tanishapatil1234 wants to merge 1 commit intoTheoH32:masterfrom
tanishapatil1234:master
Open

2D Gaming/Collisions in Java Files#1
tanishapatil1234 wants to merge 1 commit intoTheoH32:masterfrom
tanishapatil1234:master

Conversation

@tanishapatil1234
Copy link
Contributor

1. Entity Class (Entity.java)

  • Purpose: Parent class for all character classes.
  • Fields:
    • int x, y: Position of the entity.
    • int speed: Speed of the entity.
    • String direction: Current direction of the entity.
image

2. GamePanel Class (GamePanel.java)

  • Purpose: JPanel for rendering the game and handling user input.

  • Fields:

    • int originalTileSize: Default tile/character size.
    • int scale: Scaling factor.
    • int tileSize: Scaled tile size.
    • int maxScreenCol, maxScreenRow: Maximum columns and rows on the screen.
    • int screenWidth, screenHeight: Dimensions of the screen.
    • KeyHandler keyH: Handler for keyboard input.
    • Thread gameThread: Thread for game loop.
    • Player player: Player character.
    • int playerX, playerY, playerSpeed: Initial position and speed of the player.
    • int FPS: Frames per second for the game.
    • Image backgroundImage: Background image for the game.
  • Methods:

    • GamePanel(): Constructor for initializing the panel.
    • startGameThread(): Starts the game loop thread.
    • run(): Game loop that updates and repaints the screen.
    • update(): Updates the game state.
    • paintComponent(Graphics g): Renders the game components.

Implementation of Runnable

Runnable is an interface that is part of the 'java.lang' package. It is used for creating threads for execution. It defines a method 'run()' which contains code for constituting new threads. GamePanel implements 'Runnable' so it can be executed by a thread.

public void startGameThread() {
// creates new Java Thread associated with the current instance of GamePanel
gameThread = new Thread(this);

// starts thread's execution. runs 'run' method in a new thread
gameThread.start();

}

Purpose of startGameThread is to initiate a new thread specifically for running the game loop in GamePanel class. This includes methods that will be described later, resposible for periodically updating and repainting the game components to keep the game animated and responsive.

3. GameWindow Class (GameWindow.java)

  • Purpose: Main class to launch the game window.
  • Methods:
    • main(String[] args): Entry point for the application.

4. KeyHandler Class (KeyHandler.java)

  • Purpose: Handles keyboard input for player movement.
  • Fields:
    • boolean upPressed, downPressed, leftPressed, rightPressed: Flags for key states.
  • Methods:
    • keyPressed(KeyEvent e): Handles key press events.
    • keyReleased(KeyEvent e): Handles key release events.
    • keyTyped(KeyEvent arg0): Handles key typed events.

5. Player Class (Player.java)

  • Purpose: Represents the player character in the game.

  • Fields:

    • GamePanel gp: Reference to the game panel.
    • KeyHandler keyH: Reference to the key handler.
    • int currentSpriteFrame: Current frame of the player sprite animation.
    • Map<String, Image[]> sprites: Map to store sprite animations.
    • String currentDirection: Current direction of the player.
    • int movementsCount: Count of player movements.
  • Methods:

    • Player(GamePanel gp, KeyHandler keyH): Constructor for player initialization.
    • setDefaultValues(): Sets default values for player properties.
    • update(): Updates the player's position and sprite animation.
    • updateSpriteFrame(): Updates the sprite frame based on the direction.
    • loadSprites(): Loads player sprite images.
    • draw(Graphics2D g2): Draws the player on the screen.

This documentation provides a high-level overview of each class and its purpose, fields, and methods in the Java application.

2D Game Application - in Java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant