These are logic bricks
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!
void processActions() {
switch(map[next_y][next_x]) {
case '~':
foundItem();
break;
case ' ':
case '/':
move();
break;
default: } }
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; }
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.
Code
-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[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; } }
int mapnumber = 0;
char maps[2][40][80] = ...
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?
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: } }
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.
- 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.
- You should also start splitting up stuff to different files, because the big map files will clutter your code a lot :P