Posts: 3,012
Threads: 115
Joined: May 2009
"Don't do this Sengir, you're embarrassing yourself..."
Hello all and welcome to the Coder's Resource: SmallBasic. Every tutorial series prior to this it seems like I've expected a small amount of understanding how programming works, which was in error on my part. That's why I chose SmallBasic as the language for this series; it's a very easy language to learn programming logic with before you move up. This will hopefully be a weekly thing as time permits, posted every Friday.
You can ask questions if you need help! SmallBasic has an easy code-sharing system, just press publish and it will give you a code. Putting that code in the import window will, uh, import the project.
I'll be using this code-sharing method in later tutorials so you can import the lesson's code.
Key Words and Concepts
Object
Method
Parameter
Downloads
SmallBasic Installer
This will be a very short lesson, mostly just describing how things work.
Upon starting up SmallBasic (from now on shortened to "SB") you will be greeted with a friendly-looking user interface and an empty, welcoming notepad. If you start to type you'll notice that SB gives you a list of objects and methods.
Let's type this out:
Code: TextWindow.WriteLine("Hello World")
It should be pretty self-explanatory, but let's go over it anyways:
TextWindow is an object that references the Console. As an object it can hold variables (fields for numbers , text or more), methods and even other objects.
One of TextWindow's methods is called WriteLine. Methods are like a list of actions to take. You can define a method in code and later go through those actions. It's very helpful if you have some specific code you would like to run over and over throughout your app.
The parentheses after WriteLine let the computer know that you are running the method, not just referencing it. The text (or in programming terms, "string") in between the parentheses is a parameter. A lot of methods have parameters, they are used to give variables to the method. In this case we are sending (or "passing") "Hello World" to the method, otherwise the compiler wouldn't know what to do and crash the application.
Now that we know how that code works, let's try this:
Code: sHelloWorld = "Hello World"
TextWindow.WriteLine(sHelloWorld)
Here we are creating a variable that holds our "Hello World" string and passing that as a parameter. Why would we want to do that instead of just passing the string itself? Well, take a look at this:
Code: sHelloWorld = "Hello World"
TextWindow.WriteLine(sHelloWorld)
sHelloWorld = Text.ConvertToUpperCase(sHelloWorld)
TextWindow.WriteLine(sHelloWorld)
Now you're probably wondering what's going on in line 3. Methods can return certain values after being run (or in programming terms, "called"), which is really helpful. In this case we are giving the ConvertToUpperCase method our variable and it is returning a new value that we are putting back into the variable. It should be noted that whenever a single equal sign is being used to set something the methods on the right side of the equal sign always run first, so using sHelloWorld to set sHelloWorld isn't a paradox!
Now we have one more thing before this lesson is over:
Code: TextWindow.WriteLine("Hello, what's your name?")
sName = TextWindow.Read()
sName = Text.Append("Hello, ",sName)
TextWindow.WriteLine(sName)
Here we're setting sName in a different way: we're checking what the user has typed. The TextWindow.Read method is an interesting one, it pauses the code until the player presses Enter. So now when a user enters their name the app take the string from the TextWindow, appends a greeting before it and writes it back out to the TextWindow.
That's all for this week, I'm afraid. Next week we shall step into The World of If and Else: Beginning Logic and make some neat applications that actually do things.
For this week's "homework" I'd like to see you guy experiment with the TextWindow and post your creations' sharing codes here
Thanked by: Britt, Zadaben, Virt, Vipershark, Chris2Balls [:B], canasniimehugh, StarSock64, Crappy Blue Luigi, recme, puggsoy, Garamonde, Zac, Baegal, Shade, Gors, Iceman404, BobbyLala, Guy, Candel, TGE, ~Axis~
Posts: 1,213
Threads: 15
Joined: May 2008
I did it, yay
I guess I'll be looking forward to next time too...
I don't really have an interest in coding, but why not
I hope others follow along too
Posts: 694
Threads: 16
Joined: May 2008
I will be following the thread, I need to brush up on my coding.
No semicolons on the end of lines.
What sort of sick language is this.
Seriously though, this looks cool. I mean I already code but this is really handy and easy for people who want to learn general programming conventions and logic before moving on to more advanced languages, with semicolons and stuff.
You may have a fresh start any moment you choose, for this thing that we call "failure" is not the falling down, but the staying down. -Mary Pickford
Posts: 3,012
Threads: 115
Joined: May 2009
(08-11-2012, 05:08 AM)puggsoy Wrote: No semicolons on the end of lines.
What sort of sick language is this.
Seriously though, this looks cool. I mean I already code but this is really handy and easy for people who want to learn general programming conventions and logic before moving on to more advanced languages, with semicolons and stuff.
I've a long-term plan for this whole thing, eventually ending with XNA. And while this week was a bit lacking in content I've already begun working on the next lesson for Friday
I'll try this when I'm at home, but can I make a suggestion? While the text explains the actions very well, could you use visual metaphors or graphs to aid explanation? I think it'll make the learning much more enjoyable if you do so; I'll try making an example if I have some time.
Posts: 55
Threads: 1
Joined: Oct 2008
I honestly don't know shit about coding,and it's something I don't even wanna learn at all for some personal reasons.
But I'm just dropping by to say that it's really cool of you to take your time and explain things like this to TSR.You're a pretty cool guy.
I agree with Gors btw,I believe images would really help the learning proccess.
Posts: 3,012
Threads: 115
Joined: May 2009
I'll update with pics and better explanations when I get back from school after this week in the dorms. In the meantime I'm writing up the next lesson (albeit without SmallBasic even installed)
Wh-why in the world does that program call you a liar...? That's really freaky and makes no sense...
Posts: 3,787
Threads: 75
Joined: May 2008
Quote:Milk?
I'm ok with this.
Oh okay.
Code: Text.ConvertToLowerCase(x)
If x = "milk" Then
Sorry to interrupt and no hard feelings okay
but I do use shift a lot
it's ok, I'm not used to Writing Like A Loser
(jk ilu )
Posts: 3,012
Threads: 115
Joined: May 2009
Sorry for being so late!
Key Words and Concepts
Boolean
Conditional
Now that we have an app that takes input, processes it and outputs it to the TextWindow we can move on to the next step, figuring out what the player entered.
Last lesson we had a simple app that had you enter your name and greeted you:
Code: TextWindow.WriteLine("Hello, what's your name?")
sName = TextWindow.Read()
sName = Text.Append("Hello, ",sName)
TextWindow.WriteLine(sName)
Today we'll be adding onto that and start our first game project. It will be an extremely basic game (similar to Hamurabi for those interested) and we'll be working on it for the next few lessons.
So let's start out with some stuff we know how to do already:
Code: iYear = 2006
iMonth = 1
iActiveMembers = 20
TextWindow.WriteLine("Hey Dazz, you just became the new Head Admin for tSR!")
TextWindow.WriteLine("Here are some monthly tSRc stats for " + iMonth + " " + iYear + ":")
TextWindow.WriteLine("There are " + iActiveMembers + " active members on the forum")
TextWindow.WriteLine("")
TextWindow.WriteLine("Would you like to [advertise], [update] the mainsite or run a forum [event]?")
sChoice = TextWindow.Read()
"Wait Sengir, we can add append text without using the Text.Append method??"
Yes you can, just use the plus sign and it will connect strings of text together. It's very handy.
The next thing we will do is learn about booleans. A boolean is pretty much a true-or-false logic type. A common use for boolean logic is when checking if something meets a set of requirements. We can say it like this:
"If tSR becomes world-famous then Sengir will sell his mod-stock and cash out"
This is a conditional. We use conditionals all the time in real life, so why not in code? I'll show you how it's done:
Code: iYear = 2006
iMonth = 1
iActiveMembers = 20
TextWindow.WriteLine("Hey Dazz, you just became the new Head Admin for tSR!")
TextWindow.WriteLine("Here are some monthly tSRc stats for " + iMonth + "/" + iYear + ":")
TextWindow.WriteLine("There are " + iActiveMembers + " active members on the forum")
TextWindow.WriteLine("")
TextWindow.WriteLine("Would you like to [advertise], [update] the mainsite or run a forum [event]?")
sChoice = TextWindow.Read()
sChoice = Text.ConvertToLowerCase(sChoice)
If sChoice = "advertise" Then
iActiveMembers = iActiveMembers + 5
TextWindow.WriteLine("You advertise on Facebook and get 5 new members!")
EndIf
If sChoice = "update" Then
iActiveMembers = iActiveMembers + 2
TextWindow.WriteLine("You make an update to the mainsite and 2 rippers joined!")
EndIf
If sChoice = "event" Then
iActiveMembers = iActiveMembers - 3
TextWindow.WriteLine("You let Sengir start a forum event but it drove 3 people away!")
EndIf
iMonth = iMonth + 1
TextWindow.WriteLine("It is "+ iMonth + "/" + iYear + " and you have " + iActiveMembers + " active members on the forum")
It might look a little daunting but it's really easy to understand if you look at it like that sentence earlier. For example, this code:
Code: If sChoice = "advertise" Then
iActiveMembers = iActiveMembers + 5
TextWindow.WriteLine("You advertise on Facebook and get 5 new members!")
EndIf
could be read as
"If the player's choice was to advertise then increase the number of members by 5 and display some text informing the player."
That's much If Statements right there. I'd like to do Else Statements in this lesson but my plans for next lesson make for a better introduction.
It's pretty much just a "If tSR becomes world-famous then Sengir will sell his mod-stock and cash out else he'll continue working for Dazz happily."
EITHER WAY that concludes this week's lesson
Voluntary "homework" is to, uh, make an app that uses commands? Knock yourself out with whatever, I'd just like to see it
http://smallbasic.com/program/?KGQ218
whipped this up at work, might have bugs and stuff but I couldn't test it often OTL
COLLECT 1000 BOTTLE CAPS TO WIN
Posts: 3,787
Threads: 75
Joined: May 2008
|