Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <conio.h>
- #include <time.h>
- #include <windows.h>
- using namespace std;
- int const width = 20;
- int const height = 20;
- int playerX = 10, playerY = 10;
- int fruitX = rand() % 20;
- int fruitY = rand() % 20;
- int direction = 10;
- int tailX[100], tailY[100];
- int nTail; //Length of the tail
- int end_game = 0;
- int points = 0;
- int y;
- int x;
- bool print = false;
- void draw_arena_and_character()
- {
- for (int y = 0; y < height; y++)
- {
- for (int x = 0; x < width; x++)
- {
- if (playerX == 0)
- {
- direction = 4;
- }
- else if (playerX == 19)
- {
- direction = 2;
- }
- else if (playerY == 0)
- {
- direction = 3;
- }
- else if (playerY == 19)
- {
- direction = 1;
- }
- if (y == 0 || y == 19)
- {
- cout << "#";
- }
- else if (x == 0 || x == 19)
- {
- cout << "#";
- }
- else if (y == playerY && x == playerX)
- {
- cout << "O";
- }
- else if (y == fruitY && x == fruitX)
- {
- cout << "F";
- }
- else
- {
- print = false;
- for (int k = 0; k < nTail; k++)
- {
- if (x == tailX[k] && y == tailY[k])
- {
- cout << "o";
- print = true;
- }
- }
- if (!print)
- cout << " ";
- }
- }
- cout << endl;
- }
- cout << "Points: " << points;
- Sleep(100);
- system("cls");
- }
- void input()
- {
- if (_kbhit())
- {
- char input_variable;
- input_variable = _getch();
- if (input_variable == 'w')
- {
- if (direction == 3)
- ;
- else
- direction = 1;
- }
- else if (input_variable == 'a')
- {
- if (direction == 4)
- ;
- else
- direction = 2;
- }
- else if (input_variable == 's')
- {
- if (direction == 1)
- ;
- else
- direction = 3;
- }
- else if (input_variable == 'd')
- {
- if (direction == 2)
- ;
- else
- direction = 4;
- }
- }
- }
- void logic()
- {
- if (playerX == fruitX && playerY == fruitY)
- {
- nTail++;
- points += 10;
- srand(time(NULL));
- fruitY = (rand() % 18) + 1;
- fruitX = (rand() % 18) + 1;
- }
- int prevX = tailX[0];
- int prevY = tailY[0];
- int prev2X;
- int prev2Y;
- tailX[0] = x;
- tailY[0] = y;
- for (int i = 1; i < nTail; i++)
- {
- prev2X = tailX[i];
- prev2Y = tailY[i];
- tailX[i] = prevX;
- tailY[i] = prevY;
- prevX = prev2X;
- prevY = prev2Y;
- }
- //Check death
- if (nTail > 0)
- {
- for (int m = 0; m < nTail; m++)
- {
- if (tailX[m] == playerX && tailY[m] == playerY)
- {
- end_game = 1;
- }
- }
- }
- /*
- tailX[3] = tailX[2];
- tailY[3] = tailY[2];
- tailX[2] = tailX[1];
- tailY[2] = tailY[1];
- tailX[1] = tailX[0];
- tailY[1] = tailY[0];
- tailX[0] = playerX;
- tailY[0] = playerY;
- */
- tailX[0] = playerX;
- tailY[0] = playerY;
- /*
- if (nTail > 0)
- {
- for (int k = nTail; k >= 1; k--)
- {
- tailX[k] =
- }
- }
- */
- if (direction == 1)
- {
- playerY--;
- }
- else if (direction == 2)
- {
- playerX--;
- }
- else if (direction == 3)
- {
- playerY++;
- }
- else if (direction == 4)
- {
- playerX++;
- }
- }
- int main()
- {
- while (end_game == 0)
- {
- draw_arena_and_character();
- input();
- logic();
- }
- system("cls");
- cout << "You died!" << endl;
- cout << "Points: " << points;
- int pause; cin >> pause; //Pause the program
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement