Please help out a newb [Archive] - C Board

PDA

View Full Version : Please help out a newb


Marcos
01-26-2003, 08:44 PM
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)


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?

MrWizard
01-26-2003, 11:09 PM
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:


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:

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.

RoD
01-27-2003, 05:12 AM
Seems like DOS to me....console that is, try this example:

Travis Dane
01-27-2003, 05:41 AM
Originally posted by MrWizard

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.

Marcos
01-27-2003, 03:33 PM
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?

RoD
01-27-2003, 04:37 PM
sure, my mistake.

Marcos
01-27-2003, 07:06 PM
Thanks a lot. :)

Stan100
02-02-2003, 01:47 PM
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):D