# Brain freeze on how to program

**URL:** <https://www.ruby-forum.com/t/brain-freeze-on-how-to-program/149931>\
**Category:** wxRuby\
**Created:** [October 1, 2008, 9:50am UTC](https://www.ruby-forum.com/t/brain-freeze-on-how-to-program/149931 "2008-10-01T09:50:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joseph\_edelstein](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@joseph\_edelstein](https://www.ruby-forum.com/u/joseph_edelstein)\
**Post date:** [October 1, 2008, 9:50am UTC](https://www.ruby-forum.com/t/brain-freeze-on-how-to-program/149931/1 "2008-10-01T09:50:44Z")

</div>

I have come to a complete brick wall in my mind. I am trying to write a  
game where the user has to hit the space bar as close to a sound that  
plays every second. If the user hits the space bar and releases it  
before the sound, one set of boxes will light up.

If the user hits and releases the space bar after the sound, another set  
of boxes will light up.

I have some code but am blanking on how to move forward.

Need some guidance from better programmers then I.

Thanks in advance.

Joe

Code sample:

def ag\_start

total = 5  
c = 0  
sound\_timer = Timer.every(1000)  
{Sound.play(“c:\windows\media\chimes.wav”)}  
while c \< total  
####listen for half a second for if user presses the space bar before  
the sound

# 

# 

evt\_key\_up() { | event | if event.get\_key\_code == 32 then #get the  
time difference from the key up to the sound

```
  }

```

# 

# 

#play sound ########################

# 

# 

evt\_key\_up() { | event | if event.get\_key\_code == 32 then #get the  
time difference from the sound to the key up

```
  }

```

# 

# 

#listen for half a second for if the user presses the space bar after  
the sound

# 

#repeat for time required  
c = c + 1  
end

#sound\_timer.stop  
end

---

<div class="post-metadata">

**Author:** ![joseph\_edelstein](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@joseph\_edelstein](https://www.ruby-forum.com/u/joseph_edelstein)\
**Post date:** [October 1, 2008, 5:01pm UTC](https://www.ruby-forum.com/t/brain-freeze-on-how-to-program/149931/2 "2008-10-01T17:01:42Z")

</div>

Joseph E. wrote:

> I have come to a complete brick wall in my mind. I am trying to write a  
> game where the user has to hit the space bar as close to a sound that  
> plays every second. If the user hits the space bar and releases it  
> before the sound, one set of boxes will light up.
> 
> If the user hits and releases the space bar after the sound, another set  
> of boxes will light up.
> 
> I have some code but am blanking on how to move forward.

A few suggestions:

- Just after the chime sounds, in your Timer event handler, store the  
current time in an instance variable, eg  
@chime\_time = Time.now

- At the same time you could set a flag that indicates that the Space  
bar is now pressable…

- If the user presses the space bar within the right time, get the new  
time and deduct the @chime\_time to see how long they took - this should  
give you a milleseconds result, eg:  
how\_quick = Time.now - @chime\_time

- You can test whether the space bar is depressed when the sound is  
played by using Wx::get\_key\_state(Wx::K\_SPACE) - see my reply to Ross

hth  
alex
