Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 assigning array char of boolean values?
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic
Author Message
Fire_Storm




PostPosted: 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
 
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Sat Sep 25, 2004 6:24 pm   Post subject: (No 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...
 
Mazer




PostPosted: Sat Sep 25, 2004 6:25 pm   Post subject: (No 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.
 
Fire_Storm




PostPosted: Sat Sep 25, 2004 6:35 pm   Post subject: (No 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
 
Delos




PostPosted: Sat Sep 25, 2004 6:59 pm   Post subject: (No 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.
 
Fire_Storm




PostPosted: Sat Sep 25, 2004 7:10 pm   Post subject: (No 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
code:

if chars (KEY_UP_ARROW)  then
*chars:= KEY_UP_ARROW* (ive tried this, it doenst work,)
exit
end if

that way when it hits the next check for what chars is it will see that its up
 
Delos




PostPosted: Sat Sep 25, 2004 7:12 pm   Post subject: (No 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.

code:

var chars : array char of boolean
var p : string (1)
var test : array 1 .. 200000 of int
for i : 1 .. upper (test)
    randint (test (i), 1, 200)
    % Just some junk stuff...no importance.
end for

put "Press 'q' to exit these loop.  Press any key to continue"
Input.Pause

% Test 1:  Input.KeyDown() version.
loop
    Input.KeyDown (chars)
    exit when chars ('q')
    % Set the exit condition.

    % Some random stuff to keep the CPU busy for a bit.
    drawfillbox (Rand.Int (0, 100), Rand.Int (0, 100), Rand.Int (101, 200), Rand.Int (101, 300), Rand.Int (0, 7))
    for i : 1 .. upper (test) - 1
        if test (i) > test (i + 1) then
            test (i) := test (i)
        end if
    end for
end loop

put "Done 1."
delay (100)
cls

loop
    % More random stuff, the same though!
    drawfillbox (Rand.Int (0, 100), Rand.Int (0, 100), Rand.Int (101, 200), Rand.Int (101, 300), Rand.Int (0, 7))
    for i : 1 .. upper (test) - 1
        if test (i) > test (i + 1) then
            test (i) := test (i)
        end if
    end for

    % Now for the kool check.
    if hasch then
        % This checks for buffered unread input.
        getch (p)
        % If it exists, assign it to a variable
        if p = "q" then
            % Now if the value is the exit condition then...
            exit
        else
            Input.Flush
            % Otherwise, clear the buffer!  Not really necassary, but kool.
        end if
    end if
end loop



I'm very happy with that code, it does cool stuff.
 
Fire_Storm




PostPosted: Sat Sep 25, 2004 7:14 pm   Post subject: (No 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
 
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Sep 25, 2004 7:28 pm   Post subject: (No 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"?
 
Fire_Storm




PostPosted: Sat Sep 25, 2004 8:02 pm   Post subject: (No 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



The Extension 'avi' was deactivated by an board admin, therefore this Attachment is not displayed.

 
Mazer




PostPosted: Sat Sep 25, 2004 8:26 pm   Post subject: (No subject)

Even if you want to do movement at once, you can do this without processes. You can use this for input code:
code:

if chars (KEY_UP_ARROW) then
    % red up
elsif chars (KEY_DOWN_ARROW) then
    % red down
elsif chars (KEY_LEFT_ARROW) then
    % red left
elsif chars (KEY_RIGHT_ARROW) then
    % red right
end if

if chars ('w') then
    % blue up
elsif chars ('s') then
    % blue down
elsif chars ('a') then
    % blue left
elsif chars ('d') then
    % blue right
end if

Then put the actual movement code in there instead of those comment lines.
 
Fire_Storm




PostPosted: Sat Sep 25, 2004 8:38 pm   Post subject: (No subject)

I DO have that, the problem is exiting the movement code and going to the next one
 
AsianSensation




PostPosted: Sat Sep 25, 2004 9:02 pm   Post subject: (No 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

code:
        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


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:

code:
loop
    Input.KeyDown (chars)

    if chars (KEY_UP_ARROW) then
        % red up
    elsif chars (KEY_DOWN_ARROW) then
        % red down
    elsif chars (KEY_LEFT_ARROW) then
        % red left
    elsif chars (KEY_RIGHT_ARROW) then
        % red right
    end if

    if chars ('w') then
        % blue up
    elsif chars ('s') then
        % blue down
    elsif chars ('a') then
        % blue left
    elsif chars ('d') then
        % blue right
    end if
   
    %Check collision here
    %Draw the lines here
end loop


This way, there are no processes to fork, and you only have 1 loop to exit if your collision detection goes off.
 
Mazer




PostPosted: Sat Sep 25, 2004 9:04 pm   Post subject: (No 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
 
Fire_Storm




PostPosted: Sat Sep 25, 2004 9:20 pm   Post subject: (No 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
 
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic

Page 1 of 2  [ 20 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: