Skip to main content
GameDev.net gamedev.net
🔒 Locked

2D Game Engine (Help)

Started by ZeroTech Jan 8, 2007 at 6:22 PM 5 replies 1.1k views
Original Post
ZeroTech
ZeroTech
Hello, I'm kind of new to game programming and was wondering if someone here might help me get started. So far I've been able to make single game "scenes" using XNA GSE (C#). That is, a window with some sprites (background and clouds) with a character sprite that can be moved with the keyboard. All that is relatively easy and can be acheived by following the tutorials on the web. My main problem comes when trying to design the full game. One with a Main Menu, Character Selection Menu, UIs, Controlable Characters, Enemies(with their AI), etc. I mainly need help on how to structure the program, what classes to make, and what each class should do/have. I've been following the tutorials at www.XNATutorial.com but they aren't finished. I was thinking of making an engine (or psudo engine) for a game similar to Metal Slug (Simplified Version). Just have the character run thru the mission map, have some enemies to shoot at, and finish the mission. Well, any help will be appreciated. And Thank You in advance.
CIJolly
CIJolly
An easy way to do it is to have a Game_Mode int, and every frame perform a switch statement on that int. The following example assumes everything is hard coded.

So, initialise the int to 0 = main menu mode.
In main menu mode, render a background and the various choices that can be made (at it's most simple, present the player with one option "press enter to start game").
The player presses enter, and Game_Mode is changed to 1 = character select mode.
Character select mode renders the character choices and allows the player to highlight the various characters. When enter is pressed, the highlighted character is noted (eg, player_character = 3) and the game enters mode 2 = load level.

Perform a switch statement on Level_Number. Let's say this defaults to level 1. The game will see that Level_Number == 1, and you will have supplied it data which says what level one entails (eg, enemy placement, item placement, terrain, etc). Once the level data is converted into some kind of functional form (eg, enemy and item classes are added to a linked list, the level terrain is loaded into an array), set the Game_Mode to 3 = run level.

Here, the level is rendered, enemies move around, the player's input controls the character, physics and collision detection are carried out, etc.
Once the player position is at the end of the level (eg, x > 10000), Game_Mode becomes 4 = Level Wrap up. The players stats are displayed.
The player presses fire or some such, the Level_Number is incrimented to 2, Game_Mode changes to 2 to load the level, the level is loaded, game mode changes to 3, the next level is played, etc. etc.

Once Level_Number = 10 (or whatever) Game_Mode is set to 5 = spectacular ending sequence, and you're done.
luke2
luke2
I would prefer a stack based task system (although your kernel has all the standard tasks all the time, but your current game task is determined by the stack), if you don't know what a stack is; its basically a stack of pancakes, first in -> last out, so your main menu goes on first and then it decides what task goes on after it eg skirmish or campaign and when your current game task finishes, it automatically goes back to the previous one. Nifty eh?
ZeroTech
ZeroTech
Great, thanks.

Now, I know the game needs an image class, an animation cls, level, sound, character, enemy, maybe bullet.cs, etc.
How should these be implemented, what methods MUST they contain, etc.

Maybe giving me a list of all the classes needed to make a game like the one I said above might give me an idea of how everything works.

Thanks,
EGD Eric
EGD Eric
you may be getting ahead of yourself. By the sounds of it, this is your first game, so just don't worry about making all the classes yet. First things first: you've got a sprite moving around on the level, right? Now, implement yourself a menu. Assuming that when you press escape, the game pauses and you go to the menu, then pressing escape again brings you back into the game? Implement this (or whatever it is you want to have). Then worry about implementing other menus, multiple levels, etc..

here, I just want to visually explain what CI_Jolly already explained:
void Render(){  switch(gameState)  {   case GAMEPLAY:      //do whatever it is you do to display all the sprites      break;   case MENU:      Menu.render();  //or whatever other way you have of displaying a bunch of    //buttons.      break;  }//you can have the same idea in whatever event-handler you have for input.//I don't know C# that well (hence, you might find some of the syntax 'wrong', but whatever - programming's all the same), but I'll assume it has an OnKeyDown event handler.void OnKeyDown(UINT key){   switch(gameState)   {       case GAMEPLAY:           switch(key)           {               case VK_RIGHT:                  //move sprite right                  break;               case VK_LEFT:                  //move sprite left                  break;           }        break;        case MENU:           {                 //if pressing a key doesn't do anything in the menu, then don't                //bother implementing a case MENU           }   }}

the following function is called every frame
void Run(){     if(gameState == GAMEPLAY)    {      for(int i =0; i < num_sprites; i++)      {          pObj = sprites.get(i);          pObj.run();      }    }}

each 'sprite' or 'object' class (you know - missiles, enemies, projectiles, player, etc..) does something every frame. A projectile moves a tiny bit every frame. A heat-seaking missile moves a bit, but also curves towards the player (I've got code that will do this, it involves 2D vectors - not too complicated).
An enemy follows some kind of AI function- (that's a complex subject, and I'm really inexperienced in that - you'll have to ask someone else for AI advice)
point is, everything is constantly DOING something.

I hope that clears things up for how to handle different gamestates. Anyways, once you've got that down, then you can worry about multiple levels. One thing at a time.

ZeroTech
ZeroTech
Here is a screenshot of my main menu.
Made it so that I can move up and down, and select with enter key. So far I only have "Start Game" active. When I press enter on it, it goes into the game (which is only a blue screen since no game has been added)



Now, just checking if this was done correctly:
I created a class called MainMenu which has the options of the menu (StartGame, Options, Intructions, HighScore), and also takes care of updating and drawing everything in that window.
ZeroTech
ZeroTech
What would be the next step of this process? I guess I have learned how to make the main menu, and any other menu.
Now maybe someone could help me out on how to start making the game. Like what classes to have (ei. sprite, animation, sound, etc), what each should do/behave, etc.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.