Author Topic: simultaneous loops?  (Read 4091 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
simultaneous loops?
« 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?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: simultaneous loops?
« Reply #1 on: May 02, 2021, 04:04:43 pm »
You can draw things individually and then _DISPLAY all when at end of loop.

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
Re: simultaneous loops?
« Reply #2 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

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: simultaneous loops?
« Reply #3 on: May 02, 2021, 04:21:24 pm »
Hey if you get stuck just post code and describe problem, we will try to help.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: simultaneous loops?
« Reply #4 on: May 03, 2021, 01:53:17 pm »
Just perform both movements\Actions in a single loop. thats your best bet.
Granted after becoming radioactive I only have a half-life!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: simultaneous loops?
« Reply #5 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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
Re: simultaneous loops?
« Reply #6 on: May 05, 2021, 05:20:58 pm »
That only works if you want the characters to move at the same speed

Marked as best answer by Jaze on May 08, 2021, 04:30:41 pm

Offline 191Brian

  • Newbie
  • Posts: 91
    • View Profile
    • My Itch page
Re: simultaneous loops?
« Reply #7 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
Brian ...

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: simultaneous loops?
« Reply #8 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.
« Last Edit: May 06, 2021, 11:59:22 am by bplus »

Offline Jaze

  • Newbie
  • Posts: 86
    • View Profile
Re: simultaneous loops?
« Reply #9 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

Offline 191Brian

  • Newbie
  • Posts: 91
    • View Profile
    • My Itch page
Re: simultaneous loops?
« Reply #10 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.
« Last Edit: May 09, 2021, 04:05:09 am by 191Brian »
Brian ...