ch1 cpp

lifestyleappsios windows appcolt wargamesios
 08 March 06:08   

    In this program ming/php/affiliate- program s-list/' onMouseOver="tip('infobox1')" onMouseOut="untip()"> affiliate you will address your additional program for C++. It will advice you apprentice about variables. It will awning what they do and advice you adjudge what you charge in adjustment to use them. This affiliate will use ascendancy loops but will not explain them in depth, because that is the job for the next chapter; and we would not wish to abash you. This affiliate will aswell use variables, but not absolutely explain what their types mean; this will delay for the addition chapter.

    You will accept to understand how to use your specific compiler. Analysis the appendixes for added information.

    The program you will be accounting using accepted C++ libraries, but some compilers tend to not chase the accepted a hundred percent, so there will be a supplement absorbed to the basal of this affiliate that explains what to do if you run into problems with specific compilers.

    The first affair we wish to do with any program is to address the capital access point.

    int main()

    main() is basically what the arrangement calls if you alpha the program . It should consistently accept the blazon accumulation (int), and barring absurdity should consistently acknowledgment 0. Some IDEs (the affair you address your cipher in) are set up so they can acquaint you the acknowledgment value. You can yield advantage of that to advice you in debugging your program .

    Variables are pieces of advice that can vary from time to time (ie they are changeable). All of your non-variable cipher exists alone to dispense variables. The eventually you apprehend this, the bigger a coder you will become. One affair this means, is that you should typically, alive that not aggregate can be foreseen, ascertain your variables first, then starting autograph your code.

    In adjustment to understand what variables you will need, you accept to understand what you charge to do, ie understand your program . We are traveling to program a argument cartoon bold alleged Pitfall. Its a bold were you abatement through a pit and action about aggravating to abstain hitting the bedrock walls about you. If you atrociously accident into the ancillary of the wall, you are dead, and the bold is over.

    So we understand that in our program that we are traveling to accept a being and a pit. We aswell know, from our accomplished experiences, that any program that runs in absolute time, ie does not delay for user input, and does not accomplish a hidden data action and abdicate (ie a compiler), needs a action to accumulate clue of time. With a timer, the program can be kept beneath ascendancy so it does not accomplish calculations at millions of times the amount that the user can anticipate at.

    So now we understand that we are traveling to charge variables for a person, a pit, and a timer.

    We are traveling to charge to understand what our being looks like. We accept decided1 to represent a being with a individual character, the Y. It looks affectionate of like a being with their accoutrements afraid out into the air.

    char being = Y;

    Now, you are apparently already thinking, why not just if it comes to announcement the being on the screen, just acquaint it to book the Y directly! It would be the easier way to do it, but it would create the program harder to change after on. What if you absitively you capital it to be a X instead, cerebration that it looks added like a being back it has accoutrements and legs. This way you can just change one variable, and not coursing down all the commands that use it. And if you absolutely wish to accomplish this fact, and its acceptable abstraction to do so, you ascertain it as:

    const char Being = Y;      //capitalizing can be acclimated to differentiate constants from approved variables

    This agency that the appearance is a constant! Ie, a non capricious variable. It absolutely isnt constant. You could change it while the program is active through accumulation if you absolutely acquainted the need, but the const just tells C++ to not let you change it (this makes it harder to create mistakes). If you acquisition out after that you charge to change it during runtime, then you can just abolish the const from out in foreground of it.

    We aswell charge to understand area our being is at. We are traveling to accord him a x and a y position. Our bodies x position refers to his accumbent (-) position beyond the screen. Our bodies y position refers to his vertical (|) position on the screen.

    int personX = 39;

    int personY = 5;

    An adroit clairvoyant would accept noticed that it seems I accept added some numbers to our declarations. These numbers specify the starting position of our person. In a accepted graphical argument mode, which we will not be alteration back altered compilers accept different, or no adjustment at all, for accomplishing this, there are 80 characters angular and 25 characters angular (80x25). 39 characters from the larboard is appropriate in the average of the screen. You ability say 40 is the middle, but bethink affiliate 0, computers calculation from 0, not 1 (so decrease 1). 5 characters is just a little down from the top of the screen. We wish to accumulate our being up top so the bouldered walls can be added calmly abhorred (look ahead), but not too far up because it looks bad.

    Now we can move to defining our pit. Our pit is just traveling to be a tunnel. Back it is consistently a acceptable abstraction to use as little variables as accessible to conserve ram, a little antic here, but about a acceptable program ming trait, we are just traveling to ascertain variables for both abandon of the pit (if you are clue beneath appropriate now why this is, then use your acuteness to account a array of a behemothic pit). We are traveling do draw the pit so it extends from the top of the awning down to band amount 20; because of this we charge to ascertain it as an array.

    int start[20];

    int end[20];

    An arrangement is just a alternation of variables, and in this case they are start[0], start[1], start[2] ..., start[18], start[19] (20 variables starting from 0). It is an easier way to ascertain 20 variables after accounting a altered name for anniversary one. Also, it helps if you dont understand which accurate one you wish to use in advance.

    By now you accept apparently noticed the twenty and anticipation able-bodied if we are not declared to anon use the Y, then we shouldnt anon use the 20 also, and you are right! It would be bigger authentic as:

    const int LastLine = 20;

    int start[LastLine];

    int end[LastLine];

    This way in the approaching if we adjudge to create changes to the screen, for example, aggrandize the bulk of curve it shows, we can just change the variables. You ability never create such changes generally in a program this small, but in bigger projects accomplishing this will save a lot of time and grief, abnormally if one capricious controls the accomplishments of hundreds of statements and functions.

    Now we charge to adjudge what the pit will be create out of. Acutely a agglomeration of ks or os wouldnt attending actual good, so we are traveling to use one of the continued characters. Appearance 219 is a solid block, hence:

    const int block = 219;

    Next we will ascertain the variables we will use to do the timing.

    int timeCounter = 0;

    clock_t timeRate = 0.5

    We will use timeCounter to accumulate clue of how abundant time has anesthetized faculty the alpha of the program , and timeRate to adjudge how abundant time will canyon amid updates; in this case its traveling to be bisected a second. Some compilers accept a altered bulk of alarm ticks they accomplish per second, so they all (if they chase the standard) will accept a connected alleged CLOCKS_PER_SEC that contains the amount of how some alarm ticks there are per second. By adding .5 by the constant, timeRate will authority how some ticks action every bisected second.

    The capricious timeRate is declared as a clock_t, back CLOCKS_PER_SEC is aswell a clock_t. clock_t is a blazon declared in acclimated to represent times. As, clock_t is in namespace std we will use the declaration

       using std::clock_t;

    so that we can accredit to it.

    The time routines are allotment of the C++ accepted library. Back it is a library, even admitting its standard, you accept to cover it in your antecedent cipher through a attack file. This attack book lets the compiler understand how to admit the timer functions if you use them. All you accept to do is add the #include to the top of your antecedent cipher and the compiler will do the rest.

    #include

    You ability be apprehensive if that band is a typo, but nope, you do not charge the ; at the end of the #include like you do aggregate else. If you do cover it, your book will apparently not abridge at all.

    Now, lets alpha coding something additional than variables. First we are traveling to create it so that the program will run until you columnist escape (escape = key 27). We will use the while(); ascendancy statement. You can use it to do something while a action is true (like escape not getting pressed). We accept not yet authentic a capricious to abundance what key has currently been pressed, so we will just do them both at the aforementioned time.

    int key = 0;

    while (key != 27)

    Your antecedent cipher should now attending like this

    #include

    using std::clock_t;

    int main()

       return 0;

    }

    Within the while(); ascendancy account we are first traveling to abode the functions that will amend the accompaniment of the key. This cipher can be aggregate appropriate now, but after a way to amend the key variable, it will go on forever, back the capricious key will never according 27 (esc). Luckily for us the accepted library contains a few means to collaborate with the keyboard.

    #include       //add this band at the actual top

    while (key != 27)

    }

    return 0;

    The attack book conio.h includes commands advised accurately for animate ascribe and output. This includes the kbhit() function. What kbhit() does is allotment a true if there is a key cat-and-mouse in the keyboard absorber (where keys go if they are pressed), contrarily it allotment false. So if kbhit() is true then, and alone then will it assassinate key = getch();. The action getch() gets the next appearance from the keyboard absorber and allotment it as an accumulation (int). In adjustment to aggregate this value, we set key according to it.

    The cause the action getch() is central the if ascendancy account testing whether a key has been hit or not is because getch() will delay for a key to be pressed, if no key has already been pressed. This is a analgesic if it comes to any bold implemented in absolute time, so this way getch() will alone be accomplished if it does not accept to delay for a appearance beneath this scheme.

    Now you can abridge the cipher and it will sit there, not absolution you do annihilation until you columnist escape.

    Next we should amount out what keys the keyboard absorber letters were apprenticed if you use the arrow keys. Two keys are reported, but altered compilers can address altered keys2. We are traveling to adapt the bend like this:

    #include    //you should aswell add me to the actual top

    using namespace std;

    while (key != 27)

    }

    The attack includes the cout object. The using namespace std; declares that you are using the accepted namespace. If you accept agitation accumulation it, and get some absurdity about a non absolute namespace, then you should try to get a compiler and library that conforms to ISO C++ standard. If you cant, removing the band ability work. If you do crave that line, you could bead it and address cout as std::cout instead, but this way there is alone one band of aberration amid the compilers that do, and do not charge it, which makes this easier to teach2. It aswell saves use from accepting to blazon std:: over and over again.

    Now if you abridge and run it, you shall see a screen, or a dos box, which is absolutely bare until you columnist a key. If that happens, its agnate key codes will get printed. You can play with this till your hearts content, but we are alone absorbed in 4 key codes, which are the up, down, left, and appropriate arrow keys. Columnist them all and appropriate down your numbers. Create actually abiding you accept them right, and bethink them; we will use them later.

    Next we are traveling to set up the timing routines! The accepted timing routines are a little weird, but simple to use. The clock() action will alpha counting on its first use, appropriately ambience it according to our timeCounter capricious should do the trick. It aswell insures that our timer starts at aught too; you can never be too prudent3.

    timeCounter = clock();

    After the first use of the clock() action it will acknowledgment the bulk of ticks back its first use. We can accordingly analyze it to our timeCounter capricious to see if clock() allotment a greater amount than timeCounter contains (ie which agency a assertive bulk of time has passed). We can use the if(); ascendancy account to analysis for it.

    When that time has passed, then we will accept to add our timeRate to our timeCounter, so it will yield clock() addition 0.5 abnormal to become bigger than our timeCounter again. We can use += for that, which agency timeCounter = timeCounter + timeRate; its just a lot easier!

    while (key != 27)

       //Manage Timing

       if (timeCounter - clock() < 0)

       

    }

    You may accept noticed, you can now yield out the account that prints the key codes. To analysis our routines you should aswell add the account that says 1/2 abnormal pass.... It will book every 1/2 seconds.

    Source Cipher Check: (It should attending something like this.)

    #include

    #include       //bug: Alive On This, charge alternative

    #include

    using namespace std;

    int main()

          //Manage Timing

          if (timeCounter - clock() < 0)

          

       }

       return 0;

    }

    If you accept not already, it would be a acceptable abstraction to abridge this to see if it works. If it does not work, and you accept absolutely what is above, allege to your instructor, sometimes awe-inspiring things happen.

    Next we are traveling to apparatus announcement our person, and the arrow key routines! What fun!

    ----

    1 Capitalism is fun.

    2 Verses authoritative this two chapters... Note: Your adviser can specify these things for you if you are not sure.

    3 Well, until you alpha axis out ultra apathetic software, then youre getting too prudent.

    

 


Tags: program, little, change, position, different, starting, numbers, easier, person, character, press, characters, includes, looks, contains, functions, value, control, start, keyboard

  , clock, variables, program, person, timecounter, start, variable, standard, screen, chapter, pressed, namespace, position, different, function, statement, timerate, change, define, compilers, control, routines, return, second, compiler, const, characters, getch, character, source, keyboard, returns, library, ticks, looks, header, little, constant, escape, press, means, value, timer, easier, compile, write, equal, kbhit, buffer, includes, seconds, codes, arrow, noticed, directly, later, probably, starting, thing, really, numbers, passed, timing, functions, lastline, contains, ,    , wait for, control statement, namespace std, source code, key codes, timecounter clock, keyboard buffer, key has, header file, int main, time has,      , manage timing   , timing     ,   manage timing ,     return, int main  , main    ,     manage,

Share ch1 cpp: Digg it!   Google Bookmarks   Del.icio.us   Yahoo! MyWeb   Furl  Binklist   Reddit!   Stumble Upon   Technorati   Windows Live   Bookmark

Text link code :
Hyper link code:

Also see ...

Permalink
Article In : Computers & Technology  -  Programming