I'm very new to this library, I've never made a game before.
This is my unfinished attempt to make a game, the user is supposed to press each button on their keyboard, and they only have 500 millisecond intervals between keys. But I constantly ran into problems, mainly with counting the time (500ms) (and if it passed the time, then it'd stop the game) and listening to the key events.
A lot of stuff is commented out. But right now tht program won't get past the "You have 500ms to press the following button: A" in the game() function.
Can someone please help me adapt to game programming and criticise my code, and also help me see where my problem is at? I'd really appreciate it.
#include "SFML/Graphics.hpp"
#include <string>
void introduction(sf::RenderWindow* main);
void game(sf::Event* button,sf::RenderWindow* window);
void delaythread();
int scankey(sf::RenderWindow* main);
int main()
{
sf::RenderWindow window(sf::VideoMode(245,150),"Alphabetame game",sf::Style::Resize | sf::Style::Close);
window.setVerticalSyncEnabled(true);
window.clear(sf::Color::Blue);
window.display();
introduction(&window);
while(window.isOpen())
{
sf::Event msg;
while(window.pollEvent(msg))
{
switch(msg.type)
{
case sf::Event::Closed:
{
window.close();
}break;
case sf::Event::KeyPressed:
{
switch(msg.key.code)
{
case sf::Keyboard::Space:
{
//game(&msg,&window);//Pass msg object by reference to game() function
game(&msg,&window); //Call game function
/*for(int x=1;x<26;x++)
{
game(&msg,&window);
}*/
}break;
}
}break;
}
}
//CLOSE MESSAGE
}
}
void introduction(sf::RenderWindow* main) //pass main window object by reference
{
main->clear(sf::Color::Blue);
sf::Font font;
font.loadFromFile("C:\\Windows\\Fonts\\Arial.ttf");
sf::Text intro; //Define sf::Text object
intro.setFont(font);
intro.setString("Alphabetame v1.0\nBy http://random.com/\nPress [SPACE] to begin\n");
intro.setCharacterSize(20);
intro.setStyle(sf::Text::Bold);
intro.setColor(sf::Color::White);
main->draw(intro);
main->display();
main=NULL;
}
void game(sf::Event* button,sf::RenderWindow* window)//abcdefghijklmnopqrstuvwxyx - 26
{ //
sf::Text text, keybutton;
sf::Font font;
font.loadFromFile("C:\\Windows\\Fonts\\Arial.ttf");
text.setColor(sf::Color::White);
keybutton.setColor(sf::Color::White);
text.setFont(font);
keybutton.setFont(font);
text.setCharacterSize(18);
keybutton.setCharacterSize(20);
keybutton.setStyle(sf::Text::Bold|sf::Text::Underlined);
std::string message="You have 500 ms to press the\nfollowing button:\n\t";
for(int x=1;x<3;x++)//26 letters.
{
window->clear(sf::Color::Blue);
switch(x)
{
case 1:
{
message.append("A");
text.setString(message);
window->draw(text);
window->display();
if(scankey(window)!=1) //wait get the key the user presses button
{
return;
}
}break;
case 2:
{
message.append("B");
text.setString(message);
window->draw(text);
window->display();
if(scankey(window)!=2)
{
return;
}
}
}
}
}/*
void delaythread(sf::RenderWindow* window)
{
sf::Sleep(sf::milliseconds(500));
window->Clear(sf::Color::Red); //When 500ms is up, make the screen red.
}*/
int scankey(sf::RenderWindow* main)
{
sf::Event event;
// t.launch(main);
for(int dlay=0;main->pollEvent(event)&&dlay<500;dlay++) //While dlay is less than 5000 milliseconds, increase dlay and pop event from event que.
{
if(event.type==sf::Event::KeyPressed)
{
switch(event.key.code)
{
case sf::Keyboard::A:
{
return 1;
} break;
case sf::Keyboard::B:
{
return 2;
}break;
}
}
sf::sleep(sf::milliseconds(1));
}
return 0;
}