Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <conio.h>
- #include <windows.h>
- #include <math.h>
- #include <stdio.h>
- #include <iostream>
- using namespace std;
- bool gameover;
- // Buffer size
- const char WIDTH = 120,
- HEIGHT = 30,
- //Dirrections:
- STOP = 0,
- UP = 1,
- RIGHT = 2,
- DOWN = 3,
- LEFT = 4,
- //Other settings
- MAX_TAIL_LENGTH = 100;
- void sleep(int time)
- {
- for (int i = 0; i < time * 10000; ++i)
- {
- cos(i);
- }
- }
- typedef struct
- {
- char x,
- y;
- }Point;
- inline void printBorder(char* screenBuffer)
- {
- int score = 0;
- for (int i = 0; i < 61; ++i)
- {
- screenBuffer[i] = 178;
- }
- for (int i = 0; i < HEIGHT * WIDTH; i += 120)
- {
- screenBuffer[i + 60] = screenBuffer[i] = 178;
- }
- for (int i = (HEIGHT * WIDTH)-120; i <( HEIGHT * WIDTH)-60; i++)
- {
- screenBuffer[i] =178;
- }
- }
- int main()
- {
- char* screenBuffer = new char[HEIGHT * WIDTH];
- Point tail[MAX_TAIL_LENGTH];
- unsigned short tailLength = 0;
- char xPos = 20,
- yPos = 15,
- xFruitPos = rand() % 59,
- yFruitPos = rand() % 29;
- int score = 0;
- DWORD dword = 0;
- HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
- char dirrection = STOP;
- CONSOLE_CURSOR_INFO cursorInfo;
- GetConsoleCursorInfo(out, &cursorInfo);
- cursorInfo.bVisible = false;
- SetConsoleCursorInfo(out, &cursorInfo);
- while (!gameover)
- {
- for (int i = 0; i < HEIGHT * WIDTH; ++i)
- {
- screenBuffer[i] = '\0';
- }
- screenBuffer[yFruitPos * 120 + xFruitPos] = 'O';
- screenBuffer[yPos * 120 + xPos] = 'S';
- //cout << "Score:" << score << endl;
- printBorder(screenBuffer);
- for (int i = 0; i < tailLength; ++i)
- {
- screenBuffer[tail[i].y * 120 + tail[i].x] = 's';
- }
- WriteConsoleOutputCharacterA(out, screenBuffer, WIDTH * HEIGHT, { 0,0 }, &dword);
- if (yFruitPos == yPos && xFruitPos == xPos)
- {
- xFruitPos = rand() % 59,
- yFruitPos = rand() % 29;
- score +=5;
- tail[tailLength].x = xPos;
- tail[tailLength].y = yPos;
- ++tailLength;
- }
- else
- {
- for (int i = 0; i < tailLength - 1; ++i)
- {
- tail[i] = tail[i + 1];
- }
- tail[tailLength - 1].x = xPos;
- tail[tailLength - 1].y = yPos;
- }
- if (_kbhit())
- {
- char key = _getch();
- switch (key)
- {
- case 97:
- dirrection = LEFT;
- break;
- case 100:
- dirrection = RIGHT;
- break;
- case 115:
- dirrection = DOWN;
- break;
- case 119:
- dirrection = UP;
- break;
- }
- }
- switch (dirrection)
- {
- case LEFT:
- if (xPos > 0) --xPos;
- break;
- case RIGHT:
- if (xPos < 60) ++xPos;
- break;
- case DOWN:
- if (yPos < 29) ++yPos;
- break;
- case UP:
- if (yPos > 0) --yPos;
- break;
- }
- if (xPos < 1)
- {
- xPos = 58;
- }
- else if (xPos > 59)
- {
- xPos = 1;
- }
- if (yPos < 1)
- {
- yPos = 27;
- }
- else if (yPos >28)
- {
- yPos = 1;
- }
- for (int i = 0; i < tailLength - 1; ++i)
- {
- if (tail[tailLength].x == xPos && tail[tailLength].y == yPos)
- gameover = true;
- }
- sleep(120);
- }
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement