SPRTTST2.bas suggested modification

A place to upload updates that you have made to any demo included with Just BASIC.
This is only for updates of existing demos, not new candidates,.

JanetTerra
Site Admin
Posts: 117
Joined: Wed Nov 24, 2004 2:49 am

SPRTTST2.bas suggested modification

Post by JanetTerra » Thu Oct 26, 2006 1:45 am

The sprite example programs found in Just BASIC were very helpful to me when I first began learning to program sprites. One thing I found was that I needed to actually see what was happening to learn from the code. I found if I used a timer to pause between the drawsprites, I could better visualize what was happening. The original code found in the Just BASIC example files -

Code: Select all

    loadbmp "smiley1", "sprites\smiley1.bmp"
    loadbmp "smiley2", "sprites\smiley2.bmp"
    loadbmp "smiley3", "sprites\smiley3.bmp"
    loadbmp "smiley4", "sprites\smiley4.bmp"
    loadbmp "landscape", "sprites\bg1.bmp"
    WindowHeight = 300
    WindowWidth = 400
    'graphicbox #wg, 0, 0, 400, 300
    open "sprite test" for graphics_nf_nsb as #wg
    print #wg, "background landscape";
    print #wg, "addsprite smiley smiley1 smiley2 smiley3 smiley4";
    print #wg, "addsprite smiler smiley1 smiley2 smiley3 smiley4";
    print #wg, "addsprite smiled smiley1 smiley2 smiley3 smiley4";
    print #wg, "addsprite smiles smiley1 smiley2 smiley3 smiley4";
    print #wg, "cyclesprite smiley 1 once"
    print #wg, "cyclesprite smiler 1"
    print #wg, "cyclesprite smiled 1"
    print #wg, "cyclesprite smiles 1"
    for x = 1 to 100 step 2
        print #wg, "spritexy smiley "; x; " "; x
        print #wg, "spritexy smiler "; 100-x; " "; x
        print #wg, "spritexy smiled "; 100-x; " "; 100-x
        print #wg, "spritexy smiles "; x; " "; 100-x
        print #wg, "drawsprites";
        print #wg, "spritecollides smiley list$"
        if list$ > "" then print #wg, "cyclesprite smiley 1 once"
    next x

    input a$
Suggested Modifications
Add some remarks for clarification
Remove the initial Cyclesprite 1 once command to avoid confusion
Add a proper Trapclose statement
Use a Wait instead of Input
Unload the bitmaps before the program ends
Add a timer to pause between drawsprites to see what is happening

The modification

Code: Select all

' SPRTTST2a.bas
' A modification of the original example program SPRTTST2.bas

' A demo to illustrate the CYCLESPRITE command

' 4 sprites are loaded in this demo.  3 of these sprites are issued the
' cyclesprite 1 command, causing those 3 sprites to cycle forward
' through the list of images each time a drawsprites command is
' issued.  The first sprite, smiley, is not issued the cyclesprite 1
' command until it has collided with another sprite.  Smiley starts
' off not cycling.  Later, when smiley collides with the other sprites,
' smiley begins to cycle like the others.

' Load the sprite bitmaps
    loadbmp "smiley1", "sprites\smiley1.bmp"
    loadbmp "smiley2", "sprites\smiley2.bmp"
    loadbmp "smiley3", "sprites\smiley3.bmp"
    loadbmp "smiley4", "sprites\smiley4.bmp"
    loadbmp "landscape", "sprites\bg1.bmp"

' Define the size of the window
    WindowHeight = 300
    WindowWidth = 400
'Open the window
    open "sprite test" for graphics_nf_nsb as #wg
' Trapclose commands tells code where to go when window closed
    print #wg, "trapclose [quit]"
' Set the landscape bitmap as the background
    print #wg, "background landscape";
' Add the sprites; chain the images for cycling
    print #wg, "addsprite smiley smiley1 smiley2 smiley3 smiley4";
    print #wg, "addsprite smiler smiley1 smiley2 smiley3 smiley4";
    print #wg, "addsprite smiled smiley1 smiley2 smiley3 smiley4";
    print #wg, "addsprite smiles smiley1 smiley2 smiley3 smiley4";
' Cyclesprite smiley1 is not issued a cyclesprite command, so image doesn't change

' Cyclesprite smiler 1 says to cycle forward one image on each drawsprites command
    print #wg, "cyclesprite smiler 1"
' Cyclesprite smiled 1 says to cycle forward one image on each drawsprites command
    print #wg, "cyclesprite smiled 1"
' Cyclesprite smiles 1 says to cycle forward one image on each drawsprites command
    print #wg, "cyclesprite smiles 1"
' Move each sprite 50 times
    for x = 1 to 100 step 2
' spritexy smiley designates the next x, y position for this sprite
        print #wg, "spritexy smiley "; x; " "; x
' spritexy smiler designates the next x, y position for this sprite
        print #wg, "spritexy smiler "; 100-x; " "; x
' spritexy smiled designates the next x, y position for this sprite
        print #wg, "spritexy smiled "; 100-x; " "; 100-x
' spritexy smiles designates the next x, y position for this sprite
        print #wg, "spritexy smiles "; x; " "; 100-x
' The drawsprites command shows in the sprite in its updated position
        print #wg, "drawsprites";
' spritecollides puts the names of any 'touching' sprites in variable list$
        print #wg, "spritecollides smiley list$"
' If there has been a collision (contact) then cyclesprite smiley forward (1) just once
        if list$ > "" then print #wg, "cyclesprite smiley 1 once"
' Add a pause routine to see movement
        timer 250, [resume] ' sets timer to 250 milliseconds
        wait ' wait here until timer fires
        [resume]
        timer 0 ' turn off timer
    next x

    wait

[quit]
    timer 0 ' turn off timer
' Unload the bitmaps
    unloadbmp "smiley1"
    unloadbmp "smiley2"
    unloadbmp "smiley3"
    unloadbmp "smiley4"
    unloadbmp "landscape"
' Close the window
    close #wg
' End the program
    end
Would it be better to keep the Cyclesprite 1 once command in?

Janet
You do not have the required permissions to view the files attached to this post.