QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Jaze on May 02, 2021, 03:57:48 pm

Title: simultaneous loops?
Post by: Jaze on May 02, 2021, 03:57:48 pm
I have one loop keeping track of something moving diagonally in one of the four directions. I have a second thing going vertically down. They have to be running at the same time but I don't know how to run two separate loops simultaneously. Is this even possible?
Title: Re: simultaneous loops?
Post by: bplus on May 02, 2021, 04:04:43 pm
You can draw things individually and then _DISPLAY all when at end of loop.
Title: Re: simultaneous loops?
Post by: Jaze on May 02, 2021, 04:19:43 pm
OK. I'm not sure how to implement that suggestion. But I just thought of a way to put them both in the same loop. Thanks for pointing me to that tutorial because I'm going to need to study collision detection when I get there
Title: Re: simultaneous loops?
Post by: bplus on May 02, 2021, 04:21:24 pm
Hey if you get stuck just post code and describe problem, we will try to help.
Title: Re: simultaneous loops?
Post by: Cobalt on May 03, 2021, 01:53:17 pm
Just perform both movements\Actions in a single loop. thats your best bet.
Title: Re: simultaneous loops?
Post by: SMcNeill on May 03, 2021, 02:09:22 pm
OK. I'm not sure how to implement that suggestion. But I just thought of a way to put them both in the same loop. Thanks for pointing me to that tutorial because I'm going to need to study collision detection when I get there

A simple example:

DO
    CLS
    Y = Y + 1
    X = X + 1
    LOCATE Y, 1: PRINT “HELLO”
    LOCATE Y, X: PRINT “WORLD”
    _DISPLAY
    _DELAY .5
LOOP UNTIL Y > 20

One locate prints from top tobottom, moving down the screen.   The other prints and moves diagonally across the screen.
Title: Re: simultaneous loops?
Post by: Jaze on May 05, 2021, 05:20:58 pm
That only works if you want the characters to move at the same speed
Title: Re: simultaneous loops?
Post by: 191Brian on May 06, 2021, 02:47:37 am
You can make them move different speeds by adding a different offset i.e
Char1X = Char1X + 1
Char2X = Char2X + 2

Will make character 2 move twice as fast as character 1 horizontally
Title: Re: simultaneous loops?
Post by: bplus on May 06, 2021, 11:57:25 am
That only works if you want the characters to move at the same speed

use x, y to track object location like objX, objY

then use dx to track object "speeds"  along x axis and dy to track object speed on y axis (per frame or  drawing loop)
dx, dy = change of x, y on respective axis like objDX, objDY

Use the x, y for drawing obj, and dx, dy for updating the location ie
x = x + dx
y = y + dy

so
objX = objX + objDX
objY = objY = objDY

then check if x, y still inbounds correcting x, y and dx, dy when not.
Title: Re: simultaneous loops?
Post by: Jaze on May 08, 2021, 08:31:39 pm
That might work. Putting them in the same loop will be a challenge but I think I'm up to it
Title: Re: simultaneous loops?
Post by: 191Brian on May 09, 2021, 04:02:14 am
Hi Jaze

Good luck hope you get it working. I think you would find that's how game loops are programmed for example in my invaders game the game loop updates the players ship, the players bullets, the aliens, the random alien ship and explosion effects that's 5 different elements moving at various speeds.