Blitz Basic Tute #2 - Cards All Over

The plan here is to slowly fill up the screen with playing cards.

  1. Extract the 53 files from cards.zip into the directory where you intend to do this tutorial. The cards are numbered from 1.png to 52.png.
  2. Create a new file called cards_all_over.bb and double click it to start Blitz Basic.
  3. The first line is always graphics 640,480 so you can type that in straight away.
  4. Now we want to pick a card - this means picking a number from 1 to 52. This is how we tell BB to do this: card=rand(52) "rand" is short for "random" - note that it is a word that BB "knows" and that "card" is our word.
  5. card_img=loadimage(card+".png")
    drawblock card_img,rand(640),rand(480)

    Finish off in the same way as the first tute and try your program - you should see a single card appear on the screen.
  6. Now the idea is to repeat the above three steps until the viewer presses the Esc key. We do that by surrounding the above three lines with this pair:
    repeat
    until keyhit(1)

  7. The effect needs to be slowed down - we need a delay in the loop: delay 100
  8. If you were to leave this program running all night, your computer would slowly die as we filled its memory up with card images. It is very important to free up memory when you no longer need it. So after the card is drawn you need:
    freeimage card_img

Again there are things I want to point out to you:

This time I have shown the lines as I typed them - i.e. all in lower case. You will notice that BB does the Capitalization for you. This Capitalization is simply for clarity.

The lines in the loop are indented for clarity not because BB requires it. Use the Tab key to start indentation and the BackSpace key to stop it.

keyhit(1) - the "1" is the code for the Esc key. There is a code for each of the keys on the keyboard but we will adopt the convention of ALWAYS using the Esc key to Escape from our program.

The delay is measured in milliseconds (thousandths of a second). Deciding on what value to use is best done by experiment.


Valid HTML 4.0 Strict Valid CSS