• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Vero

Preventing Chaos in your game code

7 posts in this topic

Hi Everyone, I've been coding a simple game in C++ with OpenGL. I'm pretty new to creating games and though I've had a lot of college experience in coding with C++ and Java and .Net I've never really developed anything professionally on this large a scale. The thing is I'm worried things will become more and more disorganized as I progress in making the game.

So far the game is a maze where you load the maze data from a text file and then you have to navigate to the goal. It will progress from there but I want to go over how I'm implementing it.

the Game Loop:
[code]
Level1= new Grid("data/Testlvl.txt",20,70);
max_fps=2; //TODO may need to incorporate a file load or mabye just play game from start to finish
int fcount=0; //frame counter
long long fps_sum=0LL; //sum of frame rate
long long start=milliseconds_now();
//intialise player speed
Level1->player->Velocity.x=40; //40 pixels per second
Level1->player->Velocity.y=40;
while(!done) //Game LOOOP!
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) //is there a message?
{ //peek will not halt program while it looks for a message
if(msg.message==WM_QUIT) // have we received a quit message?
{
done=TRUE;
}
else{ //if not deal with the window messages
TranslateMessage(&msg); //Translate the Message
DispatchMessage(&msg); //Dispatch the Message
}
}
else{ // if there are no messages

// Draw the Scene. Watch for ESC Key and Quit Messages From DrawGLScene()
if((active && !DrawGLScene())|| keys[VK_ESCAPE])
{
done=TRUE; // ESC Signalled a Quit
}
else{ //not time to quit, update screen
//drew scene in the if statement
while(FPS(start,milliseconds_now())>max_fps)
{/*wait*/}
if(fcount < 60)
{
fps_sum=fps_sum+FPS(start,milliseconds_now());
++fcount;
}
else
{
fps=fps_sum/fcount;
fcount=0;
fps_sum=0LL;
}
start=milliseconds_now();
SwapBuffers(hDC); //SwapBuffers (double buffering)
}
}
} if (!gameover && active) // If Game Isn't Over And Programs Active Move Objects
{
if (keys[VK_RIGHT])
{
Level1->MovePlayer(M_RIGHT);
}
if (keys[VK_LEFT])
{
Level1->MovePlayer(M_LEFT);
}
if (keys[VK_DOWN])
{
Level1->MovePlayer(M_DOWN);
}
if (keys[VK_UP])
{
Level1->MovePlayer(M_UP);
}

Level1->Step(start);// what happens next?
}
//outside of live play:
if(keys['A'] && !ap)
{
ap=TRUE;
max_fps++;
}
if (!keys['A']) // If 'A' Key Has Been Released
{
ap=FALSE; // ap Becomes FALSE
}
if(keys['Z'] && !zp && max_fps>2)
{
zp=TRUE;
max_fps--;
}
if (!keys['Z']) // If 'A' Key Has Been Released
{
zp=FALSE; // ap Becomes FALSE
}

//*********update scene****************
/// Beat first Level Snippet
if(Level1->player->isTouching(*Level1->goal))
levelend=true;
///end of snippet
}

[/code]

So right now inorder to switch scenes in the game loop I set global flags to true and false ( good or bed way to do it) and the DrawGLScene() will check the flags and determine which scene to draw,

After declaring a few setup variables I move everything along via time, I even force a max frame rate so that i can control the FPS. (I did this starting out just because I could and now I'm thinking about turning the FPS into something like a reward system collect items increase the FPS makes for smooth gameplay)

All my classes are contained within the Grid Class, the Grid Class will contain a player and a List of objects (coins enemies and such all will inherit the MObject class which has a bunch of virtual functions so I can iterate through the list and call Step() and Draw() functions. Step basically updates all the objects positions and other such statuses given that point in time. everything goes through Grid so when the user enters any key commands the Grid can determine what information flows to what objects it's managing. I keep most things public right now but I hope to eventually makes the objects private and get implement public functions to keep things from getting confuzzled.

so kind of breaking it down to the flow so far psuedo codeish:
[code]
GameLoop:
DrawScene:
[chooses what scene to draw based on flags]
assuming game is running
Draw header()
Grid->Draw()
Draws grid
Draws Players
Draws Goal
todo: draw enemies
todo: draw items
Isgamerunning?
Check for input
Move player, enemies
Step
grid.Step()
update animated items, players enemies,

Are game changingg conditions met
progress game
[/code]


I'd really appreciate constructive criticism and how to make it better, methods to keep things organized, what your own ways to coding the loop are.

I hope my post makes some sense too
1

Share this post


Link to post
Share on other sites
#frob This needs one of my favourite quotes: "Sanity? I don't remember having such a useless thing in a first place." [img]http://public.gamedev.net//public/style_emoticons/default/ph34r.png[/img]

To the topic...

Anyway if you want to investigate some more into how to "prevent chaos in code" - try reading something on design patters, language conventions, naming conventions and it's never too late to start to use F.e. Doxygen style commenting, that is actually very helpful to keep code nice and clean...

Also I recommend reading something on coding style, naming conventions, etc. - and try to keep single coding style and naming convention in every single of your project (and don't always use just one, use different coding styles in different projects - you might once come to contribute to applications on SourceForge - then it's huge advantage to adapt to different coding styles) [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
0

Share this post


Link to post
Share on other sites
As others have said, reading books and other resources about clean code, coding style, and design patterns are all very useful.

Here's my advice:
- Use static analysis tools to check for styling issues and possible bugs. For Java, there's Checkstyle and Findbugs. For Python, you have pep8, pylint, and pyflakes. There are equivalents for other languages; just check google.
- Check your code/test coverage. (You are writing unit tests, right?) I found this immensely helpful in pursuing game programming as a hobby. Before I started doing rigorous testing and coverage analysis, I was just kind of thrashing around and things just sort of worked. Now, when I implement a new feature, I check my test coverage. If there's a large body of code with no coverage, I stop coding and I test it. If it can't be tested easily, it probably needs to be refactored.
- Write as much as you can in a pure functional style. That doesn't mean you have to go crazy with functional programming concepts, but the fact is purely functional code is much easier to test. So if you can move a bunch of the complexity into small, easily testable functions, you'll that you'll have greater confidence in your code and that it's easier to make changes. (BTW, John Carmack has talked about the benefits of writing functional-style code: http://www.altdevblogaday.com/2012/04/26/functional-programming-in-c/)

That's my 2 cents.
1

Share this post


Link to post
Share on other sites
First things first. Does it work? If not, get it working. Then think about refactoring.

A good metric for determining if refactoring is required is if the code makes sense to you at a glance a few weeks or months later. You can often overlook obvious design flaws when the code is fresh in your mind.

IMHO, it's better to code an imperfect solution and then improve it later (if you have time) than spending hours agonising over producing the perfect design first time. I know this because I'm guilty of it all the time.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0