Skip to main content
gamedev.net
🔒 Locked

Question about implementation

Started by caffeineaddict Jan 4, 2003 at 9:05 PM 1 replies 150+ views
Original Post
caffeineaddict
caffeineaddict
I just started making a game and I had an idea for a portion of it, I want to have something like sheet music displayed, and a cursor is under each note as you play it, I was wondering how I would go about implementing it so that it could tell if the right key was pressed at the right time?
Dovyman
Dovyman
I'm not sure how detailed you want to get with this.. but here goes anyway, you can strip it down if you like, I wrote a little bit of pseudo-c++:

//time now
float old_time = 0;
float new_time = 0

//position in song
float song_position = 0;

//length of song in quarter notes (arbitrary value)
float song_length = 36;

//info about a single note
typedef _note {
int duration;
int pitch;
keytype key;
} NOTE;

//now create some type of data structure that can take type "NOTE" and fill it up with your song

//is song playing
bool song_playing = true;

//set the iterator for the songs data structure
iter = noteVector.begin();

while(song_playing)
{
//set the current time
old_time = get_time();

//right key was pressed
if(GetKeyPressed() == *iter->key)
HandlePlaying(some parameters here);

//incorrect key pressed
else
{
YouPlayedTheFrickinSongWrong();
song_playing = false
}

//if it hasn't been the notes duration then pause for a bit
if((new_time-old_time)>0)
//pause for already duration minus already elapsed time
Pause(*iter->duration-(new_time-old_time));

//check if the song is over yet
if(song_position == song_length)
{
YouPlayedTheSongRightGoodJob();
song_playing = false;
}

//update song position
song_position += *iter_duration

//get the new time
new_time = get_time();
}

Well that was a bit more long winded than I had anticipated, but I hope it helps.
Basically you need to set it to check every time a note is supposed to be being played to see if the user pressed the right key.


[edited by - dovyman on January 4, 2003 11:40:08 PM]
Pseudo
Pseudo
Wow, that''s a lot of pseudo-code. I think a simple explanation might help. I would do some pre-processing to make a list of all notes sorted by when they should be played. Then for any instance in time, it should be easy to find the notes in the list that should be played (with some margin of error, like 1/16th of a second.). So then you should have a list of current notes (or a single note, you didn''t say) and you can just check if that key is pressed.

To check for key''s pressed on a keyboard, I''d use DirectInput, or GetAsyncKeyState (from the Win32 sdk).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.