Author Topic: Need help from C++ coders.  (Read 636 times)

So i'm making an ASCII rogue game, and it's in C++ coded in Visual Studio 2010. There are boundaries, and there is an @ sign (the player). You can move in all directions (up,left,down,right). You cannot pass the boundaries though, because they're boundaries :I

The problem is, the player moves normally, only one space, when moving up or left; but when he moves down or right, he goes all the way to the bottom of the screen, or to to the right of the screen, without passing the boundaries thankfully. I only want him to move one space right and down like he does when moving up or left. I don't know how to fix this, so can some C++ coders help me?

Code: [Select]
#include <iostream>
#include <Windows.h>
#include <ctime>

using namespace std;

char Map[10][20] = {"###################",
                                        "#                 #",
                                        "#                 #",
                                        "#                 #",
                                        "#                 #",
                                        "#                 #",
                                        "#   @             #",
                                        "#                 #",
                                        "#                 #",
                                        "###################"};

bool running = true;
int gametick = 100;

int main() {
        while(running) {
                system("cls");

                for(int i = 0; i < 10; i ++) {
                        cout << Map[i] << endl;
                }

                for(int y = 0; y < 10; y ++) {
                        for(int x = 0; x < 20; x++) {
                                switch(Map[y][x]) {
                                        case '@':
                                                if(GetAsyncKeyState(VK_UP)) {
                                                        if(Map[y-1][x] != '#' && Map[y-1][x] != '%') {
                                                                Map[y][x] = ' ';
                                                                Map[y-1][x] = '@';
                                                        }
                                                }
                                                else if(GetAsyncKeyState(VK_LEFT)) {
                                                        if(Map[y][x-1] != '#' && Map[y][x-1] != '%') {
                                                                Map[y][x] = ' ';
                                                                Map[y][x-1] = '@';
                                                        }
                                                }
                                                else if(GetAsyncKeyState(VK_DOWN)) {
                                                        if(Map[y+1][x] != '#' && Map[y+1][x] != '%') {
                                                                Map[y][x] = ' ';
                                                                Map[y+1][x] = '@';
                                                        }
                                                }
                                                else if(GetAsyncKeyState(VK_RIGHT)) {
                                                        if(Map[y][x+1] != '#' && Map[y][x+1] != '%') {
                                                                Map[y][x] = ' ';
                                                                Map[y][x+1] = '@';
                                                        }
                                                }
                                }
                        }
                }
                Sleep(gametick);
        }
}

I should learn C++ because I did something similar in darkBASIC
I did it in a really weird and most likely inefficient way, but it was a good exercise

I don't know C++ but I can read it. It seems fine to me.

Is there anything capable of making an if statement repeat itself until it is not fulfilled without going to the next tick?

If you are going to ask for C++ help PM me for a link to the c++ forum site. I don't want to post here I might get banned.

my guess is that because of the way your loop iterates, the next cell on the axis will always be the cell that it moved to, and the key is still considered down, so it will just move as far as it could.

perhaps you should record if the player already moved that tick?

What otto said, either have it break the loops after it's finished moving or store the position of the @ so you don't have to loop through the entire thing every time.

If you are going to ask for C++ help PM me for a link to the c++ forum site. I don't want to post here I might get banned.
Why would you be banned?


If you are going to ask for C++ help PM me for a link to the c++ forum site. I don't want to post here I might get banned.
wut

Forum advertising I guess.
Yeah, that rule only applies to people actually spamming it.
Plenty of people have posted links to their own sites (me included) and not been banned for it.


As for OP, bluntly put you're doing that completly wrong, but you're new, so it's understandable.
I'd store the player as two intergers as such:
Code: [Select]
int playerX = 5;
int playerY = 5;
And each of the # tiles as integers in an array:
Code: [Select]
int numTiles = 0;
int tileX[100];
int tileY[100];

void addTile(int x,int y)
{
   tileX[numTiles] = x;
   tileY[numTiles] = y;
   numTiles++;
}
And then generate the map dynamically.
Here are some base functions:
Code: [Select]
char map[20][20];

void clearScreen()
{
   for(int x =0; x<20; x++)
   for(int y =0; y<20; y++)
   map[x][y] = " ";
}

void showScreen()
{
   for(int y = 0; y<20; y++)
   {
      string line = "";
      for(int x = 0; x<20; x++)
      {
         line = line + map[x][y];
      }
      cout<<line<<"\n";
   }
}

void placeObject(int x,int y,string char)
{
   map[x][y] = char;
}

Then you'd have a main loop like this:
(only the graphics part, you'd still need to add in the input and waiting and such)
Code: [Select]
while(running)
{
   clearScreen();
   placeObject(playerX,playerY,"@");
   for(int a = 0; a<numTimes; a++)
      placeObject(tileX[a],tileY[a],"#");
   showScreen();
}

Granted this may or may not be as efficent, but it'll get you used to programming for actual graphics.


(sorry about no indents, can't use tabs on the forum)
« Last Edit: May 27, 2013, 04:25:47 PM by DrenDran »

Yeah, that rule only applies to people actually spamming it.
Plenty of people have posted links to their own sites (me included) and not been banned for it.
Oh, ok. cplusplus.com