Thread: A few questions in C++

  1. #1
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33

    A few questions in C++

    Hello, I'm new around here.
    I started learning C++ from a book about two weeks ago. I've also learned Pascal (at school) so I was familiar with some of C++ aspects, such as loops, if statements, etc...
    I'm using Microsot visual C++ and i'm not programming in MFC or something like that, but in a dos console.
    Anyway, I started making a simple (classic) game, in which the user controls a spacecraft, and above of him there are aliens that try to kill him. Of course I don't use any graphics becuase I don't know how. Anyway, my questions are:
    1. How can I tell the compiler to go to certain x,y coordinates? (I want to "print" the spacecraft in a simple letter, and it has to be in the bottom of the window, in the center).
    2. How can the compiler check if the user pressed a key, such as space, left, right...?
    3. I heard from a friend that, in order to make the aliens move, I need to clear the screen and print them again, after they moved. How do I do that?
    4. Most important, I want to make the screen size constant, becuase I don't want the user to change it. How do I do that?

    Thank you

  2. #2
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33
    Well, I thought it wouldn't be a problem to answer all these questions, after all I don't want you to make the game for me.
    The problem is, I can't keep going unless I get these questions answered.
    another problem is that i'm not asking about specific topics that are the chapter of a book. I'm not asking, for example, about pointers, what it is, what it does, how to use, etc...
    I'm asking about things that are hard to find in the internet, and thats why I went to that forum.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Platform specific, but there is a well stocked set of console API's that you can work with.

    http://msdn.microsoft.com/library/de..._functions.asp

    You might be able to lock the size of the console by having a set size for your console screen, and you can get notifications of it being changed and then change it back. THings you can look at if you're still motivated after Salem's post (kinda harsh)

    SetConsoleWindowInfo
    SetConsoleCursorPosition

    SetConsoleMode/ENABLE_WINDOW_INPUT
    ReadConsoleInput/INPUT_RECORD

    You can read arrow keys and get window sizing notifications from ReadConsoleInput. Change the window size to what you want with SetConsoleWindowInfo, and uh, ya.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    It will depend on the system you want to run your game in. If you want portability the answer could be a library like PDCurses that allows you to code for the console with the functionality you are after. If strictly windows you can either go with PDCurses, or choose something more specific like the Conio or the WinBGIm library. And you can even use windows.h and console.h supplied with most compilers for windows.

    As for the keyboard you can use the normal C/C++ input functions defined in cstdio and possibly those in iostream.

    You need to learn about user input/output rom the many tutorials you can find on the web.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I started making a simple (classic) game, in which the user controls a spacecraft, and above of him there are aliens that try to kill him.
    Have you written any piece of code for your game? If so, send it here.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33
    Code:
    #include <iostream>
    using namespace std;
    void display_aliens();
    int main()
    {
    	int aliens[10][10] = {{0}, {0}}; // The array elements' number hold for alien coordinates
    	// Whereas 0 means they are alive, 1 means they are dead
    	int spacecraft[12] = {0}; // Same, array elements' number hold for spacecraft x coordinate
    	display_aliens(); // Display aliens
    	return 0;
    }
    void display_aliens()
    {
    	int i,j;
    	for (i=0; i<10; i++)
    	{
    		cout << "\n";
    		for (j=0; j<10; j++)
    			cout << "Y "; // Display aliens as 'Y's
    	}
    }
    It is important to mention I don't know classes at all.
    Last edited by ThWolf; 08-19-2006 at 01:32 PM.

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I started learning C++ from a book about two weeks ago.
    Why don't keep on with the book? I think this code will put you into trouble.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33
    Well, I think that making a simple game will help me learn a bit. In addition, reading and reading non-stop is boring, I just have to do something. I keep reading from the book, anyway.
    Tonto and Mario F., I didn't really understand you guys.
    Thank you for informing me about the libraries, but I don't know what the functions are.

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    14
    Quote Originally Posted by ThWolf
    Well, I think that making a simple game will help me learn a bit. In addition, reading and reading non-stop is boring, I just have to do something.
    The problem might be that you choose a really hard game to start with. Why don't you try to do a pick a path kind of game? Their easier, can get very big but are playable after a very small amout of time. For example,

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void startWizardQuest();
    void startKnightQuest();
    
    string job;
    string name;
    
    int main(void) {
        cout << "Welcome to fantasy land\n";
        cout << "What is your name? ";
        
        cin >> name;
        cout << "do you want to be a wizard or knight (w/k): ";
        char answer;
        cin >> answer;
        if (answer == 'w') {
            cout << "you are a wizard\n";
            job = "wizard";
        } else {
            cout << "you are a knight!\n";
            job = "knight";
        }
        
        if (job == "wizard") {
            startWizardQuest();
        } else {
            startKnightQuest();
        }
        cout << "press enter to quit";
        cin.get(); cin.get();
    	return 0;
    }
    
    void startWizardQuest() {
        cout << "\"Hello " << name << "\", said the archmage.";
        cout << " \"Do you want to practice alchemy or mysticism?\" (a/m)";
        char choice;
        cin >> choice;
        if (choice == 'a') {
            cout << "Alchemy is boring!!!\n";
            // more stuff
        } else {
            cout << "Mysticism is boring!!!\n";
            // more stuff
        }
        cout << "ahh well you lose...\n";
    }
    void startKnightQuest() {
        cout << "Knights are cool. You win!!!!\n";
    }

  10. #10
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33
    Thanks for putting the time for writing this code, but those games are pretty boring and will teach me nothing, I'm certain of that, especially due to the fact that I've learned Pascal for two years in school.
    Anyway, I will keep learning through my book, but I really hope I'll get these questions answered.

    Edit:
    BTW, in the book I learned that in order to input a string, you need a char array. Never thought that you can do "string name", for example.
    Last edited by ThWolf; 08-20-2006 at 07:09 AM.

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by ThWolf
    Well, I think that making a simple game will help me learn a bit. In addition, reading and reading non-stop is boring, I just have to do something. I keep reading from the book, anyway.
    Which is fine, however you may want to try things differently. Instead of trying to write any practical application like a complex game, you may want to try instead just to write small snpipets of code that address the issue you are studying at that time so that you can better understand the concepts. The reason I suggest this is exactly because of what you are already experiencing; You are putting the wagon in fron of the horses and are having difficulties putting into code more complex concepts that you haven't studied yet.

    Quote Originally Posted by ThWolf
    Tonto and Mario F., I didn't really understand you guys.
    Thank you for informing me about the libraries, but I don't know what the functions are.
    You would need to read those libraries documentation to know how to use them. You would of course also have to install them. Once you do that and start trying to use them it will be easier to come back here with more specific questions on how to use them. Although I really suggest you take my advice above in consideration. Try to first understand the programming language. You will see that most of the doubts you have now will be answered by your own study.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33
    Good idea Mario F, I guess you're right.
    As much as I want to make that game, it would be a good choice to keep studying a bit more

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Well, ThWolf there is one thing I would take note of, writing a game like to one that Monkeymagic does teach you something whether you know it or not... because how many different ways are there to write that same game? more than you can count I assure you. Also, you become more familiar with the functions and flow of C++. and I also figure youll say you know all this because you learned pascal for 2 years, but C++ is a far more advanced and useful language than pascal, and much much different. I am really not trying to be rude, but recently it seems alot of people on these boards ask questions, then they return rude replies to the answers they get, just because they arnt the answers they expected, and I think it is sad, because these good programmers are taking time out of thier day to help you, should you not return some courtesy? sorry if this post is off-topic

  14. #14
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I thought I was being pretty specific, I gave a little list of what functions you'd probably use. If you want more help, you should search for it, whether you do go for the Windows console API's or some other console library. I am not sure whether you could lock the console size with another console library, but I have not looked very hard.

  15. #15
    Registered User
    Join Date
    Aug 2006
    Location
    Israel
    Posts
    33
    Quote Originally Posted by Raigne
    Well, ThWolf there is one thing I would take note of, writing a game like to one that Monkeymagic does teach you something whether you know it or not... because how many different ways are there to write that same game? more than you can count I assure you. Also, you become more familiar with the functions and flow of C++. and I also figure youll say you know all this because you learned pascal for 2 years, but C++ is a far more advanced and useful language than pascal, and much much different. I am really not trying to be rude, but recently it seems alot of people on these boards ask questions, then they return rude replies to the answers they get, just because they arnt the answers they expected, and I think it is sad, because these good programmers are taking time out of thier day to help you, should you not return some courtesy? sorry if this post is off-topic
    Ahmmm I don't remember myself saying something like "Damn you why didn't you answer my questions?!!?!?".
    I thank everyone that takes some of his time to help me, and I don't recall myself being rude.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM