Results 1 to 2 of 2

Thread: More move at same time

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2018
    Posts
    41

    More move at same time

    Hi, i wanna make a program, where 3-7 circle (inside circle are letter/letters) they move at the same time, and if someone hit buttons on keyboard ( letters in circle) the circles are disappeared.

    So. how can i make moves, at sime time...independently of each other? Like multitasking?

    I wane no idea, or code, so plz, advise me.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: More move at same time

    You won't, really. You'll just fake it.

    Nothing moves on the screen. The screen is always showing a static image, it's just capable of changing from one static image to another so fast that it looks like the objects are moving.

    Therefore, to move multiple objects, you'd be drawing them all in their new positions, and doing so over and over again. There are two ways that this can be done.

    1) Use a timer.

    2) Use a game loop.

    The former is the easier one to do, but the motion might appear jerky unless the timer is very fast and the drawing is carefully thought out. For example, if your timer interval is 100, then the images will change 10 times per second. That will likely look a bit jerky unless they move very slowly. You could decrease the timer interval to 50, or so, which will make the motion smoother, but you can't get the timer interval too low, or the timer will tick again before you calculate the new position and do the drawing. So, if things get too jerky, a game loop would work.

    The idea behind a game loop is that you are drawing as fast as you are able. It is essentially doing this:
    Code:
    Do
     timeInterval = Now - lastInterval
     MoveItems(timeInterval)
     DrawItems()
    Loop
    What is happening is that the time since the last drawing is calculated each time through the loop. The MoveItems method would figure out how far the item would have moved during the interval, and draws the item at its new location.

    Normally, a game loop would include a few other steps, like getting input from the user (keyboard and mouse inputs), figuring out behavior, and so forth. It will also be drawing everything on the screen, so if there are a lot of items moving around on the screen, then there will be a whole lot of calculations in the Move method, which will mean that the whole loop will take longer.

    Game loops are almost certainly not necessary if you are just moving some circles around on the screen. Drawing some circles is not very taxing for the computer, especially if there aren't all that many. Therefore, the first thing to try is adding a timer with an interval of around 100, then every time it ticks, move the circle a bit and invalidate the screen. Do all the drawing in the Paint event handler. If that isn't smooth enough, then you can try decreasing the timer interval. If you can't get it smooth enough, still, then the drawing might be sufficiently complex that looking into a different approach using a game loop (in which case, look at MonoGame) would be necessary. Game loops are more complicated, though, so start with the timer approach and only move on if you can't make the timer work sufficiently.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width