Introduction to Writing a Java Platformer Game

Slide Note
Embed
Share

Learn the basics of developing a platformer game in Java, covering concepts such as velocity, position, acceleration, and putting it all together with simple formulas. Explore the key elements required to create an engaging platformer game with step-by-step explanations and visual aids.


Uploaded on Sep 10, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. How to Write a Platformer Game in Java

  2. Some Basic Physics Velocity of an object is the rate of change of its position. It is a vector and can be decomposed into a x-component and a y-component. A Sprite object has attributes change_x and change_y for its velocity. Origin (0,0) In our games, velocity is measured in pixels per frame. (center_x, center_y) Y change_x change_y X

  3. Frame 1 Origin (0,0) In our games, velocity is measured in pixels per frame. Y (5, 8) 3 pixels per frame 4 pixels per frame X

  4. Frame 2 Origin (0,0) In our games,velocity is measured in pixels per frame. Y 3 pixels per frame 4 pixels per frame (8, 12) X

  5. Position and Velocity New Position = Old Position + Velocity Origin (0,0) In our games, velocity is measured in pixels per frame. Y 3 pixels per frame center_x = center_x + change_x center_y = center_y + change_y 4 pixels per frame (8, 12) X

  6. Velocity The velocity of an object is the rate of change of its position. New Position = Old Position + Velocity center_x = center_x + change_x center_y = center_y + change_y

  7. Acceleration The acceleration of an object is the rate of change of its velocity. New Velocity = Old Velocity + Acceleration change_x = change_x + acceleration_x change_y = change_y + acceleration_y For us, we will only have acceleration in the y-direction in the form of gravity. change_y += gravity

  8. Putting it Together Thus, we just have three very simple formulas: change_y += gravity center_y += change_y center_x += change_x

  9. Position and Velocity Origin (0,0) Y change_y change_x Since positive vertical velocity points down, gravity is positive. gravity Gravity only affects vertical component of velocity. X

  10. Frame 1 Origin (0,0) Y change_y += gravity change_y = -12 gravity = 4 X

  11. Frame 1 Origin (0,0) Y change_y += gravity center_y += change_y change_y = -8 gravity = 4 X

  12. Frame 2 Origin (0,0) Y change_y += gravity change_y = -8 gravity = 4 X

  13. Frame 2 Origin (0,0) Y change_y += gravity center_y += change_y change_y = -4 gravity = 4 X

  14. Frame 3 Origin (0,0) Y change_y = -4 change_y += gravity gravity = 4 X

  15. Frame 3 Origin (0,0) Y change_y = 0 change_y += gravity center_y += change_y gravity = 4 X

  16. Frame 4 Origin (0,0) Y change_y = 0 change_y += gravity gravity = 4 X

  17. Frame 4 Origin (0,0) Y change_y = 4 change_y += gravity center_y += change_y gravity = 4 X

  18. Frame 5 Origin (0,0) Y change_y = 4 change_y += gravity gravity = 4 X

  19. Frame 5 Origin (0,0) Y change_y = 8 change_y += gravity center_y += change_y gravity = 4 X

  20. Frame 6 Origin (0,0) Y change_y += gravity change_y = 8 gravity = 4 X

  21. Frame 6 Origin (0,0) Y change_y += gravity change_y = 12 gravity = 4 X

  22. Resolving Platform Collisions Instead of moving in both the x and y directions and then try to resolve collisions, it is easier to change_y += gravity center_y += change_y center_x += change_x 1) move in y direction, check for collision 2) then move in the x direction and then check for collision again.

  23. Resolving Platform Collisions change_y += gravity # move in vertical direction center_y += change_y # resolve collisions # move in horizontal direction center_x += change_x # resolve collisions

  24. Resolving Platform Collisions move in vertical direction

  25. Resolving Platform Collisions move in vertical direction compute list of all platforms which collide with player if list not empty: if player is moving up:

  26. Resolving Platform Collisions move in vertical direction compute list of all platforms which collide with player if list not empty: if player is moving up: set top of player = bottom of a collided platform

  27. Resolving Platform Collisions move in vertical direction compute list of all platforms which collide with player if list not empty: if player is moving up: set top of player = bottom of a collided platform if player is moving down:

  28. Resolving Platform Collisions move in vertical direction compute list of all platforms which collide with player if list not empty: if player is moving up: set top of player = bottom of a collided platform if player is moving down:

  29. Resolving Platform Collisions move in vertical direction compute list of all platforms which collide with player if list not empty: if player is moving up: set top of player = bottom of a collided platform if player is moving down: set bottom of player = top of a collided platform set player's change_y = 0

  30. Resolving Platform Collisions move in horizontal direction

  31. Resolving Platform Collisions move in horizontal direction compute list of all platforms which collide with player if list not empty: if player is moving right:

  32. Resolving Platform Collisions move in horizontal direction compute list of all platforms which collide with player if list not empty: if player is moving right: set right side of player = left side of a collided platform

  33. Resolving Platform Collisions move in horizontal direction compute list of all platforms which collide with player if list not empty: if player is moving right: set right side of player = left side of a collided platform if player is moving left:

  34. Resolving Platform Collisions move in horizontal direction compute list of all platforms which collide with player if list not empty: if player is moving right: set right side of player = left side of a collided platform if player is moving left:

  35. Resolving Platform Collisions move in horizontal direction compute list of all platforms which collide with player if list not empty: if player is moving right: set right side of player = left side of a collided platform if player is moving left: set left side of player = right side of a collided platform

  36. Player Jumps Jumping Rule: Player can only jump when he is on a platform. No multi-jumping

  37. Player Jumps Jumping Rule: Player can only jump when he is on a platform. No multi-jumping CAN jump!

  38. Player Jumps Jumping Rule: Player can only jump when he is on a platform. No multi-jumping CANNOT jump!

  39. is_on_platform(sprite, platforms) This method returns whether the sprite is on one of the platforms. Algorithm: move sprite down say 5 pixels

  40. is_on_platform(sprite, platforms) This method returns whether the sprite is on one of the platforms. Algorithm: move sprite down say 5 pixels compute collision list with platforms restore position by moving up 5 pixels

  41. is_on_platform(sprite, platforms) This method returns whether the sprite is on one of the platforms. Algorithm: move sprite down say 5 pixels compute collision list with platforms restore position by moving up 5 pixels

  42. is_on_platform(sprite, platforms) This method returns whether the sprite is on one of the platforms. Algorithm: move sprite down say 5 pixels compute collision list with platforms restore position by moving up 5 pixels if collision list not empty return true otherwise return false

  43. Jumps if key pressed is A and sprite is on platform: sprite.change_y = -JUMP_SPEED

  44. Jumps if key pressed is A and sprite is on platform: sprite.change_y = -JUMP_SPEED

Related


More Related Content