使用GamemakerStudio2制作《坦克大战》学习总结

本篇为观看up主红色激情的教学视频所写的总结 视频地址:
由于这次教程红激老师并没有列大纲,而是一步一步带着我们写,所以接下来我直接就放整个事件代码,而不是一点一点加了。导入资源
同《功夫》篇相同
创建房间
宽高老样子分别设为256和240
新建灰色墙体对象,并铺在房间中
obj_wall_gray
新建玩家对象
这里我们一步到胃嗷,各种功能都写好了 obj_player创建事件:
state=0;// 状态0正常1死亡
face=0;//0123上右下左
image_speed=0;
shoot_timer=0;
步事件
/// @description Insert description here
// You can write your code in this editor
var dx,dy,psp,tmp;
dx=0;//左右按键情况
dy=0;//上下
psp=1;// 移动速度
//***移动
if(state==0)
{
if(keyboard_check(ord("W"))){dy=-1;face=1;}
else if(keyboard_check(ord("S"))){dy=1;face=3;}
else if(keyboard_check(ord("A"))){dx=-1;face=2;}
else if(keyboard_check(ord("D"))){dx=1;face=0;}
//**射击
if(keyboard_check_pressed(ord("J"))&&shoot_timer==0&&instance_number(obj_bullet)<2)
{
shoot_timer=10
tmp=instance_create_depth(x,y,0,obj_bullet);
tmp.speed=2;
tmp.image_index=face;
tmp.direction=face*90;
audio_play_sound(snd_bullet,10,false);
}
}
//***射击CD
if(shoot_timer>0)shoot_timer-=1;
//***碰墙检测
if(collision_rectangle(x-7+dx*psp,y-7+dy*psp,x+7+dx*psp,y+7+dy*psp,obj_master,0,0))
{
dx=0;
dy=0;
}
x+= dx*psp;
y+= dy*psp;
//***动画控制
if(dx!=0||dy!=0)
{
image_index+=0.1;
//audio_play_sound(snd_my_move,10,false);
}
//***方向控制
switch(face)
{
case 1:sprite_index=spr_my0_up;break;
case 0:sprite_index=spr_my0_right;break;
case 3:sprite_index=spr_my0_down;break;
case 2:sprite_index=spr_my0_left;break;
}
创建墙体对象
obj_wall(红砖墙) 添加创建事件:
/// @description Insert description here
// You can write your code in this editor
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
instance_create_depth(x+i*8,y+j*8,0,obj_wall_a);
instance_create_depth(x+i*8+4,y+j*8,0,obj_wall_b);
instance_create_depth(x+i*8,y+j*8+4,0,obj_wall_b);
instance_create_depth(x+i*8+4,y+j*8+4,0,obj_wall_a);
}
}
instance_destroy();
创建obj_wall_a,和obj_wall_b 创建obj_wall_father作为obj_wall_a,和obj_wall_b的父类对象,可摧毁的墙体对象 创建obj_wall_master作为obj_wall_father和obj_wall_gray的父类对象,用于碰撞检测 很巧,创建的这些对象,都没有代码
创建子弹对象
创建obj_bullet作为玩家的子弹 添加创建事件
image_speed=0;
state=0;
添加步事件
if(state==1)
instance_destroy();
添加碰撞事件,对象为obj_wall_gray
state=1;
instance_create_depth(x,y,0,obj_boom_a);
audio_play_sound(snd_bullet_ion,10,false);
添加碰撞事件,对象为obj_wall_father
with(other)instance_destroy();
instance_create_depth(x,y,0,obj_boom_a);
audio_play_sound(snd_bullet_wall,10,false);
state=1;
添加碰撞事件,对象为obj_enemy
with(other)instance_destroy();
instance_create_depth(x,y,0,obj_boom_a);
instance_create_depth(other.x,other.y,0,obj_boom_b);
state=1;
audio_play_sound(snd_em_boom,10,false);
创建敌人对象
obj_enemy创建事件
state=0;// 状态0正常1死亡
face=4;//0123上右下左
image_speed=0;
shoot_timer=0;
timer=0;
dx=0;
dy=1;
步事件
/// @description Insert description here
// You can write your code in this editor
/// @description Insert description here
// You can write your code in this editor
var psp,tmp;
psp=1;// 移动速度
//**AI移动
if(timer>0)timer--;
if(timer==0)
{
dx=0;
dy=0;
switch(irandom(3))
{
case 0:{dy=-1;face=1;} break;
case 1:{dy=1;face=3;} break;
case 2:{dx=-1;face=2;}break;
case 3:{dx=1;face=0;}break;
}
timer=irandom(64)+16;
}
if(state==0)
{
//**射击
if(irandom(100)>99)
{
tmp=instance_create_depth(x,y,0,obj_bullet_2);
tmp.speed=2;
tmp.image_index=face;
tmp.direction=face*90;
audio_play_sound(snd_bullet,0,false);
}
}
//***射击CD
//***碰墙检测
if(collision_rectangle(x-7+dx*psp,y-7+dy*psp,x+7+dx*psp,y+7+dy*psp,obj_master,0,0))
{
timer=0;
dx=0;
dy=0;
}
//***移动
x+= dx*psp;
y+= dy*psp;
//***动画控制
if(dx!=0||dy!=0)
{
image_index+=0.1;
//audio_play_sound(snd_em_move,0,false);
}
//***方向控制
switch(face)
{
case 1:sprite_index=spr_em0_up;break;
case 0:sprite_index=spr_em0_right;break;
case 3:sprite_index=spr_em0_down;break;
case 2:sprite_index=spr_em0_left;break;
}
创建生成敌人的老巢
这里我没有用红激的办法,因为运行没有效果,我也不知道为啥,自己改了改 obj_enemy_born 创建事件
timer=60;
步事件
image_index+=0.1;
绘制事件
if(timer<60)
{
draw_sprite(spr_born,image_index,x+8,y+8);
}
if(timer>0)timer--;
if(timer==0)
{
instance_create_depth(x+8,y+8,1,obj_enemy);
timer=irandom(300)+300;
}
创建爆炸对象
obj_boom_a和obj_boom_b代码一模一样,只是精灵不一样罢了 创建事件
image_speed=0.5;
动画结束事件
instance_destroy();
结语
至此,视频内容的代码都包含了,我自己实现了敌人的发射子弹,和自己阵亡,但我懒得再把代码贴出来了😝反正也没人看,有人要我就发吧🧐

越来越懒了,可能之后都不会写了吧?