So a while ago @Bentley asked to see some of my code, so i posted it to my game's forum page, But seeing as that page isnt really popular I thought I would share the code here as well, If you want to see my project or the Original post check out my games page, a link to it is in my signature.
Fell Free to improve this code and comment the improved code so that others may benefit from it
I give full rights and any legal stuff for anyone to use this code in there own projects, as long as I'm credited and given a link to the project ^W^
Fell Free to improve this code and comment the improved code so that others may benefit from it
I give full rights and any legal stuff for anyone to use this code in there own projects, as long as I'm credited and given a link to the project ^W^
Code:
//Obj_door collision with obj_activate event:
//well first off can we activate the door?
if (canactivate==1)
{
//is it locked?
if (locked==true)
{
//how locked?
locklevel = locked_skill;
//find level locked and then do this
switch(locklevel)
{
//not locked?
case 0:
locked=false;
break;
//easy medium and hard
case 1:
case 2:
case 3:
//if the player has lock picks make the minigame
if (global.itemlockpick != 0)
{
if instance_exists(obj_activate2)
{
instance_destroy(obj_activate2);
}
if !instance_exists(obj_picklock)
{
instance_create(x,y,obj_picklock);
}
} else {
//stop trying to activate and make a textbox
if !instance_exists(obj_player_TB_nolockpick)
{
instance_create(obj_player.x,obj_player.y,obj_player_TB_nolockpick);
}
if instance_exists(obj_activate2)
{
instance_destroy(obj_activate2);
}
}
break;
//needs key
case 4:
//find out if player has key (THIS IS A PLACE HOLDER FOR MY GAME)
if !audio_is_playing(snd_door_locked)
{
audio_play_sound(snd_door_locked,1,0);
}
instance_destroy(other);
break;
}
} else {
if (closed=true)
{
instance_destroy(other);
if !audio_is_playing(snd_door_open)
{
audio_play_sound(snd_door_open,1,0);
}
opening=1;
closing=0;
image_speed=0.2;
canactivate=0;
}
if (closed=false)
{
instance_destroy(other);
if !audio_is_playing(snd_door_close)
{
audio_play_sound(snd_door_close,1,0);
}
closing=1;
opening=0;
image_speed=-0.2;
canactivate=0;
}
}
}
Code:
//Obj_picklock Create event
//Set some var's
pick_image_angle = 90;
draw=1;
level = 0;
sweetspot = 0;
playsound=1;
spotmax=1;
spotmin= -1;
image_angle=90;
pick_subimg=0;
reset=0;
lockstrength = door.locklevel;
level = lockstrength;
// set the var's for set difficulty
switch (level)
{
case 1:
sweetspot = irandom(90);
spotmax= 7;
spotmin = -7;
break;
case 2:
sweetspot = irandom(180);
spotmax = 5;
spotmax = -5;
case 3:
sweetspot = irandom(360);
spotmax = 3;
spotmin = -3;
}
Code:
//Obj_picklock Step event
//Quit
if keyboard_check(vk_escape)
{
instance_destroy();
}
//image_angle max and min
if image_angle > 90
{
image_angle = 90;
}
if image_angle < 0
{
image_angle = 0;
}
//Reset if not pressing stuff
if !keyboard_check(ord('D')) && !keyboard_check(ord('A')) && (image_angle<90) && !(reset = 2)
{
image_angle=image_angle+90/8;
}
//Turn lock
if keyboard_check(ord('D')) ^^ keyboard_check(ord('A'))
{
if !(image_angle <= 0) && !(pick_subimg = 1)&& !(reset = 2)
{
image_angle=(image_angle-3);
}
}
//Woah Noises!
if keyboard_check(ord('D')) ^^ keyboard_check(ord('A')) && (image_angle<90) && !(image_angle <= 0)
{
if !audio_is_playing(snd_lockturn)
{
audio_play_sound(snd_lockturn,0,0);
}
}
//Turn pick
if keyboard_check(vk_right) && !keyboard_check(ord('D')) && !keyboard_check(ord('A'))&& !(pick_subimg = 1)&& !(reset = 2)
{
if (pick_image_angle < 0){pick_image_angle=360}
pick_image_angle=(pick_image_angle-1);
}
//Turn pick other way
if keyboard_check(vk_left) && !keyboard_check(ord('D')) && !keyboard_check(ord('A'))&& !(pick_subimg = 1)&& !(reset = 2)
{
if (pick_image_angle > 360){pick_image_angle=0}
pick_image_angle=(pick_image_angle+1);
}
//Reset the var sound
if !(pick_image_angle = sweetspot)
{
playsound=1;
}
//Set the sound effect
lockClick = choose(snd_lockClick_1,snd_lockClick_2);
//Check if pick is in right place
if (pick_image_angle = sweetspot)
{
if !audio_is_playing(lockClick) && playsound=1
{
audio_play_sound(lockClick,1,0);
playsound=0;
}
}
//Unlock
//is the lock turned?
if (image_angle <= 0)
{
//Pick is in the sweetspot
if (pick_image_angle = sweetspot)
{
//Unlock door
if !audio_is_playing(snd_lock_Unlock)
{
audio_play_sound(snd_lock_Unlock,50,0);
}
door.locked_skill=0;
door.locked=false;
if !instance_exists(obj_activate2){instance_create(door.x,door.y,obj_activate2)};
instance_destroy();
} else {
//Pick is not in the sweetspot
spot = (pick_image_angle - sweetspot);
if (spot < spotmax) && (spot > spotmin)
{
sweetspot = pick_image_angle;
image_angle = 0;
} else {
//break lock pick
pick_subimg = 1;
//play break sound
if !audio_is_playing(snd_lock_breakpick) && !(reset = 2)
{
audio_play_sound(snd_lock_breakpick,50,0);
}
//reset lock
if (reset=0)
{
global.itemlockpick = (global.itemlockpick - 1);
alarm[0]= room_speed*1;
reset=2;
}
}
}
}
Code:
//Obj_picklock Draw Event
//Draws the Lock and the lockpick sprite
draw_set_alpha(0.99);
draw_set_color(c_black);
draw_rectangle(0,0,room_width,room_height,0);
draw_set_alpha(1);
draw_sprite_ext(spr_lock,0,view_wview[view_current]/2,view_hview[view_current]/2,2,2,image_angle,c_white,1);
draw_sprite_ext(spr_lockpick,pick_subimg,view_wview[view_current]/2,view_hview[view_current]/2,2,2,pick_image_angle,c_white,1);
draw_set_color(c_white);
draw_set_font(fnt_coderscrux);
draw_text(view_wview[view_current]/2,((view_hview[view_current]/2)+200),"Lockpicks: "+string(global.itemlockpick));
//Debug stuff not necessary
if (global.lockdebug = true)
{
draw_text(x,y+200, image_angle)
draw_set_color(c_green);
draw_text(x,y+300, sweetspot)
}
//fix everything
draw_set_halign(fa_left);
draw_set_valign(fa_top);
draw_set_alpha(1);
draw_set_font(-1);
draw_set_color(c_black);
Code:
//Obj_picklock Alarm[0] Event
// Check to see if the player has no lockpicks left and if they dont exit the minigame and display a textbox
// saying so
if (global.itemlockpick = 0)
{
if !instance_exists(obj_player_TB_nolockpick)
{
instance_create(obj_player.x,obj_player.y,obj_player_TB_nolockpick);
}
instance_destroy(self);
} else {
pick_subimg = 0;
pick_image_angle = 90;
image_angle = 90;
reset=0;
}
Code:
//Obj_picklock Destroy event
if instance_exists(obj_playerlockcontroll)
{
instance_destroy(obj_playerlockcontroll);
}
instance_activate_all();