How to make and animate a sprite within one program


I was experimenting with sprites the other day, and how to make them move. And then I thought, it would be nice to have a program that doesn't need any external sprites. It's more convenient if you are intending to ask questions about your program. People can run your program directly without having to download bmp files.
This program makes 3 stickman sprites that can then be used for animation.
At this stage Stickman can only walk, but you can experiment and try to make him jump or fly for example !


(To start the animation press the left or right arrow key)

- alix alix


 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' STICKMEN.BAS by Alix Whittal on 05/09/06
'
' This program will draw 3 stickmen, each in a different walking position.
' Each stickman is then transformed into an image with a mask and saved into memory.
' From there on, we have a sprite that we can animate.
' At this stage stickman can only walk, but you can experiment
' and try to make him jump for example.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

nomainwin
WindowWidth = 400
WindowHeight = 400
open "Stickman" for graphics_nsb_nf as #1
#1, "trapclose quit"
''''''''''''''''''''''''''''''''''''''''''''
' make 3 sprites and save them to memory
''''''''''''''''''''''''''''''''''''''''''''
x=50
for i=1 to 3
select case i
case 1
angle = 145
case 2
angle = 168
case 3
angle = 180
end select
#1 "down"
'draw 2 rectangles
gosub [drawrectangles]
'draw sprite image in lower black box
#1, "color darkgray; backcolor darkgray"
y=270
gosub [drawman]
y=170
#1, "color black; backcolor black"
gosub [drawman]
'place the bitmap in Memory
#1, "Getbmp stick"+str$(i)+" 30 150 75 200"
call Pause 500
#1, "cls"
next i
'''''''''''''''''''''''''''''''''''''
'Draw background + sprite on window
'''''''''''''''''''''''''''''''''''''
#1, "fill white"
#1, "getbmp bg 0 0 400 400"
#1, "background bg"
#1, "addsprite stickman stick1 stick2 stick3"
#1, "spritexy stickman 100 250"
#1, "drawsprites"
 
'*************************************
'Animate with keyboard
'*************************************
#1, "setfocus"
#1, "when characterInput [KeyControl]"
wait
 
[KeyControl]
k$=Inkey$
if asc(right$(k$,1))=_VK_LEFT then
image=image+1
if image>3 then image=1
#1 "spriteimage stickman stick";image
#1 "drawsprites"
#1, "spritemovexy stickman -5 0"
end if
 
if asc(right$(k$,1))=_VK_RIGHT then
image=image+1
if image>3 then image=1
#1 "spriteimage stickman stick";image
#1 "drawsprites"
#1, "spritemovexy stickman 5 0"
end if
wait
 
close #1
end
 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' SUBS
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
[drawrectangles]
'upper rectangle
#1, "Color white; Backcolor white"
#1, "Place 30 250; Boxfilled 75 150"
'lower rectangle
#1, "Color black; Backcolor black"
#1, "Place 30 350; Boxfilled 75 250"
return
 
[drawman]
#1, "place "; x;" ";y
#1, " circlefilled 10"
#1, "line "; x; " ";y+10;" "; x; " ";y+50
#1, "place "; x; " ";y+20
#1, " north; turn "; angle*-1
#1, "go 20; place "; x; " ";y+21
#1, " north; turn "; angle; "; go 20"
#1, "place "; x; " ";y+50
#1, " north; turn "; angle*-1
#1, "go 28; place "; x; " ";y+50
#1, " north; turn "; angle; "; go 28"
return
 
'
' QUIT
'
sub quit handle$
close #handle$
end
end sub
 
'
' PAUSE
'
Sub Pause mil
timer mil, [cont]
wait
[cont]
timer 0
End Sub