Welcome, Guest
You have to register before you can post on our site.

Username
  

Password
  





Search Forums

(Advanced Search)

Forum Statistics
» Members: 35
» Latest member: coronaman
» Forum threads: 137
» Forum posts: 1,163

Full Statistics

Online Users
There are currently 97 online users.
» 0 Member(s) | 97 Guest(s)

Latest Threads
Manual
Forum: Everything else
Last Post: 1micha.elok
40 minutes ago
» Replies: 1
» Views: 19
Xmas New Year
Forum: Suggestions
Last Post: Marcus
11-22-2024, 03:36 PM
» Replies: 17
» Views: 3,039
Santa by Marcus
Forum: NaaLaa 6 Code
Last Post: johnno56
11-18-2024, 05:41 PM
» Replies: 7
» Views: 156
How to code efficiently
Forum: NaaLaa 7 Questions
Last Post: 1micha.elok
11-17-2024, 09:31 AM
» Replies: 4
» Views: 171
How to make a race track ...
Forum: NaaLaa 7 Questions
Last Post: kevin
11-10-2024, 04:02 PM
» Replies: 8
» Views: 426
Start of a silly Galaga s...
Forum: NaaLaa 7 Code
Last Post: johnno56
11-08-2024, 08:22 AM
» Replies: 3
» Views: 201
Theme Editor
Forum: Everything else
Last Post: johnno56
11-05-2024, 06:45 PM
» Replies: 2
» Views: 155
Scrolling Text
Forum: Everything else
Last Post: kevin
11-04-2024, 02:17 PM
» Replies: 10
» Views: 485
Naalaa origins
Forum: Everything else
Last Post: johnno56
10-25-2024, 07:21 AM
» Replies: 2
» Views: 296
Maze generation
Forum: NaaLaa 7 Code
Last Post: johnno56
10-23-2024, 09:05 AM
» Replies: 17
» Views: 1,679

 
  problems with collisions in pong
Posted by: aliensoldier - 01-24-2024, 06:59 PM - Forum: NaaLaa 7 Questions - Replies (7)

I'm trying to create the pong game and I'm having problems when the ball collides with the top and bottom of the moving player, the ball doesn't bounce, it just goes into the player's graph.

This happens when the player is moving, standing still it does not happen, the problem is in the ball.collision_player method of the ball.

I show all the code I have for now:

Code:
'pong
include "player.n7"
include "player-cpu.n7"
include "ball.n7"

set window "example pong",640,480,false
set redraw off


'objeto-----------------------------------
player = Player()
player_cpu = Player_Cpu()
ball = Ball()

while not keydown(KEY_ESCAPE,true)
    set color 0,0,0
    cls
   
    'objetos-----------------------------
    player.update()
    player.Draw()
    player_cpu.update()
    player_cpu.Draw()
    ball.update()
    ball.Draw()
    'lineas para el fondo------------------ 
    'draw rect 320,32,2,60,true
    'draw rect 320,120,2,60,true
    'draw rect 320,210,2,60,true
    'draw rect 320,300,2,60,true
    'draw rect 320,390,2,60,true
    'draw line 320,32,320,460
    'draw line 330,32,330,460
     
           
    redraw
    fwait 60
wend

Code:
'player
visible player = []

function Player()
    player.Width = 20
    player.Height = 100
    player.x = 32 - player.Width/2
    player.y = 240 - player.Height/2
    player.speed = 5
    player.live = true
   
    player.update = function()
        if this.live = true
            this.move()
        endif
    endfunc
   
    player.Draw = function()
        if this.live = true
            set color 255,0,205
            draw rect this.x,this.y,this.Width,this.Height,true
        endif
    endfunc
   
    player.move = function()
        if keydown(KEY_UP,false) and this.y > 16
            this.y = this.y - this.speed
        elseif keydown(KEY_DOWN,false) and this.y < 370
            this.y = this.y + this.speed
        endif
    endfunc
   
    return player
endfunc

function get_player()
    return player
endfunc

Code:
'player-cpu
include "ball.n7"

visible ball = get_ball()
visible player_cpu = []
function Player_Cpu()
    player_cpu.Width = 20
    player_cpu.Height = 100
    player_cpu.x = 608 - player_cpu.Width/2
    player_cpu.y = 240 - player_cpu.Height/2
    player_cpu.speed = 5
    player_cpu.live = true
 
    player_cpu.update = function()
    endfunc
 
    player_cpu.Draw = function()
        if this.live
            set color 255,204,0
            draw rect this.x,this.y,this.Width,this.Height,true
        endif
    endfunc
 
    return player_cpu
endfunc
function get_player_cpu()
    return player_cpu
endfunc


Code:
'ball
include "player.n7"
include "miscellaneous.n7"
include "player-cpu.n7"

visible player = get_player()
visible player_cpu = get_player_cpu()
visible ball = []

function Ball()
    ball.Width = 16
    ball.Height = 16
    ball.x = 320 - ball.Width / 2
    ball.y = 240 - ball.Height / 2
    ball.speedX = 2
    ball.speedY = 2
    ball.live = true
 
    ball.update = function()
        if this.live = true
            this.move()
            this.bounce()
            this.collision_player()
        endif
    endfunc
 
    ball.Draw = function()
        if this.live = true
            set  color 200,200,200
            draw rect this.x,this.y,this.Width,this.Height,true
            'draw ellipse this.x,this.y,9,9,true
        endif
    endfunc
 
    ball.move = function()
        this.x = this.x + this.speedX
        this.y = this.y + this.speedY
    endfunc
 
    ball.bounce = function()
        if this.x <= 0 or this.x >= 640
            this.speedX = this.speedX * -1
        endif
     
        if this.y <= 0 or this.y >= 480
            this.speedY = this.speedY * -1
        endif
    endfunc
 
    ball.collision_player = function()
        'colision con la x
        if collision_rect(this.x+this.speedX,this.y,this.Width,this.Height,player.x,player.y,player.Width,player.Height)
            this.speedX = this.speedX * -1
        endif
        'colision con la y
        if collision_rect(this.x,this.y+this.speedY,this.Width,this.Height,player.x,player.y,player.Width,player.Height)
            this.speedY = this.speedY * -1
        endif
    endfunc

    return ball
endfunc

function get_ball()
    return ball
endfunc

Code:
'Miscellaneous

'variables y funciones para manejar los puntos
visible score_player = 0
visible score_player_cpu = 0

function get_score_player()
    return score_player
endfunc

function get_score_player_cpu()
    return score_player_cpu
endfunc

'funcion de colision--------------------------------
function collision_rect(x1,y1,w1,h1,x2,y2,w2,h2)
    return x1 + w1 > x2 and x1 < x2 + w2 and
            y1 + h1 > y2 and y1 < y2 + h2
endfunc

Print this item

  Bombsweeper
Posted by: johnno56 - 01-23-2024, 07:12 PM - Forum: NaaLaa 7 Questions - Replies (11)

I know... I know... not your favourite game (in any version lol...)... As I have never played it before, would you be so kind, as to provide instructions etc... I do not know the significance of the "numbered" squares... Thank you.

J

Print this item

  N7 version 24.01.23 released
Posted by: Marcus - 01-23-2024, 06:00 PM - Forum: Announcements - Replies (3)

Sorry about posting another update so shortly after the last one, but it's an important update ... for myself.

https://naalaa.com/n7/N7_240123.zip

2024-01-23

  • Added the 'download' function
  • Added the game Denaalaafender to examples/other

I want to write a library for online leaderboards (there was such a library for N6, but I'm not sure if it's still working), so I really need the 'download' function, which you can use to retrieve data from php scripts and such.

This is the download.n7 example from the examples/help folder. It downloads the n7 changelog in three different ways.

Code:
' download.n7
' -----------

' You can use 'download(url, filename)' to download something from the internet to a destination
' file. The function returns true on success or false on failure. Depending on where you've put
' your N7 directory, this example might fail.
if download("https://naalaa.com/n7/CHANGELOG.txt", "my_file.txt")
    pln "Downloaded to file successfully!"
    pln
else
    pln "Download to file failed!"
    pln
endif
wait 1000

' If you know that the url will produce pure text, you can use 'download(url, TYPE_STRING)' to
' get the result as a string. If the download fails, an unset variable is returned.
aString = download("https://naalaa.com/n7/CHANGELOG.txt", TYPE_STRING)
if typeof(aString) ' TYPE_UNSET is 0, so we can just write this instead of 'if typeof(aString) = TYPE_STRING'
    pln "Downloaded to string successfully!"
    pln
    wait 1000
    ' Print the string.
    pln aString
    pln
else
    pln "Download to string failed!"
endif
wait 1000

' If you, for some reason, want the downloaded data as an array of bytes, you can use 'download(url,
' TYPE_TABLE)'. If the download fails, an unset variable is returned.
anArray = download("https://naalaa.com/n7/CHANGELOG.txt", TYPE_TABLE)
if typeof(anArray)
    pln "Downloaded to byte array successfully!"
    pln
    wait 1000
    ' Just print the first line of text (we know it's text this time)
    i = 0
    ' Use 'chr' to convert the ASCII values (bytes) to characters.
    while anArray[i] <> 10
        write chr(anArray[i])
        i = i + 1
    wend
    pln
else
    pln "Download to byte array failed!"
endif
pln

system "pause"

Print this item

  The maximum size of a multidimensional array
Posted by: Zuzikofon - 01-22-2024, 11:06 AM - Forum: NaaLaa 7 Questions - Replies (3)

Hello.

A great programming language, very well thought out - the quintessence of what you need. Congratulations.

Question - what is the maximum size of array ?
Can it be enlarged?
In a one-dimensional array I managed to get to 1 million.
Below are sample data that I want to convert. I will write a CSV to array parser. However, there are e.g. 30 million rows of this data.
How to do it?

20210103 170000;3.724250;3.724550;3.724250;3.724250;0
20210103 170500;3.723850;3.725150;3.723650;3.724160;0
20210103 170600;3.724160;3.724160;3.723850;3.723850;0
20210103 170700;3.724250;3.724460;3.724250;3.724460;0
20210103 170800;3.724450;3.724450;3.722350;3.722350;0
20210103 170900;3.722150;3.723950;3.722150;3.723950;0
20210103 171000;3.724750;3.724750;3.724750;3.724750;0
20210103 171100;3.722850;3.723160;3.722560;3.723160;0
20210103 171200;3.723050;3.724250;3.723050;3.724250;0
20210103 171500;3.724150;3.724250;3.723250;3.723250;0
20210103 171900;3.723260;3.723450;3.722450;3.722460;0
20210103 172000;3.722950;3.723350;3.720850;3.723350;0
20210103 172100;3.724250;3.724250;3.724250;3.724250;0
20210103 172200;3.724260;3.724260;3.724150;3.724150;0

Print this item

  NGUI examples
Posted by: Marcus - 01-21-2024, 03:15 PM - Forum: NaaLaa 7 Code - Replies (8)

I must have planned to write some examples for the widget library ngui, for I found a couple in a folder. I've attached the ones I found and I'll try to write some more - there are many widgets to cover.

buttons.n7 is probably a good start.

It can take some seconds to compile a program that includes ngui.n7, because it contains thousands of lines of code (I should put the "larger" widgets in separate files).



Attached Files
.n7   buttons.n7 (Size: 5.4 KB / Downloads: 9)
.n7   checkboxes_and_radiobuttons.n7 (Size: 7.47 KB / Downloads: 9)
.n7   combobox_and_listbox.n7 (Size: 4.92 KB / Downloads: 9)
.n7   menus.n7 (Size: 3.31 KB / Downloads: 9)
.n7   sliders.n7 (Size: 3.45 KB / Downloads: 4)
Print this item

  Sound effects experimentation
Posted by: kevin - 01-20-2024, 05:12 PM - Forum: NaaLaa 7 Code - Replies (7)

I love the new sound effects in the latest N7 version. I am working on this program to let me see the effects of changing the various parameters that you need to use when creating the sound effects.

You need to click the mouse when inside the blue buttons to change the paramenters, and click the red button to play the new sound. 

I've only incorporated one of the new functions so far, and spent little time on aesthetics, as you can see.

I'll leave this now until tomorrow, and see whether I can think of any changes that I can make, before moving on to the other 2 functions. In the meantime, I would love to hear any suggestions for improvements that you may have?

All the best - Kevin.


Code:
visible screen_w = 1024,screen_h = 768
'Open a window
set window "Sound FX",screen_w,screen_h,1
'enable double buffering
set redraw off


sampleRate = 12000
duration = 0.25
startFreq = 500
endFreq = 100
fadeOut = 0.75




############  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()
###########################
'clear the screen
set color 255,255,255
cls
set color 0,0,0

set caret 110,30
set justification center
write "CreateSquareSfx"
'-----------------------------------------------------------------
play_sound = false
if mousebutton(0)
    if mousey() > 60 and mousey() < 110
        if mousex() > 20 and mousex() < 220
            play_sound = true
            laserShotSnd = CreateSquareSfx(duration, startFreq, endFreq, fadeOut, sampleRate)
            play sound laserShotSnd
        endif   
    endif
endif
if play_sound = true
    set color 0,0,0
else
    set color 255,0,0
endif
draw rect 20,60,200,50,1
set caret 120,75
set color 255,255,255
write "PLAY SOUND"
set color 0,0,0
set caret 120,130

write "DURATION 0.1 - 2.0"
set caret 90,180
write "DURATION = "
set caret 160,180;write duration
set color 128,128,226
draw rect 20,150,200,25,1
set color 0,0,0
draw rect 20 + duration * 200/2,150,2,25
if mousebutton(0)
    if mousey() > 150 and mousey() < 175
        if mousex() > 20 + duration * 200/2 and mousex() < 220
            duration = duration + 0.05
        elseif mousex() > 20 and mousex() < 20 + duration * 200/2
            duration = duration - 0.05
        endif
    endif
endif

'-------------------------------------------------

set caret 120,230

write "startFreq 0 - 1000"
set caret 90,280
write "startFreq = "
set caret 160,280;write startFreq
set color 128,128,226
draw rect 20,250,200,25,1
set color 0,0,0
draw rect 20 + startFreq * 200/1000,250,2,25
if mousebutton(0)
    if mousey() > 250 and mousey() < 275
        if mousex() > 20 + startFreq * 200/1000 and mousex() < 220
            startFreq = startFreq + 10
        elseif mousex() > 20 and mousex() < 20 + startFreq * 200/1000
            startFreq = startFreq - 10
        endif
    endif
endif

'-----------------------------------------------------------------

'-------------------------------------------------

set caret 120,330

write "endFreq 0 - 1000"
set caret 90,380
write "endFreq = "
set caret 160,380;write endFreq
set color 128,128,226
draw rect 20,350,200,25,1
set color 0,0,0
draw rect 20 + endFreq * 200/1000,350,2,25
if mousebutton(0)
    if mousey() > 350 and mousey() < 375
        if mousex() > 20 + endFreq * 200/1000 and mousex() < 220
            endFreq = endFreq + 10
        elseif mousex() > 20 and mousex() < 20 + endFreq * 200/1000
            endFreq = endFreq - 10
        endif
    endif
endif

'-----------------------------------------------------------------

'-------------------------------------------------

set caret 120,430

write "fadeOut 0 - 1"
set caret 90,480
write "fadeOut = "
set caret 160,480;write fadeOut
set color 128,128,226
draw rect 20,450,200,25,1
set color 0,0,0
draw rect 20 + fadeOut * 200/1,450,2,25
if mousebutton(0)
    if mousey() > 450 and mousey() < 475
        if mousex() > 20 + fadeOut * 200/1 and mousex() < 220
            fadeOut = fadeOut + 0.005
        elseif mousex() > 20 and mousex() < 20 + fadeOut * 200/1
            fadeOut = fadeOut - 0.005
        endif
    endif
endif

'-----------------------------------------------------------------

'-------------------------------------------------

set caret 120,530

write "sampleRate 8000 - 22050"
set caret 90,580
write "sampleRate = "
set caret 160,580;write sampleRate
set color 128,128,226
draw rect 20,550,200,25,1
set color 0,0,0
draw rect 20 + (sampleRate - 8000) * 200/14050,550,2,25
if mousebutton(0)
    if mousey() > 550 and mousey() < 575
        if mousex() > 20 + (sampleRate - 8000) * 200/14050 and mousex() < 220
            sampleRate = min(22050,sampleRate + 200 )
        elseif mousex() > 20 and mousex() < 20 + (sampleRate - 8000) * 200/14050
            sampleRate = max(8000,sampleRate - 200 )
        endif
    endif
endif

'-----------------------------------------------------------------

set color 226,226,226
draw rect 20,630,200,25,1
set color 0,0,0
draw rect 20,630,200,25
set caret 120,635
write "RESTORE DEFAULTS"

if mousebutton(0)
    if mousey() > 630 and mousey() < 655
        if mousex() > 20  and mousex() < 220
            sampleRate = 12000
            duration = 0.25
            startFreq = 500
            endFreq = 100
            fadeOut = 0.75
           
        endif
    endif
endif





set caret 120,680
write "CreateSquareSfx(" + duration + ", " + startFreq + ", ";wln
write  endFreq+", " +fadeOut+", " +sampleRate + ")"

set caret screen_w/2,screen_h - 110
write "mouse = " + mousex() + " / " + mousey();wln
write "FPS = " + str(fps);wln

'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  ###########################################
#########################################################################################
' CreateSquareSfx
' ---------------
' Same as CreateSineSfx but using a square wave.
function CreateSquareSfx(duration, startFreq, endFreq, fadeOut, sampleRate)
    data = []
    a = 0
    da = 2*PI*startFreq/sampleRate
    dda = (2*PI*endFreq/sampleRate - 2*PI*startFreq/sampleRate)/(duration*sampleRate)
    vol = 1
    fadeOut = fadeOut*duration*sampleRate
    fadeOutDelta = 1/(duration*sampleRate - fadeOut)
    for i = 0 to duration*sampleRate - 1
        ' No, using sin here is stupid.
        sa = sin(a)
        if sa < 0  sa = -1
        elseif sa > 0 sa = 1
        data[i] = sa*vol
        a = a + da
        da = da + dda
        if i > fadeOut  vol = vol - fadeOutDelta
    next
    ' 'createsound(leftData, rightData, sampleRate)' creates a new sound and returns a sound id.
    ' 'leftData' is an array with data for the left channel and 'rightData' is for the right
    ' channel. The values in the arays should be in the range [-1..1]. 'sampleRate' is the number
    ' of samples per second, So if you want to create a sound that lasts for three seconds with a
    ' sample rate of 11025, the data arrays should contain 33075 (11025*3) elements each.
    '   You can also use 'create sound sound_id, leftData, rightData, sampleRate' if you want to
    ' use your own sound id.
    return createsound(data, data, sampleRate)
endfunc

Print this item

  N7 version 24.01.19 released
Posted by: Marcus - 01-19-2024, 10:02 PM - Forum: Announcements - Replies (7)

Small update, just a new function (but a fun one) and some more examples: https://naalaa.com/n7/N7_240119.zip

2024-01-19

  • Added the 'createsound' function and 'create sound' command
  • Added the game Space Race (spacerace.n7) and Bomb Sweeper (bomb_sweeper.n7) to the examples/other folder

An example of the 'createsound' function, create_sound.n7, can be found under examples/help. As with all other commands and functions you can write 'createsound' in NED, put the caret somewhere in the function name, and press F1 to see the syntax and F1 again to load an example in a new tab.

In create_sound.n7 I've implemented three helper functions to create noise sounds (for explosions, guns etc) and sine wave and square wave sounds (useful for simple beeps, boops and boings). 'createsound' itself only takes 3 parameters: an array with data for the left channel, an array with data for the right channel and a sample rate.

Code:
' create_sound.n7
' ---------------

#win32

' Create some sound effects. Look at the implementations of CreateSineSfx, CreateSquareSfx and
' CreateNoiseSfx to see how 'createsound' works.
sampleRate = 11025
explosionSnd = CreateNoiseSfx(0.6, 0.9, 0, sampleRate)
laserShotSnd = CreateSquareSfx(0.25, 500, 100, 0.75, sampleRate)
jumpSnd = CreateSineSfx(0.2, 50, 600, 0.9, sampleRate)
pickupSnd = CreateSineSfx(0.1, 1500, 1500, 0.2, sampleRate)
gunShotSnd = CreateNoiseSfx(0.2, 1.2, 0, sampleRate)

' Create a window and output some info.
set window "Create sound", 640, 480
wln "1 - Explosion"
wln "2 - Laser shot"
wln "3 - Jump"
wln "4 - Pickup"
wln "5 - Gun shot"

' Loop until user presses escape.
while not keydown(KEY_ESCAPE)
    ' Use the numeric keys to play different sound effects.
    if keydown(KEY_1, true) play sound explosionSnd
    if keydown(KEY_2, true) play sound laserShotSnd
    if keydown(KEY_3, true) play sound jumpSnd
    if keydown(KEY_4, true) play sound pickupSnd
    if keydown(KEY_5, true) play sound gunShotSnd
    ' Sleep some.
    wait 16
wend

' CreateSineSfx
' -------------
' Create a sine wave sound effect. 'duration' is the duration of the sound in seconds. 'startFreq'
' is the frequency at the start of the effect and 'endFreq' is the frequency at the end. You can
' use different values of 'startFreq' and 'endFreq' to create slide effects. 'fadeOut' determines
' when/if the sound should start fading out. If 'fadeOut' is 0, the fade out starts immediately, and
' if it's 1 there is no fade out. 'sampleRate' (samples per second) should be in the range [8000 ..
' 22050]. N7 outputs audio at a sample rate of 22050, so higher values than that makes no sense.
function CreateSineSfx(duration, startFreq, endFreq, fadeOut, sampleRate)
    data = []
    a = 0
    da = 2*PI*startFreq/sampleRate
    dda = (2*PI*endFreq/sampleRate - 2*PI*startFreq/sampleRate)/(duration*sampleRate)
    vol = 1
    fadeOut = fadeOut*duration*sampleRate
    fadeOutDelta = 1/(duration*sampleRate - fadeOut)
    for i = 0 to duration*sampleRate - 1
        data[i] = sin(a)*vol
        a = a + da
        da = da + dda
        if i > fadeOut  vol = vol - fadeOutDelta
    next
    ' 'createsound(leftData, rightData, sampleRate)' creates a new sound and returns a sound id.
    ' 'leftData' is an array with data for the left channel and 'rightData' is for the right
    ' channel. The values in the arays should be in the range [-1..1]. 'sampleRate' is the number
    ' of samples per second, So if you want to create a sound that lasts for three seconds with a
    ' sample rate of 11025, the data arrays should contain 33075 (11025*3) elements each.
    '  You can also use 'create sound sound_id, leftData, rightData, sampleRate' if you want to
    ' use your own sound id.
    return createsound(data, data, sampleRate)
endfunc

' CreateSquareSfx
' ---------------
' Same as CreateSineSfx but using a square wave.
function CreateSquareSfx(duration, startFreq, endFreq, fadeOut, sampleRate)
    data = []
    a = 0
    da = 2*PI*startFreq/sampleRate
    dda = (2*PI*endFreq/sampleRate - 2*PI*startFreq/sampleRate)/(duration*sampleRate)
    vol = 1
    fadeOut = fadeOut*duration*sampleRate
    fadeOutDelta = 1/(duration*sampleRate - fadeOut)
    for i = 0 to duration*sampleRate - 1
        ' No, using sin here is stupid.
        sa = sin(a)
        if sa < 0  sa = -1
        elseif sa > 0 sa = 1
        data[i] = sa*vol
        a = a + da
        da = da + dda
        if i > fadeOut  vol = vol - fadeOutDelta
    next
    ' 'createsound(leftData, rightData, sampleRate)' creates a new sound and returns a sound id.
    ' 'leftData' is an array with data for the left channel and 'rightData' is for the right
    ' channel. The values in the arays should be in the range [-1..1]. 'sampleRate' is the number
    ' of samples per second, So if you want to create a sound that lasts for three seconds with a
    ' sample rate of 11025, the data arrays should contain 33075 (11025*3) elements each.
    '  You can also use 'create sound sound_id, leftData, rightData, sampleRate' if you want to
    ' use your own sound id.
    return createsound(data, data, sampleRate)
endfunc

' CreateNoiseSfx
' --------------
' Create a noise sound effect. 'duration' is the duration of the sound in seconds. A 'pitch' value
' < 0 increases the lower noise frequencies, while a value > 1 increases the higher frequencies.
' 'fadeOut' determines when/if the sound should start fading out. If 'fadeOut' is 0, the fade out
' starts immediately, and if it's 1 there is no fade out. 'sampleRate' (samples per second) should
' be in the range [8000 .. 22050]. N7 outputs audio at a sample rate of 22050, so higher values than
' that makes no sense.
function CreateNoiseSfx(duration, pitch, fadeOut, sampleRate)
    assert sampleRate >= 8000, "CreateBoomSfx: invalid sample rate"
    assert pitch > 0, "CreateBoomSfx: invalid pitch"
 
    ' Mix four different noise frequencies weighted, in a weird way, by the pitch value.
    freqs = [
            [v: 0, p: sampleRate/500, d: 0, t: 0, w: pitch],
            [v: 0, p: sampleRate/1000, d: 0, t: 0, w: pitch^2],
            [v: 0, p: sampleRate/2000, d: 0, t: 0, w: pitch^3],
            [v: 0, p: sampleRate/8000, d: 0, t: 0, w: pitch^4]]
   
    s = sizeof(freqs)
    data = []
    vol = 1
    fadeOut = fadeOut*duration*sampleRate
    fadeOutDelta = 1/(duration*sampleRate - fadeOut)
    for i = 0 to duration*sampleRate - 1
        v = 0
        w = 0
        for j = 0 to s - 1; f = freqs[j]
            f.t = f.t - 1
            if f.t <= 0
                f.t = f.p
                f.d = ((rnd()*2 - 1) - f.v)/f.p
            endif
            f.v = f.v + f.d
            v = v + f.v*f.w
            w = w + f.w
        next
        data[i] = vol*v/w
        if i > fadeOut  vol = vol - fadeOutDelta
    next
    ' 'createsound(leftData, rightData, sampleRate)' creates a new sound and returns a sound id.
    ' 'leftData' is an array with data for the left channel and 'rightData' is for the right
    ' channel. The values in the arays should be in the range [-1..1]. 'sampleRate' is the number
    ' of samples per second, So if you want to create a sound that lasts for three seconds with a
    ' sample rate of 11025, the data arrays should contain 33075 (11025*3) elements each.
    '  You can also use 'create sound sound_id, leftData, rightData, sampleRate' if you want to
    ' use your own sound id.
    return createsound(data, data, sampleRate)
endfunc

Print this item

  Audio creation
Posted by: Marcus - 01-18-2024, 06:46 PM - Forum: Suggestions - Replies (3)

Make it possible to create a sound effect from an array of raw data. With such a function you could write a library in n7 for generating simple sound effects, such as explosions and beeps and boops.

/Marcus

Print this item

  Defender
Posted by: Marcus - 01-18-2024, 06:35 PM - Forum: NaaLaa 7 Code - Replies (6)

Another game I started working on but most likely won't finish. I think I just wanted to implement "wrapped scrolling" - the level is six screens wide, but you can still travel endlessly in any direction. It's an unfinished Defender (Atari 2600) clone Smile

Prevent the green aliens from obducting the dudes. The red spaceships just fly around randomly and shoot at you.

[Image: denaalaafender.jpg]

Edit  Attached the full game (denaalaafender.n7) from a post further down (denaalaafender.zip is the old version).



Attached Files
.zip   denaalaafender.zip (Size: 7.04 KB / Downloads: 10)
.n7   denaalaafender.n7 (Size: 33.1 KB / Downloads: 4)
Print this item

  Rock Blaster in Naalaa7
Posted by: 1micha.elok - 01-18-2024, 02:13 PM - Forum: NaaLaa 7 Code - No Replies

'Inspired by Rock Blaster ( Naalaa v6 )
'Simplified version of Rock Blaster written in Naalaa v7

'What are in the simplified version ?
' - Player's movement LEFT or RIGHT in 30,60,90,120,150 degree
' - Auto Shoot with guided bullet direction capability Smile
' - Lives = 3
' - Scores= 5 to get the stage completed.
' - Animated Starfleet background (thanks to Marcus)
' - Asteroids move from top to bottom diagonally and slowly
'  I don't know how to make it move faster Smile
' - A fun way to play around with Naalaa (code in Naalaa v7 included)
'
'This simplified version needs a lot of improvement Smile
'You are most welcome to change the code to suit your needs...
'Don't forget to post and share your own version

[Image: rock_blaster.jpg]



Attached Files
.zip   RockBlaster_simplified.zip (Size: 346.59 KB / Downloads: 8)
Print this item