背包结构是用表单格式,之前开发的游戏《纸牌记忆》已经使用过了,就是洗牌,拿牌,比较两张牌。这与我们现在需要做的背包系统没有太大的不同!
我们开始修改obj_player里的create事件,添加两行代码
//背包系统的开发 items=ds_list_create();创建一个DS List数据结构。 equipped=ds_map_create();
修改obj_controller里的create事件,添加下面的代码
//背包系统的开发 enum item_type{ weapon, cure }; inv_open=false;//已经打开背包 inv_index=0;
并且还要在step事件里,增加打开背包的按钮,按I键或者手柄上的X键,
var inv_key=keyboard_check_pressed(ord("I")) or gamepad_button_check_pressed(0,gp_face4);//打开背包
打开背包前的状态判断,这个和打开地图的逻辑是一样的,就是和暂停状态是互斥的。如果已经是暂停状态,就不能打开背包,如果已经打开背包,就要关闭背包,否则就可以打开。
代码如下
if inv_key { audio_play_sound(snd_menu,1,false); if inv_open { global.game_state=states.playing; global.can_pause=true; inv_open=false; } else { if not global.can_pause exit; global.game_state=states.paused; global.can_pause=false; inv_open=true; } }
背包打开后,要显示获取的道具,道具可能是武器,或者药品。如果是武器,要判断是否已经装备,如果已经装备好了,点确点就是卸下,否则就是装备。如果是道具,就直接使用。
添加代码如下
if inv_open { if instance_exists(obj_player) { if (move!=0) audio_play_sound(snd_menu,1,false); inv_index+=move; if inv_index<0 { inv_index=ds_list_size(obj_player.items)-1; } if inv_index>=ds_list_size(obj_player.items) { inv_index=0; } if enter_pressed and ds_list_size(obj_player.items)>0 { switch(ds_map_find_value(obj_player.items[| inv_index],"type")) { case item_type.weapon: { if(ds_map_empty(obj_player.equipped) or ds_map_find_value(obj_player.equipped,"key")!=ds_map_find_value(obj_player.items[|inv_index],"key")) { var key=ds_map_find_value(obj_player.items[| inv_index],"key"); var name= ds_map_find_value(obj_player.items[| inv_index],"name"); var type= ds_map_find_value(obj_player.items[| inv_index],"type"); var value= ds_map_find_value(obj_player.items[| inv_index],"value"); var desc= ds_map_find_value(obj_player.items[| inv_index],"desc"); var bullet_type= ds_map_find_value(obj_player.items[| inv_index],"bullet_type"); ds_map_replace(obj_player.equipped,"key",key); ds_map_replace(obj_player.equipped,"name",name); ds_map_replace(obj_player.equipped,"type",type); ds_map_replace(obj_player.equipped,"value",value); ds_map_replace(obj_player.equipped,"desc",desc); ds_map_replace(obj_player.equipped,"bullet_type",bullet_type); } else { ds_map_destroy(obj_player.equipped); obj_player.equipped=ds_map_create(); } break; } case item_type.cure: { obj_player.hp+=ds_map_find_value(obj_player.items[| inv_index],"value"); ds_list_delete(obj_player.items,inv_index); } } } } }
剩下的是图形界面的绘制,我们切换到 Draw GUI事件,添加下面的代码
//draw inventory if(inv_open) { if instance_exists(obj_player) { draw_set_alpha(0.5); draw_set_color(c_black); draw_rectangle(0,0,cam_w,cam_h,0); draw_set_alpha(1); draw_set_font(fnt_text); draw_set_color(c_white); draw_text(200,100,"ITEMS"); draw_set_font(fnt_text); draw_set_color(c_white); var list_x=200; var list_y=300; var text_offset_x=500; var text_offset_y=50; draw_set_color(c_silver); draw_text(list_x,list_y-text_offset_y-20,"Name"); draw_text(list_x+text_offset_x,list_y-text_offset_y-20,"Description"); draw_text(list_x+text_offset_x*3,list_y-text_offset_y-20,"Stat"); for(var i=0;i<ds_list_size(obj_player.items);i++) { draw_set_color(c_white); if inv_index==i { draw_rectangle(list_x-50,list_y+(text_offset_y*i),list_x-30,list_y+(text_offset_y*i)+20,0); } if not ds_map_empty(obj_player.equipped)//有装备的武器就执行下面的内容 { if ds_map_find_value(obj_player.items[| i],"key")==ds_map_find_value(obj_player.equipped,"key") { draw_set_color(c_yellow); } } var t_name=ds_map_find_value(obj_player.items[|i],"name"); var t_desc=ds_map_find_value(obj_player.items[|i],"desc"); var t_value=ds_map_find_value(obj_player.items[|i],"value"); draw_text(list_x,list_y+(text_offset_y*i),t_name); draw_text(list_x+text_offset_x,list_y+(text_offset_y*i),t_desc); draw_text(list_x+(text_offset_x*3),list_y+(text_offset_y*i),string(t_value)); } } }
上面的代码,会显示我们拾取的道具,武器装备后,字体会显示黄色。
我们按F5,看一下最终效果。
还有剩下的道具的创建代码,后面再补上去。
暂无关于此日志的评论。