2D Gaming/Collisions in Java Files#1
Open
tanishapatil1234 wants to merge 1 commit intoTheoH32:masterfrom
Open
Conversation
2D Game Application - in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
1. Entity Class (Entity.java)
int x, y: Position of the entity.int speed: Speed of the entity.String direction: Current direction of the entity.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)
main(String[] args): Entry point for the application.4. KeyHandler Class (KeyHandler.java)
boolean upPressed, downPressed, leftPressed, rightPressed: Flags for key states.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.