May 24, 2019, 11:15:39 PM

Author Topic: GameMaker - How do I make a simple starfield?  (Read 100 times)

Offline Amon.

  • Jr. Member
  • **
  • Posts: 97
  • What? There's no ceiling outside?
    • Amon.Pro
GameMaker - How do I make a simple starfield?
« on: May 12, 2019, 10:37:01 PM »
In agk2 I would do this
Code: [Select]
// 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?
Windows 10 Pro - 32GB DDR4 RAM - GEFORCE RTX 2070 8GB - AMD RYZEN 7 8 CORE - WATERCOOLING.

Offline MikeHart

  • Hero Member
  • *****
  • Posts: 571
  • Cerberus-X Dev Team
    • Cerberus X
Re: GameMaker - How do I make a simple starfield?
« Reply #1 on: May 13, 2019, 05:38:26 AM »
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.

Offline Xerra

  • Hero Member
  • *****
  • Posts: 586
    • Retro Evolved
Re: GameMaker - How do I make a simple starfield?
« Reply #2 on: May 13, 2019, 07:10:07 AM »
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?

Offline Amon.

  • Jr. Member
  • **
  • Posts: 97
  • What? There's no ceiling outside?
    • Amon.Pro
Re: GameMaker - How do I make a simple starfield?
« Reply #3 on: May 13, 2019, 06:40:52 PM »
Just a simple startfield was needed. :)
I created a controler object and placed it in my room:
In its step event
Code: [Select]
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:

Code: [Select]
y += vspeed;    if ( y > 770)
    {
        y = -2;
        x = random_range(1, 1024);
    }
Windows 10 Pro - 32GB DDR4 RAM - GEFORCE RTX 2070 8GB - AMD RYZEN 7 8 CORE - WATERCOOLING.