Starfield

Discussion in 'Programming' started by Jan2, Jul 20, 2016.

  1. Jan2

    Jan2 Member

    Joined:
    Jul 20, 2016
    Posts:
    3
    How can I make a randomly generated starfield, where stars are placed randomly? I expect stars not to be instances, because this will be a very bad solution. They should be DRAWN like this!

    rgbcolor = make_colour_rgb(255,255,255);
    draw_set_color(rgbcolor);
    for(i=0;i<50;++i)
    {
    x_pos = random_range(0,room_width);
    y_pos = random_range(0,room_height);
    draw_circle(x_pos,y_pos,3,false);
    }
     
    Last edited: Jul 20, 2016
  2. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,846
    If no other random functions have to be performed while the stars exist,
    you could use randomize in the create event, store the current seed
    and restore that seed every time before drawing.

    Otherwise, you could calculate every x_pos and _ypos in the create event and store the coordinates in an array
    and loop through that array in the draw event.
     
    Jan2 likes this.
  3. maartenvt

    maartenvt Guest

    If you don't need to interact with the stars later you could draw them on a surface.
     
    Jan2 likes this.
  4. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,846
    The surface idea has its advantages and disadvantages.
    It will allow faster drawing, but you will need to take into account the fact that surfaces can be deleted whenever (e.g. when minimizing).
    They also take up way more memory.
     
    Jan2 likes this.
  5. icuurd12b42

    icuurd12b42 TMC Founder GMC Elder

    Joined:
    Apr 22, 2016
    Posts:
    1,840
    draw a sprite not a circle
    call random_set_seed(1000); //arbitrary value
    before the loop

    and
    randomize() after the loop.

    this should make the stars draw at the same position each draw
     
    Last edited: Jul 22, 2016
    Aura and Jan2 like this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice