Game programming/Questions

From Wikiversity
Jump to navigation Jump to search


Game programming questions asked by users and answered by Davichito:

1. I want to know about the games and their complexity. What is the simpler and harder games I can make and what do I need to know in each phase?

The simpler games you can make are text based adventures and turn based games. in construction

2. Tell me about file structure. How should I structure my files?

The first thing you need to do is to think about a file structure. It should be related to the complexity of your game and the data structures you will use. Here is an example:
  • Folders bmp, cfg, snd, and the code would be in the master directory or in ./src.
Also, you have to think about a naming convention for your files and structures/classes, variables, etc. They depend on the complexity of the game: most games will need game.c for Game class/structure and other files such as gfx.c, sprites.c, player.c, etc. The most important after determining a basic file structure and naming conventions is the design of the game, point 4.

3. Which libraries and languages can I use?

4. Tell me about game design. I want an example of a simple game design with structures, files and some fields. Also, the interaction between them.

This is usually the most important part of the game. You need to have a robust design for the application to succeed. This includes making structure/class diagrams (depending if your game will use structured or OOP programming), deciding which classes you will have, the use of global variables and specially the dependencies between modules. At this time, you will need to decide which classes will interact with others and if you will use composition or inheritance (in OOP languages).

Composition is including a structure inside another structure, usually through a pointer or reference. This example will clarify it:

typedef struct _player; { struct sprite * sprite; int lives; int energy; unsigned int flags; } player;

This shows the declaration of the type player which includes a pointer to its own sprite. That way the sprite can be accessed from there to be able to draw it, load its images, among other things.

So, using header files each class includes the definitions the other needs. This step is common in programming, so it is a generic concept instead of specific to game programming.

The specific parts of game programming are deciding this structure/class organization. Usually, it may involve having several abstract data types to model the objects in the game. The most common are:

  • Player data structure. It contains fields such as energy, lives, flags (states for the player, boolean values), position information (such as x, y), size, image to blit and other data about the sprite (usually through composition using struct sprite * sprite;), velocity, etc. The function members it will have (or just functions if done in a structured programming language, such as C) depends on what we need to do with the player. That includes behavior such as moving it, jumping, etc.

List of possible functions for the behavior of player data type.

  • Draw, Move(int direction), Face(direction), StartJumping(), HandleJump(), StopJump(), Attack(), etc.

The movement functions are pretty straight, they will update the function using the x, y fields and the velocity and acceleration of the player. So, the code would be something like this:

x += vx;
y += vy;

notice that some of them can be zero. Discussing jumping is implementing the physics equations related to gravity, which is similar to this.

vx += ax;
vy += vy;

The player must have flags or boolean values to know where it's facing. It usually goes inside the sprite structure, so we will discuss that later.