GameMaker - How do I make a simple starfield?

Started by Amon, May 12, 2019, 22:37:01

Previous topic - Next topic

Amon

In agk2 I would do this
// Project: ZoneBuster
// Created: 19-05-11

// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle("ZoneBuster")
SetWindowSize(1024, 768, 0)
SetWindowAllowResize(1) // allow the user to resize the window

// set display properties
SetVirtualResolution(1024, 768) // doesn't have to match the window
SetOrientationAllowed(1, 1, 1, 1) // allow both portrait and landscape on mobile devices
SetSyncRate(60, 0) // 30fps instead of 60 to save battery
SetScissor(0, 0, 0, 0) // use the maximum available screen space, no black borders
UseNewDefaultFonts(1)

#insert "constants.agc"

img_RedShip64x64 = LoadImage( "RedShip64x64.png" )
img_star1px = LoadImage( "star1px.png" )

type ship
    sprite as integer
    x as float
    y as float
    sp as float
endtype
global shiplist as ship[]

type starfield
    sprite as integer
    x as float
    y as float
    sp as float
endtype
global slist as starfield[]

for x = 0 to 250
    local s as starfield
    s.sprite = CreateSprite( img_star1px )
    s.x = Random(1, 1024) / 1.0
    s.y = random(1, 768) / 1.0
    s.sp = Random(100, 500) / 1.0
    slist.insert(s)
next




global PauseCounter as integer
PauseCounter = 0
global ftimer as float = 60
Dim FrameLast#[0]
Dim FrameX#[0]

do

    FrameTimer(ftimer)

    if GetRawKeyPressed(KEY_P) and PauseCounter = 0
        PauseCounter = PauseCounter + 1
        ftimer = 0
    elseif GetRawKeyPressed(KEY_P) and PauseCounter = 1
        FrameTimer_Reset()
        ftimer = 60
        PauseCounter = 0
    endif   
           
    if slist.length > -1
        for x = slist.length to 0 step -1
            slist[x].y = slist[x].y + FrameX#[0] * slist[x].sp
            if slist[x].y > 770
                slist[x].y = -2
                slist[x].x = Random(1, 1024) / 1.0
            endif
            SetSpritePosition(slist[x].sprite, slist[x].x, slist[x].y)
        next
    endif
   
    Print(ScreenFPS())
    Sync()
loop

Function FrameTimer(Ref#)
   Diff# = ABS(Timer() - FrameLast#[0])
   Ideal# = 1000.0 / Ref#
   FrameX#[0] = Diff# / Ideal#
   If Diff# > 1000 then FrameX#[0] = 0
   FrameLast#[0] = Timer()
EndFunction

Function FrameTimer_Reset()
   FrameLast#[0] = Timer()
EndFunction

Hows can I do's it in teh Gamemaker?

MikeHart

Mmh, i would say... read the docs, study examples, watch some YT tutorials. I am sure that there is plenty of stuff out there about game maker.

Xerra

It's going to depend on what type of starfield you're actually looking for to give a definitive answer but this thread is a good starting point.

https://forum.yoyogames.com/index.php?threads/starfield-help.57027/

It's actually much easier to just use an object based system for a starfield if you don't want anything too clustered. GMS2 can throw hundreds of small objects around with no real speed loss, especially simple pixel sprites as stars. However, sometimes you want to use rotation effects, make it a simulated 3D (where they travel towards you) or something a little more complicated than just a top/down scrolling parallax effect, for example.

Can you be a bit more specific about what type of effect you're looking for?
M2 Pro Mac mini - 16GB 512 SSD
ACER Nitro 5 15.6" Gaming Laptop - Intel® Core™ i7, RTX 3050, 1 TB SSD
Vic 20 - 3.5k 1mhz 6502

Latest game - https://xerra.itch.io/Gridrunner
Blog: http://xerra.co.uk
Itch.IO: https://xerra.itch.io/

Amon

Just a simple startfield was needed. :)
I created a controler object and placed it in my room:
In its step event
globalvar sp;globalvar xl;
globalvar yl;

for ( xi = 0; xi < 100; xi++ )
{
    xl = random_range(1, 1024);
    yl = random_range(1, 768);
    sp = random_range(1, 3);
    var inst = instance_create_layer(xl, yl, "Instances", objStar);
    with (inst)
    {
        inst.vspeed = sp;
        //inst.x = xl;
        //inst.y = yl;
    }
}

in the star objects step event:

y += vspeed;    if ( y > 770)
    {
        y = -2;
        x = random_range(1, 1024);
    }