|
OBEYBREW.COM | Tutorials | A Crash Course In HuC Part 2 A Crash Course In HuCPart 2 - To Infinity And Beyond!...well, not quite. This lesson is slightly more advanced than the last because we're going to introduce one of the most essential building blocks of any language: variables.What You'll LearnIn this lesson, you'll learn about variables and more text output stuff.What's A Variable?Simply put, a variable is a place where a quantity can be stored and this quantity can be changed. We use many variables in our everyday life without even knowing it: the time on the clock, the temperature of the oven, the number of times the boss blew his top at the lazy employee, etc. I'm sure you can think of hundreds, if not thousands, of things that are considered variables. So by understanding this concept, variables in computer terms are going to be really easy to understand.Variable TypesWhile the price of gas seems to have no upper limit, computer variables do. In HuC, there are two main types of variables. They are known as the char and the int short for character and integer. We could get into all kinds of trouble with bits and bytes right here and most language texts do... and that's exactly where most people get scared off. So in order to keep your interest, we're going to approach this subject from a different angle. For now, we will only care that each variable type can hold a certain quantity.A char is a small variable type. It can hold a number between 0 and 255. An int is a large variable type. It can hold a number between -32768 and 32767. Yes, that's right... an int can hold a negative quantity, sort of like the temperature in Canada. So, if this is all clear to you, let's get into some code and stop the techno-jabber. Setting Up Our ProgramRemember the first crucial part of any HuC program?#include "huc.h" main() { Creating VariablesWell then, let's put our newfound knowledge of variables to work right away.int myint; char mychar; Making Them Worth SomethingVariables are pretty useless empty. In fact, the compiler moans like a child who dropped his ice cream if we don't actually do something with them. Fortunately, variables are painfully easy to make worth something. Observe:myint = 15000; mychar = 5; Now, myint is worth 15000 and mychar is worth 5. Great, now what? Well, perhaps we should show the public the results of our hard work, eh? We're going to use a function from the standard library for showing numbers, called put_number. But wait a second... we haven't set up any means of displaying text, so we have to add in our little font stuff from the last lesson before we use put_number. No problem! set_color_rgb(1, 7, 7, 7); set_font_color(1, 0); set_font_pal(0); load_default_font(); put_number(myint, 5, 0, 0); Okay, we have one number on the screen, but we created two variables, right? So, we should put the other on the screen too! put_number(mychar, 1, 0, 1); Making Variables Be VariableA variable is pretty useless if we can't change its value. So next, we're going to do a little messing around with our two variables and they'll even work together a bit.myint = myint + mychar; put_number(myint, 5, 0, 2); Another Way To Addmyint now holds a value of 15005. Let's mess with it a little more though. I'm going to show you a little bit of C shorthand here.myint += mychar; put_number(myint, 5, 0, 3); Extremely Simple AdditionNow for everyone's favorite C addition technique! Here's the simplest thing you'll ever see when you mess with variables in HuC:myint++; put_number(myint, 5, 0, 4); It Works Both WaysFinally, let's mess around a little more. Up to this point we've only done addition, so let's do a little subtraction. We're going to use both the long and the short forms for this last example.myint = mychar - myint; myint -= mychar; myint--; put_number(myint, 6, 0, 5); Okay then, that just about wraps it up, but we've not finished the function yet... remember how to do it? :) So... What's Next?If you have grasped this lesson, you are already halfway there. By understanding variables at the basic level, the rest will easily fall into place. You will utilize variables from here on, because they are essential to programming any program, especially games. After all, how do you think things like high scores, energy, lives, even character positions on the screen are kept? Yep, variables. You should now understand just how important these are and it cannot be stressed enough that you understand how they work.In the next lesson, we'll look at another core concept of programming: the for loop . We'll also get our feet wet with some basic sprite function. And of course... variables will rear their ugly heads throughout the entire lesson. Full Program Listing#include "huc.h" main() { int myint; char mychar; myint = 15000; mychar = 5; set_color_rgb(1, 7, 7, 7); set_font_color(1, 0); set_font_pal(0); load_default_font(); put_number(myint, 5, 0, 0); put_number(mychar, 1, 0, 1); myint = myint + mychar; put_number(myint, 5, 0, 2); myint += mychar; put_number(myint, 5, 0, 3); myint++; put_number(myint, 5, 0, 4); myint = mychar - myint; myint -= mychar; myint--; put_number(myint, 6, 0, 5); } Sample OutputSee AlsoA Crash Course In HuC - Part 3 | put_number |