Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
return movement
#9
(05-22-2024, 05:13 AM)kevin Wrote: Thanks for the example Kevin. Smile

But it is too complicated for me and I can't understand it, the only way I could understand it would be to create an example where there is only one enemy, who after a few seconds moves down and after a few seconds returns to his initial position.

For the code to be as simple as possible, it would be best to only have the code of that enemy and nothing else, this way you could study more clearly what the code does. No matter how much time passes, I still don't understand well when there is a lot code, sorry for my clumsiness.


I fully understand. Hope this is better for you - it is based on Marcus's solution to my question on this subject from last March.
Code:
include "polyline.n7"

visible screen_w = 640,screen_h = 480
'Open a window
set window "Diving example",screen_w,screen_h
'enable double buffering
set redraw off

invader = [x:300,y:50,dx:1,w:32,h:16,dive_x:unset,dive_y:unset,diving:false]

##############################################################################################
#######################################   GAME LOOP  #########################################
##############################################################################################
do
'release the invader occasionally
timer = (timer + 1 ) % 500
if timer = 499
    invader.diving = true
    ' This is the additional code provided by Marcus back in March
    ' Very simple path, just dive to the bottom center of the screen and back again.
    invader.path = PolyLine([[invader.x, invader.y], [width(primary)/2, height(primary) - 20], [invader.x, invader.y]],false)
    ' Set traveled distance to 0.
    invader.path_dist = 0
    ' We still need to update the diver's regular position. So when diving, the invader's position is
    ' stored in dive_x, dive_y. Use these coordinates when drawing and doing collision tests for a
    ' diving invader.
    invader.dive_x = invader.x
    invader.dive_y = invader.y
    ' EndMarcus
endif

'move the invader and keep it on screen
invader.x = invader.x + invader.dx
if invader.x > 600 or invader.x < 40 then invader.dx = - invader.dx 

' Marcus code
    if invader.diving = true
        ' Set the last control point of the path to the invaders normal position.
        '   A problem here is that the regular invaders move vertically with huge steps. This
        ' doesn't look good for the movement of a diver. So rather than doing this:
        'invaders[i].path.ModifyControlPoint(2, invaders[i].x, invaders[i].y)
        ' , we can interpolate the y coordinate:
        y = invader.path.GetControlPoint(2)[1] ' current y of the last control point.
        invader.path.ModifyControlPoint(2, invader.x, y*0.95 + invader.y*0.05)
        ' Move 4 pixels.
        invader.path_dist = invader.path_dist + 4
        pos = invader.path.GetPoint(invader.path_dist, true)
        ' Still on path?
        if pos
            invader.dive_x = pos[0]
            invader.dive_y = pos[1]
        ' No longer diving.
        else
            invader.diving = false
        endif
    endif
' EndMarcus


'clear the screen
set color 255,255,255
cls
set color 0,0,0

'draw the invader as a solid rectangle - only show an outline when diving to see
'where the original path would have been
if invader.diving = false
    draw rect invader.x,invader.y,invader.w,invader.h,true
else
    draw rect invader.x,invader.y,invader.w,invader.h,false
    draw rect invader.dive_x,invader.dive_y,invader.w,invader.h,true
endif

'copy back buffer to the screen
redraw
'cap FPS to 60
fwait 60

until keydown(KEY_ESCAPE)

Thank you Kevin. Smile

This example is much easier to study. I think that Naalaa should have more short examples like this that teach different game mechanics and useful tricks but always with super short examples so that everyone can study them easily. Greetings
Reply


Messages In This Thread
return movement - by aliensoldier - 05-20-2024, 02:47 PM
RE: return movement - by Marcus - 05-20-2024, 03:27 PM
RE: return movement - by aliensoldier - 05-20-2024, 08:13 PM
RE: return movement - by kevin - 05-21-2024, 04:37 AM
RE: return movement - by kevin - 05-21-2024, 07:44 AM
RE: return movement - by aliensoldier - 05-21-2024, 08:59 PM
RE: return movement - by kevin - 05-22-2024, 05:13 AM
RE: return movement - by aliensoldier - 05-22-2024, 02:02 PM
RE: return movement - by johnno56 - 05-21-2024, 10:28 AM
RE: return movement - by kevin - 05-22-2024, 04:28 PM

Forum Jump:


Users browsing this thread: 3 Guest(s)