www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/23/12:45:31

From: "Locke" <lockeg AT usa DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: My sprites move too fast
Date: 23 Sep 1997 05:24:52 GMT
Organization: InterActive Network - Serving S. NJ (609)227-4428
Lines: 80
Message-ID: <01bcc7e1$83383840$812042d1@ghema.jaguarsystems.com>
References: <01bcc76c$97466660$473e64c2 AT default>
NNTP-Posting-Host: 209.66.32.129
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Laine <laine AT iwn DOT fi> wrote in article
<01bcc76c$97466660$473e64c2 AT default>...
> Hi!
> 
> I'm doing a game with DJGPP and Allegro and
> I've got problems. Not much anymore, but still
> one. I've got too sprites, one is a falling object
> and the other is a board with wich the player tries
> to catch the object. I've managed to make them move
> at a different speed, but they both go too fast.

First Off, This is NEVER a PROBLEM!  You can always slow things down, but
somtimes it's VERRRRRYYYY Hard to speed things up!

> So here's the QUESTION:
>  How can I slow them down. I don't think a for-loop
>  is a good idea, is it? What is a good idea?

A for-loop is not a good idea.  You said you were using Allegro, right? 
Well, now would be a good time to learn about the timer functions.  If you
will all please exuse my listing code here, I will give you an example.

//First you need variables to use as a counter, an animation flag, and a
wait flag:
   volatile int counter=0;
   int time_to_animate=0;
   int wait=0;

// Next, Write a function to increment the counter:
   void inc_counter(void)
   {
      counter++;
      if(counter>1000){counter = o;}
   }
   END_OF_FUNCTION(inc_counter);

// Now, somewhere in your initialization code, you have to put:
   install_timer();
   LOCK_VARIABLE(counter);
   LOCK_FUNCTION(inc_counter);
   install_int(inc_counter,10); //The counter will now increment  every 10
milliseconds
   
// Then, in the beginning your main loop:
   time_to_animate = 0;
   if(counter > 990){counter = 0;wait = 0;}
   if(counter > wait+ANIMATION_SPEED){wait = counter;time_to_animate = 1;}
   // ANIMATION_SPEED is the delay between movement int 10's of
milliseconds

// When it is time to animate or move  something, just use:
   if(time_to_animate){
      Put all your animation code here
      ...
   }

That's all there is too it!  This is how I do it anyway.  There are
probable much better ways to do it, but I find that this works pretty well
for me and is fairly easy to implement.  If you nee different speeds, you
can use other counters, but you should increment them all with the same
timer function, since you only get a few to work with.

> 
> PS. was this message too long? Thanks for reading
>       it. (I'm new here and I don't know how I'm
>       supposed to behave here :-)

It was just fine, my reply, however, may be a bit long for the reagulars. 
Sorry about that!  If you have any questions, problems, or suggestions
about what I said, then feel more than free to write me... I'd love to help
in any way and would be more than willing to explain this more thouroughly
if you want me to.

----------------------------------------------------------------------------
------------
-- locke
lockeg AT usa DOT net
----------------------------------------------------------------------------
------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019