''' ''' playfield tutorial - part 4 ''' putting the playfield in a file ''' ''' by greg smith ''' ''' ''' use c coordinates ''' OPTION C_COORDS ''' ''' turn off escape processing for speed ''' OPTION ESCAPE OFF dim x as int 'position of lemming from 0-480 dim y as int 'position of lemming from 0-300 dim z_dir as int 'which direction of lemming dim z_cnt as int 'animation image of lemming dim sprite_field as int 'the sprite number of the playfield sprites dim sprite_lemming as int 'the sprite number of the lemming sprites dim lemming_left as int 'index into lemming sprite for going left dim lemming_right as int 'index into lemming sprite for going right dim lemming_up as int 'index into lemming sprite for going up dim lemming_down as int 'index into lemming sprite for going down dim collision_street as char 'the street's collision value dim collision_table[30,31] as char 'the collision table ''' ''' update file - copy file from app to flash ''' sub update_file(fname[] as char) inline File_remove(fname); inline _load(fname); end sub ''' ''' initialization ''' sub init update_file("playfield1.dat") 'get the .dat file into flash sprite_field = 0 'playfield sprite sprite_lemming = 31 'lemming sprite is largest possible sprite - on top lemming_left=0 'lemming left image lemming_right=5 'lemming right image lemming_up=10 'lemming up image lemming_down=15 'lemming down image ''' ''' the lemming has 20 bitmaps 5 for each direction sprite sprite_lemming, "lemming.pic" ''' ''' the lemming can be anywhere on the playfield from ''' x = 0-480 ''' y = 0-300 x=240 'center of the playfield y=150 'center of the playfield z_dir=lemming_right ' lemming faces right z_cnt=0 ' animation counter ''' ''' set the collision flags ''' collision_street = \x\ end sub sub repaint dim field_x as int '0,1,2 for the horizontal bitmap dim field_y as int '0,1,2 for the vertical bitmap dim field as int '0-8 for the bitmap currently visible dim lemming_x as int '0-159 for the x coord on the screen dim lemming_y as int '0-99 for the y coord on the screen field_x = x / 160 ' compute the horizontal bitmap field_y = y / 100 ' compute the vertical bitmap field = field_y*3 + field_x ' combine them to compute the bitmap number lemming_x = x mod 160 ' compute the lemming's position on the screen lemming_y = y mod 100 ' compute the lemming's position on the screen move sprite_field, 0, 0, field 'display the current field move sprite_lemming, lemming_x, lemming_y, z_dir+z_cnt 'display the lemming paper white 'transparent color for the lemming ink ltgrey redraw 'redraw all the sprites end sub ''' ''' increment the z_cnt variable ''' sub update_z_cnt z_cnt=(z_cnt+1) MOD 5 end sub ''' ''' check for collision against table ''' function collision_check(x as int, y as int) as char collision_check = collision_table[y/10,x/16] end function ''' ''' process the keyboard ''' sub get_keys dim tmp as int if key(#KEY_UP) then 'move the lemming up tmp=y-3 if collision_check(x,tmp) = collision_street then y=tmp z_dir=lemming_up update_z_cnt end if end if if key(#KEY_DOWN) then 'move the lemming down tmp=y+3 if collision_check(x,tmp) = collision_street then y=tmp z_dir=lemming_down update_z_cnt end if end if if key(#KEY_LEFT) then 'move the lemming left tmp=x-3 if collision_check(tmp,y) = collision_street then x=tmp z_dir=lemming_left update_z_cnt end if end if if key(#KEY_RIGHT) then 'move the lemming right tmp=x+3 if collision_check(tmp,y) = collision_street then x=tmp z_dir=lemming_right update_z_cnt end if end if end sub ''' ''' get a string from the file ''' a string ends in CR/LF ''' sub getstring(f as int, s[] as char) dim x as char dim n as int n=0 while(true) get f,,x if x<>13 then if x=10 then exit while s[n] = x s[n+1] = 0 n=n+1 endif wend end sub ''' ''' read the playfield file defined by ''' 'field' and set up the sprites and ''' the collision table ''' sub init_playfield(field as int) dim fname[32] as char dim s[32] as char cls print "Loading..." ''' create the filename to read sprint fname, "playfield", field, ".dat" ''' open the playfield definition file open fname for read as 1 getstring(1, s) 'get the sprite name ''' ''' the playfield sprite has 9 bitmaps ''' one for each of the different areas of the ''' playfield sprite sprite_field, s ''' ''' read the 30 playfield collision rows ''' for i=0 to 29 getstring(1, collision_table[i]) next ''' ''' close the playfield.dat file ''' close 1 end sub ''' ''' main subroutine ''' sub mainsub init init_playfield(1) while true inline _escape(0); /* check the escape key */ repaint get_keys wend end sub ''' ''' call the main subroutine ''' mainsub