Sign in to follow this  
lookyboop

walking c++ game

Recommended Posts

lookyboop    100
i am making a c++ game its a peice of sh@t but i dont really care. you charector is a 0 and i am trying to make him walk through paths like this but without the periods: ..............|.....| _________|.....|________ ..................0 _________.......________ ..............|......| ..............|......| please tell me the best way to do that. i tryed a simple if statement(for example you would type down and you would go down). then i wanted my guy to go back up, so i used a goto statment, but it would work. can anyone help me have him walk(i dont really care the method needed to be used)?

Share this post


Link to post
Share on other sites
Gage64    1235
You can use a 2D array to store the map data, and keep track of the player location with a pair of x, y integers. When the player tries to move, you check to see if it's possible to move to that location (that is, it's not a wall), and if it is, you update the player's location accordingly and reposition it in the map. Then when you draw the map, the player will be drawn at the correct location.

Share this post


Link to post
Share on other sites
speciesUnknown    527
This is the basics of a tile map. You know what tile your character is on, and you know which tiles are around him.

So you give the player the choices N NW W SW S SE E and NE as commands. (or you could accept WAS and D)

Then, you use a function to decide if the character is able to move onto that position.

If he can, you add/subtract to the X or Y position, or both, and update the game again, and draw your character on that position.

If he can't, you do nothing, or possibly print an error message.

What library are you using? For this kind of stuff you can use the command line. Once you get it working why not try SDL or Allegro?
If you choose SDL (MY RECOMMENDATION) I suggest you go to lazy foos tutorials

Also, don't knock yourself by saying what you did is sh@t, its not good for your self esteem... You won't get mocked in For Beginners.

Share this post


Link to post
Share on other sites
BladeStone    100
OOOoooo I've thought about adding a character map like what you've got to my text game, but I plan to move it to 3D so I've not added one of those. Although, I have thought about it and this is what I've done with my map.

I did the two demention array char map[MAP_NUMBER][TILE_NUMBER]; What I did was also have an extra array which int mapWidth[MAP_NUMBER]; // say for example it's 100 wide.

If your map is 100 wide, then the first row was 0-99, and when I went down and down I either + or - map width. Left and right was + or - 1. But the up and down code looked like this: int currentTile += mapWidth[currentMap]; And going up was currentTile -= mapWidth[currentMap];

Although, I would highly recomment actually using a structure (for C, or a class for C++). That way you can have all the details in one spot.

These maps are fun.

Word of advice, my first map stated 'exits' for every tile, and I would have an array of 6 numbers 0 or 1 for true or false. But in my next rewrite, (I've designed but haven't implimented yet) I'm going to instead set terrain passages speed instead. Smooth level land is a 1, wall is a 0, and rocky terrain would be a 2. From your position you only have to check the direction you are going. This format holds less data, and less to process through. You can expand this easier for hard coding unlike 6 exits. You have one variables for each terrain type in your game, like water and air values. This saves you from writing 6 different 0&1 values for each direction for movement type per square. (3 movement types: land, water, air. 3types x 6 exits = 18 1&0 to write per square for the old way.)

I hope this helps!
BladeStone

Share this post


Link to post
Share on other sites
Captain P    1092
Quote:
Original post by lookyboop
please tell me the best way to do that. i tryed a simple if statement(for example you would type down and you would go down). then i wanted my guy to go back up, so i used a goto statment, but it would work. can anyone help me have him walk(i dont really care the method needed to be used)?

Can you show us your code? I think we'll be able to give you some tips then on how to get this working - and on how you can improve your code. Goto, for example, should be avoided because it makes a program hard to follow, which often leads to subtle mistakes.

Oh, and if you want to display some ascii art, you can place it between [code] and [/code] tags. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this