`Coding Challenge #7 - Stick Man Challenge
`Bill Robinson
`Started 4-23-06 --- Deadline 5-3-06
`All Rights Reserved for use as a published game.
`
`
`Press SPACE bar to start Random mode.
`
set display mode 1024,768,32
sync on : sync rate 60
set text opaque
sync
autocam off
randomize timer()
 
dim stickman_anims(20,12,12)
dim stickman_poses(20,12,12)
dim stickman_inmotion#(3,12,12)
dim stickman_info(20,4)
dim pose_index(127)
 
global playerx#
global playery#
global playerxinc#=0
global playeryinc#=0
global animspeed#=10.0
global numframes
global stickman_frame
global scale#=1.0
global numposes=7
global posemode
global ground=500
global posenum
global autoflag=-1
 
gosub _intro
 
`--- build small icons for screen
scale#=1.0
 
restore stickman_static
_store_pose(1)
restore stickman_tut1
_store_pose(2)
restore stickman_tut2
_store_pose(3)
restore stickman_armsout
_store_pose(4)
restore stickman_armstouching
_store_pose(5)
restore stickman_leftlean
_store_pose(6)
restore stickman_rightlean
_store_pose(7)
 
remstart
restore stickman_handstand1
_store_pose(4)
restore stickman_handstand2
_store_pose(5)
remend
 
_build_pose_icons()
_show_poses()
_build_menu()
 
`--- build full size stickman for dancing
scale#=3.0
restore stickman_static
_store_pose(1)
restore stickman_tut1
_store_pose(2)
restore stickman_tut2
_store_pose(3)
restore stickman_armsout
_store_pose(4)
restore stickman_armstouching
_store_pose(5)
restore stickman_leftlean
_store_pose(6)
restore stickman_rightlean
_store_pose(7)
 
remstart
restore stickman_handstand1
_store_pose(4)
restore stickman_handstand2
_store_pose(5)
remend
playerx#=screen width()/2
playery#=ground
 
` --- build stickman
numframes=int(animspeed#)
posenum=1
_stickman_init(posenum)
_draw_stickman2(1,0)
 
ink rgb(0,255,255),0
rem ----- main loop -----------------------------------------------------
do
   cls
   line 0,ground,1024,ground
 
   keypress=scancode()                             :`scan for keys pressed, subroutine to hadle keys
   if keypress>0 then _key_action(keypress)
 
   mx=mousex()
   mousebutton=mouseclick()
`   if mousebutton=1
`   if mousebutton=2
 
   if leftkey()=1
      if posenum>1
         dec posenum
         _load_newpose(posenum)
         wait 100
      endif
   endif
 
   if rightkey()=1
      if posenum<=numposes
         inc posenum
         _load_newpose(posenum)
         wait 100
      endif
   endif
 
   if upkey()=1
`      dec playery#
   endif
 
   if downkey()=1
`      inc playery#
   endif
 
   if stickman_frame<numframes
      _update_frame()
      inc stickman_frame
   else
      if autoflag>0
         num=rnd(numposes-1)+1
         _load_newpose(num)
      endif
   endif
 
`sprite 100,mousex,mousey,100
`if sprite collision (11,100)=1
`   if mouseclick()=1
   `put command here
`   endif
`endif
 
   _draw_stickman2(1,0)
 
   sync
loop
rem --------------------------------------------------------------------
end
 
 
rem --- show score on screen
_show_score:
`set cursor 1,100
`print screen fps()
`print stickman_frame
`center text screenxcenter,2,"StickMan"
return
 
 
function _key_action(keypress)
select keypress
   case 57
      autoflag=autoflag*-1
      if autoflag>0
         set sprite priority 20,0
         set sprite priority 21,1
      else
         set sprite priority 20,1
         set sprite priority 21,0
      endif
      wait 100
   endcase
   case default
      if keypress>=16 and keypress<=25
         saveposenum=posenum
         ptemp=keypress-15
         if ptemp<=numposes
            posenum=ptemp
            _load_newpose(posenum)
         endif
      endif
   endcase
endselect
endfunction
 
 
function _stickman_init(num)
for i=1 to 10
   for j=1 to 4
      `load first pose into stickman array
      stickman_inmotion#(1,i,j)=stickman_poses(num,i,j)
      stickman_inmotion#(2,i,j)=stickman_poses(num,i,j)
      `calculate delta for each frame update
      stickman_inmotion#(3,i,j)=(stickman_inmotion#(2,i,j)-stickman_inmotion#(1,i,j))/animspeed#
   next j
next i
stickman_frame=0
endfunction
 
 
function _update_frame()
for i=1 to 10
   for j=1 to 4
      v1#=stickman_inmotion#(3,i,j)
      inc stickman_inmotion#(1,i,j),v1#
   next j
next i
endfunction
 
 
function _load_newpose(num)
for i=1 to 10
   for j=1 to 4
      `load new pose into slot2
      stickman_inmotion#(2,i,j)=stickman_poses(num,i,j)
      `calculate delta for each frame update
      v1#=stickman_inmotion#(1,i,j)
      v2#=stickman_inmotion#(2,i,j)
      stickman_inmotion#(3,i,j)=(v2#-v1#)/animspeed#
   next j
next i
stickman_frame=0
endfunction
 
 
function _draw_stickman2(num,iconflag)
if iconflag=1
   groundoffset=0
else
   groundoffset=stickman_inmotion#(num,stickman_info(posenum,1),4)
endif
for i=1 to 9
   line playerx#+stickman_inmotion#(num,i,1),playery#+stickman_inmotion#(num,i,2)-groundoffset,playerx#+stickman_inmotion#(num,i,3),playery#+stickman_inmotion#(num,i,4)-groundoffset
next i
circle playerx#+stickman_inmotion#(num,10,1),playery#+stickman_inmotion#(num,10,2)-groundoffset,5.0*scale#
endfunction
 
 
stickman_static:
`body
data 0,0,0,-20
`left upper arm
data 0,-20,-10,-15
`right upper arm
data 0,-20,10,-15
`left thigh
data 0,0,-7,7
`right thigh
data 0,0,7,7
`left forearm
data -10,-15,-15,-5
`righ forearm
data 10,-15,15,-5
`left leg
data -7,7,-12,18
`right leg
data 7,7,12,18
`head
data 0,-25,0,-25
`ground touching node
data 8
 
stickman_tut1:
`body
data 0,0,0,-20
`left upper arm
data 0,-20,-10,-20
`right upper arm
data 0,-20,10,-20
`left thigh
data 0,0,-10,0
`right thigh
data 0,0,10,0
`left forearm
data -10,-20,-10,-30
`righ forearm
data 10,-20,10,-10
`left leg
data -10,0,-10,10
`right leg
data 10,0,10,10
`head
data 0,-25,0,-25
`ground touching node
data 8
 
stickman_tut2:
`body
data 0,0,0,-20
`left upper arm
data 0,-20,-10,-20
`right upper arm
data 0,-20,10,-20
`left thigh
data 0,0,-10,0
`right thigh
data 0,0,10,0
`left forearm
data -10,-20,-10,-10
`righ forearm
data 10,-20,10,-30
`left leg
data -10,0,-10,10
`right leg
data 10,0,10,10
`head
data 0,-25,0,-25
`ground touching node
data 8
 
 
stickman_armsout:
`body
data 0,0,0,-20
`left upper arm
data 0,-20,-10,-20
`right upper arm
data 0,-20,10,-20
`left thigh
data 0,0,-7,7
`right thigh
data 0,0,7,7
`left forearm
data -10,-20,-20,-20
`righ forearm
data 10,-20,20,-20
`left leg
data -7,7,-12,18
`right leg
data 7,7,12,18
`head
data 0,-25,0,-25
`ground touching node
data 8
 
 
stickman_armstouching:
`body
data 0,0,0,-20
`left upper arm
data 0,-20,-8,-27
`right upper arm
data 0,-20,8,-27
`left thigh
data 0,0,-6,9
`right thigh
data 0,0,6,9
`left forearm
data -8,-27,0,-35
`righ forearm
data 8,-27,0,-35
`left leg
data -6,9,-3,18
`right leg
data 6,9,3,18
`head
data 0,-25,0,-25
`ground touching node
data 8
 
stickman_leftlean:
`body
data 0,0,-10,-20
`left upper arm
data -10,-20,-16,-8
`right upper arm
data -10,-20,4,-16
`left thigh
data 0,0,-6,9
`right thigh
data 0,0,6,9
`left forearm
data -16,-8,-6,-5
`righ forearm
data 4,-16,3,-5
`left leg
data -6,9,-3,18
`right leg
data 6,9,3,18
`head
data -10,-25,-10,-25
`ground touching node
data 8
 
stickman_rightlean:
`body
data 0,0,10,-20
`left upper arm
data 10,-20,-6,-15
`right upper arm
data 10,-20,13,-10
`left thigh
data 0,0,-6,9
`right thigh
data 0,0,6,9
`left forearm
data -6,-15,-3,-5
`righ forearm
data 13,-10,7,-5
`left leg
data -6,9,-3,18
`right leg
data 6,9,3,18
`head
data 10,-25,10,-25
`ground touching node
data 8
 
 
stickman_handstand1:
`body
data 0,-20,0,0
`left upper arm
data 0,0,10,0
`right upper arm
data 0,0,-10,0
`left thigh
data 0,-20,-10,-20
`right thigh
data 0,-20,10,-20
`left forearm
data -10,0,-10,10
`righ forearm
data 10,0,10,10
`left leg
data -10,-20,-10,-30
`right leg
data 10,-20,10,-30
`head
data 0,5,0,5
`ground touching node
data 6
 
stickman_handstand2:
`body
data 0,-20,0,0
`left upper arm
data 0,0,10,0
`right upper arm
data 0,0,-10,0
`left thigh
data 0,-20,-10,-20
`right thigh
data 0,-20,10,-20
`left forearm
data -10,0,-10,10
`righ forearm
data 10,0,10,10
`left leg
data -10,-20,-20,-20
`right leg
data 10,-20,20,-20
`head
data 0,5,0,5
`ground touching node
data 6
 
function _store_pose(posenum)
for i=1 to 10
   for j=1 to 4
      read d
      stickman_poses(posenum,i,j)=d*scale#
   next j
next i
read stickman_info(posenum,1)
endfunction
 
 
_intro:
sync
set text font "Arial"
set text size 30
set text opaque
cx=screen width()/2
cy=100
 
introtext:
data "-green"
data "StickMan Dancing"
data "   "
data "-purple"
data "Random mode - Press SPACE bar for random dance moves"
data "   "
data "Live Dance mode - build a sequence of moves in REAL TIME! - not finished yet"
data "   "
data "Sequence mode - build a sequence of moves, not in real time - not finished yet"
data "   "
data "-cyan"
data "   "
data "-yellow"
data "Press a key to continue"
data "999"
 
cls
restore introtext
read t$
while t$<>"999"
   if left$(t$,1)="-"
      if t$="-green" then ink rgb(0,255,0),0
      if t$="-purple" then ink rgb(200,0,200),0
      if t$="-yellow" then ink rgb(255,255,0),0
      if t$="-cyan" then ink rgb(0,255,255),0
   else
      center text cx,cy,t$
      inc cy,30
   endif
   read t$
endwhile
 
sync
wait key
ink rgb(0,255,255),0
cls
set text size 16
sync
return
 
 
function _build_pose_icons()
pose$="QWERTYUIOP"
xsize=60
ysize=60
playerx#=xsize/2
playery#=ysize/2+7
for i=1 to numposes
   cls
   ink rgb(255,255,255),0
   _stickman_init(i)
   _draw_stickman2(1,1)
   ink rgb(0,200,0),0
   line 0,0,xsize,0
   line xsize,0,xsize,ysize
   line xsize,ysize,0,ysize
   line 0,ysize,0,0
   ink rgb(0,255,255),0
   text xsize-13,ysize-17,mid$(pose$,i)
   get image i,0,0,xsize+1,ysize+1,1
next i
endfunction
 
 
function _show_poses()
xpos=0
ypos=0
for i=1 to numposes
   sprite i,xpos,ypos,i
   inc xpos,60
next i
endfunction
 
function _build_menu()
xsize=100
ysize=40
cls
ink rgb(255,255,0),rgb(50,50,50)
text 3,3,"Random Mode"
get image 20,0,0,xsize+1,ysize+1,1
sprite 20,10,200,20
set sprite priority 20,1
ink rgb(255,255,0),rgb(200,0,0)
text 3,3,"Random Mode"
get image 21,0,0,xsize+1,ysize+1,1
sprite 21,10,200,21
set sprite priority 21,0
 
cls
text 3,3,"Sequence Mode"
get image 22,0,0,xsize+1,ysize+1,1
cls
text 3,3,"Live! Mode"
get image 24,0,0,xsize+1,ysize+1,1
endfunction