Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <conio.h>
- #include <time.h>
- using namespace std;
- void wait(double seconds)
- {
- clock_t endwait;
- endwait = clock() + seconds * CLOCKS_PER_SEC;
- while (clock() < endwait) {}
- }
- void game()
- {
- int heady = 5, headx = 10, lengh = 3, inp = 'a', last = 'a', score = 0;
- int mapa[21][21] = {};
- int fruity = 2, fruitx = 2;
- double delay = 0.2;
- while (true) {
- system("cls");
- if (heady == fruity && headx == fruitx) {
- lengh++;
- fruity = rand() % 10;
- fruitx = rand() % 20;
- score += 50;
- delay /= 1.05;
- }
- cout << "#######################" << endl;
- for (int i = 0; i <= 10; i++) {
- cout << "#";
- for (int j = 0; j <= 20; j++) {
- if (mapa[i][j] > 0) { mapa[i][j]--; }
- if (i == heady && j == headx) {
- mapa[i][j] = lengh;
- }
- else if (i == fruity && j == fruitx) {
- mapa[i][j] = -1;
- }
- if (mapa[i][j] > 0) {
- if (mapa[i][j] % 2 == 0) { cout << "0"; }
- if (mapa[i][j] % 2 == 1) { cout << "*"; }
- }
- else if (mapa[i][j] == -1) {
- cout << "o";
- }
- else {
- cout << " ";
- }
- }
- cout << "#";
- cout << endl;
- }
- cout << "#######################" << endl;
- cout << "score: " << score << endl;
- if (_kbhit() != 0) {
- inp = _getch();
- wait(delay / 2);
- }
- else { wait(delay); }
- if (inp == 's' && last != 'w') { last = 's'; }
- else if (inp == 'w' && last != 's') { last = 'w'; }
- else if (inp == 'a' && last != 'd') { last = 'a'; }
- else if (inp == 'd' && last != 'a') { last = 'd'; }
- if (last == 's') {
- if (heady == 10) {
- if (mapa[0][headx] > 1) { break; }
- heady = 0;
- }
- else {
- if (mapa[heady + 1][headx] > 1) { break; }
- heady++;
- }
- }
- else if (last == 'w') {
- if (heady == 0) {
- if (mapa[10][headx] > 1) { break; }
- heady = 10;
- }
- else {
- if (mapa[heady - 1][headx] > 1) { break; }
- heady--;
- }
- }
- else if (last == 'a') {
- if (headx == 0) {
- if (mapa[heady][20] > 1) { break; }
- headx = 20;
- }
- else {
- if (mapa[heady][headx - 1] > 1) { break; }
- headx--;
- }
- }
- else if (last == 'd') {
- if (headx == 20) {
- if (mapa[heady][0] > 1) { break; }
- headx = 0;
- }
- else {
- if (mapa[heady][headx + 1] > 1) { break; }
- headx++;
- }
- }
- cout << '\a';
- }
- cout << "you lose";
- char endGame = _getch();
- }
- int main() {
- char input;
- int crs = 0;
- while (true) {
- if (crs == 0) { cout << "Start game! <<" << endl; }
- else { cout << "Start game!" << endl; }
- if (crs == 1) { cout << "Score <<" << endl; }
- else { cout << "Score" << endl; }
- if (crs == 2) { cout << "Exit <<" << endl; }
- else { cout << "Exit" << endl; }
- input = _getch();
- if (input == 'w' && crs > 0) { crs--; }
- if (input == 's' && crs < 2) { crs++; }
- if (input == 'd') {
- if (crs == 0) game();
- if (crs == 2) break;
- }
- system("cls");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement