` coding challenge : touch typing game
` bob saunders
 
` enemies come at you from a distance.
` fire your guns by typing the letter corresponding to the proper gun.
` try it you'll see...
 
set display mode 800,600,32
autocam off
sync on
sync rate 0
sync
randomize timer()
 
create bitmap 1,20,20
set text size 22
for t=65 to 90
 box 0,0,19,19,0,0,0,0
 center text 9,0,chr$(t)
 get image t,0,0,19,19
next t
 
box 0,0,19,19,0,0,0,0
for t=1 to 100
 dot rnd(19),rnd(19)
next t
get image 10,0,0,19,19
 
box 0,0,19,19,100,1100,11100,111100
get image 11,0,0,19,19
 
box 0,0,19,19,33100,331100,3311100,33111100
get image 12,0,0,19,19
 
delete bitmap 1
 
make object cube 99,1000
set object cull 99,0
set object light 99,0
set object collision off 99
texture object 99,12
 
type enemy
x
lif
spd
endtype
 
dim rock(20) as enemy
dim key(5)
dim b#(50) as float
 
for t=1 to 5
 make object cube t,5
 position object t,(t-3)*10,0,10
 assign_key(t,0)
next t
 
for t=10 to 20
 make object sphere t,5
 texture object t,10
 position_rock(t)
 hide object t
next t
 
for t=30 to 50
 make object cube t,2
 texture object t,11
 hide object t
next t
 
for t=101 to 190
 make object sphere t,1
 hide object t
 texture object t,10
 set object collision off t
next t
 
global life
global score
global gs#
 
nme=13
life=5
score=0
 
position camera 0,10,-30
 
`````````````````````````````````````````````````````````````````
t1#=timer()
do
GS#=(TIMER()-T1#)*0.010
T1#=TIMER()
`````````````````````````````````````````````````````````````````
 
` CONTROL ENEMIES
for t=10 to nme
 show object t
 move object t,rock(t).spd*gs#/2.0
 
` this will check to see if this rock has hit you:
if object position z(t)<10
 ` you've been hit
 dec life
 position_rock(t)
endif
 
next t
 
` CHECK FOR KEY-GUN-FIRING
for t=1 to 5
 x=key(t)
 a=asc(UPPER$(inkey$()))
 if a=x
  ` you have pushed one of the keys to fire
  ` shoot a bullet...:
  shoot_bullet(t)
   ` choose a new letter to fire this gun:
  assign_key(t,x)
 endif
next t
 
` DEAL WITH YOUR BULLETS:
for t=30 to 50
 dec b#(t),gs#
 if b#(t)>0.0
  ` b#(t) is a timer...if the bullet is active, it's >0
  move object t,5*gs#
  b=object collision(t,0)
  if b>9 and b<21
   ` bullet has hit a rock
   b#(t)=0
   inc score
   dec rock(b).lif
    if rock(b).lif<1
     score=score+10+rock(b).spd
     explode_rock(b)
     position_rock(b)
 
     ` this make the game get harder (adds more rocks)
      if score>100 then nme=15
      if score>300 then nme=16
      if score>500 then nme=17
      if score>700 then nme=18
      if score>1000 then nme=19
      if score>1500 then nme=20
 
    endif : ` dead rock
  endif : ` hit rock
 else
  hide object t
  set object collision off t
 endif
 
next t
 
control_explosions()
 
text 0,0,"score: "+str$(score)
text 0,20,"life: "+str$(life)
 
if life<1 then end_game()
 
`````````````````````````````````````````````````````````````````
sync : loop
`````````````````````````````````````````````````````````````````
 
 
 
function position_rock(t)
` this will position enemies (rocks) in one of the 5 "X-SLOTS" available,
` and give it new variables.
 x=rnd(4)-2
 position object t,x*10,0,250+t*10
 yrotate object t,180
 rock(t).x=x
 rock(t).lif=rnd(3)+2
 rock(t).spd=rnd(2)+2
 set object collision on t
 show object t
 
endfunction
 
 
function assign_key(t,z)
  ` this assigns letters to keys, and makes sure that the key is not the same as another
 repeat
 k=1
 key(t)=rnd(25)+65
 if key(t)=z then k=0
 for x=1 to 5
  if x<>t
   if key(x)=key(t) then k=0 : exit
  endif
 next x
 until k=1
 
texture object t,key(t)
 
endfunction
 
function shoot_bullet(x)
 
a=0
for t=30 to 50
  ` here we are looking for a currently un-used bullet to fire.
 if b#(t)<1.0 then a=t : exit
next t
 
if a>0
 
position object a,(x-3)*10,0,12
show object a
set object collision on a
b#(a)=100.0
 
endif
 
endfunction
 
function control_explosions()
 ` this perpetually moves all explosion objects
for t=101 to 190
 move object t,12*gs#
next t
endfunction
 
function explode_rock(x)
 
for t=101 to 190
 if rnd(2)=1
  ` this randomly chooses explosion objects to make up this particular explosion
  show object t
  rotate object t,rnd(360),rnd(360),rnd(360)
  position object t,object position x(x),object position y(x),object position z(x)
 endif
next t
 
endfunction
 
 
function  end_game()
 
sync
center text 400,200,"GAME OVER"
center text 400,220,"your score:"
center text 400,240,str$(score)
center text 400,540,"press any key to end"
sync
wait 1000
wait key
end
 
endfunction