AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Off-Topic (https://forums.alliedmods.net/forumdisplay.php?f=15)
-   -   [Off-Topic] C++ Game (https://forums.alliedmods.net/showthread.php?t=312207)

edon1337 11-21-2018 08:56

[Off-Topic] C++ Game
 
Hi,

I was trying to make a simple snake game with C++ but I'm encountering a problem, there's a spamming _ running around the screen without stopping, I tried an already made code from YouTube which is supposed to work but the same happened.

PHP Code:

#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <ctime>

using namespace std;

int MapX 20;
int MapY 20;

int SnakeX;
int SnakeY;

int FruitX;
int FruitY;

int Score;

bool gameOver;

// Map Over-view
/*
012345678911234567892
0: ########################### --> X
1: #                         #
2: #                         #
3: #                         #
4: #                         #
5: #                         #
6: #                         #
7: #                         # Y
8: ###########################*/

enum eDirections
{
    
STOP 0,
    
UP,
    
DOWN,
    
LEFT,
    
RIGHT
};

eDirections dir;

void Setup( )
{
    
srandtime ) );

    
dir STOP;

    
SnakeX MapX 2;
    
SnakeY MapY 2;

    
FruitX rand( ) % MapX;
    
FruitY rand( ) % MapY;

    
Score 0;
}

void DrawMap( )
{
    
system"cls" );

    
int xy;
    for( 
0MapYy++ )
    {
        if( ( 
== || == MapY ) ) // first or last row
        
{
            for( 
0MapXx++ ) // print X 10 times
            
{
                
cout << "#";
            }
        }

        else 
// 1-9 row
        
{
            
cout << "#"// # which goes to the first space of the line

            
int spaces;
            
spaces 0;

            while( 
spaces != MapX // keep putting spaces till we reach the end of the line - 1
            
{
                if( 
spaces == SnakeX && == SnakeY )
                {
                    
cout << "O";
                }

                else if( 
spaces == FruitX && == FruitY )
                {
                    
cout << "F";
                }

                else
                {
                    
cout << " ";
                }
                
spaces++;
            }
            
cout << "#"// finally print # at the end of the line
        
}
        
cout << endl;
    }
}

void Input( )
{
    if( 
_kbhit( ) ) // keyboard key was used
    
{
        switch( 
_getch( ) )
        {
        case 
'a':
            {
                
dir LEFT;
                break;
            }

        case 
'd':
            {
                
dir RIGHT;
                break;
            }

        case 
'w':
            {
                
dir UP;
                break;
            }

        case 
's':
            {
                
dir DOWN;
                break;
            }

        case 
'x':
            {
                
gameOver true;
                break;
            }

        default:
            {
                break;
            }
        }
    }
}

void Logic( )
{
    switch( 
dir )
    {
    case 
LEFT:
        {
            
SnakeX--;
        }

    case 
RIGHT:
        {
            
SnakeX++;
        }

    case 
UP:
        {
            
SnakeY--;
        }

    case 
DOWN:
        {
            
SnakeY++;
        }
    }
}

int main( )
{
    
Setup( );
    while( ! 
gameOver )
    {
        
Input();
        
Logic();
        
DrawMap();
    }

    return 
0;



Bacardi 11-21-2018 09:21

Re: [Off-Topic] C++ Game
 
Interesting :D, I want to try also... you have YouTube link ?

Here one way hide console line
https://stackoverflow.com/questions/...=votes#tab-top

edon1337 11-21-2018 10:26

Re: [Off-Topic] C++ Game
 
Quote:

Originally Posted by Bacardi (Post 2625018)
Interesting :D, I want to try also... you have YouTube link ?

Here one way hide console line
https://stackoverflow.com/questions/...=votes#tab-top

The so called cursor isn't the problem, but it's constant movement around the screen is.

klippy 11-21-2018 11:17

Re: [Off-Topic] C++ Game
 
I don't really understand what's happening. Can you record it? I can't try out the code, there's no conio.h on Linux.

HamletEagle 11-21-2018 11:25

Re: [Off-Topic] C++ Game
 
Not the ideal place to get help with your homework, but w/e.

Now, you have some big crystal clear issues, wondering how you didn't catch them:
1.In Logic() you are not using break; in any switch case, meaning that moving up and left is going to do nothing, while moving down and right is working because of how you ordered the switch cases.
2.But you will quickly notice that if you fix the above mistake once you press one key the cursor will not move only one position, but it will move until it gets out of the board.
3.2 (and also your weird cursor movement) happens because you are always redrawing the map, while redrawing should be done only when you actually got some input.

(also _kbhit is not needed, _getch already waits for input)

edon1337 11-21-2018 11:51

Re: [Off-Topic] C++ Game
 
Quote:

Originally Posted by HamletEagle (Post 2625038)
Not the ideal place to get help with your homework, but w/e.

Now, you have some big crystal clear issues, wondering how you didn't catch them:
1.In Logic() you are not using break; in any switch case, meaning that moving up and left is going to do nothing, while moving down and right is working because of how you ordered the switch cases.
2.But you will quickly notice that if you fix the above mistake once you press one key the cursor will not move only one position, but it will move until it gets out of the board.
3.2 (and also your weird cursor movement) happens because you are always redrawing the map, while redrawing should be done only when you actually got some input.

(also _kbhit is not needed, _getch already waits for input)

Not homework, simply trying to learn new languages. Also most of the scripters here are also experienced C++ programmers.

My apologies, I haven't ever coded in C++ so that's why there's lots of mistakes. I'm going to fix the moving out of the map logic later.

I did as you told me and now it's okay, but I still don't get why it worked for the guy in the video but doesn't work when I use his code.

Video Link: https://www.youtube.com/watch?v=W1e5wO7XR2w&t=446s
Code:
PHP Code:

#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
bool gameOver;
const 
int width 20;
const 
int height 20;
int xyfruitXfruitYscore;
int tailX[100], tailY[100];
int nTail;
enum eDirecton STOP 0LEFTRIGHTUPDOWN};
eDirecton dir;
void Setup()
{
    
gameOver false;
    
dir STOP;
    
width 2;
    
height 2;
    
fruitX rand() % width;
    
fruitY rand() % height;
    
score 0;
}
void Draw()
{
    
system("cls"); //system("clear");
    
for (int i 0width+2i++)
        
cout << "#";
    
cout << endl;
 
    for (
int i 0heighti++)
    {
        for (
int j 0widthj++)
        {
            if (
== 0)
                
cout << "#";
            if (
== && == x)
                
cout << "O";
            else if (
== fruitY && == fruitX)
                
cout << "F";
            else
            {
                
bool print = false;
                for (
int k 0nTailk++)
                {
                    if (
tailX[k] == && tailY[k] == i)
                    {
                        
cout << "o";
                        print = 
true;
                    }
                }
                if (!print)
                    
cout << " ";
            }
                 
 
            if (
== width 1)
                
cout << "#";
        }
        
cout << endl;
    }
 
    for (
int i 0width+2i++)
        
cout << "#";
    
cout << endl;
    
cout << "Score:" << score << endl;
}
void Input()
{
    if (
_kbhit())
    {
        switch (
_getch())
        {
        case 
'a':
            
dir LEFT;
            break;
        case 
'd':
            
dir RIGHT;
            break;
        case 
'w':
            
dir UP;
            break;
        case 
's':
            
dir DOWN;
            break;
        case 
'x':
            
gameOver true;
            break;
        }
    }
}
void Logic()
{
    
int prevX tailX[0];
    
int prevY tailY[0];
    
int prev2Xprev2Y;
    
tailX[0] = x;
    
tailY[0] = y;
    for (
int i 1nTaili++)
    {
        
prev2X tailX[i];
        
prev2Y tailY[i];
        
tailX[i] = prevX;
        
tailY[i] = prevY;
        
prevX prev2X;
        
prevY prev2Y;
    }
    switch (
dir)
    {
    case 
LEFT:
        
x--;
        break;
    case 
RIGHT:
        
x++;
        break;
    case 
UP:
        
y--;
        break;
    case 
DOWN:
        
y++;
        break;
    default:
        break;
    }
    
//if (x > width || x < 0 || y > height || y < 0)
    //  gameOver = true;
    
if (>= width0; else if (0width 1;
    if (
>= height0; else if (0height 1;
 
    for (
int i 0nTaili++)
        if (
tailX[i] == && tailY[i] == y)
            
gameOver true;
 
    if (
== fruitX && == fruitY)
    {
        
score += 10;
        
fruitX rand() % width;
        
fruitY rand() % height;
        
nTail++;
    }
}
int main()
{
    
Setup();
    while (!
gameOver)
    {
        
Draw();
        
Input();
        
Logic();
        
Sleep(10); //sleep(10);
    
}
    return 
0;


Besides from that, I managed to fix the spamming cursor, but now I'm trying to slow down the process of moving by using Sleep() but I'm not sure why it's not slowing it down
PHP Code:

#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <ctime>
#include <Windows.h>

using namespace std;

int MapX 20;
int MapY 20;

int SnakeX;
int SnakeY;

int FruitX;
int FruitY;

int Score;

bool gameOver;

// Map Over-view
/*
012345678911234567892
0: ########################### --> X
1: #                         #
2: #                         #
3: #                         #
4: #                         #
5: #                         #
6: #                         #
7: #                         # Y
8: ###########################*/

enum eDirections
{
    
STOP 0,
    
UP,
    
DOWN,
    
LEFT,
    
RIGHT
};

eDirections dir;

void Setup( )
{
    
srandtime ) );

    
dir STOP;

    
SnakeX MapX 2;
    
SnakeY MapY 2;

    
FruitX rand( ) % MapX;
    
FruitY rand( ) % MapY;

    
Score 0;
}

void DrawMap( )
{
    
system"cls" );

    
int xy;
    for( 
0MapYy++ )
    {
        if( ( 
== || == MapY ) ) // first or last row
        
{
            for( 
0MapXx++ ) // print X 10 times
            
{
                
cout << "#";
            }
        }

        else 
// 1-9 row
        
{
            
cout << "#"// # which goes to the first space of the line

            
int spaces;
            
spaces 0;

            while( 
spaces != MapX // keep putting spaces till we reach the end of the line - 1
            
{
                if( 
spaces == SnakeX && == SnakeY )
                {
                    
cout << "O";
                }

                else if( 
spaces == FruitX && == FruitY )
                {
                    
cout << "F";
                }

                else
                {
                    
cout << " ";
                }
                
spaces++;
            }
            
cout << "#"// finally print # at the end of the line
        
}
        
cout << endl;
    }
}

void Logic( )
{
    switch( 
dir )
    {
    case 
LEFT:
        {
            
SnakeX--;
            break;
        }

    case 
RIGHT:
        {
            
SnakeX++;
            break;
        }

    case 
UP:
        {
            
SnakeY--;
            break;
        }

    case 
DOWN:
        {
            
SnakeY++;
            break;
        }
    }
}

int Input( )
{
    switch( 
_getch( ) )
    {
    case 
'a':
        {
            
dir LEFT;
            break;
        }

    case 
'd':
        {
            
dir RIGHT;
            break;
        }

    case 
'w':
        {
            
dir UP;
            break;
        }

    case 
's':
        {
            
dir DOWN;
            break;
        }

    case 
'x':
        {
            
gameOver true;
            
dir STOP;

            break;
        }
    }
    
Logic();
    return 
1;
}

int main( )
{
    
Setup( );
    while( ! 
gameOver )
    {
        if( 
Input( ) == )
            
DrawMap( );
    }

    return 
0;



HamletEagle 11-21-2018 12:08

Re: [Off-Topic] C++ Game
 
Now your Input() function never returns 0. It is working however because you moved Logic() after _getch which waits for input.

A better approach:
PHP Code:

int main()
{
    
Setup();
    
DrawMap();
    while (!
gameOver)
    {
        if (
Input() == 1)
        {
            
Logic();
            
DrawMap();
            
Sleep(60 60);
        }
    }

    return 
0;


And
PHP Code:

int Input()
{
    switch (
_getch())
    {
        case 
'a':
        {
            
dir LEFT;
            return 
1;
        }

        case 
'd':
        {
            
dir RIGHT;
            return 
1;
        }

        case 
'w':
        {
            
dir UP;
            return 
1;
        }

        case 
's':
        {
            
dir DOWN;
            return 
1;
        }

        case 
'x':
        {
            
gameOver true;
            
dir STOP;

            return 
1;
        }
    }
    
    return 
0;


Sleep takes the input in miliseconds. 10 miliseconds is nothing

edon1337 11-21-2018 13:19

Re: [Off-Topic] C++ Game
 
Quote:

Originally Posted by HamletEagle (Post 2625049)
Now your Input() function never returns 0. It is working however because you moved Logic() after _getch which waits for input.

A better approach:
PHP Code:

int main()
{
    
Setup();
    
DrawMap();
    while (!
gameOver)
    {
        if (
Input() == 1)
        {
            
Logic();
            
DrawMap();
            
Sleep(60 60);
        }
    }

    return 
0;


And
PHP Code:

int Input()
{
    switch (
_getch())
    {
        case 
'a':
        {
            
dir LEFT;
            return 
1;
        }

        case 
'd':
        {
            
dir RIGHT;
            return 
1;
        }

        case 
'w':
        {
            
dir UP;
            return 
1;
        }

        case 
's':
        {
            
dir DOWN;
            return 
1;
        }

        case 
'x':
        {
            
gameOver true;
            
dir STOP;

            return 
1;
        }
    }
    
    return 
0;


Sleep takes the input in miliseconds. 10 miliseconds is nothing

Thanks a bunch! I'm on my way to perfecting it now by adding more logic and options to it.

HamletEagle 11-21-2018 13:33

Re: [Off-Topic] C++ Game
 
As a side note, you should avoid to use global variables when they are not really needed.
You may need them when writing plugins because we are working with external events so we can't pass whatever variables we may need to them, but in most other cases you work only with functions you created so you have control over what arguments they take.

edon1337 11-21-2018 13:37

Re: [Off-Topic] C++ Game
 
Quote:

Originally Posted by HamletEagle (Post 2625062)
As a side note, you should avoid to use global variables when they are not really needed.
You may need them when writing plugins because we are working with external events so we can't pass whatever variables we may need to them, but in most other cases you work only with functions you created so you have control over what arguments they take.

By 'external events' you mean the private functions? I'm sure all of the variables I've created must be global (score, gameOver, coordinates)


All times are GMT -4. The time now is 03:41.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.