Toribash
Hahaha nice ideia for the dead bird xD

Also what I talking about is this:

These are logic bricks




It's really easy to learn how to use them! Just go check some tutorials and you can do everything with them!
I just started messing around in C++ for fun and after a little while of trying to make something resembling a rogue like I got this.

I know it sucks and it isn't the way you should do it. But considering that's my first ever attempt and I haven't been taught anything game or database related I'm pretty proud of it xD..

The only thing we've been taught how to make is a job estimator program that uses formulas and user input to come up with a correct pricing, and we weren't taught functions or how to call them then so that is just one giant main file.

But yeah- wanted to share this if you know a better way I can go at it. The lever bool is in the wrong place (should rename it to door_passageway or something) it was made so when you walk into the ! you would spawn on the next map. But it went way over my head and I just simply threw it with the stepping on the ~ icon to remind me it's there to be used in the future.

Anywho- the code I've been working so hard on tonight xL Don't laugh too hard.

Code


-----
Oh and yeah I'll check out the logic bricks ^^
Last edited by souldevilj; Feb 13, 2014 at 05:52 AM. Reason: <24 hour edit/bump
That's pretty cool Soul.

I'd change a few things; first change indenting style, looks like you are using a combination of Allman and GNU so it's a bit strange looking, try make everything consistent. For reference, the style I use in this example code is usually called LISP or python style.

I think you should change the way you check for input, because you are repeating the if statements in two places so you can probably factor out the common behavior. You should also keep track of what the player is standing on, so for example you could make a patterned or checkered floor or have special tiles like lava or water or sand or stone or whatever.

Maybe something like
C code:

int next_x, next_y; // The next position the player will move to
char standingOn = ' '; // The tile the player is currently standing on

void processInput() {
if(GetAsyncKeyState(VK_UP)) next_y=y+1;
if(GetAsyncKeyState(VK_LEFT)) next_x=x-1;
if(GetAsyncKeyState(VK_DOWN)) next_y=y-1;
if(GetAsyncKeyState(VK_RIGHT)) next_x=x+1; }

void processActions() {
switch(map[next_y][next_x]) {
case '~': // Item
lever = true;
map[2][74] = '!';
map[next_y][next_x] = ' ';
break; // If you want players to immediately move on to the item's position after activating it, delete this line.
case ' ': // Empty space
case '/': // Door
map[y][x] = standingOn; // So if we were standing on a door, put a door back there
standingOn = map[next_y][next_x];
map[next_y][next_x] = '*';
break;
default: } }
// Cannot perform an action on that tile!


Then in your main loop call processInput(); processActions(); This method is much more scalable and will make it easier to add new tiles. I recommend making things as generic as possible, and factor out everything wherever possible. For example you could change the above to something like;

C code:

void processActions() {
switch(map[next_y][next_x]) {
case '~':
foundItem();
break;
case ' ':
case '/':
move();
break;
default: } }


This further simplifies your inner values and makes it easier to modify things. You can also do something like

C code:

int map1height=10,map2height=20;

char (*currentMap)[80]; // 80 = max width of any map
int currentHeight;

int main() {
currentMap = map;
currentHeight = map1height;

while(!GetAsyncKeyState(VK_ESCAPE)) {
system("cls");

for(int i=0; i<currentHeight; i++) {
cout << currentMap[display] << endl; }

cout << endl << endl << "- Arrow keys to move" << endl;
cout << "- Escape key to exit" << endl; // you should probably put these 2 strings in to a var so you can display other info if you want here

if (lever) {
lever = false;
currentMap = map2;
currentHeight = map2height; }

system("pause>nul");

processInput();
processActions(); }

cout << "\nGame Exited";
return 0; }


All in all very cool so far.
Last edited by ImmortalPig; Feb 13, 2014 at 12:26 PM.
<Faint> the rules have been stated quite clearly 3 times now from high staff
This might sound weird but I'm half asleep atm, so I'll just reply to the first part that I can process with my brain at the moment >.<

Um how would I change my indenting style?
So far I've just had it how I am comfortable reading it, but if there is a standard/better way then I'd much rather force myself into that whilst I can to stop myself having bad habits.

Later on after I've woken up a bit I'll try adapt my code to the way you've set it up ^^
Thanks so much Immortal, it means a lot to hear that x)
Read this: http://en.wikipedia.org/wiki/Indent_style#Allman_style

Allman is the most commonly used style for textbooks, so you will probably see it around a lot.

The main thing though is just keeping your tabbing consistent - some people indent by 2 spaces, 4, 8, whatever, just keep it consistent. Make sure to keep consistent when you tab too, eg in player_movement() sometimes you indent your brackets in an if statement, and sometimes you don't, make it all the same.
<Faint> the rules have been stated quite clearly 3 times now from high staff
Originally Posted by ImmortalPig View Post
Read this: http://en.wikipedia.org/wiki/Indent_style#Allman_style

Allman is the most commonly used style for textbooks, so you will probably see it around a lot.

The main thing though is just keeping your tabbing consistent - some people indent by 2 spaces, 4, 8, whatever, just keep it consistent. Make sure to keep consistent when you tab too, eg in player_movement() sometimes you indent your brackets in an if statement, and sometimes you don't, make it all the same.

Oh I see, I guess you looked at my code and thought that my indent style was more similar to the Allman Style, I'll try my best to keep it consistent :')
I was at College when I tried to adapt the code, then my computer got a DPR_WATCHDOG_(something) error and restarted itself and updated itself in college. Guess it forced an update since I rarely update Win 8.

It didn't save my work so I'm going to go back and try adapt it now ^^

As for the currentMap part and the displaying of the map (btw I love how you made it so easy for me to change maps )
I'm trying to wrap my head around it as I read the code and try to process it and see how I change my code to match, I get it 99%. Perhaps it's just a case of me needing to code with those kinds of things more to not feel so uncomfortable trying to use it in a way I understand.

-edit I understand now, mostly. Could you explain what char something (* ); something does? Mainly what the * does :') -

Oh I have a question about the variables next_x and next_y.
When you press a key due to the if statements it changes the value of y and x. So if I approached the item from above by pressing down thus making next_y = that space.
Will the next_x also become a ' ' (blank space) due to the map[next_y][next_x] = ' '; line of code?

I'll post the adapted code here when I'm done ^^
-----
And now I'm stumped ^^

89 16 CUsers\Joseph\Documents\Projects\C++ Stuff\BasicC++game.cpp [Error] cannot convert 'char [20][40]' to 'char (*)[80]' in assignment

Do I need to create my maps differently? :o

And heres the code so far :')

Code

Last edited by souldevilj; Feb 15, 2014 at 09:32 AM. Reason: <24 hour edit/bump
I didn't actually test anything I wrote it was just example, so I probably made some mistakes lol.

Originally Posted by souldevilj View Post
-edit I understand now, mostly. Could you explain what char something (* ); something does? Mainly what the * does :') -

89 16 CUsers\Joseph\Documents\Projects\C++ Stuff\BasicC++game.cpp [Error] cannot convert 'char [20][40]' to 'char (*)[80]' in assignment

Do I need to create my maps differently? :o

char (*currentMap)[80] means "a variable called currentMap which contains the address of a char [80]"

Yeah I totally did it wrong, you can't use that because the arrays are different lengths, that's why you get that error :x
There's a few ways to do it, but all of them are more complex...

You could make currentMap an array of max size then keep track of the length and width, and copy the contents in to the map when you want to swap.
c code:

char currentMap[100][100];
int width,height;

void loadMap1() {
width=map1width;
height=map1height;
for(int i=0;i<height;i++)
for(int j=0;j<width;j++)
currentMap[i][j]=map1[i][j]; }

// And when you want to display your map to the screen do something like;
void displayMap() {
for(int i=0;i<height;i++) {
for(int j=0;j<width;j++)
cout << currentMap[i][j];
cout <<endl; } }


You could also do a memcpy or whatever else kind of thing. If you wanted to you could also put all your maps in to an array (!!!) and then just switch when you want to;
c code:

int mapnumber = 0;
char maps[2][40][80] = ...

In the long run you may EVENTUALLY want to make a map class, but from the looks of things you don't know object oriented programming, so it will just be a bother and make more problems :P


Originally Posted by souldevilj View Post
Oh I have a question about the variables next_x and next_y.
When you press a key due to the if statements it changes the value of y and x. So if I approached the item from above by pressing down thus making next_y = that space.
Will the next_x also become a ' ' (blank space) due to the map[next_y][next_x] = ' '; line of code?

Well I'll answer your question in a second, but first I realized I made a mistake in processActions function, I need to reset x and y at the end;
c code:

void processActions() {
switch(map[next_y][next_x]) {
case '~': // Item
lever = true;
map[2][74] = '!';
map[next_y][next_x] = ' ';
break; // If you want players to immediately move on to the item's position after activating it, delete this line.
case ' ': // Empty space
case '/': // Door
map[y][x] = standingOn; // So if we were standing on a door, put a door back there
standingOn = map[next_y][next_x];
map[next_y][next_x] = '*';
x=next_x; // Update x and y positions
y=next_y;
break;
default: } }


Now your question, if you press only the down key then next_y = y - 1, and but your x position will be unchanged so next_x = x, so it will change the tile directly below you.

If you hold right and down then next_y = y - 1 and next_x = x+1 so it will move/grab item from the square to the bottom right of you diagonally.

Does that make sense? Your current position is (y,x) and you will activate square (next_y,next_x).



By the way, I never coded c++ very much before, but I know it's not a very good language at all, and a more modern language like python, java, ruby, etc would be better. A lot of things that you might have to learn to program c++ are just irrelevant in modern programming. For example in c you must always say "int i = 1", but in python you can just say "i = 1" because typing is not important. Other things like pointer don't exist in modern languages, you can just say "i = j" and if they are objects then it will be a pointer. I'd recommend checking out python because it's a lot better in a lot of ways.
Last edited by ImmortalPig; Feb 15, 2014 at 12:38 PM.
<Faint> the rules have been stated quite clearly 3 times now from high staff
I see, I'm going to see if this will repair my movement.
The movement I made from a bunch of if statements. Is that okay the way it is? -check, yeah it is-

Haha yes I did not want to learn C++ but because of the way the world works it's one of those "you must learn it you don't get a choice kiddo" kinda things.
I wanted to learn Java at first, I even took some courses and got pretty okay at it. As for object orientated programming, I accidentally first started with it a long time ago. So I think I'm more familiar with it than I am with procedural programming xP

Let you know what happens after I add the fixes
-----
Yay! The resetting of the x and y positions fixed the movement completely! everything runs just as it did before I edited it, only with the much more efficient and scale able layout you've bestowed upon me

Going to attempt to fix this map issue
As for the explanation, yeah I get it now thanks ^^ It's probably because I read it without the values getting reset, so in my head I thought if an item was on the side of the character when he went to pick up an item above him, he may end up changing the side space into a blank space also, but with the resetting of the x and y coordinates. This shouldn't happen

For the first piece of code about displaying the map:

From what I can tell with my brain, you've basically made it so all of my map is in 1 char but you use the first loop and the 2nd loop to have the ranges of each map. For example.

Map 1 could be from range 1-10 i being 1 and j being 10.
but when you want map2 you could then display I being 11 and J being 30?.. Kinda?..
xD

I'm still unsure how to pull it off more over I don't know how I'd update the x and y values so I could continue to move on the 2nd map x(

I'm quite interested in that array option though, seems like a cleaner and more scale able way of doing things. I'd love to learn how to use an array for storing maps. So far I've only used an array for storing 3 different strings in my Job Estimator program, calling the position 0 for "Economy Paint" position 1 for "Standard Paint" and position 2 for "Luxury Paint".

That's pretty much the only time I've used arrays so I do know 'how' to use them I'm just not comfortable about making my own and placing it within my code
Last edited by souldevilj; Feb 15, 2014 at 01:04 PM. Reason: <24 hour edit/bump
Well the way I did it on the two for loops would just be to copy the map you want in to the map variable then just keep rendering the map.

The way you said is possible, and some games do use it, but I don't think it's very good.

How to organise the maps themselves (in array, in seprate variables, whatever), is a good question and there's many solutions :P You may eventually want to create an object or class for each, I don't think making an array of maps is the best idea because there's a lot of information (like which maps connect to which) that you may need to keep track of too.

You should also start splitting up stuff to different files, because the big map files will clutter your code a lot :P
<Faint> the rules have been stated quite clearly 3 times now from high staff
Originally Posted by ImmortalPig View Post
  • Well the way I did it on the two for loops would just be to copy the map you want in to the map variable then just keep rendering the map.
How would I change the map that is being rendered ' Might take a whack at it in a moment myself and see if I can sort it. Should be simple enough. Just worried about positioning the player in the next map.
  • You should also start splitting up stuff to different files, because the big map files will clutter your code a lot :P

Yeah.. I don't have any idea on how to accomplish either of those. :L And despite asking my teacher about how to grab another file and use the functions stored in it. He wouldn't give me a straight forward answer.

My guess is #include <filepath> or something.