Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: My first game...Failure.  (Read 3109 times)

0 Members and 1 Guest are viewing this topic.

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
My first game...Failure.
« on: June 21, 2014, 07:43:19 am »
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;
}
 
« Last Edit: June 21, 2014, 07:57:38 am by BeautiCode »

OutlawLee

  • Jr. Member
  • **
  • Posts: 50
  • This is my personal text. Dont read it.
    • View Profile
    • Email
Re: My first game...Failure.
« Reply #1 on: June 21, 2014, 09:52:27 am »
Hey, nice game :) Im fairly new to c++ too, but i think i can point some stuff out.

Firstly i think sf::Sleep is the wrong way to go, though you have wrote some interesting code lol.

Use sf::Time and sf::Clock.

sf::Time passed_time // outside loop
passed_time += clock.restart(); // inside loop

if(passed_time >= sf::seconds(1.f / 2.f))
   //do something


Also i think 'break' statement goes inside the {}.

Passing my reference seems more logical for me, unless you want to take ownership with pointers or something.

and by reference it is: void introduction(sf::RenderWindow& main);
and introduction(window);

What is also important that you should not load the font anew every time.
You load "arial.ttf" twice, thats a waste :)
Loading resources is usually slow, so better do it outside loop, and pass it in.

It may seem "unclean" because of the number of things you have to pass in, but its okay.
To solve this, you should learn object oriented c++ (classes, inheretence..), it really isnt hard.

With function you could try to sort out things a bit better. Like:

// function definitions

int main()
{
     // declare variables
     // initialize variables (load resources)

   
    // create game loop
   // get input - function
   // update logic - function
  // render - function
}


And you maybe want to add this: sf::Window::setKeyRepeatEnabled(bool).
If you set it to false, you want receive multiple events for one e.g. key press.


I am not sure where the problem exactly is, but i think you should rewrite every thing.   
BEST ADVICE: i really really recommend that you take some c++ book and read about classes, inheretence, polymorphism, abstraction, encapsulation, understand those terms and try to apply them. With them you will be able to create more complicated games EASY.
« Last Edit: June 21, 2014, 10:30:27 am by OutlawLee »

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: My first game...Failure.
« Reply #2 on: June 21, 2014, 10:07:13 am »
Thanks :p. Ill definitely check that outlawlee.
Right now I'm on my cell
And Ik OOP but just this first time I didn't want to deal with a bunch of classes abd stuff,  I just wanted to whip up a quick simple game to get better experience.
« Last Edit: June 21, 2014, 07:12:38 pm by BeautiCode »

OutlawLee

  • Jr. Member
  • **
  • Posts: 50
  • This is my personal text. Dont read it.
    • View Profile
    • Email
Re: My first game...Failure.
« Reply #3 on: June 21, 2014, 09:57:07 pm »
Well yea sure man, i would recommend reading some smaller tutorials on the internet. There are like sfml pong game, etc.. Or ultimately the sfml game development book, its really good.

goodluck();

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: My first game...Failure.
« Reply #4 on: June 22, 2014, 01:02:47 am »
Thanks OutLawLee. :)
Btw you got skype?

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: My first game...Failure.
« Reply #5 on: June 23, 2014, 10:18:17 am »
I re-coded the whole thing btw, and got it working for the most part. The only thing Im having trouble with is getting it to call a function when the user is paused for too long.

 

anything