Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PolyLine library
#7
(03-01-2024, 06:32 PM)Marcus Wrote: There's actually a built in command to insert stuff into an existing array. Nope, for some reason it's not documented, and F1 in NED shows no help. But here we go:

Code:
                points = insert_into_array(points,i + 1,[mousex(),mousey()])

can be done with:

Code:
  
                insert points, i + 1, [mousex(), mousey()]

That's good to know - I thought that it would have been a good command to have, and spent a while going through the documentation. No harm done though - it was a good challenge to write a function for it Smile
When you are considering examples to write for the library, would you consider this, if it is possible with the library? I have code below which is the basis for a space invader type game. Every 2 seconds, an invader is chosen at random, and its state is changed from "active_now" to "diving". What I have been trying to code is for the chosen invader to proceed along the Polyline for a full cycle, and then return to where he would have been in the original invader grid. It would then revert from the state of "diving"  back to "active_now". Hope this is clear? Thanks.

Code:
include "polyline.n7"

visible screen_w = 640,screen_h = 480
'Open a window
set window "Invaders",screen_w,screen_h
'enable double buffering
set redraw off
'====  set up the invaders  ========
visible number_of_invaders = 24
visible invaders = fill([x:0,y:0,w:32,h:16,img:0,x_inc:1,y_inc:20,active_now:true,diving:false],number_of_invaders)
startx = 100
starty = 50
countx = 0
for i = 0 to number_of_invaders - 1
    invaders[i].x = startx
    invaders[i].y = starty
    startx = startx + 40
    countx = countx + 1
    if countx = 8
        countx = 0
        startx = 100
        starty = starty + 24
    endif   
next
'========================================
visible spaceship = [x:screen_w / 2,y:screen_h - 80,w:64,h:32]
'========================================
visible spaceship_missiles = []
visible max_spaceship_missiles = 12
'======== polyline stuff ====================
visible points = [[100,100],[100,400],[400,400],[400,100],[100,100]]
' Create a polyline from the points.
visible path = PolyLine(points)
distance = 0
curved = false
speed = 4

timer = 0

############  to calculate FPS  ##########################
visible framecount,lasttime = 999,fps,frametime = 0,starttime = 0,endtime = 0
##########################################################
##############################################################################################
#######################################   GAME LOOP  #########################################
##############################################################################################
do
###  for FPS calc #########
framecount = framecount + 1
starttime = clock()
###########################
timer = (timer + 1 ) % 120
if timer = 119 then launch_invader()

move_invaders()
move_spaceship()



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

for i = 0 to number_of_invaders - 1
    if invaders[i].active_now or invaders[i].diving
        if invaders[i].diving = true
            draw rect invaders[i].x,invaders[i].y,invaders[i].w,invaders[i].h,false
        else
           draw rect invaders[i].x,invaders[i].y,invaders[i].w,invaders[i].h,true
       endif
    endif   
next
set color 255,0,0
draw rect spaceship.x,spaceship.y,spaceship.w,spaceship.h
set color 0,0,0
set caret screen_w/2,10
write "mouse = " + mousex() + " / " + mousey();wln
write "FPS = " + str(fps);wln
write sizeof(path)

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

#######  FPS calc  ############################
endtime = clock()
frametime = frametime + endtime - starttime
if frametime > 1000 # 1 second
    fps = framecount
    framecount = 0
    frametime = 0
endif
################################################


until keydown(KEY_ESCAPE)

#########################################################################################
#################################  FUNCTIONS  ###########################################
#########################################################################################
function move_invaders()
for i = 0 to number_of_invaders - 1
    if invaders[i].active_now or invaders[i].diving
        invaders[i].x = invaders[i].x + invaders[i].x_inc
        if invaders[i].x > screen_w - invaders[i].w * 2 or invaders[i].x < invaders[i].w
            for j = 0 to number_of_invaders - 1
                invaders[j].x_inc = - invaders[j].x_inc
                invaders[j].y = invaders[j].y + invaders[j].y_inc
            next
        endif       
    endif
next   
   
endfunc
################################################
function move_spaceship()
if keydown(KEY_LEFT) then spaceship.x = max(spaceship.x - 2,0)
if keydown(KEY_RIGHT) then spaceship.x = min(spaceship.x + 2,screen_w - spaceship.w)
endfunc
################################################
function launch_invader()

possible_diver = []
selected_diver = 9999
for p = 0 to number_of_invaders - 1
    if invaders[p].active_now
        possible_diver[sizeof(possible_diver)] = p
    endif
next   
selected_diver = rnd(sizeof(possible_diver))
invaders[selected_diver].diving = true
invaders[selected_diver].active_now = false
endfunc
################################################
Reply


Messages In This Thread
PolyLine library - by Marcus - 02-29-2024, 04:25 PM
RE: PolyLine library - by aliensoldier - 02-29-2024, 04:54 PM
RE: PolyLine library - by kevin - 03-01-2024, 11:32 AM
RE: PolyLine library - by Marcus - 03-01-2024, 02:40 PM
RE: PolyLine library - by kevin - 03-01-2024, 03:02 PM
RE: PolyLine library - by Marcus - 03-01-2024, 06:32 PM
RE: PolyLine library - by kevin - 03-01-2024, 08:05 PM
RE: PolyLine library - by Marcus - 03-02-2024, 01:23 PM
RE: PolyLine library - by johnno56 - 03-01-2024, 09:36 PM
RE: PolyLine library - by Marcus - 03-01-2024, 10:03 PM
RE: PolyLine library - by kevin - 03-02-2024, 04:48 PM
RE: PolyLine library - by Marcus - 03-02-2024, 06:20 PM
RE: PolyLine library - by 1micha.elok - 03-05-2024, 06:53 AM
RE: PolyLine library - by kevin - 03-05-2024, 08:42 AM
RE: PolyLine library - by 1micha.elok - 03-05-2024, 11:15 AM
RE: PolyLine library - by Marcus - 03-05-2024, 08:52 PM
RE: PolyLine library - by Marcus - 03-08-2024, 08:21 PM
RE: PolyLine library - by 1micha.elok - 03-09-2024, 02:53 AM
RE: PolyLine library - by Marcus - 03-09-2024, 07:43 AM
RE: PolyLine library - by Marcus - 03-10-2024, 10:10 AM
RE: PolyLine library - by 1micha.elok - 03-11-2024, 12:52 AM

Forum Jump:


Users browsing this thread: 3 Guest(s)