1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <string>
#include <thread>
using namespace std;
void gotoxy(int x, int y)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
_COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hConsole, pos);
}
//------------------------------------------------------------------------------
void ohrada() //creating area for snake
{
int i;
for (size_t i = 0; i < 60; i++)
{
cout << "X";
}
for (size_t i = 1; i < 40; i++)
{
gotoxy(0, i); _putch('X');
gotoxy(59, i); _putch('X');
}
cout << endl;
for (size_t i = 0; i < 60; i++)
{
gotoxy(i, 40); _putch('X');
}
}
//------------------------------------------------------------------------------
void food() // generating food
{
srand(time(NULL));
gotoxy((rand() % 50) + 5, (rand() % 35) + 5);
_putch('*');
}
void had()
{
char smer = 'H';
int x[20], y[20], n = 2, i;
string s;
x[0] = 20; y[0] = 20;
gotoxy(x[0], y[0]);
_putch('0');
while (1)
{
if (_kbhit()) //checking if key was pressed
{
smer = _getch();
if (!smer)
{
smer = _getch();
}
}
//-----------------------
for (i = n; i > 0; i--)
{
x[i] = x[i - 1]; y[i] = y[i - 1];
}
//-------------------------------
switch (smer) // moving snake
{
case 'H': y[0]--; break;
case 'P': y[0]++; break;
case 'M': x[0]++; break;
case 'K': x[0]--; break;
}
gotoxy(x[0], y[0]); //moving th head
//gettext(x[0], y[0], x[0], y[0],s); if (s[0] == '*')n++;
_putch('0');
gotoxy(x[n], y[n]); //deleting last part
_putch(' ');
Sleep(200);
if (rand()%10 == 5) food(); //generating food
}
}
//------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
ohrada();
had();
getchar();
return 0;
}
|