''' ''' playfield tutorial - part 7 ''' sprite sharing ''' ''' by greg smith ''' ''' ''' use c coordinates ''' OPTION C_COORDS ''' ''' turn off escape processing for speed ''' OPTION ESCAPE OFF ''' ''' turn off automatic graphics show ''' OPTION SHOW 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 obj_ctr as int 'the next object to be allocated dim current_playfield as int 'the current playfield onscreen dim current_screen as int dim points as long 'your score dim n_playfields as int 'the number of playfields dim current_sprite as int 'the number of loaded sprites 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) playfield as int 'playfield this sprite belongs on 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[40] as obj 'all the sprites dim sprite_names[30,32] as char ' sprite names ''' ''' 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\ obj_ctr=0 'initialize the object counter points = 0 'reset points n_playfields = 2 'maximum number of playfields current_sprite = 1 'the next sprite number available obj_init_all 'init all objects 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 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 current_screen = 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, current_screen 'display the current field redraw sprite_field paper white 'transparent color for the lemming ink ltgrey ''' ''' iterate across all sprites and display ''' for i=0 to obj_ctr-1 ''' ''' 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 objs[i].playfield=current_playfield and objs[i].x >= 0 then if objs[i].screen = current_screen then ' is the sprite on-screen? move objs[i].mysprite, objs[i].x, objs[i].y redraw objs[i].mysprite endif endif next 'i ''' ''' display points ''' ink white font "mini_bold_font" printxy 0,0,points move sprite_lemming, lemming_x, lemming_y, z_dir+z_cnt 'display the lemming redraw sprite_lemming redraw show '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 ''' ''' load_sprite will find a previously loaded sprite and return its ''' sprite number or load the sprite and add its name to the list ''' and return the new sprites index ''' function load_sprite(name[] as char) as int dim found as int found = false for i=0 to current_sprite-1 if name = sprite_names[i] then found = true load_sprite = i+1 exit for end if next 'i if not found then load_sprite = current_sprite sprite current_sprite, name sprite_names[current_sprite-1]=name current_sprite = current_sprite + 1 end if end function ''' ''' obj_init will allocate a single object and attach it to a sprite ''' sub obj_init(pfld as int, fname[] as char, x as int, y as int, amt as int, sound[] as char) objs[obj_ctr].playfield = pfld objs[obj_ctr].x = x mod 160 objs[obj_ctr].y = y mod 100 objs[obj_ctr].screen = (x/160)+(y/100)*3 objs[obj_ctr].mysprite = load_sprite(fname) objs[obj_ctr].amt = amt objs[obj_ctr].sound = sound ' sprite command deleted ' move obj_ctr, 160, 100 'hide sprite obj_ctr = obj_ctr+1 end sub ''' ''' init all objects ''' sub obj_init_all print "Loading Sprites..." obj_init(0, "money.pic", 32,50,100, "money.mus") obj_init(0, "money.pic", 20,140,100, "money.mus") obj_init(0, "money.pic", 20,240,100, "money.mus") obj_init(0, "money.pic", 224,40,100, "money.mus") obj_init(0, "money.pic", 224,140,100, "money.mus") obj_init(0, "money.pic", 224,240,100, "money.mus") obj_init(0, "money.pic", 432,40,100, "money.mus") obj_init(0, "money.pic", 432,110,100, "money.mus") obj_init(0, "money.pic", 432,170,100, "money.mus") obj_init(0, "money.pic", 432,240,100, "money.mus") print "Still Loading Sprites..." obj_init(1, "money.pic", 64, 40, 100, "money.mus") obj_init(1, "money.pic", 64,140, 100, "money.mus") obj_init(1, "money.pic", 64,260, 100, "money.mus") obj_init(1, "money.pic",224, 40, 100, "money.mus") obj_init(1, "money.pic",224,140, 100, "money.mus") obj_init(1, "money.pic",224,260, 100, "money.mus") obj_init(1, "money.pic",384, 40, 100, "money.mus") obj_init(1, "money.pic",384,140, 100, "money.mus") obj_init(1, "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 for i=0 to obj_ctr-1 if objs[i].playfield = current_playfield and objs[i].screen = current_screen and objs[i].x>=0 then move objs[i].mysprite, objs[i].x, objs[i].y if collision(sprite_lemming, objs[i].mysprite) then objs[i].x=-1 ' turn off this sprite points = points + objs[i].amt music foreground, objs[i].sound music foreground, play exit for end if endif 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