"moving strings"
Apr 17, 2013 at 9:19pm UTC
Hello, been trying to get this code to work, it's supposed to move the '@' sign up and down with the arrow keys, when in fact it does nothing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
char Map[10][30] =
{
{"###################" },
{"# #" },
{"# #" },
{"# #" },
{"# #" },
{"#@ #" },
{"###################" }
};
bool GameStoped = false ;
int main()
{
//pointers();
while (GameStoped == false ) {
system("cls" );
for (int i = 0; i < 10; ++i) {
cout << Map[i] << endl;
}break ;
for (int y = 0; y < 10; ++y) {
for (int j = 0; j < 10; ++j) {
switch (Map[y][j]) {
case '@' :
{
if (GetAsyncKeyState(VK_UP) != 0) {
int y2 = (y-1);
switch (Map[y2][j]) {
case ' ' :
{
Map[y][j] = ' ' ;
y-=1;
Map[y2][j] = '@' ;
}break ;
}
}
if (GetAsyncKeyState(VK_DOWN) != 0) {
int y2 = (y+1);
switch (Map[y2][j]) {
case ' ' :
{
Map[y][j] = ' ' ;
y+=1;
Map[y2][j] = '@' ;
}break ;
}
}
}break ;
} Sleep(10000);
}
}
}
cin.get();
return 0;
}
Apr 18, 2013 at 2:17pm UTC
Bump. You don't have to re write/correct the code, I only require a reason why it's not working.
Apr 18, 2013 at 2:28pm UTC
It's not working due to the break ;
on line 24
Apr 18, 2013 at 2:35pm UTC
reply clashed.
Apr 18, 2013 at 2:39pm UTC
Ok thanks, the reason why I added that break was because it was basically loading the map constantly and flickering (really annoying) How can I solve this? Maybe a bool saying if the map is loaded?
EDIT: tried this:
1 2 3 4 5 6
if (MapLoaded == false ) {
for (int i = 0; i < 10; ++i) {
cout << Map[i] << endl;
}
MapLoaded = true ;
}
But then the map doesn't load at all.
Last edited on Apr 18, 2013 at 2:44pm UTC
Apr 18, 2013 at 2:49pm UTC
Maybe a bool saying if the map is loaded?
No, a bool that says
map_changed
(or so).
Set it initially to true and then when you indeed change it (like after
GetAsyncKeyState()
):
1 2 3 4 5 6 7
if (map_changed)
{
system("cls" );
for (int i = 0; i < 10; ++i) {
cout << Map[i] << endl;
}break ;
}
Apr 18, 2013 at 2:57pm UTC
I've used your idea, but it's still flickers :/
this is the code(note, I haven't changed the name as of this moment. I wanted to see if works first.)
1 2 3 4 5 6 7 8
if (MapLoaded == true ) {
system("cls" );
for (int i = 0; i < 10; ++i) {
cout << Map[i] << endl;
}
//MapLoaded = false;
}
I first tried putting it to false (it's finished updating) then setting it to true in the key detection but that didn't work.
I know what's doing .. it's obviously the loop (while)
Last edited on Apr 18, 2013 at 3:08pm UTC
Apr 18, 2013 at 3:04pm UTC
yes sorry, I forgot line 6. It is required so remove the comments.
Set it to true after Map[y2][j] = '@' ;
Apr 18, 2013 at 3:15pm UTC
That's what i'm doing :/ It very annoying with the flickering.
This is how it is in my code:
1 2 3 4 5 6 7 8
if (MapLoaded == true ) {
system("cls" );
for (int i = 0; i < 10; ++i) {
cout << Map[i] << endl;
}
MapLoaded = false ;
}
After button detect:
1 2 3 4 5 6 7
case ' ' :
{
Map[y][j] = ' ' ;
y-=1;
Map[y2][j] = '@' ;
MapLoaded = true ;
}break ;
Apr 18, 2013 at 6:39pm UTC
this is because you are painting the whole screen. If you can device a trick to just change specific characters on the screen then you would be good.
Topic archived. No new replies allowed.