I got inspired by tutorials on youtube, about building java 2D games, so I started  project “MedBoy”.  I  will not build a game engine, or use any game library.  Building game engine instead of building games makes more sense, but it’s not easy for beginner like me.

This is an ongoing project, and check the project out in MedBoy github repository.

2D Games contains these parts:
Rendering:  Stuffs about displaying things on screen.
Sprite: Images needed in games.
Logic: Logic of how users interact with resources in games.
Sound: Sound system, controls how sounds are played.
Physics: Simulate how objects interact with each other in real world.
Inputs: User control commands, such as mouse clicks and key pressing.
File System: Storing and accessing data of game.

If we have an idea of what 2D games need, then we can come down to building each details. But before that, we need to understand how games work.

How games work:
Basically, games always run in a loop, and in each loop it receives player commands, with that information, the game updates data and render a frame. These codes shows the idea.

loop(true)
{
Input();
update();
render();
}

MedBoy is a platform game, so the input function needs to get information of arrow keys. If right arrow key is pressed, we need to update the boy’s position. I don’t want to scroll the background directly. Scrolling background is not intuitive, and if I want to add some speed change, then it becomes disaster, the relative movement becomes impossible to understand. Instead, I imagine a camera on top of background. The input from the user changes boy’s relative position to background. And the camera follow the boy with some lerp. The camera is always trying to center the boy, but the speed of the camera is constantly changing.

MedBoy

Level Update

MedBoy_LevelUpdate

In the previous version, the level was generated by random numbers.  All the tiles are represented by numbers, saving into a 2D array, integer 0 representing background while 1 means tiles.

This time I also save the level into an array, like this.

{ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

Saving levels inside codes is not convenient for later change, I will come back to this topic later.

Debug Info Update

MedBoy_DebugUpdate

I always need to know what is going on with some variables, so by displaying on top, I get these info.

Taking Care of Collision

This part is really not that simple to implement. The guide to implementing 2D platformers explains 4 ways of doing collision detection.

Advertisements