Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <windows.h>
- using namespace std;
- void Postavka();
- void Crtanje();
- void Unos();
- void Logika();
- bool krajIgre;
- const int width = 20;
- const int height = 20;
- int x, y, hranaX, hranaY, rezultat;
- int repX[100], repY[100];
- int rep;
- enum Smjer { STANI = 0, LIJEVO, DESNO, GORE, DOLJE};
- Smjer smjer;
- int main()
- {
- Postavka();
- while (!krajIgre) {
- Crtanje();
- Unos();
- Logika();
- Sleep(10);
- }
- return 0;
- }
- void Postavka(){
- krajIgre = false;
- smjer = STANI;
- x = width / 2;
- y = height / 2;
- hranaX = rand() % width;
- hranaY = rand() % height;
- rezultat = 0;
- }
- void Crtanje(){
- system("cls");
- for (int i = 0; i < width+2; i++)
- cout << "#";
- cout << endl;
- for (int i = 0; i < height; i++){
- for (int j = 0; j < width; j++){
- if (j == 0) cout << "#";
- if (i == y && j == x) cout << "O";
- else if (i == hranaY && j == hranaX)cout << "F";
- else{
- bool print = false;
- for (int k = 0; k < rep; k++){
- if (repX[k] == j && repY[k] == i){
- cout << "o";
- print = true;
- }
- }
- if (!print) cout << " ";
- }
- if (j == width - 1) cout << "#";
- }
- cout << endl;
- }
- for (int i = 0; i < width+2; i++) cout << "#";
- cout << endl;
- cout << "Rezultat:" << rezultat << endl;
- }
- void Unos(){
- if (_kbhit()){
- switch (_getch()) {
- case 'a':
- smjer = LIJEVO;
- break;
- case 'd':
- smjer = DESNO;
- break;
- case 'w':
- smjer = GORE;
- break;
- case 's':
- smjer = DOLJE;
- break;
- case 'x':
- krajIgre = true;
- break;
- }
- }
- }
- void Logika(){
- int prevX = repX[0];
- int prevY = repY[0];
- int prev2X, prev2Y;
- repX[0] = x;
- repY[0] = y;
- for (int i = 1; i < rep; i++){
- prev2X = repX[i];
- prev2Y = repY[i];
- repX[i] = prevX;
- repY[i] = prevY;
- prevX = prev2X;
- prevY = prev2Y;
- }
- switch (smjer){
- case LIJEVO:
- x--;
- break;
- case DESNO:
- x++;
- break;
- case GORE:
- y--;
- break;
- case DOLJE:
- y++;
- break;
- default:
- break;
- }
- if (x >= width) x = 0; else if (x < 0) x = width - 1;
- if (y >= height) y = 0; else if (y < 0) y = height - 1;
- for (int i = 0; i < rep; i++) if (repX[i] == x && repY[i] == y) krajIgre = true;
- if (x == hranaX && y == hranaY){
- rezultat += 10;
- hranaX = rand() % width;
- hranaY = rand() % height;
- rep++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement