Better Way Than If..Then?

General FreeBASIC programming questions.
Post Reply
davidshq
Posts: 52
Joined: Oct 29, 2008 3:28
Contact:

Better Way Than If..Then?

Post by davidshq »

I have some QuickBasic code like so:

Code: Select all

		IF INKEY$ <> "" GOTO notitle
		PLAY "MST170o1e8o0b8o1e8"
		IF INKEY$ <> "" GOTO notitle
		PLAY "e8e4f#8g4f#8"
It continues on with the same pattern, the purpose being to play some music but to allow the individual to interrupt the music by pressing a key.

What I'm wondering is if there is a better way to do this as I move the code over to FreeBasic?
Thanks,
Dave
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Better Way Than If..Then?

Post by MrSwiss »

Create an array for your music String(s), then run it in a loop:

Code: Select all

dim as string music(0 to 99)    ' as many as needed
dim as LongInt cnt = 0    ' just to be on the save side

' first fill the array with strings, then ...

do
  ' you should allways check for array bounds
  if cnt > 99 then cnt = 0    ' depends on array size !!
  play music[cnt]    ' play one after another (from array)
  if inkey <> "" then exit do    ' exit when keypress detected
  cnt += 1    ' increment counter
loop

.... ' your code again
badidea
Posts: 2635
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Better Way Than If..Then?

Post by badidea »

In similar way:

Code: Select all

'play emulation :-)
sub play(notes as string)
	dim as integer i
	for i = 1 to len(notes)
		print mid(notes, i, 1);
		sleep 100,1
	next
	print
end sub

dim as integer i, numLines
dim as string music(0 to 99)

'read and store music data
read numlines
for i = 0 to numLines-1
	read music(i)
next

'play music lines until key press
for i = 0 to numLines-1
	if (inkey() <> "") then exit for
	play music(i)
next

data 6 'numLines
data "MST170o1e8o0b8o1e8"
data "e8e4f#8g4f#8"
data "this is not music"
data "this is just text"
data "bye, bye"
data "end......."
Post Reply