game design question

Pages: 12
@Yemeni Cpluspluser

Your code has an infinite loop. The break statements only break out of the switch, not the while loop.

This is why it is not a good idea to routinely use infinite loops because you can't think of an end condition.

@the prince

Have a go with a bool controlled while loop & switch like in this example:

http://www.cplusplus.com/forum/beginner/104553/2/#msg564228


Hope all goes well.
closed account (30X1hbRD)
Thanks for the link!
@TheIDeasMan
Thanks for the tip, I am just trying to make an example about 2d games.

@the prince
No, I was doing it wrong, and its still wrong, I want to make the @ appear only once in the new location, I am working on that now.
I fixed it now

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
#include <iostream>
#include <conio.h>
#include <cstdlib>
using namespace std;
int main()
{
    char arena [22] [22] = {0};
    int pos_x =0 , pos_y = 0 ;

    arena [pos_x] [pos_y] = '@';

    char answer;
    while(1)
    {


    answer=getch();
    switch(answer)
    {

    case 'd' :
    pos_y += 1;
    break;

    case 'a' :
    pos_y -= 1;
    break;

    case 'w' :
    pos_x -= 1;
    break;

    case 's' :
    pos_x += 1;
    break;


    }

    for(int a=0;a<22;a++)
    {
        for(int b=0;b<22;b++)
            arena[a][b] = ' ';

    cout<<endl;
    }
    system("cls");

    arena [pos_x] [pos_y] = '@';

    for(int a=0;a<22;a++)
    {
        for(int b=0;b<22;b++)
            cout<<arena[a][b];

    cout<<endl;
    }
    }




}


I am still a beginner as well, it takes time.
@Yemeni Cpluspluser

So what is different about your latest code?

Did you read the link I posted?

You should also learn to indent your code, if using an IDE should be easy.
Yes I read it, but there are libraries that I don't know yet.
The last code sets the '@' to the needed pos_x , pos_y, and delete the previous pos_x , pos_y , then output it, making it look like an old MS-DOS game engine.

I wrote the code in the forums first, then I copied it to Code::blocks
If I wrote it in Code::blocks directly, it was going to be readable.
Yemeni Cpluspluser wrote:
Yes I read it, but there are libraries that I don't know yet.


What libraries? - it is only a snippet of pure C++ code. There are no includes - so what do you mean? Maybe the std::cout? I only say that because I am guessing that you may not be used to not having using namespace std; in your code.

Your code still has the infinite loop. If you read my code, you will see how to avoid that.

EDIT:

Yemeni Cpluspluser wrote:
I wrote the code in the forums first, then I copied it to Code::blocks
If I wrote it in Code::blocks directly, it was going to be readable.


Write the code in c::b first - make sure to set your indenting to be 4 spaces (not tabs) so that it displays nicely as code in this forum, when you copy it over.
Last edited on
You mean this one?, I copied the other code before.
Yes I got the idea of your code, by using recursive method with functions, I will try to do that tomorrow.
Good night.
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
// put this before main()
#include <iostream>
void ShowMenu();

int main()
{


// put all this in main()
bool Quit = false;
int MenuSelection = 0;

while (!Quit) {
     ShowMenu();

     std::cin >> MenuSelection;

     switch(MenuSelection) {
          case 1 :

               //call function to do this menu item
               break;

          case 2 :
               //call function to do this menu item
               break;

          case 3 :
               Quit = true;
               std::cout << "Quitting program\n";
               return 0; // omit this line if you execution to continue after while loop
               break;

          default :
                std::cout << "Bad Input - options are 1 or 2 or 3\n";
     }

}
}
// put this after main()
void ShowMenu() {
    std::cout << "CRYPTO GUESS" << std::endl;
    std::cout << "Version 2.3\n" << std::endl;

    std::cout << "Do you want to enter words yourself or have the computer give you words?" << std::endl;
    std::cout << "1) Enter words myself" << std::endl;
    std::cout << "2) Have the computer give me words" << std::endl;
    std::cout << "3) Quit" << std::endl;
}
Yemeni Cpluspluser wrote:
Yes I got the idea of your code, by using recursive method with functions,


It's not recursive.

I need to go as well - it's 07:20 at this end !!
closed account (30X1hbRD)
@yemeni cpluspluser The code now works I had just assumed it was sort of like a python/tron type of thing, leaving a trail is all. But the code works excellently now! (although I will admit, I had some fun making shapes with the last one lol.) Thanks all!
Topic archived. No new replies allowed.
Pages: 12