Raised This Month: $12 Target: $400
 3% 

[Off-Topic] C++ Game


Post New Thread Reply   
 
Thread Tools Display Modes
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 11-22-2018 , 10:52   Re: [Off-Topic] C++ Game
Reply With Quote #21

Quote:
Originally Posted by KliPPy View Post
You initialize it in main() and pass it to both Setup() and DrawMap(). You pass it to any function that needs to access it.
Thanks, now I get it. What's the difference between int &Score and int *Score?
PHP Code:
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <ctime>
#include <Windows.h>

using namespace std;

const 
int MapX 50;
const 
int MapY 20;

int SnakeX;
int SnakeY;

int FruitX;
int FruitY;

bool gameOver;

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

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

eDirections dir;

void RandomizeFruit( )
{
    
FruitX rand( ) % ( MapX ) + 1;
    
FruitY rand( ) % ( MapY ) + 1;
}

void Setupint &Score )
{
    
srandtime ) );

    
SnakeX MapX 2;
    
SnakeY MapY 2;

    
RandomizeFruit( );

    
Score 0;
}

void DrawMapint &Score )
{
    
system"cls" );

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

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

            
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;
    }
    
cout << "Score: " << Score;
}

void Logicint &Score )
{
    switch( 
dir )
    {
    case 
LEFT:
        {
            
SnakeX--;
            break;
        }

    case 
RIGHT:
        {
            
SnakeX++;
            break;
        }

    case 
UP:
        {
            
SnakeY--;
            break;
        }

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

    if( 
SnakeX == FruitX && SnakeY == FruitY )
    {
        
Score++;
        
RandomizeFruit( );
    }

    if( 
SnakeX MapX )
        
SnakeX 0;

    else if( 
SnakeY MapY )
        
SnakeY 1;

    else if( 
SnakeX )
        
SnakeX MapX 2;

    else if( 
SnakeY )
        
SnakeY MapY 2;
}

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;
            return 
1;
        }
    }

    return 
0;
}

void ShowConsoleCursor(bool showFlag)
{
    
HANDLE out GetStdHandle(STD_OUTPUT_HANDLE);

    
CONSOLE_CURSOR_INFO     cursorInfo;

    
GetConsoleCursorInfo(out, &cursorInfo);
    
cursorInfo.bVisible showFlag// set the cursor visibility
    
SetConsoleCursorInfo(out, &cursorInfo);
}

int main()
{
    
ShowConsoleCursor(false);

    
int Score;
    
Score 0;

    
Setup(Score);
    
DrawMap(Score);
    while (!
gameOver)
    {
        if (
Input() == 1)
        {
            
Logic(Score);
            
DrawMap(Score);
            
Sleep(100);
        }
    }

    return 
0;

__________________

Last edited by edon1337; 11-22-2018 at 10:55.
edon1337 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 11-22-2018 , 11:02   Re: [Off-Topic] C++ Game
Reply With Quote #22

Among some other minor syntactic differences (don't think of it too much for now, or just search if you are interested), the main one is that the reference has to be valid, i.e. can't be nullptr. You don't have to think about or check if a pointer is null when using references, making for better code. If you can use a reference, always use it over a pointer; Underneath they are the same thing.
There's hardly ever a need for raw pointers in modern C++. It's usually only used for performance critical code and implementation details.
__________________

Last edited by klippy; 11-22-2018 at 11:05.
klippy is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 11-22-2018 , 11:13   Re: [Off-Topic] C++ Game
Reply With Quote #23

Quote:
Originally Posted by KliPPy View Post
Among some other minor syntactic differences (don't think of it too much for now, or just search if you are interested), the main one is that the reference has to be valid, i.e. can't be nullptr. You don't have to think about or check if a pointer is null when using references, making for better code. If you can use a reference, always use it over a pointer; Underneath they are the same thing.
There's hardly ever a need for raw pointers in modern C++. It's usually only used for performance critical code and implementation details.
Thanks for the explanation.
__________________
edon1337 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-22-2018 , 12:28   Re: [Off-Topic] C++ Game
Reply With Quote #24

Klippy mentioned a "raw" pointer, so it's worth nothing that you can also use something called smart pointer. A smart pointer is an object that acts as a wrapper around a regular pointer. The main use of this is to avoid memory leaks(leaks caused by not deallocating memory when we are done with it). The object is on the stack and when it gets out of scope it's destructor will also free the memory of the underlying pointer.

Raw pointers are also used for feeling like a badass while writting code that no one will understand(not even the one who wrote it). You know how it goes, write intensive pointer code in 30 minutes, debug it in 30 hours.
On a more serious note, one can do some mind blowing tricks with pointers, but this are good only for experimenting and learning, not using it in production code.
I'd say you should definitely learn to use pointers, even if you are not going to use them a lot in real code, it will give a good understanding of how memory works.
__________________

Last edited by HamletEagle; 11-22-2018 at 12:34.
HamletEagle is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 11-23-2018 , 17:05   Re: [Off-Topic] C++ Game
Reply With Quote #25

Quote:
Originally Posted by HamletEagle View Post
Klippy mentioned a "raw" pointer, so it's worth nothing that you can also use something called smart pointer. A smart pointer is an object that acts as a wrapper around a regular pointer. The main use of this is to avoid memory leaks(leaks caused by not deallocating memory when we are done with it). The object is on the stack and when it gets out of scope it's destructor will also free the memory of the underlying pointer.

Raw pointers are also used for feeling like a badass while writting code that no one will understand(not even the one who wrote it). You know how it goes, write intensive pointer code in 30 minutes, debug it in 30 hours.
On a more serious note, one can do some mind blowing tricks with pointers, but this are good only for experimenting and learning, not using it in production code.
I'd say you should definitely learn to use pointers, even if you are not going to use them a lot in real code, it will give a good understanding of how memory works.
Yeah I'll definitely give pointers a shot. Thanks for the help.
__________________
edon1337 is offline
Reply


Thread Tools
Display Modes

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 12:06.


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