Thread: Please help out a newb

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    118

    Talking Please help out a newb

    I am quite a starter in c++ but I know baisc things like functions, loops, and things like that, but I want to start a simple game, but I have no idea how to say, do something like this (if its possible)

    Code:
    if(  /*player presses up arrow*/ )
    {
        Ypos++;
    }
    and as the Y position increments, the player (a simple character like (char)1 or something) moves up.

    Whow can I do that?
    Why drink and drive when you can smoke and fly?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well, you could have a statement like that, however, incrementing a variable and not doing anything with it is pretty useless. Also it should be noted that from the upper-left corner of the screen the coordinate system is x increasing to right and y increasing down so your code would actually move a player down. Lets say you had a sprite class like this:

    Code:
    class CSprite
    {
      private:
    
        int m_x; // Player x position
        int m_y; // Player y position
    
        int m_width; // Width of sprite
        int m_height; // Height of sprite
    
      public:
    
        CSprite( ) : m_x(0), m_y(0) { }
        CSprite( int _x, int _y ) : m_x(_x), m_y(_y) { }
    
        int GetX( void ) const { return m_x; }
        int GetY( void ) const { return m_y; }
    
        void SetX( int _x ) { m_x = _x; }
        void SetY( int _y ) { m_y = _y; }
    };
    Then you could have an instance of the class say, Player, and manipulate his data accordingly.

    Pseudo-Code:
    Code:
    CSprite Player( 100, 100 );
    
    if DOWN_ARROW PRESSED
      Get player current Y
      Decrement Y value
      Set new value to player
    Then all you have to do is do your screen drawing with the player class x and y position and not have to worry about it except for in your input section.

    I tryed to keep this non-api specific so you could just get the idea of it. Let me know if you are using Win32, OpenGL, DirectX or what and I can help you more.

    Good Luck.

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Seems like DOS to me....console that is, try this example:

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by MrWizard
    Code:
    class CSprite
    {
      private:
    
        int m_x; // Player x position
        int m_y; // Player y position
    
        int m_width; // Width of sprite
        int m_height; // Height of sprite
    
      public:
    
        CSprite( ) : m_x(0), m_y(0) { }
        CSprite( int _x, int _y ) : m_x(_x), m_y(_y) { }
    
        int GetX( void ) const { return m_x; }
        int GetY( void ) const { return m_y; }
    
        void SetX( int _x ) { m_x = _x; }
        void SetY( int _y ) { m_y = _y; }
    };
    Wow, I think he's a little new at this and he said he only knew
    loops if's and all, so that's a little advanced.

    Hm, Has anyone on this board ever tried to make a console
    app 'engine'? That would be a nice solution for people that
    are just starting and want to make something cool.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    118

    Smile Thanks a lot Mr Wizard :)

    I am starting to get the idea, although that is more advanced than I am, I do understand more or less what the class is doing, but I dont understand at all the pseudo code and I have no idea how to draw that in the screen.

    BTW DoD, can you post the "keys.h" file for the caverns game please?
    Last edited by Marcos; 01-27-2003 at 03:51 PM.
    Why drink and drive when you can smoke and fly?

  6. #6
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    sure, my mistake.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Thanks a lot.
    Why drink and drive when you can smoke and fly?

  8. #8
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361

    Btw...

    By the way, what does everyone here consider better?:
    int a=getch()
    if (a==97)
    blahblahblahh

    OR

    INPUT_RECORD InputRecord;
    if (Event.KeyEvent.I.Foget.How.It.Goes==Who.Cares)
    blahblahblah

    I find it much easier to do the first one, but I gues I have to learn if I wanna do Win32 (not that way sickos)
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-22-2008, 11:37 PM
  2. Dogpile the newb!
    By lindy in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 05-23-2008, 08:17 AM
  3. Total newb directx invisable geometry question
    By -pete- in forum Game Programming
    Replies: 5
    Last Post: 08-13-2006, 01:45 PM
  4. Newb C++ Programmer
    By Philandrew in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 08:44 PM
  5. if your not newb you can help me
    By Klinerr1 in forum C++ Programming
    Replies: 6
    Last Post: 05-05-2002, 12:09 AM