Introduction to Writing a Java Platformer Game

How to Write a Platformer Game in Java
 
 
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)
X
Y
(center_x, center_y)
 
change_x
 
change_y
In our games, velocity is
measured in pixels per frame. 
Frame 1
 
X
(5, 8)
3 pixels per frame
4 pixels
per frame
In our games, velocity is
measured in pixels per frame. 
Origin (0,0)
Y
Frame 2
 
X
(8, 12)
3 pixels per frame
4 pixels
per frame
In our games,velocity is
measured in pixels per frame. 
Origin (0,0)
Y
Position and Velocity
   
   
New Position = Old Position + Velocity
X
(8, 12)
3 pixels per frame
4 pixels
per frame
In our games, velocity is
measured in pixels per frame. 
 
center_x = center_x + change_x
center_y = center_y + change_y
Origin (0,0)
Y
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
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
Putting it Together
 
 
 
Thus, we just have three very simple formulas:
 
change_y += gravity
center_y += change_y
center_x += change_x
Position and Velocity
   
X
change_y
change_x
gravity
 
Since positive vertical velocity points down,
gravity is positive.
 
Gravity only affects vertical component
of velocity.
Origin (0,0)
Y
Frame 1
   
X
change_y = -12
gravity = 4
change_y += gravity
Origin (0,0)
Y
Frame 1
   
X
change_y = -8
gravity = 4
 
change_y += gravity
center_y += change_y
Origin (0,0)
Y
Frame 2
   
X
change_y = -8
gravity = 4
 
change_y += gravity
Origin (0,0)
Y
Frame 2
   
X
change_y = -4
gravity = 4
 
change_y += gravity
center_y += change_y
Origin (0,0)
Y
Frame 3
   
X
change_y = -4
gravity = 4
 
change_y += gravity
Origin (0,0)
Y
Frame 3
   
X
change_y = 0
gravity = 4
 
change_y += gravity
center_y += change_y
Origin (0,0)
Y
Frame 4
   
X
change_y = 0
gravity = 4
 
change_y += gravity
Origin (0,0)
Y
Frame 4
   
X
change_y = 4
gravity = 4
 
change_y += gravity
center_y += change_y
Origin (0,0)
Y
Frame 5
   
X
change_y = 4
gravity = 4
 
change_y += gravity
Origin (0,0)
Y
Frame 5
   
X
change_y = 8
gravity = 4
 
change_y += gravity
center_y += change_y
Origin (0,0)
Y
Frame 6
   
X
change_y = 8
gravity = 4
change_y += gravity
Origin (0,0)
Y
Frame 6
   
X
change_y = 12
gravity = 4
change_y += gravity
Origin (0,0)
Y
Resolving Platform Collisions
 
 
 
Instead of moving in both the x
and y directions and then try to
resolve collisions, it is easier to
 
1) move in y direction, check for
collision
2) then move in the x direction
and then check for collision
again.
 
 
 
change_y += gravity
center_y += change_y
center_x += change_x
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
 
 
Resolving Platform Collisions
 
 
move in vertical direction
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:
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
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:
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:
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
Resolving Platform Collisions
 
 
move in horizontal direction
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:
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
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:
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:
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
Player Jumps
 
 
 
Jumping Rule: Player can only jump when he is on a platform.
No multi-jumping
Player Jumps
 
Jumping Rule: Player can only jump when he is on a platform.
No multi-jumping
 
CAN jump!
Player Jumps
 
Jumping Rule: Player can only jump when he is on a platform.
No multi-jumping
 
CANNOT jump!
is_on_platform(sprite, platforms)
 
 
This method returns whether the sprite is on one of the platforms.
Algorithm:
move sprite down say 5 pixels
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
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
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
Jumps
 
if key pressed is A and sprite is on platform:
 
sprite.change_y = -JUMP_SPEED
Jumps
 
if key pressed is A and sprite is on platform:
 
sprite.change_y = -JUMP_SPEED
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.

  • Java Game Development
  • Velocity
  • Position
  • Acceleration
  • Platformer

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.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

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.

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

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#