# 用GMS2写一个坦克大战的Player

지난달

``````face=1;//初始化方向
play_timer = 0;//死亡计时
image_speed=0;//贴图速度，就是坦克的移动动画速度
player_HP=100;//玩家HP
timer4 = 0;//主炮CD
psp=30;//移动速度
``````

``````if(player_HP <= 0)//如果玩家HP少于等于0，倒计时200帧并退出下列代码
{
speed=0
if(play_timer == 200)
{
audio_stop_all();//停止所有音乐
room_goto(room_lose)//跳转到死亡界面（需先定义一个死亡界面）
}
play_timer +=1;//时间加1
exit;//退出下面代码
}

//===============================================================

var dx=0,dy=0,player_speed=psp/10+psp2+fan_ying/30,tmp;//临时变量

if(player_HP>0)//如果非死亡状态
{
if(keyboard_check(ord("W")) ){dy-=1;face =1;}//按下键盘W，朝上移动
else
if(keyboard_check(ord("S")) ){dy+=1;face =3;} //按下键盘S，朝下移动

else
if(keyboard_check(ord("A")) ){dx-=1;face =2;} //按下键盘A，朝左移动

else
if(keyboard_check(ord("D")) ){dx+=1;face =0;} //按下键盘D，朝右移动

}

if(dx!=0 || dy!=0)//如果在移动中，就播放动画
{
image_index +=0.5;//动画速度
}

//================================================================
//以下定义玩家坦克对碰撞的处理，部分与原游戏不同，比如BOSS和水部分

if(collision_rectangle(x-25+dx*(player_speed),y-25+dy*(player_speed),x+25+dx*(player_speed),y+25+dy*(player_speed),obj_qiang_boss,0,0) || collision_rectangle(x-25+dx*(player_speed),y-25+dy*(player_speed),x+25+dx*(player_speed),y+25+dy*(player_speed),obj_boss10,0,0))//如果撞墙就无法前进,x-=左，y-=上，x+=右，y+=下
{
dx=0;
dy=0;
}

if(collision_rectangle(x-25+dx*(player_speed),y-25+dy*(player_speed),x+25+dx*(player_speed),y+25+dy*(player_speed),obj_boss3_1,0,0) || collision_rectangle(x-25+dx*(player_speed),y-25+dy*(player_speed),x+25+dx*(player_speed),y+25+dy*(player_speed),obj_boss10,0,0))//如果撞墙就无法前进,x-=左，y-=上，x+=右，y+=下
{
dx=0;
dy=0;
}

if(collision_rectangle(x-25+dx*psp/10+fan_ying/30,y-25+dy*psp/10+fan_ying/30,x+25+dx*psp/10+fan_ying/30,y+25+dy*psp/10+fan_ying/30,obj_diren_laoda,0,0) || collision_rectangle(x-25+dx*psp+fan_ying/30,y-25+dy*psp+fan_ying/30,x+25+dx*psp+fan_ying/30,y+25+dy*psp+fan_ying/30,obj_shui,0,0))//如果撞BOSS或水移动速度减慢
{
x+=dx*(psp/10*0.7+psp2*0.7);
y+=dy*(psp/10*0.7+psp2*0.7);
}
else
{
x+=dx*(player_speed);
y+=dy*(player_speed);
}

//================================================================
//以下为原创内容，坦克添加了副炮，可以不必理会，上面的相关代码我也没列出来。

scr_system()

if(nano_timer>0)nano_timer -=1
if(timer>0)timer -=1
if(timer2>0)timer2 -=1
if(timer_pao>0)timer_pao-=1

//副炮发射
if(timer3>0)timer3-=1;
if(keyboard_check(ord("K"))  && timer3<=0)
{
timer3=FP_CD;//子弹时间=10
if(fp_jgp=2){
if(face=1){tmp = instance_create_depth(x,y-25,100,obj_guangsu_s)}//上
if(face=3){tmp = instance_create_depth(x,y+25,100,obj_guangsu_x)}//下
if(face=2){tmp = instance_create_depth(x-25,y,100,obj_guangsu_z)}//左
if(face=0){tmp = instance_create_depth(x+25,y,100,obj_guangsu_y)}//右
}
else//***或者
{tmp =instance_create_depth(x,y,100,obj_zd);}
tmp.direction = face*90;//direction默认为0*90度
tmp.speed =FP_SPEED;//速度为4
}

//主炮发射========================================================

if(timer4>0)timer4-=1;
if(keyboard_check(ord("J"))  && timer4<=0)
{
scr_zhupao()
}

//按下键盘J时发射主炮

//以下为原创内容，坦克可以装载各种系统，和添加了副炮，可以不必理会，上面的相关代码我也没列出来。

if(waiguan=0)//普通形态
{
switch(obj_player.face)//条件判断
{
case 1:sprite_index = spr_lz_s;break;
case 0:sprite_index = spr_lz_y;break;
case 3:sprite_index = spr_lz_x;break;
case 2:sprite_index = spr_lz_z;break;
}
}

if(waiguan=1)//变身系统
{
switch(obj_player.face)//条件判断
{
case 1:sprite_index = spr_r_s;break;
case 0:sprite_index = spr_r_y;break;
case 3:sprite_index = spr_r_x;break;
case 2:sprite_index = spr_r_z;break;
}
}

if(waiguan=2)//闪光模式
{
switch(obj_player.face)//条件判断
{
case 1:sprite_index = spr_s_s;break;
case 0:sprite_index = spr_s_y;break;
case 3:sprite_index = spr_s_x;break;
case 2:sprite_index = spr_s_z;break;
}
}

if(waiguan=3)//狂战士
{
switch(obj_player.face)//条件判断
{
case 1:sprite_index = spr_b_s;break;
case 0:sprite_index = spr_b_y;break;
case 3:sprite_index = spr_b_x;break;
case 2:sprite_index = spr_b_z;break;
}
}

if canying=1 && instance_number(obj_canying)=0 {
instance_create_depth(x,y,260,obj_canying)
}

if p_55mm=2 && instance_number(obj_55_p)<1 instance_create_depth(x,y,245,obj_55_p)
if p_zp_gs=2 && instance_number(obj_gsp_p)<1 instance_create_depth(x,y,245,obj_gsp_p)
if p_sdp=2 && instance_number(obj_sdp_p)<1 instance_create_depth(x,y,245,obj_sdp_p)
if p_black=2 && instance_number(obj_black_p)<1 instance_create_depth(x,y,245,obj_black_p)
if p_pao=2 && instance_number(obj_cjp_p)<1 instance_create_depth(x,y,245,obj_cjp_p)
if p_sl=2 && instance_number(obj_sl_p)<1 instance_create_depth(x,y,245,obj_sl_p)
if p_zui=2 && instance_number(obj_ttp_p)<1 instance_create_depth(x,y,245,obj_ttp_p)

if fp_9mmjq=2 && instance_number(obj_FP_1)<1 instance_create_depth(x,y,200,obj_FP_1)

``````