Second week of Game Programming 1 – Pong
|
This week we got introduced to our first session of game programming. We got introduced to SDL(Simple DirectMedia Library), the library we will be using in the course to create our own games. The game we got to work on and experiment with was Pong. Pong is a game simulating a game of table tennis. Our task was to implement movement and collision, it was a great task to get a feeling of how a basic game structure looks. However the code structure was a bit messy, but that is something we are going to work on next week when we go through Classes. This week we also learnt how to use functions, arrays and pointers. Here is an example code that generates a level by using a 2D array for X and Y coordinates. The two for loops generates every position in the array, and the if/else assignes the position to different symbols. In this code, every final row is assigned into ”#” to simulate walls. And random positions in the middle of the walls is assigned to ”&” to simulate trees. If a position is not assigned to either # or &, it is assigned to _ instead to simulate areas where the player can walk. for (int y = 0; y < yLength; y++)
{
for (int x = 0; x < xLength; x++)
{
if (y == 0 || y == yLength - 1 || x == 0 || x == xLength - 1)
{
lvlArray[x][y] = '#';
}
else
{
int treeGen = rand() % 9;
if (treeGen == 1)
{
lvlArray[x][y] = '&';
}
else
{
lvlArray[x][y] = '_';
}
}
std::cout << lvlArray[x][y];
}
std::cout << std::endl;
}
std::cin.get();
|