#include <iostream>
#include <conio.h>
#include <time.h>
bool gameOver;
const int width = 20;
const int height = 20;
void startSetup(int* score, int* cordX, int* cordY, int* fruitX, int* fruitY)
{
score = 0;
gameOver = false;
*cordX = width / 2 - 1;
*cordY = height / 2 - 1;
*fruitX = rand() % width;
*fruitY = rand() % height;
}
void drawAll(int* cordX, int* cordY, int* fruitX, int* fruitY)
{
system("cls");
for (int i = 0; i < width + 1; i++)
{
std::cout << "#";
}
std::cout << '\n';
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0 or j == width - 1)
{
std::cout << "#" ;
}
if (*cordY == i and *cordX == j )
{
std::cout << "0";
}
else if (*fruitX == j and *fruitY == i)
{
std::cout << "F";
}
else
{
std::cout << ' ';
}
}
std::cout << '\n';
}
for (int i = 0; i < width + 1; i++)
{
std::cout << "#";
}
std::cout << '\n';
}
void control(int* x, int* y)
{
if (_kbhit())
{
auto key{ _getch() };
if (key == 'w' or key == 'W')
{
y--;
}
if (key == 's' or key == 'S')
{
y++;
}
if (key == 'a' or key == 'A')
{
x--;
}
if (key == 'd' or key == 'D')
{
x++;
}
}
}
int main()
{
int x;
int y;
int fruitX;
int fruitY;
int* ptx = &x;
int* pty = &y;
int* ptfruitX = &fruitX;
int* ptfruitY = &fruitY;
int score;
int* pscore = &score;
startSetup(pscore, ptx, pty, ptfruitX, ptfruitY);
while (gameOver != 1)
{
drawAll(ptx, pty, ptfruitX, ptfruitY);
control (ptx, pty);
}
}
Replies to Re: snake
Title |
Name |
Language |
UNIX |
When |
Re: Re: snake |
me |
cpp |
1589569286 |
3 Years ago. |
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}