Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
slow down time
#6
(09-29-2024, 09:40 AM)Marcus Wrote: Here's an old example of something where I just added a slowmotion effect.


Code:
include "polyline.n7"

set window "path", 512, 512
set redraw off

' Create ship image.
img = createimage(32, 23)
set image img
set color 192, 192, 192
draw poly [0, 0, 31, 12, 0, 22], true
set color 0, 128, 255
draw ellipse 8, 12, 6, 4, true
set image primary
set image colorkey img, 0, 0, 0

' Create path for ships to follow.
path = PolyLine([[-16, -16], [384, 128], [384, 384], [128, 384], [128, 128], [528, -16]], false)
' An array for the ships.
ships = []
' A counter, everytime it reaches 0 a new ship will spawn.
spawnTimer = 0

' Some background stars.
stars = []
for i = 1 to 250
    c = 64 + rnd(192)
    stars[sizeof(stars)] = [
            x: rnd(512), y: rnd(512),
            dy: 25 + rnd(200),
            c: [c, c, c],
            Update: function(dt)
                this.y = (this.y + this.dy*dt)%512
            endfunc,
            Draw: function()
                set color this.c
                draw pixel this.x, this.y
            endfunc]
next

' Game speed.
gameSpeed = 1

prevTime = clock()
while not keydown(KEY_ESCAPE)
    ' Delta time, low cap at 15 fps.
    t = clock()
    dt = (min(t - prevTime, 66))/1000
    prevTime = t

    ' Slow down by pressing spacebar.
    if keydown(KEY_SPACE)  gameSpeed = max(gameSpeed - 2*dt, 0.25)
    else  gameSpeed = min(gameSpeed + 2*dt, 1)
    ' Multiply delta time with gameSpeed.
    dt = dt*gameSpeed       

    ' Time to spawn a new ship?
    spawnTimer = spawnTimer - dt
    if spawnTimer <= 0
        spawnTimer = spawnTimer + 0.246
        ' Add a ship to the ships array.
        ships[sizeof(ships)] = [
                img: img,              ' Image
                x: 0, y: 0, angle: 0,  ' Position and angle.
                path: path, dist: 0,    ' Path and traveled distance along path.
                ' Update function.
                Update: function(dt)
                    ' Get position and direction from path.
                    pos = this.path.GetPoint(this.dist, true)
                    dir = this.path.GetDirection(this.dist, true)
                    ' Get point returns an unset variable if the distance parameter is invalid. In
                    ' that case the ship should be removed - return false.
                    if pos
                        this.x = pos[0]
                        this.y = pos[1]
                        this.angle = atan2(dir[1], dir[0])
                        ' Move 300 pixels per second.
                        this.dist = this.dist + 300*dt
                    else
                        return false
                    endif
                    return true
                endfunc,
                ' Draw function.
                Draw: function()
                    set color 255, 255, 255
                    draw image xform this.img, this.x, this.y, 1, 1, this.angle, 15, 12
                endfunc]
    endif
   
    ' Update ships.
    i = 0
    while i < sizeof(ships)
        ' If update returns false, the ship should be removed.
        if ships[i].Update(dt)  i = i + 1
        else  free key ships, i
    wend
   
    ' Update stars.
    foreach s in stars  s.Update(dt)
   
    ' Draw.
    set color 0, 0, 0
    cls
    foreach s in stars  s.Draw()
    foreach s in ships  s.Draw()
   
    set color 255, 255, 255
    set caret 256, 248
    center sizeof(ships) + " ships"
    set caret 256, 480
    center "Hold spacebar for slow motion!"
   
    redraw
    wait 1
wend

Thank you very much Marcus, that's what I wanted. Smile

(09-28-2024, 09:28 PM)johnno56 Wrote: My apologies for butting in on your conversation... After reading the part about 'delta time', I could not help but recall a similar problem that I had using rcbasic... Movement of spites/bullets were moving way too fast.

Code:
timeDelay = 0

...

function updateDelta()
    dt = (Timer - timedelay) * 0.001
    timedelay = Timer
    return timedelay
end function

Calling updateDelta() during the main game loop solved my issue. Of course, this was not written with N7, but should be easily converted... I hope that I have not added to your difficulties... I will not be offended if you choose not to use this or find a better method...

J

Every example is always welcome, thank you. Smile
Reply


Messages In This Thread
slow down time - by aliensoldier - 09-28-2024, 01:45 PM
RE: slow down time - by Marcus - 09-28-2024, 05:53 PM
RE: slow down time - by johnno56 - 09-28-2024, 09:28 PM
RE: slow down time - by Marcus - 09-29-2024, 09:40 AM
RE: slow down time - by 1micha.elok - 09-30-2024, 04:53 AM
RE: slow down time - by aliensoldier - 10-04-2024, 05:36 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)