GameMaker: Studio

GameMaker: Studio

View Stats:
ZyroSH May 9, 2017 @ 10:02am
Random keys
I have 2 problems, i am making a game where the keys you press to move and shoot etc are random, however it isnt doing anything.

//Create

///Variables and such

vspd = 0
hspd = 0
spd = 4
grav = 1
jspd = 10
bulletspd = 8
bulletdir = 0

global.W = keyboard_check(choose(ord("Q"),ord("W"),ord("E"),ord("R"),ord("T"),ord("Y"),ord("U"),ord("I"),ord("O"),ord("P"),ord("A"),ord("S"),ord("D"),ord("F"),ord("G"),ord("H"),ord("J"),ord("K"),ord("L"),ord("M"),ord("B"),ord("V"),ord("C"),ord("Z"),ord("X")))
global.A = keyboard_check(choose(ord("Q"),ord("W"),ord("E"),ord("R"),ord("T"),ord("Y"),ord("U"),ord("I"),ord("O"),ord("P"),ord("A"),ord("S"),ord("D"),ord("F"),ord("G"),ord("H"),ord("J"),ord("K"),ord("L"),ord("M"),ord("B"),ord("V"),ord("C"),ord("Z"),ord("X")))
global.S = keyboard_check(choose(ord("Q"),ord("W"),ord("E"),ord("R"),ord("T"),ord("Y"),ord("U"),ord("I"),ord("O"),ord("P"),ord("A"),ord("S"),ord("D"),ord("F"),ord("G"),ord("H"),ord("J"),ord("K"),ord("L"),ord("M"),ord("B"),ord("V"),ord("C"),ord("Z"),ord("X")))
global.D = keyboard_check(choose(ord("Q"),ord("W"),ord("E"),ord("R"),ord("T"),ord("Y"),ord("U"),ord("I"),ord("O"),ord("P"),ord("A"),ord("S"),ord("D"),ord("F"),ord("G"),ord("H"),ord("J"),ord("K"),ord("L"),ord("M"),ord("B"),ord("V"),ord("C"),ord("Z"),ord("X")))

//step

scr_player()

scr_collsions()

//scr_player

if place_meeting(x,y+1,par_solid){
vspd = 0
if (global.W){
vspd = -jspd
}
}else{
if (vspd <10){
vspd += grav
}
}

//Move

if (global.A){
hspd = -spd
}

if (global.D){
hspd = spd
}

//Not moving

if (!global.A) && (!global.D) || (global.A) && (global.D){
hspd = 0
}


//Shooting

whereami = x - mouse_x

if (global.S){

if (sign(whereami) > 0){
bulletdir = 0
}else{
bulletdir = 180
}

s = instance_create(x,y,obj_bullet)
s.direction = bulletdir
s.speed = bulletspd
s.image_angle = s.direction
}


//scr_collsions

//Horizontal

if (place_meeting(x+hspd,y,par_solid)){

while(!place_meeting(x+sign(hspd),y,par_solid)){
x += sign(hspd)
}
hspd = 0
}
x += hspd

//Vertical

if (place_meeting(x,y+vspd,par_solid)){

while(!place_meeting(x,y+sign(vspd),par_solid)){
y += sign(vspd)
}
vspd = 0
}
y += vspd


The second problem is, you dont know which random keys to press, so how would i draw that so it tells you which you need to do certain stuff. i have tried with draw_text but it hasnt worked.

//in draw gui

ww = room_width/2
hh = room_height-20

draw_text(ww,hh,global.W + global.A + global.S + global.D)
draw_set_halign(fa_center)
draw_set_valign(fa_center)
draw_set_colour(c_aqua)




< >
Showing 1-15 of 35 comments
Jeb May 9, 2017 @ 10:05am 
One thing is that keyboard_check I beleive, returns a boolean (yes/no, 1/0 etc.) so you are setting variables like global.W to either 1 or 0.
ZyroSH May 9, 2017 @ 10:20am 
thanks for that knowledge but if not doing it like that then how?
Jeb May 9, 2017 @ 10:30am 
It has been a while since I have used gamemaker so this might not be completely accurate.

Why not try setting a variable to a random integer. The range which the value can be pulled from should be the number of keys you want to be possibly used as a movement key.

You could then use that variable in an array of each of the possible keys (example: [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]) and set the movement key to be array[random_number].

If my way of explaining this seems confusing let me know and I will try and refine what I am attempting to say.
ZyroSH May 9, 2017 @ 10:40am 
how would i make an erray
ZyroSH May 9, 2017 @ 10:40am 
i understand completely but how do you make one
ZyroSH May 9, 2017 @ 10:41am 
i do python aswell and in that you just do list = [blah, blah, blah]
but i tried that in gamemaker and its not the same
This might take a while but use "||" or "or" and use if instead of that. Example:
if (keyboard_check(ord('Q')) || keyboard_check(ord('W')) || keyboard_check(ord('E')) || ...etc) { global.w=true }
do the same for global.a and others.
Reason:
choose(); chooses a random input from one those keys and they can not be checked individualy. The method shown above does that, however it's quite a lot of copy and paste work.
Last edited by ᴳ ᵉ ᵉ ᵐ ᵃ ʷ ᵈ ᵉ ʳ; May 9, 2017 @ 10:48am
ZyroSH May 9, 2017 @ 10:48am 
i havnt tested that but im pretty sure it wouldnt work because no matter what key you pressed it global.w would equal true
Originally posted by ZyroSH:
i havnt tested that but im pretty sure it wouldnt work because no matter what key you pressed it global.w would equal true
you should try it, since it checks every single defined key to be pressed. once a key is pressed it will equal true. if none is pressed then it will return false.
Last edited by ᴳ ᵉ ᵉ ᵐ ᵃ ʷ ᵈ ᵉ ʳ; May 9, 2017 @ 10:52am
ZyroSH May 9, 2017 @ 10:52am 
i dont want that
ZyroSH May 9, 2017 @ 10:52am 
it wont work trust me
I recommend not to use the array type for the keys unless you want to use the 'for' loop to scroll through, which has almost the identical effect of the same result you entered above.
Last edited by ᴳ ᵉ ᵉ ᵐ ᵃ ʷ ᵈ ᵉ ʳ; May 9, 2017 @ 10:54am
ZyroSH May 9, 2017 @ 10:55am 
just think, if you press any key it will equal true so no matter what key i press, everything will happen thats just stupid, thanks for your input but its not going to work
I think I have the solution for that problem, in the gamemaker help it says that 'choose' has the same randomize value when a game is run, so it will always return the same. For that you need to put this in a step event of an object:
randomize(); this will randomize the selection of the 'choose' function.

and for your secondary problem, use
draw_text(ww,hh,string(chr(global.W)) + string(chr(global.A)) + string(chr(global.S)) + string(chr(global.D)))
instead of
draw_text(ww,hh,global.W + global.A + global.S + global.D)
The chr is needed because it draws ascii code only, and if you want it to be a letter you must turn it into a character by using chr();

Since randomize() is used in the step event, every frame the letter will be different, if don't want that, put randomize in an alarm loop
Last edited by ᴳ ᵉ ᵉ ᵐ ᵃ ʷ ᵈ ᵉ ʳ; May 9, 2017 @ 11:08am
ZyroSH May 9, 2017 @ 11:09am 
thankyou, can you explain how to actually code this please
< >
Showing 1-15 of 35 comments
Per page: 1530 50

Date Posted: May 9, 2017 @ 10:02am
Posts: 35