''' ''' playfield tutorial - part 6 ''' stationary sprites ''' ''' 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_pf0 as char 'playfield 0 dim collision_pf9 as char 'playfield 9 dim collision_table[30,31] as char 'the collision table dim sprite_ctr as int 'the next sprite to be allocated dim current_playfield as int 'the current playfield onscreen dim points as long 'your score dim n_playfields as int 'the number of playfields type obj 'the sprite object mysprite as int 'my sprite number x as int 'x coord on screen (0-159) y as int 'y coord on screen (0-99) screen as int 'screen within playfield (0-9) amt as int 'points for catching this sprite sound[32] as char 'the sound to make end type dim objs[2,10] as obj 'all the sprites ''' ''' setup object ''' ''' ''' 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 update_file("playfield2.dat") 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\ collision_pf0 = \0\ collision_pf9 = \9\ sprite_ctr=1 'initialize the sprite counter obj_init_all 'init all objects points = 0 'reset points n_playfields = 2 'maximum number of playfields 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 dim obj_x as int ' position of the object dim obj_y as int ' position of the object dim obj_field_x as int dim obj_field_y as int dim obj_field as int dim i as int 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 ''' ''' iterate across all sprites and either display or hide them ''' for i=0 to n_playfields-1 for j=0 to 9 'objects ''' ''' if the sprite is on the current playfield ''' and the sprite is not hidden (x=-1) then ''' if the sprite is on the current screen then display it if i=current_playfield and objs[i,j].x >= 0 then if objs[i,j].screen = field then ' is the sprite on-screen? move objs[i,j].mysprite, objs[i,j].x, objs[i,j].y else move objs[i,j].mysprite, 160, 100 'hide sprite endif else move objs[i,j].mysprite, 160, 100 'hide sprite endif next 'j next 'i ''' ''' display points ''' ink white font "mini_bold_font" printxy 0,0,points 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 ''' ''' transport lemming to another playfield ''' sub transport(f as int) init_playfield(f) if f=1 then x=240 y=150 elseif f=2 then x=230 y=150 endif end sub ''' ''' process the keyboard ''' sub get_keys dim tmp as int dim cc as int if key(#KEY_UP) then 'move the lemming up tmp=y-3 cc = collision_check(x,tmp) if cc = collision_street then y=tmp z_dir=lemming_up update_z_cnt end if if cc >= collision_pf0 and cc <= collision_pf9 then transport(cc-collision_pf0) end if end if if key(#KEY_DOWN) then 'move the lemming down tmp=y+3 cc = collision_check(x,tmp) if cc = collision_street then y=tmp z_dir=lemming_down update_z_cnt end if if cc >= collision_pf0 and cc <= collision_pf9 then transport(cc-collision_pf0) end if end if if key(#KEY_LEFT) then 'move the lemming left tmp=x-3 cc = collision_check(tmp,y) if cc = collision_street then x=tmp z_dir=lemming_left update_z_cnt end if if cc >= collision_pf0 and cc <= collision_pf9 then transport(cc-collision_pf0) end if end if if key(#KEY_RIGHT) then 'move the lemming right tmp=x+3 cc = collision_check(tmp,y) if cc = collision_street then x=tmp z_dir=lemming_right update_z_cnt end if if cc >= collision_pf0 and cc <= collision_pf9 then transport(cc-collision_pf0) 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 sub obj_init(pfld as int, objno as int, fname[] as char, x as int, y as int, amt as int, sound[] as char) objs[pfld,objno].x = x mod 160 objs[pfld,objno].y = y mod 100 objs[pfld,objno].screen = (x/160)+(y/100)*3 objs[pfld,objno].mysprite = sprite_ctr objs[pfld,objno].amt = amt objs[pfld,objno].sound = sound sprite sprite_ctr, fname move sprite_ctr, 160, 100 'hide sprite sprite_ctr = sprite_ctr+1 end sub ''' ''' init all objects ''' sub obj_init_all print "Loading Sprites..." obj_init(0, 0, "money.pic", 32,50,100, "money.mus") obj_init(0, 1, "money.pic", 20,140,100, "money.mus") obj_init(0, 2, "money.pic", 20,240,100, "money.mus") obj_init(0, 3, "money.pic", 224,40,100, "money.mus") obj_init(0, 4, "money.pic", 224,140,100, "money.mus") obj_init(0, 5, "money.pic", 224,240,100, "money.mus") obj_init(0, 6, "money.pic", 432,40,100, "money.mus") obj_init(0, 7, "money.pic", 432,110,100, "money.mus") obj_init(0, 8, "money.pic", 432,170,100, "money.mus") obj_init(0, 9, "money.pic", 432,240,100, "money.mus") print "Still Loading Sprites..." obj_init(1, 0, "money.pic", -1, -1, 100, "money.mus") obj_init(1, 1, "money.pic", 64, 40, 100, "money.mus") obj_init(1, 2, "money.pic", 64,140, 100, "money.mus") obj_init(1, 3, "money.pic", 64,260, 100, "money.mus") obj_init(1, 4, "money.pic",224, 40, 100, "money.mus") obj_init(1, 5, "money.pic",224,140, 100, "money.mus") obj_init(1, 6, "money.pic",224,260, 100, "money.mus") obj_init(1, 7, "money.pic",384, 40, 100, "money.mus") obj_init(1, 8, "money.pic",384,140, 100, "money.mus") obj_init(1, 9, "money.pic",384,260, 100, "money.mus") 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 current_playfield = field-1 ''' 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 ''' ''' check collisions ''' sub check_collisions dim i as int dim j as int i = current_playfield for j=0 to 9 if collision(sprite_lemming, objs[i,j].mysprite) then objs[i,j].x=-1 ' turn off this sprite points = points + objs[i,j].amt music foreground, objs[i,j].sound music foreground, play end if next end sub ''' ''' main subroutine ''' sub mainsub init init_playfield(1) while true inline _escape(0); /* check the escape key */ repaint get_keys check_collisions wend end sub ''' ''' call the main subroutine ''' mainsub