使用GamemakerStudio2制作《坦克大戰(zhàn)》學(xué)習(xí)總結(jié)

本篇為觀看up主紅色激情的教學(xué)視頻所寫的總結(jié) 視頻地址:
由于這次教程紅激老師并沒有列大綱,而是一步一步帶著我們寫,所以接下來我直接就放整個(gè)事件代碼,而不是一點(diǎn)一點(diǎn)加了。導(dǎo)入資源
同《功夫》篇相同
創(chuàng)建房間
寬高老樣子分別設(shè)為256和240
新建灰色墻體對(duì)象,并鋪在房間中
obj_wall_gray
新建玩家對(duì)象
這里我們一步到胃嗷,各種功能都寫好了 obj_player創(chuàng)建事件:
?state=0;// 狀態(tài)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;// 移動(dòng)速度
?
?//***移動(dòng)
?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;
?//***碰墻檢測(cè)
?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;
?//***動(dòng)畫控制
?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;
?}
創(chuàng)建墻體對(duì)象
obj_wall(紅磚墻) 添加創(chuàng)建事件:
?/// @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();
創(chuàng)建obj_wall_a,和obj_wall_b 創(chuàng)建obj_wall_father作為obj_wall_a,和obj_wall_b的父類對(duì)象,可摧毀的墻體對(duì)象 創(chuàng)建obj_wall_master作為obj_wall_father和obj_wall_gray的父類對(duì)象,用于碰撞檢測(cè) 很巧,創(chuàng)建的這些對(duì)象,都沒有代碼
創(chuàng)建子彈對(duì)象
創(chuàng)建obj_bullet作為玩家的子彈 添加創(chuàng)建事件
?image_speed=0;
?state=0;
添加步事件
?if(state==1)
?instance_destroy();
添加碰撞事件,對(duì)象為obj_wall_gray
?state=1;
?instance_create_depth(x,y,0,obj_boom_a);
?audio_play_sound(snd_bullet_ion,10,false);
添加碰撞事件,對(duì)象為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;
添加碰撞事件,對(duì)象為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);
創(chuàng)建敵人對(duì)象
obj_enemy創(chuàng)建事件
?state=0;// 狀態(tài)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;// 移動(dòng)速度
?
?//**AI移動(dòng)
?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
?
?//***碰墻檢測(cè)
?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;
?}
?//***移動(dòng)
?x+= dx*psp;
?y+= dy*psp;
?//***動(dòng)畫控制
?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;
?}
創(chuàng)建生成敵人的老巢
這里我沒有用紅激的辦法,因?yàn)檫\(yùn)行沒有效果,我也不知道為啥,自己改了改 obj_enemy_born 創(chuàng)建事件
?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;
?}
創(chuàng)建爆炸對(duì)象
obj_boom_a和obj_boom_b代碼一模一樣,只是精靈不一樣罷了 創(chuàng)建事件
?image_speed=0.5;
動(dòng)畫結(jié)束事件
?instance_destroy();
結(jié)語
至此,視頻內(nèi)容的代碼都包含了,我自己實(shí)現(xiàn)了敵人的發(fā)射子彈,和自己陣亡,但我懶得再把代碼貼出來了??反正也沒人看,有人要我就發(fā)吧??

越來越懶了,可能之后都不會(huì)寫了吧?