# Programming a game play Essentials

This topic is 1811 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi All,

I have a decent knowledge of c++, java,..etc, worked on QT, developt a lot of programs,  but when programming a game I find it difficult to move objects around the screen the way I want, or do behaviours like snake body movements, implementing a basic AI,..etc.

I had a problem with a basic algorithm here : http://www.gamedev.net/topic/647053-snake-body-movement-problem/

and I still didn't finish it...

1 ) What are the essential knowledge that is needed actually to program the objects, to do the behaviour ?

2) Do you guys write the behaviour, the debug and see, write, debug,..etc or write, test, run each time if you are programming the game objects, or writing the game AI, or the " Game Play " ?

3) I dozen of game programming books, like tricks of windows game programming gurus, LaMothe, Game Coding Complete,..etc. Nothing take about game play programming, how to move objects specific ways, how to really write the game.

Edited by AhmedCoeia

##### Share on other sites

If one problem your having is with movement not being smooth or uncontrollable, I think I may help.

Let's say you have a function that runs once every second through a tick system you've set up. If you want to move your object's x position by 5 pixels per second you'd do this.

StepFunc()
{
Object.x += 5;
}


Now let's say that StepFunc is now called 30 times per second, but you want to keep moving at 5 pixels per second. You'd do this:

StepFunc()
{
Object.x += ( 5 / 30 );
}


The thing is you can't always be sure that this function will be called exactly 30 times per second. It could fluctuate a little on regular use. In the case that the game slows down and now this function is taking longer than it should be called, the object is moving slower than it really should be. And in the case the game speeds up dramatically, the object will be moving faster than it should be and will also appear to be skipping along in both cases.

You can ail this hex by increasing your position based on the time elapsed from the previous movement. That way when this function is called more often than you intended, the object's x position movement is minimized. If the function is called less than what you wanted, the x position movement is upped to match the speed of 5 pixels per second. Your function would have to have an argument with the time elapsed since the last "tick"

StepFunc( double dTimeElapsed )
{
Object.x += ( 5 * dTimeElapsed );
}


I explained it in the way I understood it. You can also refer here for more information on managing your time. http://www.koonsolo.com/news/dewitters-gameloop/

##### Share on other sites

Actually, the problem is with game play coding, like moving the players for an AI, making random movements like a flying saucer,..etc. I don't know the technqieus for achiving those things. Are there any books about that, or people just look at others code, or they run test infinetly, untill they get the effect ?

Edited by AhmedCoeia

##### Share on other sites

There are books out there, though I can't think of any particularly good ones I remember specifically.  But given the scope of your question you should really break things down a bit more.  In a generalized way of looking at things, there are three levels to what you are asking about.  There is the basic level of getting things to "move" properly when coded into a game loop (what Panda mentioned), then there is the basic control level which combines movements into simple concepts such as "turn and move to this point" and finally the higher level of "intelligence".

Most game books "learn game programming" books will cover the first two layers with a very very light version of the last layer.  Take an example pong game.  The ball is an example of all three layers in a very simplistic manner.  The first step is to make the ball move across the screen smoothly.  Now you add in the control layer which applies the rules of what to do when it hits the top or bottom wall (bounce) and hits or misses a paddle (bounce or score a point for the other player).  Thats actually a trivial but very good learning item to implement as a starting point.  The intelligence portion in such a game would be adding something to the second paddle so someone could play the game against the computer.

While this sounds exceptionally trivial, learning something like this is the stepping stone to learning everything else you talk about.  Just learn each item in turn as appropriate.

##### Share on other sites

Generally, as with most things programming, it is just try stuff and see what looks the best or what plays the best. Gotta start somewhere though. When it comes to movement and gameplay, you should first have a decent idea of what it should look like in your head and then write pseudocode that you think will achieve the effect. Then you can look at your pseudocode, but the actual implementation will vary greatly depending on what language / engine / libraries you are using. If what you write is not giving the desired effect, then the next step is to answer WHY it is not giving the desired effect.

Remember that for smooth movement changes you need to be changing velocity using acceleration. If your movement is rigid, maybe you are incrementing velocity instead acceleration based on player input. Every frame should have:

position(new) = position(old) + velocity*dt + 1/2 * acceleration * dt^2

velocity(new) = velocity(old) + acceleration * dt

##### Share on other sites

It does start off difficult. It took me a week to make a monster constantly move up and down.

My advice would be to have a good understanding of how the graphics window work: how the coordinates are laid out like and how these coordinate behave: do they stay the same, increase or decrease? Like how the origin is on the top left of the window, x increase as you go right but y is constant. y increase as you go down but x is constant.

At the end of the day, they are just a bunch of logic more specifically if and else statements. A bunch of if and else statements.

Then take these ideas and write the ideas and the logic down a piece of paper. Just debug the code on paper and figure out the problems in the code. The bug can only be in one place and it will never move. Sometimes, it can also be logic that you forgot to write. I experience this when I wrote my first basic AI code.

The reason why programming books don't teach you these things is because programming is a general idea. There are infinite posssibilities on what you can do with it. You are not suppose to "plug and chug" and guess it is going to work. It needs to make sense and that can only be achieved through understanding, logic and being methodical the same why you would approach a math or physics problem with that kind of mindset.

It is like saying "why don't books teach you how to make final fantasy or starcraft or zelda? At the end of the day, the games share basic code structure. Besides, it will spoil the fun and also prevent the reader from experimenting and doing it themselves. You are suppose to have an open mind and use that mind to come up with the code yourself. That is the beauty of programming. It is suppose make you tap the power of your mind.

On top of my head, try getting the snake head to move. If you can make it move in all for four directions, try making the body movement along with the head. Take it one step at a time and build off of that.

There are many ways to solve it. This is just one way I came up with on top of my head.

Good luck.

Edited by warnexus

##### Share on other sites

the process is not that complex, but easier to explain with a concrete example.

can you select one thing as an example that you'd like to know how to do?

from that one example, you'll then apply the process repeatedly.

so pick an example, like your snake or something.

##### Share on other sites

Thank you all for the input.

Norman, For example the centipede game, I would like to simulate the snake movement.

##### Share on other sites

so you mean centipede like this, eh?

##### Share on other sites

ok right quick off the top of my head...

snake has 10 sections

each section has an x and y value.

so you'll want an array[10] of int x,y

to move the snake:

move each section, from tail to head, to the x,y of the section in front of it. then move the head to its new x,y

in c:


struct section
{
int x,y
};

section snake[10];

void move_snake(x,y)   // x,y is new location for head, body follows automatically. be sure to move the head just one tile at a time!
{
int i;
for (i=9; i>=1; i--) snake[i]=snake[i-1];
snake[0].x=x;
snake[0].y=y;
}


Edited by Norman Barrows

1. 1
2. 2
JoeJ
18
3. 3
4. 4
frob
12
5. 5

• 13
• 16
• 13
• 20
• 13
• ### Forum Statistics

• Total Topics
632188
• Total Posts
3004650

×