C++ Tutorial 18 - Simple Snake Game (Part 1)

preview_player
Показать описание
C++ Tutorial 18 - Simple Snake Game (Part 1)

Welcome to my simple game tutorial on C++. In this tutorial i will going to show you how to make a fun snake game.

We will structure the code of the game and do some basic drawing in the console.

Source code:
Download link for visual studio 2012 express:

Great C++ books that I recommend for beginners:
C++ Without Fear: A Beginner's Guide That Makes You Feel Smart (3rd Edition)
C++ Primer Plus (6th Edition) (Developer's Library)
Programming: Principles and Practice Using C++ (2nd Edition)

If you have any questions I'll be glad to answer, please leave a comment on the video.

Thanks for watching and please subscribe.
Рекомендации по теме
Комментарии
Автор

Tip for all new guys that are here. If you highlight a line of code, hold alt button and use Up/Down arrow keys to move the entire line

dragonturd
Автор

"So if we run our programm we get something that's..not good...."

Bro, i feel you so much

pRsooeem
Автор

easy to follow can recreate the code while the video is running. No useless bullshit just straight to the point, i love it. You earned yourself a sub !

andtpfack
Автор

tbh, im just here to see how the experts do it their own way, i'm a begginer at C++ and prolly noob.
but this video motivates me to learn and practice harder (since i'm self taught), now i realize i have a loooong way to go. and it makes me excited. 🔥

mr.mowgee
Автор

I really appreciate this.
I know this tutorial is very old by now, but as someone who is just getting into C++ recently this has been very enlightening to me.
Thank you!

dreamhollow
Автор

if c++ doesnt recognize rand include this: #include <cstdlib>

theencube
Автор

I haven't worked with game development in a long time. Thank you for making this video, even though it was over six years ago.
More than anything else, I liked that you assumed we knew the basics of programming. Explaining the basics wastes too much time.

cgme
Автор

This is going to be my first year project (and my first ever game) hopefully it works out. Thanks!

PaulBlxck
Автор

Adding 2 units in the for loops to make the first and the last line of the map could be replaced with the <= in the for conditions.
Do this also in the j for loop

rexmagotia
Автор

this is the code #include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
using namespace std;
bool gameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail;
enum eDirecton { STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirecton dir;
void Setup()
{
gameOver = false;
dir = STOP;
x = width / 2;
y = height / 2;
fruitX = rand() % width;
fruitY = rand() % height;
score = 0;
}
void Draw()
{
system("cls"); //system("clear");
for (int i = 0; i < width+2; i++)
cout << "#";
cout << endl;

for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0)
cout << "#";
if (i == y && j == x)
cout << "O";
else if (i == fruitY && j == fruitX)
cout << "F";
else
{
bool print = false;
for (int k = 0; k < nTail; k++)
{
if (tailX[k] == j && tailY[k] == i)
{
cout << "o";
print = true;
}
}
if (!print)
cout << " ";
}


if (j == width - 1)
cout << "#";
}
cout << endl;
}

for (int i = 0; i < width+2; i++)
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 prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for (int i = 1; i < nTail; i++)
{
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 (x >= width) x = 0; else if (x < 0) x = width - 1;
if (y >= height) y = 0; else if (y < 0) y = height - 1;

for (int i = 0; i < nTail; i++)
if (tailX[i] == x && tailY[i] == y)
gameOver = true;

if (x == fruitX && y == fruitY)
{
score += 10;
fruitX = rand() % width;
fruitY = rand() % height;
nTail++;
}
}
int main()
{
Setup();
while (!gameOver)
{
Draw();
Input();
Logic();
Sleep(10); //sleep(10);
}
return 0;
}#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
using namespace std;
bool gameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail;
enum eDirecton { STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirecton dir;
void Setup()
{
gameOver = false;
dir = STOP;
x = width / 2;
y = height / 2;
fruitX = rand() % width;
fruitY = rand() % height;
score = 0;
}
void Draw()
{
system("cls"); //system("clear");
for (int i = 0; i < width+2; i++)
cout << "#";
cout << endl;

for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0)
cout << "#";
if (i == y && j == x)
cout << "O";
else if (i == fruitY && j == fruitX)
cout << "F";
else
{
bool print = false;
for (int k = 0; k < nTail; k++)
{
if (tailX[k] == j && tailY[k] == i)
{
cout << "o";
print = true;
}
}
if (!print)
cout << " ";
}


if (j == width - 1)
cout << "#";
}
cout << endl;
}

for (int i = 0; i < width+2; i++)
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 prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for (int i = 1; i < nTail; i++)
{
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 (x >= width) x = 0; else if (x < 0) x = width - 1;
if (y >= height) y = 0; else if (y < 0) y = height - 1;

for (int i = 0; i < nTail; i++)
if (tailX[i] == x && tailY[i] == y)
gameOver = true;

if (x == fruitX && y == fruitY)
{
score += 10;
fruitX = rand() % width;
fruitY = rand() % height;
nTail++;
}
}
int main()
{
Setup();
while (!gameOver)
{
Draw();
Input();
Logic();
Sleep(10); //sleep(10);
}
return 0;
}#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
using namespace std;
bool gameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail;
enum eDirecton { STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirecton dir;
void Setup()
{
gameOver = false;
dir = STOP;
x = width / 2;
y = height / 2;
fruitX = rand() % width;
fruitY = rand() % height;
score = 0;
}
void Draw()
{
system("cls"); //system("clear");
for (int i = 0; i < width+2; i++)
cout << "#";
cout << endl;

for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0)
cout << "#";
if (i == y && j == x)
cout << "O";
else if (i == fruitY && j == fruitX)
cout << "F";
else
{
bool print = false;
for (int k = 0; k < nTail; k++)
{
if (tailX[k] == j && tailY[k] == i)
{
cout << "o";
print = true;
}
}
if (!print)
cout << " ";
}


if (j == width - 1)
cout << "#";
}
cout << endl;
}

for (int i = 0; i < width+2; i++)
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 prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for (int i = 1; i < nTail; i++)
{
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 (x >= width) x = 0; else if (x < 0) x = width - 1;
if (y >= height) y = 0; else if (y < 0) y = height - 1;

for (int i = 0; i < nTail; i++)
if (tailX[i] == x && tailY[i] == y)
gameOver = true;

if (x == fruitX && y == fruitY)
{
score += 10;
fruitX = rand() % width;
fruitY = rand() % height;
nTail++;
}
}
int main()
{
Setup();
while (!gameOver)
{
Draw();
Input();
Logic();
Sleep(10); //sleep(10);
}
return 0;
}






yea that is pretty much it :)

jeydii
Автор

you can use logical or operator
if(j == 0 || j == width - 1)

jatinnegi
Автор

Thanks for the tutorial bro, it motivated me to learn more about C++

dariuschandra
Автор

i feel like these sort of tutorials are way better than "programming tutorials" because this guy actually shows us how to use th code we learn

i_inject_crayons
Автор

My brain: What did you just watch?
Me: Programming.
My Brain: So, what did you understand?
Me: Nothing

teodortodorov
Автор

For anyone watching this, make sure that in your if statements that you use == to compare.
if (j = 0) is setting j to 0, not checking if it is 0
if (j == 0) checks if j's value is 0.

polygonerror
Автор

At this point, in order to obtain a real square 20x20 it is necessary
33. if (j>0 && j<19)
34. cout << " ";
I know you can do that, but I did it just to make the process accurate.

JOAGOSTINI
Автор

Really simplified c++ to it's core. I like it

kacperozieblowski
Автор

if you try to do this in newer versions, to put the constant rand you have to type after #include <iostream>, #include <csdtlib>

milkyfilsen
Автор

Wow I'm taking up game design and I have a class on c++ and it's my first time ever learning about it. It's a little hard but I understand it. I'll learn as I go.

Flyingsandwiches-nrzc
Автор

I know only code basics like what a variable or constant is; how to make a calculator and stuff like that but watching this and searching up the meanings of the terms really upgraded my knowledge by a whole lot.

brianscalabrine