Advanced Coding and Game Development
2D UFO
2D UFO is a game where the player (a UFO) moves on a 2D game board covered in gems. The goal of the game is to collect all of the gems. The player can move left, right, up and down with the A, D, W and S keys respectively. When the player collects all of the gem, the speed of the level will slow down for a few seconds and then the level will reset for the player to play again.
The first code snippet to the right shows the initial position of the camera which follows the player on the board. The camera keeps a constant distance away from the player while following it horizontally and vertically. The second code snippet shows how the player's position is updated based on the horizontal and vertical inputs. The camera's position is updated using LateUpdate, meaning it is the last action for the frame, so the camera's position will be based on the already updated position of the player.
The images to the left show two of the components of the player. The first is the sprite renderer, creating the 2D image of the UFO for the player. The second is the Player Controller script, which controls many of the actions of the players. The speed, the count text, win text, pick up sound and the restart delay are components which are set in the inspector.
Thsi code snippet shows how the gems are rotated to make the more noticeable and give them more life.
The image to the left shows the colliders on the walls, the player and the pickups. The code snippet shows what happens when the player has a collision. If the collision is with a gem, the gem is set inactive, the number collected is increased, the SetCountText function is run the and the pickup sound is played. The SetCountText method is below. The text in countText is first updated. Then, if the total number of gems collected is greater than or equal to twelve (the number of gems on the board), winText is updated, time is slowed down, and the game is restarted after a delay.