Computer Science Canada assigning array char of boolean values? |
Author: | Fire_Storm [ Sat Sep 25, 2004 6:12 pm ] |
Post subject: | assigning array char of boolean values? |
ive got a program that uses var chars : array char of boolean and using Input.KeyDown it checks the the key thats pressed and takes action accordinly (changes direction of movement, but i have it exiting when a certain input.keydown is pressed eg (KEY_UP_ARROW), and i have a a loop checking for what is pressed and then enters that loop, but if i press they key too fast it only exits the loop it was in but doesnt enter the loop i want it to, so i want to know how i can assign chars to KEY_UP_ARROW for example, i dont know how array char of boolean works, if u cant understand sorry, if u have an idea but arent sure i can send u the source code |
Author: | Delos [ Sat Sep 25, 2004 6:24 pm ] |
Post subject: | |
Please post your code. Trying to describe what it does is like me trying to describe the layout of the hairs on my arm... Well, there's on near the wrist, then you sorta get this bare patch... |
Author: | Mazer [ Sat Sep 25, 2004 6:25 pm ] |
Post subject: | |
Hmm... I'm afraid I don't understand exactly what you are asking. Input.KeyDown works like this: -you have an array of booleans (one for [pretty much] each key) -you call Input.KeyDown using your array as a parameter -Input.KeyDown checks which keys are being pressed at the time it is called and sets the element of the array which corresponds to that key is set to true -to check for input, you check if the element in the array for the key you want is true. That is, if you want to check if the 'a' key is being pressed you would say if chars('a') then (that's assuming your array is called chars). I'm sorry if you already knew that. It would probably be more helpful if you could post some code to describe what you want to do. |
Author: | Fire_Storm [ Sat Sep 25, 2004 6:35 pm ] |
Post subject: | |
var chars : array char of boolean var chars1 : array char of boolean drawbox (0, 0, maxx, maxy, 42) drawbox (1, 1, maxx - 1, maxy - 1, 42) drawbox (2, 2, maxx - 2, maxy - 2, 42) drawbox (3, 3, maxx - 3, maxy - 3, 42) process move1 loop if count1 = 1 then exit elsif count2 = 1 then exit end if Input.KeyDown (chars) if chars (KEY_RIGHT_ARROW) then loop if View.WhatDotColour (x1 + 3, y1) not= white then count1 := 1 exit end if Input.KeyDown (chars) if count1 = 1 then exit elsif count2 = 1 then exit end if x1 := x1 + 3 delay (100) drawfilloval (x1, y1, 2, 2, 12) drawfilloval (x1, y1, 2, 2, 12) if chars (KEY_UP_ARROW) or chars (KEY_DOWN_ARROW) then Input.KeyDown (chars) exit end if Input.KeyDown (chars) if count1 = 1 then exit elsif count2 = 1 then exit end if end loop elsif chars (KEY_LEFT_ARROW) then loop if count1 = 1 then exit elsif count2 = 1 then exit end if Input.KeyDown (chars) if View.WhatDotColour (x1 - 3, y1) not= white then count1 := 1 exit end if if count2 = 1 then exit end if x1 := x1 - 3 delay (100) drawfilloval (x1, y1, 2, 2, 12) if chars (KEY_UP_ARROW) or chars (KEY_DOWN_ARROW) then Input.KeyDown (chars) exit end if if count1 = 1 then exit elsif count2 = 1 then exit end if end loop elsif chars (KEY_UP_ARROW) then this is just a bit, basically when it when it recieves that i pressed left for example it exits that move loop, but doesnt enter the move left loop if i press the key really fast, so i want to assign chars to left when i press it and just before i exit the move right loop so that the next loop will see that chars is assigned to left and it will start moving left, if u need MORE code to maybe see how the game works and what the trouble is i guess just ask |
Author: | Delos [ Sat Sep 25, 2004 6:59 pm ] |
Post subject: | |
Ok, dude, you ever heard of [code] tags? Please use them. Soooo....this is code from a game eh. And I'm guessing that this is all part of the control of a character? One presses left/right and the character moves thusly? Point one, don't use process()es, they sux0r. You'll see why soon enough. Judging from a lot of your code, I'm quite certain you don't really know how to use Input.KeyDown(). Let's see... At the onset of a loop you initialize the check by calling Input.KeyDown(varName). Anytime during the loop after that you may call varName(argument) to see if that key has been pressed. I'm pretty sure that it requires that key to be depressed at the time that the loop reaches that part of the code for the resultant actions to take effect. I've had this happen to me a lot - I have a bunch of code, with some exit conditions in it w.r.t. an Input.KeyDown() type input. Suggestion: tighten your code so that there is less time between subsequent repitions of the loop. Hmm...I'm thinking now, perhaps there's way this can be done through buffered input...hmmm...lemme get back to you on that one. |
Author: | Fire_Storm [ Sat Sep 25, 2004 7:10 pm ] | ||
Post subject: | |||
the game is finished and works fine, except for that 1 bug, and i had to use processes cuz i have 2 characters moving at once, i cant really find a way to to tighten my code all that much, i think what i really need is to to assign chars to the up key after i exit the loop so like
that way when it hits the next check for what chars is it will see that its up |
Author: | Delos [ Sat Sep 25, 2004 7:12 pm ] | ||
Post subject: | |||
Yaay!!!!! My code works! Well, at least it does what I wanted it to. Who knows if it does anything like what you'd want...anyway, it's got stuff in it that's somewhat complex to explain...so if you're thinking of ripping it off (sorry to even think that, but one can't be too cautious), be careful as your teacher may look at it and wonder if it's really yours or not coz it may not seem to be something you might have done...etc etc...you know the usual speil.
I'm very happy with that code, it does cool stuff. |
Author: | Fire_Storm [ Sat Sep 25, 2004 7:14 pm ] |
Post subject: | |
this isnt for a school project or anything im just doing this for fun, so do u think u could explain what ur code is doing? i dont really get it. or how it will work for my program |
Author: | wtd [ Sat Sep 25, 2004 7:28 pm ] |
Post subject: | |
Am I to take it "array char of boolean" is a form of dictionary or hash data structure, where indexes are not numbers but whatever data type precedes the "of"? |
Author: | Fire_Storm [ Sat Sep 25, 2004 8:02 pm ] |
Post subject: | |
well since it may be hard to under stand my problem here is my game, i have it as an attachment, set the speed to 100 to really see the bug, its hard to notice on faster speeds the bug unless u hit 2 move keys at the same time, it is a pretty good 2 player game its a .exe to protect my code, but i had to rename it to .avi to attach it so just rename it back to .exe and it should work |
Author: | Mazer [ Sat Sep 25, 2004 8:26 pm ] | ||
Post subject: | |||
Even if you want to do movement at once, you can do this without processes. You can use this for input code:
Then put the actual movement code in there instead of those comment lines. |
Author: | Fire_Storm [ Sat Sep 25, 2004 8:38 pm ] |
Post subject: | |
I DO have that, the problem is exiting the movement code and going to the next one |
Author: | AsianSensation [ Sat Sep 25, 2004 9:02 pm ] | ||||
Post subject: | |||||
K, after looking through the code, I'd suggest you get rid of all the nested Input.KeyDown. Since this game at most will need 4 buttons to be pressed at the same time, I suggest doing some different checking method. Instead of
which is messed up, because you are calling Input.KeyDown over and over again, and it does not serve any additional purpose. Do it like Coutsos told you, scrap the processes, they are useless here. And do something like this:
This way, there are no processes to fork, and you only have 1 loop to exit if your collision detection goes off. |
Author: | Mazer [ Sat Sep 25, 2004 9:04 pm ] |
Post subject: | |
But your input code should only be changing direction based on a key being pressed. You shouldn't have a loop inside those if statements. Try something like this: -check input -change direction if necessary -update player positions -check for collisions |
Author: | Fire_Storm [ Sat Sep 25, 2004 9:20 pm ] |
Post subject: | |
i need a loop in those if statments because after i hit left it constantly moves left until a key is pressed, so i need to keep calling input.keydown |
Author: | AsianSensation [ Sat Sep 25, 2004 9:28 pm ] |
Post subject: | |
so wait, right now, if I just press the left key and let go, it still moves to the left. Though isn't that the point of the game? |
Author: | Fire_Storm [ Sat Sep 25, 2004 9:30 pm ] |
Post subject: | |
yes, that is the point, i want it to do that, but if i do as Coutsos said and take out the loop in the if statement, it WOULDNT keep going left |
Author: | wtd [ Sat Sep 25, 2004 9:56 pm ] |
Post subject: | |
So basically there're two cursors, controlled by the keyboard. For each cursor, if a directional key is pressed, the cursor will begin moving in that direction. Afterwards, pressing a different directional key will stop the cursor and change it's direction. So you need to keep track of the direction each cursor is travelling, and each cursors current position. Now, it's really quite simple. You loop through and each time you check for key presses indicating a direction change. If you do, then change the direction the cursor is travelling and update its position. Now just redraw the whole thing with the cursors in their new positions. Lather, rinse, repeat. |
Author: | Fire_Storm [ Sat Sep 25, 2004 10:34 pm ] | ||
Post subject: | |||
YAYYY!, i found my own way to fix it, cant believe i didnt think of it before
just used dir1 and dir2 to represent the directions, it may be sloppy but it works great so im happy, thanks for all your help anyway. here is the final copy if anyone wants it, rename it to .exe for it to work |
Author: | wtd [ Sat Sep 25, 2004 11:15 pm ] | ||
Post subject: | |||
Your best bet would probably be to use an enum for the directions. I on't know Turing, and there's absolutely nothing online saying how to do this, but in Pascal (from which Turing descends), it would look like:
|