basic.html 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type"
  4. content="text/html; charset=iso-8859-1">
  5. <title>Getting Started with BlitzBasic </title>
  6. <style>
  7. <!--
  8. A:link {text-decoration: none;}
  9. A:visited {text-decoration: none;}
  10. -->
  11. </style>
  12. </head>
  13. <body bgcolor="#206060" text="#ffff00" link="#00ff00"
  14. vlink="#00ff00" alink="#00ff00">
  15. <br>
  16. <center><h1>Learning how to program Blitz BASIC</center></h1>
  17. <center>You want to learn the first steps so you can evantually write a game? Well you've come to the right place.</center>
  18. <p align="left"><font size="2" face="verdana">
  19. <br>
  20. Blitz Basic uses a specially designed and easy to use programming language so that it is easy to follow and requires no previous knowledge of programming, just a love of games and your ideas.<br><br>
  21. So, welcome to the BASIC guide to Blitz - where the only restrictions are your imagination.
  22. The sole purpose of this section is to teach you how to write your own Blitz Basic programs. The traditional description of a program is a task that you want your computer to perform via a set of instructions. The task is described to the computer using an instruction set Blitz Basic can understand.
  23. The instructions in your program must be written using a set of rules known as Syntax. You must follow these rules if you are to write Blitz Basic programs. With lots of practice and a sprinkle of determination you will gain a firm and confident understanding about the general rules of BASIC and how to use them as a programmer
  24. - When you have this working knowledge of Blitz Basic, the types of games you create are entirely up to you, but with a little imagination you could be writing the next hit game.
  25. <br>
  26. <br> If you didn't know - BASIC is an acronym for <b>Beginners All-purpose Symbollic Instruction Code.</b> In this mini course you'll learn the BASIC language as implemented in Blitz and the planning of good programming structures.<br><br><b>Hello World Hello Me or <i>Hello sheep! Hello cup of tea!</i></b><br><br>
  27. OK - lets start with the <b>Print</b> command - this simply prints any text onto your screen - like this:<br><br>
  28. <b>print "Hello sheep"</b><br>
  29. <b>print "Hello cup of tea"</b><br>
  30. <b>End</b><br><br>
  31. just simply type it into your ide...
  32. <img align="left" src="hello.gif"><br><br><br><br><br><br><br><br><br><br><br><br><br>
  33. ...and click on the red rocket <img src="rocket.gif"> to run....
  34. <img align="left" src="hellooutput.gif"><br><br><br><br><br><br><br><br><br><br><br><br><br>
  35. <br><br><br><br><br><br><br><br><b>Well done - you've just written your first Blitz program.</b>
  36. <br><br>You can also include comments/notes in your programs - this is highly
  37. recommended and is good programming practice also. You do this by placing a semi-colon (;) into your program
  38. then just type your text eg:<br><br>
  39. <b>Print "George Bray" </b>; This piece of code is for printing your name to the screen<br><br>
  40. You can also stop the machine with the <b>End</b> command - this will break your program
  41. at any point that you want it to end.<br><br>
  42. You can give your code a title too by typing <b>AppTitle "gamenamehere"</b><br><br>
  43. OK let's learn how Blitz works in interpreting your programs and what you'll need to know to write them...<br><br>
  44. <b><u>Data</b></u><br><br>These are the main items in programming. Data is represented to in three basic forms. The first is a whole number which is known as an <b>Integer</b> eg. 8,20,100,1000.</b> The second type is a <b>Real Number</b>, this allows decimal
  45. points eg. 1.2, 48.20, 100.4, 3000.201. The third type of data is a <b>String</b> which is basically
  46. something that is not a number, all strings consist of characters enclosed within quotation marks.
  47. A string can include numbers eg "A", "1", "Blitz Basic", "Commodore 64", "Sheep".
  48. <br><br>
  49. <b><u>Variables</b></u><br><br>
  50. A Variable is used to store Data - to simplify this - Imagine a Variable as
  51. being one big box and within that box you can store an item plus more boxes if required.
  52. An example would be:<br><br> <b>A = 2 + 2</b><br><br>You can also <b>print</b> the contents of a variable to the screen:<br><br>
  53. <b>A = 2 + 2<br>
  54. Print A</b><br><br>
  55. Another example would be:<br><br>
  56. <b>A = 1<br>
  57. B = 2<br>
  58. C = A + B<br>
  59. PRINT C</b><br><br>
  60. If we break this down - we have 3 variables here A & B - which store 2 numbers
  61. and finally variable C which is a calculation that is based on the values stored within variables A & B
  62. so the calculation is actually C = 1 + 2. The result is then stored in C and printed to the screen.<br><br>
  63. Variables can also store <b>Real Numbers</b> and <b>Strings</b> In order to allow a variable to store these other types of data,
  64. you must make sure you tell the computer whether it is a <b>Integer</b>, <b>Real Number</b>, or a <b>String</b>
  65. variable. To specify a real number variable, you must add a hash sign (#) as the last character
  66. of the variable name. If you want your variable to store a string, you must add a dollar sign ($) as the last
  67. character of the variable name eg:<br><br>
  68. <b>A# = 1.2<br>
  69. B$ = vic20<br>
  70. Print A#:Print B$<br><br></b>
  71. You can also create mathematical functions using <b>String</b> variables eg:<br><br>
  72. <b>A$ = "Welcome to "<br>B$ = "the world "<br>C$ = "of programming."<br>
  73. D$ = A$ + B$ + C$<br>
  74. Print D$<br><br></b>
  75. When you run this program, it will print <b> Welcome to the world of programming</b> on your screen.<br><br>
  76. Easy eh?<br><br>
  77. <b><u>Arrays</b></u><br><br>
  78. Arrays are very important because you can store lots of data under a single name.
  79. You can even pinpoint an item of data and use it from within an array. If for example
  80. you want to store 7 days of the week, you would have to create 7 different
  81. variables eg. <br><br><b>day0$ = "Monday" <br>day1$ = "Tuesday" <br> </b>upto<b><br> day6$ = "Sunday"</b><br><br> But with an Array
  82. you can create a different kind of variable that can store more than one item of data.
  83. eg. For days of the week you might go:<br><br>
  84. <b>Dim day$(6) ; Dim the array... 7 elements (0 to 6)<br>
  85. day$(0)="Monday" ; Set the array data<br>
  86. day$(1)="Tuesday"</b><br><br>
  87. You can then just include:<br><br>
  88. <b>Print day$(3)</b><br><br>
  89. Which would print <b>Thursday</b><br><br>
  90. To print the seven days of the week we would include a <b>FOR NEXT</b> loop
  91. for example:<br><br>
  92. <b>For loop = 0 to 6 <br>
  93. Print day$(loop) ; loops through and prints the data.<br>
  94. Next ; this continues until loop is equal to 6 then continues with the next line of your program.</b><br><br>
  95. You can also you the <b>STEP</b> command when creating a FOR/NEXT loop eg. If you
  96. were to create a FOR/NEXT loop followed by STEP 2 the computer would count
  97. up the for next loop in increments of 2 eg. 2, 4, 6. So if we were to apply this to our days of the week
  98. program: <br><br>
  99. <b>
  100. For loop = 0 to 6 step 2</b><br><br>
  101. The computer would then print only <b>Monday Wednesday Friday & Sunday.</b><br><br>
  102. You can also step backwards by placing a minus (-) sign after the command with the number of steps backwards you would like to take:<br><br>
  103. <b>For A = 10 To 0 step -2<br>
  104. Print A<br>
  105. Next</b><br><br>
  106. This would print: <b>10 8 6 4 2 0</b> <br><br>
  107. One final point on Arrays - As documented earlier they are very versatile as they can store many
  108. levels of data so by:<br><br>
  109. <b>Dim day(7,5)</b><br><br>
  110. Here we are setting up the arrays 0 - 6 but for each one there is another
  111. 5 boxes of data numbered 0 - 4 upon which more data can be stored. So if you
  112. think back to your boxes - here we have 7 boxes with 5 boxes inside, so in all
  113. you can store 35 values (7x5) in this array. So data entry into the Array you would
  114. create something like this:<b><br><br>
  115. day(0,0) = 10<br>
  116. day(0,1) = 20<br>
  117. day(0,2) = 30<br>
  118. day(0,3) = 40<br>
  119. day(0,4) = 50<br>
  120. day(1,0) = 100<br>
  121. day(1,1) = 200<br>
  122. day(1,2) = 300<br>
  123. day(1,3) = 400<br>
  124. day(1,4) = 500<br><br>
  125. </b>
  126. As you can see arrays need to be declared as a particular type - <b>Integer, Real Number & String.</b>
  127. You can have an array of integer, real numbers or strings but you cannot have multiple
  128. types in the same array. <br><br><b><u>
  129. Mathematical Commands</u></b><br><br>
  130. The dreaded mathematical side of computers but DON'T PANIC - I'm no mathematician either.
  131. I'll just give you a quick run down on the "boring" side of things - don't worry - with
  132. a little patience you will be familiar with all these functions in no time as
  133. we will make the computer do all the hard work. Just relax - make a nice steaming hot cup of tea & read on.....<br><br>
  134. OK - lets start with the easiest stuff - <b>Add, Subtract, Divide and Multiply.</b>
  135. In computer terms these are represented by<b>(+) (-) (/) (*)</b> respectively.
  136. A simple example would be:<br><br><b>
  137. A = 2 + 2<br>
  138. B = 2 * 2<br>
  139. C = 2 - 2<br>
  140. D = 2 / 2<br>
  141. Print A: Print B: Print C: Print D</b><br><br>
  142. The result would print: <b>4 4 0 1</b> to the screen - But you knew that....
  143. You can also assign letters in mathematical functions:<br><br> <b> A + B<br> A - B<br> A * B<br> A / B</b><br><br>
  144. <b><u>Boolean Stuff</b></u><br><br>
  145. This is where you must have a cup of tea in your hand because it's time to focus!
  146. Once you've cracked this - you are well away to mastering the normally hated mathematical
  147. side of programming...<br><br>
  148. OK - Here goes...Sitting comfortably?<br><br>
  149. Boolean operators allow your program to perform logical operations on
  150. your data.<br><br>
  151. We have the <b>AND</b> and <b>OR</b> operator - this works with any integer.<br><br>
  152. The basics are (study the logic in these examples - it is pretty straight forward)<br><br>
  153. <b>agecategory$=Input("What is the age restriction on this film 15 or 18? ")</b> ; Asks a moderator to set the age rating for a film<br>
  154. <b>age$=Input("What is your age: ")</b> ; Then asks a viewer their age<br>
  155. <b>If agecategory$ = 18 And age$&lt;18 Or agecategory$ = 15 and age$&lt; 15 Then Print "Your are not allowed to view this film - Sorry"</b> ; using AND OR expressions decides if they are too young<br>
  156. <b>If agecategory$ = 18 And age$&gt;18 Or agecategory$ = 15 And age$&gt;15 Then Print "Enjoy your film."</b> ; Using AND OR expressions decides if they are old enough<br>
  157. <b>WaitMouse</b> ; waits for the mouse button to be pressed<br>
  158. <b>End</b> ; Once the mouse button has been pressed - Ends the program.<br><br>
  159. We can compare not only numbers, but strings (text) also. We have seen '=' used
  160. in 'agecategory$ = 18' so what does 'less than' & 'more than' mean for strings?
  161. One thing it does not mean is 'shorter than', so we don't make that
  162. mistake. We make the definition that one string is less than another
  163. if it come first in alphabetical order, thus<br><br>
  164. Smith &lt; Smythe<br>
  165. Smythe &gt; Smith<br>
  166. Sheep &lt; Shop<br>
  167. Shop &gt; Sheep<br><br>
  168. all hold. &lt;= means is 'is less than or equal to'and so on - just
  169. as for numbers.<br><br>
  170. We then have the <b>NOT</b> operator.<br><br>
  171. This is simply used to say:<br><br>
  172. <b>If agecategory$ not &lt;&gt; 18</b> - Is the same as <b>A = B.</b> <i>It really is that straight forward.</i><br><br>
  173. <b>You will discover how useful these operators become when writing conditions for your programs.</b><br><br>
  174. <b><u>Storing DATA using the READ command</b></u><br><br>
  175. Storing data will become an important and useful way to hold and read
  176. numbers or strings (text) easily. These can be used for storing many
  177. different types of data eg. Level Data (formations that aliens might attack or
  178. data for landscapes/scenery within your game etc). Here's a basic example of the READ/DATA
  179. commands:<br><br>
  180. <b>Example1</b><br><br>
  181. <b>Read a,b,c</b> ;read next 3 data items into variables a, b and c<br>
  182. <b>Print a+","+b+","+c</b> ;print out their values<br>
  183. <b>Data 1,2,3 </b> ;the actual data items<br><br>
  184. <b>Example 2</b><br><br>
  185. <b>Restore second</b> ;start reading data from the '.second' label<br>
  186. <b>Read a,b,c</b> ;read in data<br>
  187. <b>Print a+","+b+","+c</b> ;print out variables<br>
  188. <b>Restore first </b> ;start reading data from the '.first' label<br>
  189. <b>Read a,b,c </b> ;read in data<br>
  190. <b>Print a+","+b+","+c</b> ;print out values<br>
  191. <b>.first</b><br>
  192. <b>Data 1,2,3</b> ;data items<br>
  193. <b>.second</b><br>
  194. <b>Data 4,5,6</b> ;more data items<br><br>
  195. You may have notice that I threw in the <b>RESTORE</b> command within example 2. This is used
  196. to reset the data pointer at any time. In a games programming environment
  197. you may need to read the same data again eg. If you want to clear the
  198. screen & redraw it using your data table - by resetting the pointer
  199. the Read command will start at the top of the data list and you can read it again.<br><br>
  200. <u><b>GOTO Command</u></b><br><br>
  201. I always remember this command by saying <i>goat-to</i> in my head when calling this command :) - OK - type in following:<br><br>
  202. <b>Print "Emily & Ellis were here today"</b><br>
  203. <b>Goto goat</b><br><br>
  204. <b>.goat</b><br>
  205. <b>Print "So was George and his furry animals"</b><br><br>
  206. So here we are printing our text - then jumping to a label called <b>.goat</b>
  207. the program then continues reading the rest of your program from the label you
  208. have set downwards.
  209. <br><br><u><b>Subroutines</u></b><br><br>
  210. Often a particular sequence of commands gets used several times too within a program.
  211. You <i>may</i> be able to avoid writing the sequence several times by judicious
  212. use of <b>GOTO</b> commands; but this isn't always good enough. The command
  213. <b>GOSUB</b> is like a <b>GOTO</b> that remembers where it came from on meeting
  214. another command <b>RETURN</b> the program jumps back to the line after
  215. the particular <b>GOSUB</b> that it originally started from. The <b>GOSUB</b>
  216. must be followed by a label eg. <b>Gosub sheep</b> which sends the program
  217. to the series of commands beginning on line <b>.sheep</b> - The part of
  218. the program that lies between <b>.sheep</b> and the <b>RETURN</b> command
  219. is known as a <i>subroutine</i>. An example of the <b>GOSUB</b> command would be:<br><br>
  220. <b>Print "Baa Baa"</b> ; Prints text to the screen<br>
  221. <b>Gosub sheep</b> ; Goes to the subroutine .sheep<br>
  222. <b>WaitMouse</b> ; The computer return(s) here! - waits for the mouse button to be pressed<br>
  223. <b>End</b> ; End(s) the program when the mouse button has been pressed.<br><br>
  224. <b>.sheep</b> ; subroutine sheep<br>
  225. <b>Print "Blitz Basic"</b> ; Prints text to the screen<br>
  226. <b>Return</b> ; Return(s) to the next line after <i>gosub</i> command.<br>
  227. <br><br>
  228. <b><u>FOR NEXT Commands</b></u><br><br>
  229. OK - I'll breifly explain the <b>FOR</b>...<b>NEXT</b> commands - these beasts
  230. have already been used in an earlier example if you never realised!
  231. These two commands create a technique known as a <b>loop</b> - so your computer
  232. performs any given task several times. Try out the following program.
  233. follows.<br><br>
  234. <b>For a = 0 To 100</b> ; set up loop with <b>a</b> as a counter ranging from 0 (start) to 100 (finish)<br>
  235. <b>Print a</b> ; prints the current value of <b>a</b><br>
  236. <b>Next</b> ; Is <b>a</b> 100? No, it's 1. Add 1 to <b>a</b> to get 2 and go back to line <b>Print a</b><br>
  237. <b>Your program continues here...</b> ; When <b>a</b> does equal 100 the program ends the <b>loop</b> and continues to run here.<br>
  238. <br><br>
  239. The result will be that the computer prints the numbers 0-100 on your screen.<br><br>
  240. <b><u>STEP size</b></u><br><br>
  241. When you are creating a loop - you can also write it as below.<br><br>
  242. <b>For a = 0 To 100 Step 10</b> ; The same as the previous example except the computer will count in <b>STEPS</b> of 10 at a time<br>
  243. <b>Print a</b> ; Prints the current value of <b>a</b><br>
  244. <b>Next</b> ; If a does not equal 100 then add 10 to it and goes to the line above <b>Print</b><br>
  245. <b>Your program continues here...</b> ; When <b>a</b> does equal 100 the program ends the <b>loop</b> and continues to run here.<br><br>
  246. So the program will now give the result:<br><br>
  247. <b>0<br>10<br>
  248. 20<br>30<br>40<br>50<br>60<br>70<br>80<br>90<br>100</b><br><br>As opposed
  249. to printing out 1-100 sequencially - as per previous <b>FOR</b>...<b>NEXT loop</b> example.<br><br>
  250. <b><u>Functions</b></u><br><br>
  251. Using functions - Part 1<br><br>
  252. Now that you are getting to grips with the very "basics" of Basic
  253. programming (eg. For...Next)!
  254. A function allows you to run a commonly used piece of code. For
  255. example, this is a function which prints "Hello" to the screen
  256. whenever you "call" it:<br><br>
  257. <b>Function PrintHello ()<br>
  258. Print "Hello"<br>
  259. End Function<br></b>
  260. <b>For a=1 To 5 </b> ; Let's print "Hello" 5 times by calling the PrintHello () function:<br>
  261. <b>PrintHello ()<br>
  262. Next</b><br><br>
  263. Now run the program to see the result.<br><br>
  264. Using Functions - Part 2<br><br>
  265. OK, now we'll modify the function so that it'll print whatever we
  266. want, which can be different each time we call it.<br><br>
  267. The function:<br><br>
  268. <b>Function PrintSomething (a$)<br>
  269. Print a$<br>
  270. End Function<br>
  271. </b>; In this case, we "pass" a string (either a string variable such as blah$,
  272. or a piece of text enclosed in quotes, eg. ("Hello there") to the function<br>
  273. <b>PrintSomething ("Hello, I'm gonna be printed.")<br>
  274. PrintSomething ("So am I.")<br>
  275. </b>; Using a string variable<br>
  276. <b>sentence$="This is also being printed to the screen."
  277. PrintSomething (sentence$)<br>
  278. </b>So, whatever is put within the brackets () when you call it is
  279. "passed" to the function. If you look at the function itself, you'll
  280. see that it takes the form "PrintSomething (a$)", which means it's
  281. expecting you to pass a string variable, as we've done above.<br><br>
  282. Note that "a$" could be named anything at all - "b$" "sometext$" whatever.<br><br>
  283. Look inside the function, and it takes whatever is passed into its
  284. "a$" parameter and uses it in the Print call ("Print a$").<br><br>
  285. As an exercise, try changing "a$" to "b$". Make sure you change "a$" to "b$"
  286. all throughout the function, or it won't work! Do that before continuing, then run it.<br><br>
  287. Here's what you should have ended up with:<br><br>
  288. Function PrintSomething (b$)<br>
  289. Print b$<br>
  290. End Function<br><br>
  291. </b>Now try changing the string to something of your own choosing (as long as
  292. it ends with the $ string sign!)<br><br>
  293. Using functions - Part 3<br><br>
  294. OK, so that was a very simple function call, where a function basically acted like
  295. any other command. Now we'll look at another way to call functions. We can have them perform
  296. a calculation and "return" a value to us.<br><br>
  297. The function:<br><br>
  298. <b>Function JoinString$ (a$)<br>
  299. Return "You passed: "+a$<br>
  300. End Function<br></b>
  301. ; Again, we "pass" a string to the function, but this time, we store what the function returns. <br><br>
  302. <b>mysentence$=JoinString ("Hello, I'm gonna be printed.")
  303. Print mysentence$<br><br></b>
  304. Run the program as before - now what happens here?<br><br>
  305. First of all, looking at the function itself, we know we're returning a string
  306. from the function (it's joining "You passed: " onto whatever string you pass to it), so we add
  307. a $ (string) sign to the name of the function ("JoinString"), which gives us "JoinString$".
  308. The "Return" statement passes the joined string back to where we called it. Remember,
  309. this is why we added a "$" to the name; we're returning this STRING.
  310. Calling the function, we simply pass whatever string we want, and it's received into
  311. our string variable (in this case, "mysentence$"). So mysentence$ becomes "You passed: Hello,
  312. I'm gonna be printed." once we call the function.<br><br>
  313. <b>Some exercises:</b><br><br>
  314. Try changing the name of the variable "mysentence$" to something of your own, eg. b$,
  315. something$, whatever. Note that you'll have to change it in the Print statement too! Run it.<br><br>
  316. Change the "You passed: " string within the function to something else, and change
  317. the string you're passing ("Hello, I'm gonna be printed"). Run it. Try a few different things.<br><br>
  318. Using Functions - Part 4<br><br>
  319. By default, a function returns either:<br><br>
  320. 0 - (zero) for numeric values
  321. "" - (an empty string) for string type functions
  322. <br><br>
  323. <b>Function AddOneAndOne ()<br>
  324. a=1+1<br>
  325. End Function</b> ; This will return 0, because we haven't told it to actually return the result of 1+1!<br>
  326. <b>Print "Result of AddOneAndOne: "+AddOneAndOne ()</b> ; Try adding a line saying "Return a" (without the quotes!) to the AddOneAndOne
  327. function, then run it again. NOW it returns the value!<br>
  328. <b>Function GimmeString$ ()<br>
  329. a$="If you can read this, you must have fixed this function!"<br>
  330. End Function<br>
  331. b$=GimmeString ()<br>
  332. If b$="" <br>
  333. b$="GimmeString doesn't return a value!"<br>
  334. Else Print b$<br>
  335. EndIf<br>
  336. Print "Result of GimmeString (): "+b$<br><br>
  337. </b>Exercise: add the necessary line to the function so that it returns a$, then
  338. run it again.<br><br>
  339. <b><u>IF...THEN Commands</b></u><br><br>
  340. The <b>IF</b> statement works out the condition of something as either true or false.
  341. If the condition is true then the statement after <b>THEN</b> is executed, but otherwise it
  342. is skipped over. The most useful conditions compare two number or two strings. You can test
  343. whether two numbers are equal, or whether one is bigger than the other and they can test whether
  344. two strings are equal, or whether one come before the other in alphabetical order. They use the
  345. relations =,&lt;,&gt;,&lt;=,&gt;=, and <>. If you've not experienced
  346. these signs before they represent the following:<br><br>
  347. <b>Sign</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Meaning</b><br><br>
  348. =&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Equals<br>
  349. &lt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is less than<br>
  350. &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is greater than<br>
  351. &lt;=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is less than or equal to<br>
  352. &gt;=&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is greater than or equal to<br>
  353. <>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;is unequal to<br><br><br>
  354. Take the following example:<br><br>
  355. <b>.start</b> ; sets up the label start<br>
  356. <b>question$ = Input ("Shall I tell you the meaning of life yes/no? "</b> ; prints a question - waits for users input<br>
  357. <b>If question$ = "no" Then Print "Alright, Then I won't"</b>; IF input = no THEN print text<br>
  358. <b>If question$ = "yes" Then Print "42"</b> ; IF input = yes THEN print text<br>
  359. <b>If question$ <> "yes" Or "no" Then Goto start</b> ; IF input is not equal to either yes or no THEN GOTO start<br><br>
  360. We can also achieve the same effect with numbers too:<br><br>
  361. <b>number = Input ("How old are you ")</b> ; prints a question - waits for users input<br>
  362. <b>If number &lt; 18 Then Print "You are under 18"</b> ; IF input is smaller than 18 THEN print text<br>
  363. <b>If number > 18 Then Print "You are over 18"</b> ; IF input is greater than 18 THEN print text<br>
  364. <b>If number = 18 Then Print "you are 18"</b> ; IF input is equal to 18 THEN print text<br>
  365. <br><br>
  366. The expression IF THEN can be any combination of values, variables, arrays and operators providing the expressions are logical.<br><br>
  367. <u><b>IF ENDIF</u></b><br><br>
  368. On a similar principal to the IF command - instead of a THEN command
  369. ENDIF points to the end of the commands to be executed - the example below
  370. shows you this principle:<br><br>
  371. <b>commodore = 20</b> ; sets up the variable for commodore<br>
  372. <b>atari = 20</b> ; sets up the variable for atari<br>
  373. <b>If commodore = atari</b> ; IF both variables are the same...(execute the code between here and ENDIF)<br>
  374. <b>Print "Welcome to the machine - commodore and atari..."</b> ; code to be executed if the values are the same.
  375. <b>EndIf</b> ; Execute and ENDIF they are equal<br><br>
  376. The above code is the same as typing:<br><br>
  377. IF commodore = atari THEN PRINT "Welcome to the machine - commodore and atari..."<br><br>
  378. If you change the values of commodore and atari - so they are both different - see the end result?<br><br>
  379. You can also make your program follow a different chain of events if the values are not equal to.
  380. Here we introduce the <b>ELSE</b> command - so you would point this to a different
  381. piece of code that you wanted to execute - like below:<br><br>
  382. <b>IF commodore = atari<br>
  383. PRINT "The values are equal to"<br>
  384. ELSE<br>
  385. PRINT "The values are different"<br>
  386. ENDIF<br><br></b>
  387. You must remember that when the command <b>THEN</b> is not used you must
  388. use <b>ENDIF</b> - You will should also note that <b>ENDIF</b>
  389. is used whether or not the <b>ELSE</b> command is used.<br><br>
  390. <b><u>Types</b></u><br><br>
  391. Using normal arrays (as documented earlier) we can store important information
  392. about, for example, our players' positions:<br><br>
  393. <b>player = 3<br>
  394. x = 640<br>
  395. y = 480<br><br>
  396. Dim players (player, x, y)<br><br>
  397. players (2, 100, 100)</b> ; this sets player 2's on-screen position to x = 100, y = 100<br><br>
  398. This is fine, but what if we want to add more players? We have to re-Dim the
  399. array with the maximum number of players! It can also be quite difficult to
  400. remember what each element in the array stands for when you have a lot of
  401. elements, eg.<br><br>
  402. <b> Dim playerdata (player, x, y, animframe, weaponselected, lives, rockets,
  403. jewelscollected, blah, blah2)</b><br><br>
  404. Try just changing the "rockets" element later on when you can't see the
  405. actual list of elements without scrolling all the way back up!<br><br>
  406. A way around this general awkwardness is to use <b>types.</b> Whereas with the
  407. array, we made a "playerdata" array, here we'll make a "playerdata" type
  408. instead:<br><br>
  409. <b>Type playerdata<br>
  410. Field player<br>
  411. Field x<br>
  412. Field y<br>
  413. Field animframe<br>
  414. Field weaponselected<br>
  415. Field lives<br>
  416. Field rockets<br>
  417. Field jewelscollected<br>
  418. Field blah<br>
  419. Field blah2<br>
  420. End Type<br><br></b>
  421. Think it looks complicated? Well, let's take a look:<br><br>
  422. <b>Type playerdata</b><br><br>
  423. This line simply creates a new type (you'll also hear types referred to as
  424. "objects", so this might be called a "playerdata object").<br><br>
  425. <b>Field player<br>
  426. Field whatever</b><br><br>
  427. The field lines simply say what information we want to hold in the
  428. playerdata object.<br><br>
  429. <b>End Type</b><br><br>
  430. This just signifies the end of our type definition. <b>Don't forget it!</b><br><br>
  431. OK, in the array example, we wanted to change the rocket element (we'll give
  432. 'em 1000 rockets), which would mean typing something like:<br><br>
  433. <b> playerdata (player, x, y, animframe, weaponselected, lives, 1000,
  434. jewelscollected, blah, blah2)</b><br><br>
  435. Ouch, what a lot of typing! And I hope you remembered the order of the items
  436. (gosh, and you probably had to scroll all the way back up to find the
  437. definition of the array elements as well)! The efficient (read: cleverly
  438. lazy) programmer will use types instead. Using our playerdata type:<br><br>
  439. <b> playerdata\rockets = 1000</b><br><br>
  440. Now, not only is that shorter, but we didn't have to remember where to place
  441. the rockets value either (in fact, we couldn't even accidentally put it in
  442. the wrong place, since we're specifically saying "makes the rockets value
  443. equal 1000").<br><br>
  444. Note the way we access the type data, with a backslash like this: \<br><br>
  445. To read the jewelscollected value into a temporary variable and show the
  446. value, we'd do this:<br><br>
  447. <b>temp = playerdata\jewelscollected : Print temp</b><br><br>
  448. Of course, there's nothing to stop us printing the value directly:<br><br>
  449. <b> Print playerdata\jewelscollected</b><br><br>
  450. One very important aspect of using types is that you must tell Blitz that
  451. you're going to be assigning a variable with a custom type. If we want to
  452. make two players using the playerdata type, we do this:<br><br>
  453. <b> player1.playerdata = New playerdata<br>
  454. player2.playerdata = New playerdata</b><br><br>
  455. This can be read as "create a new playerdata object called player1" and
  456. "create a new playerdata object called player1". From here on, we can do
  457. whatever we like with our newly defined player objects.<br><br>
  458. It's important to remember this step, as you can't simply do this:<br><br>
  459. <b>player1.playerdata\rockets = 1000</b><br><br>
  460. ...without first creating the player1 playerdata object, as above!<br><br>
  461. To recap, if we want to use an object called "zoolook", of type
  462. "whatever", we do this:<br><br>
  463. ; Define our object structure:<br><br>
  464. <b>Type whatever<br>
  465. Field something</b><br>
  466. ; As many fields of information as needed for the zoolook objects<br>
  467. <b>End Type<br><br>
  468. zoolook.whatever = New whatever</b><br><br>
  469. Now we can use our zoolook object.<br><br>
  470. Where types can become really powerful is if we just need a heap of
  471. similar objects (for instance, the aliens in a "Space Invaders" clone), and
  472. want an easy way to define and manipulate them.<br><br>
  473. <b>Type invader<br>
  474. Field x<br>
  475. Field y<br>
  476. End Type</b><br><br>
  477. We can set up a huge number of alien invaders with a simple loop:<br><br>
  478. <b>For a = 1 to 100<br>
  479. alien.invader = New invader<br>
  480. alien\x = Rnd (GraphicsWidth)<br>
  481. alien\y = Rnd (GraphicsHeight)<br>
  482. Next</b><br><br>
  483. This appears to keep redefining a single object over and over! The strange
  484. thing about types, though, is that when we create a new object, we're
  485. actually creating a *reference* to an object, rather than the object itself.<br><br>
  486. Every time you say:<br><br>
  487. <b>something.mycustomtype = New mycustomtype</b> ; a new "customtype" called "something"<br><br>
  488. ...you're saying "create a new 'mycustomtype' object, and give me a
  489. reference to it; call my reference 'something'".<br><br>
  490. Although we can refer to them individually if we define objects with
  491. individual names (eg player1.playerdata, player2.playerdata), we can also
  492. ignore the names completely. If we give the same name to more than one item
  493. of a particular type, we're just adding to a hidden list of those objects,
  494. which we don't need to access individually. To draw all of our aliens, we'd
  495. simply do this:<br><br>
  496. <b> For nasties.invader = Each invader<br>
  497. DrawImage invaderimage, nasties\x, nasties\y<br>
  498. Next</b><br<br>
  499. ...and all of our aliens are drawn!<br><br>
  500. Note the importance of giving our For...Next variable (nasties) an
  501. "invaders" type while we read through our list of "invaders".<br><br>
  502. So, to recap, whenever we create a new object, we're really adding to a
  503. hidden list of objects of that type. When we want to be able to refer to
  504. each individual object, we give each a different name:<br><br>
  505. <b>Type playerdata<br>
  506. Field x<br>
  507. Field y<br>
  508. End Type<br><br>
  509. player1.playerdata = New playerdata<br>
  510. player2.playerdata = New playerdata</b><br><br>
  511. The computer adds each object to its hidden list of "playerdata" objects,
  512. which might look like this:<br><br>
  513. <b>xxxxx1.playerdata [player1]<br>
  514. xxxxx2.playerdata [player2]</b><br><br>
  515. ...but it doesn't matter to us how the computer stores its hidden list - we
  516. just refer to player1 and player2.<br><br>
  517. If we don't need to be able to access each item individually, we can do
  518. this:<br><br>
  519. <b>Type blah<br>
  520. Field whatever<br>
  521. End Type<br><br>
  522. For a = 1 to 3<br>
  523. myobject.blah = New blah<br>
  524. myobject\whatever = 5<br>
  525. Next</b><br><br>
  526. The above example creates a hidden list of "blah" objects, and sets the
  527. "whatever" value of each to 5. To the computer, they might be stored in some
  528. abstract form which we have no knowledge of, eg:<br><br>
  529. <b>xxxx1.blah [myobject]<br>
  530. xxxx2.blah [myobject]<br>
  531. xxxx3.blah [myobject]</b><br><br>
  532. Obviously, we can't read individual objects from this list, because we gave
  533. them all the same name! But we don't care; we didn't want to - we can read
  534. them "en masse" by using the For...Each...Next loop:<br><br>
  535. <b>For a.blah = Each blah<br>
  536. Print a\whatever<br>
  537. Next</b><br><br>
  538. Taken apart, we get this:<br><br>
  539. <b>For a.blah = Each blah</b><br><br>
  540. This reads each "blah" object in turn into a temporary object called "a",
  541. which has a "blah" type (so it can store the same data as our "blah"
  542. objects).<br><br>
  543. <b>Print a\whatever</b><br><br>
  544. This prints out the contents of the temporary "a" variable's "whatever"
  545. field.<br><br>
  546. <b>Next</b><br><br>
  547. This is just the normal "Next" used in a For...Next loop.<br><br>
  548. There's a lot more to types (hence their awesome power!), but this has
  549. basics, which will get you started!<br><br>
  550. <b>OK Now that you've covered the Basics look in the Blitz manual
  551. for a synopsis of the more advanced commands :) </b>
  552. <br><br><center><b>REMEMBER: Look in the Blitz Basic samples folder for lots of example code for
  553. you to play around with and more importantly - learn whilst having fun.</center><br><br>
  554. <hr><br><br>
  555. </body>
  556. </html>