April 30, 2024, 09:47:02 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Brain Reflex Test

Started by SnarlingSheep, May 20, 2009, 08:38:54 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

SnarlingSheep

May 20, 2009, 08:38:54 AM Last Edit: May 20, 2009, 08:45:08 AM by SnarlingSheep
I wrote this after a discussion on pseudocode found here: http://codecraft.proboards.com/index.cgi?board=articles&action=display&thread=691
Basically I wrote in plain English what I had coded in Visual DialogScript. Then wrote the CBasic code based on the plain English.
If you remove everything but the comments you are back to the pseudocode and could try and write it in Emergence or Aurora.

Its a simple brain reflex game where you need to click the square as soon as possible after it changes color.
The square changes to a random color anywhere from 3 to 8 seconds after you click the Start button.

;Global
;-Set a variable named time to -1.
def time as float
time = -1
;-starttime and endtime variables to hold the GetTickCount times
def starttime as int
def endtime as int

;-Set variables to check if timers are running.
def timer1 as int
def timer2 as int
timer1 = 0
timer2 = 0

;-Create a window with:
;Text at the top explaining the game.
;A big white square in the middle, that is clickable.
;A start button.
def w1 as window
window w1,100,100,270,356,@minbox,0,"Brain Reflex Test",main
control w1,"b,,45,105,180,180,0,2"
control w1,"b,Start,45,287,180,30,0,1"
;Set the square color to white
setcontrolcolor w1,2,rgb(255,255,255),rgb(255,255,255)
;Set the window color to grey
setwindowcolor w1,rgb(220,220,220)
;Change the windows font
setfont w1,"times new roman",10,0
;Set print precision to 3 for 3 decimal places
setprecision 3
;Print the game information above the white square
move w1,25,5
print w1,"Click the Start button and then get ready."
move w1,15,22
print w1," Click the Big Square when it changes color"
move w1,90,39
print w1,"Try to beat 0.25"
move w1,90,65
print w1,"Last time: None"

;-We will be using the API Function 'GetTickCount' to keep track of time so declare it or load the kernel32 library here.
declare "kernel32",GetTickCount(),int

;-Create a list or array of colors.
def colors[9] as int
;-Add the following RGB values to that list:
colors[0] = rgb(255,000,000)
colors[1] = rgb(255,000,255)
colors[2] = rgb(128,000,000)
colors[3] = rgb(000,255,000)
colors[4] = rgb(255,255,000)
colors[5] = rgb(000,128,000)
colors[6] = rgb(000,000,255)
colors[7] = rgb(000,255,255)
colors[8] = rgb(000,000,128)

waituntil w1 = 0
end

;Event Handler
;-Wait for events and then handle them.
sub main
if @class = @idcreate
centerwindow w1
endif
    if @class = @idcontrol
if @controlid = 1
;Click on the start button
;-Check to see if Timer #1 is running. This timer counts down from 3 to 8 seconds before changing the Big Square color.
if timer1 = 1
;-If it is running:
;Our button currently reads Stop and we need to reset a view things.
;Stop Timer #1.
stoptimer w1,1
timer1 = 0
;Stop Timer #2.
stoptimer w1,2
timer2 = 0
;Set the Big Square back to white.
setcontrolcolor w1,2,rgb(255,255,255),rgb(255,255,255)
;Set the Start Button to read 'Start' again.
setcontroltext w1,1,"Start"
else
;-Else if it is not running:
;Change the Start Button to read Stop.
setcontroltext w1,1,"Stop"
;Start Timer #1 to count down from a random amount of seconds between 3 and 8.
starttimer w1,rnd(5)+3000,1
timer1 = 1
;-End the if statement
endif
;-Go to window event handler.
return
endif
if @controlid = 2
;Click event on the Big Square
;-Check to see that Timer #2 is running. This timer waits for the user to click the square.
if timer2 = 1
;-If it is running:
;Use GetTickCount to get the current tick count, store it in a variable named 'endtime'.
endtime = GetTickCount()
;Subtract variable 'starttime' from variable 'endtime' and store it in a variable named 'time'.
time = endtime - starttime
;Divide variable 'time' by 1000 to convert time from milliseconds to seconds.
time = time / 1000
;Format the variable 'time' to 3 decimal places.
;time = using("##.###",time)
;Stop Timer #2
stoptimer w1,2
timer2 = 0
;Go to the 'Settext' routine which now will change the game explaination text to include the users last time.
gosub SetText
;Set the Big Square back to white.
setcontrolcolor w1,2,rgb(255,255,255),rgb(255,255,255)
;Set the Start button to read Start again.
setcontroltext w1,1,"Start"
endif
;-End the if statement.
;-Go to window event handler.
return
endif
endif
if @class = @idtimer
if @code = 1
timer1 = 0
stoptimer w1,1
;Timer #1 has ended event
;-Set the square to a random color from our color list.
def tmpcolor as int
tmpcolor = rnd(8)
setcontrolcolor w1,2,colors[tmpcolor],colors[tmpcolor]
;-Get the current tick count and store it in a variable named 'starttime'
starttime = GetTickCount()
;-Start Timer #2 which will run for 10 seconds, timing out if the user hasn't clicked the Big Square.
starttimer w1,10000,2
timer2 = 1
;-Go to window event handler.
return
endif
if @code = 2
stoptimer w1,2
timer2 = 0
;Timer #2 has ended event
;-Set the Big Square back to white.
setcontrolcolor w1,2,rgb(255,255,255),rgb(255,255,255)
;-Set the Start button to read 'Start' again.
setcontroltext w1,1,"Start"
;-Set the 'time' variable to 0.0
time = 0.0
;-Call the 'Settext' routine which will change the explaination text to tell the user they took too long.
gosub SetText
;-Go to window event handler.
return
endif
endif
    if @class = @idclosewindow
        ;Window close event
;-Exit our program.
        closewindow w1
    endif
return

sub SetText
;Settext routine
;-Set a variable named 'msg' to "You Took Too Long", this is our default message.
def msg as string
msg = "You Took Too Long!"
;-If the variable 'time' is less than 0.25
if (time > 0.0) & (time < 0.251)
;Set the variable 'msg' to "Excellent!"
msg = "          Excellent!          "
;-End the if statement.
endif
;-If the variable 'time' is greater than 0.25 but less than 0.35
if (time > 0.25) & (time < 0.35)
;Set the variable 'msg' to "Good!"
msg = "            Good!            "
;-End the if statement.
endif
;-If the variable 'time' is greater than 0.35 but less than 0.45
if (time > 0.35) & (time < 0.45)
;Set the variable 'msg' to "Keep Practicing!"
msg = "    Keep Practicing!    "
;-End the if statement.
endif
;-If the variable 'time' is greater than 0.45
if time > 0.45
;Set the variable 'msg' to "You Need Practice!"
msg = "You Need Practice!"
;-End the if statement.
endif

move w1,90,65
print w1,"                                          "
move w1,90,65
print w1,"Last time: ", time,
move w1,80,82
print w1,"                                          "
move w1,80,82
print w1,msg
;-Exit the Settext routine.
return

aurelCB

Not bad,not bad
my best result is 0.26 ;D

Egil

Nice piece!
I showed it to my neighbour. But he beat me... He got  0.263 and I managed only 0.282 :(
Guess I have to practice.
By the way, changing it to EB-code was just a matter of changing the characters for remarks, and putting in a couple of ENDSUBS.
But my scores didn't improve though...
Thanks for sharing!
Support Amateur Radio  -  Have a ham  for dinner!

psevet

0.187 ... madly double-clicking on my mouse  :D

Pierre

SnarlingSheep

Quote from: psevet on May 20, 2009, 01:24:39 PM
0.187 ... madly double-clicking on my mouse  :D
lol, need to add cheating detection ;)