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

Username:   Password: 
 RegisterRegister   
 lights game (help)
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
turing91




PostPosted: Fri Jan 12, 2007 11:08 pm   Post subject: lights game (help)

i am writing a program using the parallel port for my computers class and i got myself stuck.
For the program, we needed to use 1 button an 8 LED lights.
My program flashes random led lights, and you need to press the key on the keyboard that corresponds to the light shown before the next light appears.

i have 3 main parts, the button, random lights, and keyboard commands.
the part i need help with is connecting the keyboard commands with the flashing lights, such as
code:
if parallelput=1 'and you press a', put "good"
else end process/procedure

or something along that line.
My full code is:
code:
var x : int
var no1 : int
var keys : array char of boolean

put "Press the button to start"
put ""
loop
    x := parallelget
    if x = (56) then
        cls
        put "3..."
        delay (1000)
        cls
        put "2..."
        delay (1000)
        cls
        put "1..."
        delay (1000)
        cls
        put "GO!"
        delay (1000)
        cls
    end if
end loop


%Random lights displayed

loop
    no1 := Rand.Int (0, 7)
    put (2 ** no1)
    delay (600)
end loop


loop
    Input.KeyDown (keys)
    if keys (chr (97)) then
        put "good"
        delay (160)
    elsif keys (chr (115)) then
        put "s"
        delay (160)
    elsif keys (chr (100)) then
        put "d"
        delay (160)
    elsif keys (chr (102)) then
        put "f"
        delay (160)
    elsif keys (chr (104)) then
        put "h"
        delay (160)
    elsif keys (chr (106)) then
        put "j"
        delay (160)
    elsif keys (chr (107)) then
        put "k"
        delay (160)
    elsif keys (chr (108)) then
        put "l"
        delay (160)
    end if
end loop


please help me cause my teachers is too lazy to help anyone in the class.
Sponsor
Sponsor
Sponsor
sponsor
Ultrahex




PostPosted: Sat Jan 13, 2007 2:38 pm   Post subject: Re: lights game (help)

You have 3 infinite loops, the first one is never left, so i would recommend combining them otherwise code after will never be read, or exiting loops under a condition.

when you have:
code:
if keys(chr(97)) then

you could replace with:
code:
if keys('a') then
for easy reading

but since you want to check whether or not its the right light, you should be doing something like:
code:
if keys('a') and no1=1 then

this will check whether or not the a character is pressed, and the no1 which corresponds to the last output to the board (which is the number of the led im guessing if i look at your code)

Hope This Helps.
turing91




PostPosted: Sat Jan 13, 2007 7:14 pm   Post subject: Re: lights game (help)

ok i tired the statement, and it works, but i'm trying to make it if you press it at the wrong time the procedure ends or process ends if possible.

this code puts random numbers, but it puts "you lose" after every number, also without the else statement, i have to wait a certain amount of time before the letter shows up, is it because of the delay?

code:

procedure two
    loop
        no1 := Rand.Int (0, 7)
        put (2 ** no1)
        delay (1000)

        Input.KeyDown (keys)
        if keys ('a') and no1 = 0 then
            put "a"
            delay (1)
        else put "you lose"
        end if
      end loop
end two

two
turing91




PostPosted: Sun Jan 14, 2007 12:02 pm   Post subject: RE:lights game (help)

can some please help me, i am really stuck. I know some of you know how to help me.
Ultrahex




PostPosted: Sun Jan 14, 2007 1:14 pm   Post subject: Re: lights game (help)

This is a very rough way to do it, i do not recommend doing it this way, but by looking at this way i hope you can find a better way to do it, the idea is the same, just its not the correct way to do.

code:

var no1 : int
var keys : array char of boolean
var dlayCount : int := 0

loop
        no1 := Rand.Int (0, 7)
        put (2 ** no1) ..
        put "-", no1
        loop
            delay (10)
            dlayCount += 10

            Input.KeyDown (keys)
            if keys ('a') and no1 = 0 then
                put "Correct 'a'"
                dlayCount := 0 % Reset Count
                delay (2000) % So Next Round Is Not Instant
                exit
            elsif keys ('a') and no1 not= 0 then % If Press Key and Not Number!
                put "you lose"
                dlayCount := 0 % Reset Count
                delay (2000)
                exit
            end if

            if keys ('s') and no1 = 1 then % if hitting a, but isnt led 1 then
                put "Correct 's'"
                dlayCount := 0 % Reset Count
                delay (2000) % So Next Round Is Not Instant
                exit
            elsif keys ('s') and no1 not= 1 then
                put "you lose"
                dlayCount := 0 % Reset Count
                delay (2000)
                exit
            end if

            if dlayCount = 1000 then % Time Out
                put "you lose"
                dlayCount := 0
                exit
            end if
        end loop
end loop
turing91




PostPosted: Sun Jan 14, 2007 2:05 pm   Post subject: Re: lights game (help)

thanks a lot, it actually works now, but how would you make it where if you get the letter wrong it stops the game instead of saying "you lose"?
i tried this but it still
code:

if keys ('a') and no1 = 0 then
                put "Correct 'a'"
                dlayCount := 0
                delay (1000)
                exit
            elsif keys ('a') and no1 not= 0 then
                exit
            end if

code:

var no1 : int
var keys : array char of boolean
var dlayCount : int := 0

procedure two
    loop
        no1 := Rand.Int (0, 7)
        put (2 ** no1) ..
        put " (", no1 + 1 ..
        put ")"
        loop
            delay (10)
            dlayCount += 10

            Input.KeyDown (keys)
            if keys ('a') and no1 = 0 then
                put "Correct 'a'"
                dlayCount := 0
                delay (1000)
                exit
            elsif keys ('a') and no1 not= 0 then
                put "you lose"
                dlayCount := 0
                delay (1000)
                exit
            end if

            if keys ('s') and no1 = 1 then
                put "Correct 's'"
                dlayCount := 0
                delay (1000)
                exit
            elsif keys ('s') and no1 not= 1 then
                put "you lose"
                dlayCount := 0
                delay (1000)
                exit
            end if

            if keys ('d') and no1 = 2 then
                put "Correct 'd'"
                dlayCount := 0
                delay (1000)
                exit
            elsif keys ('d') and no1 not= 2 then
                put "you lose"
                dlayCount := 0
                delay (1000)
                exit
            end if

            if keys ('f') and no1 = 3 then
                put "Correct 'f'"
                dlayCount := 0
                delay (1000)
                exit
            elsif keys ('f') and no1 not= 3 then
                put "you lose"
                dlayCount := 0
                delay (1000)
                exit
            end if

            if keys ('h') and no1 = 4 then
                put "Correct 'h'"
                dlayCount := 0
                delay (1000)
                exit
            elsif keys ('h') and no1 not= 4 then
                put "you lose"
                dlayCount := 0
                delay (1000)
                exit
            end if

            if keys ('j') and no1 = 5 then
                put "Correct 'j'"
                dlayCount := 0
                delay (1000)
                exit
            elsif keys ('j') and no1 not= 5 then
                put "you lose"
                dlayCount := 0
                delay (1000)
                exit
            end if

            if keys ('k') and no1 = 6 then
                put "Correct 'k'"
                dlayCount := 0
                delay (1000)
                exit
            elsif keys ('k') and no1 not= 6 then
                put "you lose"
                dlayCount := 0
                delay (1000)
                exit
            end if

            if keys ('l') and no1 = 7 then
                put "Correct 'l'"
                dlayCount := 0
                delay (1000)
                exit
            elsif keys ('l') and no1 not= 7 then
                put "you lose"
                dlayCount := 0
                delay (1000)
                exit
            end if

            if dlayCount = 1000 then
                put "you lose"
                dlayCount := 0
                exit
            end if
        end loop
    end loop
end two

two
Ultrahex




PostPosted: Sun Jan 14, 2007 2:15 pm   Post subject: Re: lights game (help)

Well the easiest way would be to actually make a boolean called isEndofGame or isGameOver. the reason why i put is before is to make sure its boolean in terms of reading the code.

set it to false on start, and then make a exit when isGameOver in the outer infinite loop ontop of that exit.

code:
elsif keys ('a') and no1 not= 0 then
    isGameOver:=true
    exit
end if
turing91




PostPosted: Sun Jan 14, 2007 3:54 pm   Post subject: Re: lights game (help)

ok i'm confused. what kind of variable should gameover be? boolean? i tried this but it doesnt change anything...
code:


var no1 : int
var keys : array char of boolean
var dlayCount : int := 0
var isgameover : boolean

procedure two

    loop
        isgameover := false
        no1 := Rand.Int (0, 1)
        put (2 ** no1) ..
        put " (", no1 + 1 ..
        put ")"
        loop
            delay (10)
            dlayCount += 10

            Input.KeyDown (keys)
            if keys ('a') and no1 = 0 then
                put "Correct 'a'"
                dlayCount := 0
                delay (1000)
                exit
            elsif keys ('a') and no1 not= 0 then
                isgameover := true
                exit
            end if


god dammit i want to finish this project already Mad
Sponsor
Sponsor
Sponsor
sponsor
Ultrahex




PostPosted: Sun Jan 14, 2007 4:39 pm   Post subject: Re: lights game (help)

Something like this.....

code:
var no1 : int
var keys : array char of boolean
var dlayCount : int := 0
var isNextRound : boolean := false
var isGameOver : boolean := false

procedure nextRound
    dlayCount := 0
    delay (2000)
    isNextRound := true
end nextRound

loop
    exit when isGameOver=true
    isNextRound := false
    no1 := Rand.Int (0, 7)
    put (2 ** no1) ..
    put "-", no1
    loop
        delay (10)
        dlayCount += 10

        Input.KeyDown (keys)
        if keys ('a') then
            if no1 = 0 then
                put "Correct 'a'"
            else
                put "you lose"
                isGameOver := true
            end if
            nextRound ()
        end if

        if keys ('s') then
            if no1 = 1 then
                put "Correct 's'"
            else
                put "you lose"
                isGameOver := true
            end if
            nextRound ()
        end if

        if dlayCount = 1000 then     % Time Out
            put "You Took Too Much Time!"
            nextRound ()
        end if

        exit when isNextRound
    end loop
end loop
put "GameOver!"
turing91




PostPosted: Sun Jan 14, 2007 5:40 pm   Post subject: Re: lights game (help)

alright, i finished it! My final game is...
i hope it works if you change the put Rand.Int, with parallelput Rand.Int, or i'm screwed XD

thanks for the help Hltrahex!

code:

var x : int
var no1 : int
var keys : array char of boolean
var dlayCount : int := 0
var isNextRound : boolean := false
var isGameOver : boolean := false

procedure one
    put "Press the button to start"
    put ""
    loop
        x := parallelget
        if x = (56) then
            cls
            put "3..."
            delay (1000)
            cls
            put "2..."
            delay (1000)
            cls
            put "1..."
            delay (1000)
            cls
            put "GO!"
            delay (1000)
            cls
        end if
    end loop
end one

procedure nextRound
    dlayCount := 0
    delay (1000)
    isNextRound := true
end nextRound

procedure two
    loop
        exit when isGameOver = true
        isNextRound := false
        no1 := Rand.Int (0, 7)
        put (0)
        put (2 ** no1) ..
        put "-", no1 + 1
        loop
            delay (15)
            dlayCount += 10

            Input.KeyDown (keys)
            if keys ('a') then
                if no1 = 0 then
                    put "Correct!"
                else
                    put "You Lose"
                    isGameOver := true
                end if
                nextRound ()
            end if

            if keys ('s') then
                if no1 = 1 then
                    put "Correct!"
                else
                    put "You Lose"
                    isGameOver := true
                end if
                nextRound ()
            end if

            if keys ('d') then
                if no1 = 2 then
                    put "Correct!"
                else
                    put "You Lose"
                    isGameOver := true
                end if
                nextRound ()
            end if

            if keys ('f') then
                if no1 = 3 then
                    put "Correct!"
                else
                    put "You Lose"
                    isGameOver := true
                end if
                nextRound ()
            end if

            if keys ('h') then
                if no1 = 4 then
                    put "Correct!"
                else
                    put "You Lose"
                    isGameOver := true
                end if
                nextRound ()
            end if

            if keys ('j') then
                if no1 = 5 then
                    put "Correct!"
                else
                    put "You Lose"
                    isGameOver := true
                end if
                nextRound ()
            end if

            if keys ('k') then
                if no1 = 6 then
                    put "Correct!"
                else
                    put "You Lose"
                    isGameOver := true
                end if
                nextRound ()
            end if

            if keys ('l') then
                if no1 = 7 then
                    put "Correct!"
                else
                    put "You Lose"
                    isGameOver := true
                end if
                nextRound ()
            end if

            if dlayCount = 1000 then % Time Out
                put "You Took Too Much Time!"
                isGameOver := true
            end if

            exit when isNextRound
        end loop
    end loop
    put "GameOver!"
end two

one
two
Clayton




PostPosted: Sun Jan 14, 2007 6:19 pm   Post subject: Re: lights game (help)

I'm not too sure, as I haven't dealt with peripherals in Turing, but I believe parallelput takes an integer argument, so Rand.Int should be okay to pass...

Also, his name is Ultrahex, not Hltrahex Razz
turing91




PostPosted: Mon Jan 15, 2007 6:32 pm   Post subject: RE:lights game (help)

yeah, i tested it out at school and i needed to add another var so the button ends and the actual game starts.
code:

var isButtonOver : boolean := true
procedure one
    put "Press the button to start"
    put ""
    loop
        isButtonOver:=true
        x := parallelget
        if x = (56) then
            cls
            put "3..."
            delay (1000)
            cls
            put "2..."
            delay (1000)
            cls
            put "1..."
            delay (1000)
            cls
            put "GO!"
            delay (1000)
            cls
        exit when isButtonOver = true
    end loop
end one

and sorry bout the misspelling, i was typing fast.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: