Raised This Month: $32 Target: $400
 8% 

[Off-Topic] C++ Game


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 11-21-2018 , 08:56   [Off-Topic] C++ Game
Reply With Quote #1

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;

__________________
edon1337 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 11-21-2018 , 09:21   Re: [Off-Topic] C++ Game
Reply With Quote #2

Interesting , I want to try also... you have YouTube link ?

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

Last edited by Bacardi; 11-21-2018 at 10:01.
Bacardi is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 11-21-2018 , 10:26   Re: [Off-Topic] C++ Game
Reply With Quote #3

Quote:
Originally Posted by Bacardi View Post
Interesting , 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.
__________________
edon1337 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 11-21-2018 , 11:17   Re: [Off-Topic] C++ Game
Reply With Quote #4

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.
__________________
klippy is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-21-2018 , 11:25   Re: [Off-Topic] C++ Game
Reply With Quote #5

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)
__________________

Last edited by HamletEagle; 11-21-2018 at 11:28.
HamletEagle is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 11-21-2018 , 11:51   Re: [Off-Topic] C++ Game
Reply With Quote #6

Quote:
Originally Posted by HamletEagle View Post
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;

__________________
edon1337 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-21-2018 , 12:08   Re: [Off-Topic] C++ Game
Reply With Quote #7

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
__________________

Last edited by HamletEagle; 11-21-2018 at 12:09.
HamletEagle is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 11-21-2018 , 13:19   Re: [Off-Topic] C++ Game
Reply With Quote #8

Quote:
Originally Posted by HamletEagle View Post
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.
__________________
edon1337 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-21-2018 , 13:33   Re: [Off-Topic] C++ Game
Reply With Quote #9

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.
__________________
HamletEagle is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 11-21-2018 , 13:37   Re: [Off-Topic] C++ Game
Reply With Quote #10

Quote:
Originally Posted by HamletEagle View Post
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)
__________________
edon1337 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 21:25.


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