#include <iostream> //#include <conio.h> //#include <windows.h> using namespace std; int main() { // === init === int i; int j; int k; int l; bool gameOver = false; int direction = 0; int length = 0; int data[10][10]{}; data[rand() % 9][rand() % 9] = -1; int x; int y; int px = 5; int py = 5; HANDLE color = GetStdHandle(STD_OUTPUT_HANDLE); while (true) { // === main loop === system("cls"); SetConsoleTextAttribute(color, 0xAC); cout << " SNAKAE 2 \n"; SetConsoleTextAttribute(color, 0x05); if (GetKeyState('I') == 1) cout << "v1.0 - made by E77"; cout << "\n\n"; SetConsoleTextAttribute(color, 0x0F); cout << "Control: W A S D\n\n"; x = 0; y = 0; SetConsoleTextAttribute(color, 0x8F); cout << "XXXXXXXXXXXXXXXXXXXXXXXX\n"; SetConsoleTextAttribute(color, 0x0F); for (i = 1; i <= 10; i++) { SetConsoleTextAttribute(color, 0x8F); cout << "XX"; SetConsoleTextAttribute(color, 0x0F); x = 0; for (j = 1; j <= 10; j++) { if (x == px && y == py) { // --- head --- SetConsoleTextAttribute(color, 9); cout << "()"; if (data[x][y] == -1) { // --- eat apple --- tryAgain: k = rand() % 9; l = rand() % 9; if (data[k][l] == 0) data[k][l] = -1; else goto tryAgain; length++; Beep(1000, 25); } if (data[x][y] > 0) gameOver = true; data[x][y] = length + 1; } else if (data[x][y] > 0) { // --- tail --- SetConsoleTextAttribute(color, 0x01); cout << "[]"; data[x][y]--; } else if (data[x][y] == -1) { // --- apple --- SetConsoleTextAttribute(color, 0x46); cout << "`"; SetConsoleTextAttribute(color, 0x4A); cout << "*"; SetConsoleTextAttribute(color, 0x0F); } else if (data[x][y] == 0) cout << " "; // --- empty --- x++; } SetConsoleTextAttribute(color, 0x8F); cout << "XX\n"; SetConsoleTextAttribute(color, 0x0F); y++; } SetConsoleTextAttribute(color, 0x8F); cout << "XXXXXXXXXXXXXXXXXXXXXXXX\n\n"; SetConsoleTextAttribute(color, 0x0F); cout << "Score: " << length << "\n\n"; if (GetKeyState('I') == 1) cout << "X: " << px << "\nY: " << py << "\nDIRECTION: " << direction << "\nLENGTH: " << length; // press 'I' for more info. for (i = 1; i <= 20; i++) { if (GetKeyState('W') < 0 || GetKeyState(VK_UP) < 0 && direction != 2) direction = 0; if (GetKeyState('A') < 0 || GetKeyState(VK_LEFT) < 0 && direction != 3) direction = 1; if (GetKeyState('S') < 0 || GetKeyState(VK_DOWN) < 0 && direction != 0) direction = 2; if (GetKeyState('D') < 0 || GetKeyState(VK_RIGHT) < 0 && direction != 1) direction = 3; Beep(0, 1); // wait (with i<=20, without i<=500000) } if (direction == 0) py--; if (direction == 1) px--; if (direction == 2) py++; if (direction == 3) px++; if (px < 0 || px > 9 || py < 0 || py > 9) gameOver = true; if (gameOver == true) goto gameOver; if (length == 16) goto youWin; } // === end === youWin: SetConsoleTextAttribute(color, 0x2F); cout << "\n\n YOU WIN! \n\n"; SetConsoleTextAttribute(color, 0x0F); Beep(0, 100); // idk why but it doesn't work without that Beep(700, 200); Beep(800, 200); Beep(900, 200); Beep(1000,200); system("pause>nul"); goto end; gameOver: SetConsoleTextAttribute(color, 0x4F); cout << "\n\n GAME OVER \n\n"; SetConsoleTextAttribute(color, 0x0F); Beep(0, 100); // -||- Beep(700, 200); Beep(600, 200); Beep(500, 200); system("pause>nul"); end: return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}