Wednesday, July 6, 2016

Game Development: Alien Planet - Adding movable rocks

Movable rocks are good item to have when creating puzzles. For this reason I added them. Each movable rock is a sprite. The game checks if any sprite is colliding with a movable rock. If the player is colliding with a rock, then the rock speedx, or speedy is changed from zero to some value to make the rock move. The value is dependent on the direction of the player. In addition, the player current x and current y is set back to the x and y from the previous frame. This is needed to prevent the player from stepping over the rock and skipping it.

In addition to the above, a check for the rock and its collision with other rocks or tiles is needed. If so, then the rock should never move. This is done by simply setting speedx and speedy of the rock to zero, so even if the player is pushing the rock, it stops when it reaches water or collide with another rock.

Finally if any other sprite/enemy is colliding with a rock then the new x, and y for that enemy is the same as the previous frame thus preventing enemies from stepping over the movable rock. 

Monday, July 4, 2016

Game Development: Alien Planet - Re-spawning item

Recently I managed to find a very simple solution for the re-spawning problem. The main issue is that once the player picks an item ( in my case it is fruit ) from the map, the map data structure is updated, and the fruit is removed from the map. The game saves the updated map which means that the item/fruit is lost forever and no replacing item could appear.

This problem is the result of bad tile map design. Probably what should have been done is to store a map state data structure separately. That data structure states where the items are and if they should re-spawn or not. Instead of redesigning the data structure of the game state, a much easier workaround solved this:
1- define an invisible item, that the player can't interact with, call it for example (re-spawn item)
2- during game play if the player picks an item/fruit, then replace the tile containing the fruit with the (re-spawn item).
3- every time the game map reloads, check the tiles searching for the (re-spawn item). If found, then generate a random number between 0 and 1, if the value is less than 0.05 then convert the (re-spawn item) to an actual item that the player could interact with.

This modification needed about 20 minutes to finished and managed to solve the issue very easily.

Friday, July 1, 2016

Game Development: Alien Planet - SDL_Mixer audio problem

One of the issues I had to fix in the game was playing sound effects. For sound effects I am using SDL_Mixer. The issue I am facing was whenever I issue the command to play a new wave file/stream, the previous audio stream stops. There is no mixing happening. So, for example if you are picking a fruit while walking the audio of picking the fruit get cut and the walking audio starts immediately.

To solve this issue I used the "Mix_PlayingMusic()" function which returns true if an audio effect is playing, and false otherwise. First I created a function that loops over all the sound effects and see if any is playing. If none is doing so, then the function returns false. The game checks the result of this function before playing a new audio effect.

This solved the problem partially, what happened is that the walking sound disappears for few seconds after picking fruit. It turns out that the picking fruit audio file has few seconds of silent audio at the end. So, SDL_Mixer was playing silent audio and returning true for few seconds. The solution to this was to use Audacity to remove the trialing silent audio from all the other files, and everything worked perfectly fine.