Code and images can be downloaded from http://jbusers.com/phpBB/viewtopic.php?p=497#497
This morning I woke up and said to myself, "Welopez, let's do something different today!" Since I am not a very good artist, I thought I might like to try creating a simple image using MS Paint. After doing that, since I am not much of a graphics guru, I thought I'd like to try creating a SPRITE. If I were successful with those two tasks, I'd like to write DEMO programs to display a BMP image, and a SPRITE created by yours truly.
Creating the UFO image, no matter how poor it looks, wasn't difficult. Mission one accomplished! Before I could embark on task #2, I had to study a wee bit. I went to the Liberty Newsletters, in particular the article on sprites in LB NL#132. Aha, says I to myself! Alyce makes it seem so simple! So I created a UFO sprite. Task #2 completed.
Only two tasks remaining. Since I've had practice displaying BMP images, I chose to tackle that code first. In Demo1, the thing I want to change automatically is the background color of the Window for Graphics. Not a difficult task, simply put the FILL [i]color[/i] into a string, then use RND() and WORD$() to get a random color for fill. Ahhh, that was easy! Task #3 accomplished.
'Using BMP image over changing background colors
'by Welo, 2/11/2006
newColor$="lightgray" 'Initial color for background
bgColor$="red black cyan lightgray blue white" 'New colors
NOMAINWIN
LOADBMP "ufo", "ufoImg.bmp"
WindowWidth=200
WindowHeight=200
UpperLeftX=INT((DisplayWidth-WindowWidth)/2)
UpperLeftY=INT((DisplayHeight-WindowHeight)/2)
OPEN "Image Demo 1" FOR GRAPHICS_nsb AS #g
PRINT #g, "trapclose quit" 'Quit is located in a sub-routine
PRINT #g, "DOWN"
DO
cycle=cycle+1
IF cycle=10 THEN 'Let's choose a random background color from bgColor$
cycle=0
newColor$=WORD$(bgColor$, INT(RND(0)*6)+1)
END IF
PRINT #g, "FILL "; newColor$
x=INT(RND(0)*150)+1
y=INT(RND(0)*150)+1
PRINT #g, "PLACE "; x; " "; y
PRINT #g, "DRAWBMP ufo"
CALL pause, 350
LOOP WHILE 1=1
WAIT
SUB quit handle$
UNLOADBMP "ufo"
CLOSE #g
END 'End main program
END SUB
SUB pause mil
TIMER mil, [cont]
WAIT
[cont]
TIMER 0
END SUB
When you run the code, you'll notice the little UFO looks pretty good on a lightgray window, because I filled the image with lightgray for everything outside the UFO. This works great if the color of the window is the same as the fill color of the BMP, but not so great when another color is used. Two points for the BMP image: It's quick, and easy, so long as you display it only over the fill color.
Now to tackle task #4. I used techniques from Alyce and Janet to fill a Window for Graphics, GETBMP from the window position and add it to the program. This has to be done before displaying the newColor$ as BACKGROUND bg. Because this is a graphics window, we can change the color on the fly and we do this each time the UFO SPRITE has been displayed for 10 cycles. Success! Task #4 accomplished!
'Using SPRITE over changing background colors
'by Welo, 2/11/2006
newColor$="lightgray" 'Initial color for background
bgColor$="red black cyan lightgray blue white" 'New colors
NOMAINWIN
LOADBMP "ufo", "ufoSprite.bmp"
WindowWidth=200
WindowHeight=200
UpperLeftX=INT((DisplayWidth-WindowWidth)/2)
UpperLeftY=INT((DisplayHeight-WindowHeight)/2)
OPEN "Image Demo 2" FOR GRAPHICS_nsb AS #g
PRINT #g, "trapclose quit" 'Quit is located in a sub-routine
PRINT #g, "ADDSPRITE ufo ufo"
PRINT #g, "DOWN"
[getNewColor] 'We'll come back here after 10 cycles
PRINT #g, "FILL "; newColor$
PRINT #g, "GETBMP bg 0 0 200 200" 'Creates a new BMP for background
PRINT #g, "BACKGROUND bg"
DO
x=INT(RND(0)*150)+1
y=INT(RND(0)*150)+1
cycle=cycle+1
IF cycle=10 THEN
cycle=0
newColor$=WORD$(bgColor$, INT(RND(0)*6)+1)
GOTO [getNewColor]
END IF
PRINT #g, "SPRITEXY ufo "; x; " "; y
PRINT #g, "DRAWSPRITES"
CALL pause, 350
LOOP WHILE 1=1
WAIT
SUB quit handle$
UNLOADBMP "ufo"
UNLOADBMP "bg"
CLOSE #g
END 'End main program
END SUB
SUB pause mil
TIMER mil, [cont]
WAIT
[cont]
TIMER 0
END SUB
You'll notice the main difference between Demo1 and Demo2 is the branch to [getNewColor] for Demo2, as opposed to simply changing the "FILL "; newColor$ we used in Demo1.
Okay, this tiny UFO is not very threatening, it doesn't drop biological terror weapons or attack with disintegrator rays, but it sure looks cool flitting around the window and no matter what color is used as a background, the SPRITE remains transparent over the background. A background image could also be used. Perhaps next time I'll add an asteroid to the background, and if the UFO and asteroid positions match, BOOM! No more icky aliens! That will teach the alien pilot to pay more attention to his driving....
Okay, now for serious work again. Janet wants me to work this from the other way around... display a SPRITE in the window and capture it as a BMP "on the fly." Doesn't sound too difficult... I know Janet can do it easily. Maybe there's some trick I'll stumble onto, like sending the Worlbes out for a can of Acrylic Window-Wash to paint the proper color?