The most common error is probably sprinkling the whole place with low
level animation and control stuff…
Try using a common class or struct for all sprites, and perhaps other
objects as well, if any. Something like this:
struct gfx_object_t
{
float x, y; /* Position */
float vx, vy; /* Speed (pixels/CS frame) */
float ax, ay; /* Acceleration (pixels/(CS frame)^2) */
float aframe; /* Current sprite image frame */
float afirst; /* First frame in loop */
float alast; /* Last frame in loop */
float aspeed /* Animation speed (ms/frame) */
/*
* Called once per CS frame
*/
void (*frame)(struct gfx_object_t *me);
/*
* Called when collision is detected
*/
void (*hit)(struct gfx_object_t *me,
struct gfx_object_t *other);
...
...
...
};
Note that using floats is the easy way. If you end up doing lots of
float<->int conversions all the time, fixed point would be significantly
faster. For heavy physics and other calculations, sticking with float
might still be a good idea.
After that, you just build your game around that struct. Build game logic
into these objects (rather than around them), to keep things simple and
object oriented. (Keeping track of dynamically allocated objects “from
the outside” quickly gets very messy, and easilly results in hard to
find pointer bugs.)
Extend the struct as required, but keep in mind that adding generally
useful functionality, rather than game specific features will make your
code simpler and easier to maintain in the long run, even if you have to
spend slightly more time thinking before hacking away.
One very important rule, that applies to all (serious) forms of
programming:
NEVER underestimate the importance of clean, readable code!
Games with messy code are rarely finished, especially not if no one is
paying to get it done. Hunting stupid spaghetti induced bugs for ten
hours after every minor change quickly becomes very boring.
No, you won’t remember what you were thinking the next time you look at
that code. Trust me.
//David Olofson — Programmer, Reologica Instruments AB
.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |
-------------------------------------> http://olofson.net -'On Friday 26 October 2001 16:58, Matt Greer wrote:
on 10/26/01 3:01 AM, Steve Lupton at SL at cre.co.uk wrote:
Hi
Can anyone point me in the right direction for finding some example
Sprite Momentum code.
I would like to be able to set a sprite in motion, headed towards a
defined location. I would like to be able to specify how long (in
time) it should take to get there. I would also like to be able to
specify acceleration and deceleration rates.
I’m sure I could find what I’m looking for, just not exactly sure
what to look for in the first place!
I’m brand new to game programming and I’m finding myself stumped on a
lot of stuff like this. I’ve looked at all the sites like gamasutra,
gamedev, etc, but I’m not really finding what I’m after. Source code to
other games isn’t as helpful as I’d like (although it has helped). So
if anyone can point out some stuff I’d be very grateful
Right now I’m just making my game blind folded and I just know I’m
doing most everything very poorly