Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Hra Snake.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <conio.h>
- #include <windows.h>
- #include <cstdlib>
- #include <stdio.h>
- #define MAX 10
- #pragma warning(disable:4996)
- using namespace std;
- bool gameOver;
- const int width = 40;
- const int height = 20;
- int x, y, appleX, appleY, score;
- int choice, choice2;
- int tailX[100], tailY[100];
- int nTail;
- typedef struct
- {
- int score;
- char name[20];
- }Player;
- enum eDirecton { STOP = 0, LEFT, RIGHT, UP, DOWN};
- eDirecton dir;
- void Setup()
- {
- gameOver = false;
- dir = STOP;
- x = width / 2;
- y = height / 2;
- appleX = rand() % width;
- appleY = rand() % height;
- score = 0;
- }
- void Draw()
- {
- 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 == appleY && j == appleX)
- cout << "A";
- else
- {
- bool print = false;
- for (int k = 0; k < nTail; k++)
- {
- if (tailX[k] == j && tailY[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 << "Score:" << score << endl;
- }
- void Input()
- {
- if (_kbhit())
- {
- switch (_getch())
- {
- case 'K':
- dir = LEFT;
- break;
- case 'M':
- dir = RIGHT;
- break;
- case 'H':
- dir = UP;
- break;
- case 'P':
- dir = DOWN;
- break;
- case 'x':
- gameOver = true;
- break;
- }
- }
- }
- void Logic()
- {
- int moveX = tailX[0];
- int moveY = tailY[0];
- int move2X, move2Y;
- tailX[0] = x;
- tailY[0] = y;
- for (int i = 1; i < nTail; i++)
- {
- move2X = tailX[i];
- move2Y = tailY[i];
- tailX[i] = moveX;
- tailY[i] = moveY;
- moveX = move2X;
- moveY = move2Y;
- }
- switch (dir)
- {
- case LEFT:
- x--;
- break;
- case RIGHT:
- x++;
- break;
- case UP:
- y--;
- break;
- case DOWN:
- y++;
- break;
- default:
- break;
- }
- if (x > width || x < 0 || y > height || y < 0)
- {
- gameOver = true;
- }
- //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 < nTail; i++)
- if (tailX[i] == x && tailY[i] == y)
- {
- gameOver = true;
- }
- if (x == appleX && y == appleY)
- {
- score += 10;
- appleX = rand() % width;
- appleY = rand() % height;
- nTail++;
- }
- }
- void AddScore(int score)
- {
- FILE *scoreboardFile = fopen("scoreboard.txt", "a");
- Player player;
- printf("\n\nWrite your name: ");
- scanf("%s", player.name);
- fprintf(scoreboardFile, "name: %s \score: %d\n\n\n", player.name, player.score);
- fclose(scoreboardFile);
- }
- void ShowScore(Player player[])
- {
- FILE *scoreboardFile = fopen("scoreboard.txt", "r");
- int i, num;
- //system("cls")
- if(scoreboardFile==NULL)
- {
- printf("\nERROR opening scoreboard");
- Sleep(1000);
- return;
- }
- for (i = 0; i < MAX; i++)
- {
- num = fscanf(scoreboardFile, "%s %d", player[i].name, &player[i].score);
- if (num < 2)
- {
- player[i+1].name[0] = '\0';
- player[i+1].score = 0;
- break;
- }
- printf("name: %s, score: %d\n\n", player[i].name, player[i].score);
- }
- printf("\n\n\n");
- system("pause");
- fclose(scoreboardFile);
- }
- void SortScore(Player player[])
- {
- int i, j;
- Player temp;
- char tempName[20];
- int howMany = 0;
- FILE *scoreboardFile = fopen("scoreboard.txt", "r");
- for (i = 0; i<MAX && !feof(scoreboardFile); i++)
- {
- fscanf(scoreboardFile, "%s %d%*c", player[i].name, &player[i].score);
- printf("name: %s, points: %d\n", player[i].name, player[i].score);
- howMany++;
- }
- printf("\n\n\nhow many: %d\n\n\n", howMany);
- for (i = 0; i<howMany; i++)
- {
- for (j = 0; j<howMany - 1; j++)
- {
- if (player[j].score > player[j + 1].score)
- {
- temp = player[j];
- player[j] = player[j + 1];
- player[j + 1] = temp;
- }
- }
- }
- fclose(scoreboardFile);
- }
- int main()
- {
- gotostart:
- {
- int choice = 0;
- int choice2 = 0;
- system("cls");
- std::cout << "============Snake============\n\n" << std::endl;
- std::cout << "(1): Press 1 to start game" << std::endl;
- std::cout << "(2): Press 2 to see scoreboard" << std::endl;
- std::cout << "(3): Press 3 to exit" << std::endl;
- std::cin >> choice;
- switch (choice)
- {
- case 1:
- Setup();
- while (!gameOver)
- {
- Draw();
- Input();
- Logic();
- Sleep(50);
- }
- while (gameOver)
- {
- system("cls");
- printf("\n\n GAME OVER");
- printf("\n\n\nYour score is: %d", score);
- Sleep(1000);
- gotostart2:
- int choice2 = 0;
- system("cls");
- std::cout << "GAMEOVER" << std::endl;
- std::cout << "(1): Press 1 to menu" << std::endl;
- std::cout << "(2): Press 2 to exit" << std::endl;
- std::cin >> choice2;
- switch (choice2)
- {
- case 1:
- goto gotostart;
- return 0;
- break;
- case 2:
- break;
- default:
- goto gotostart2;
- return 0;
- break;
- }
- }
- break;
- case 2:
- ShowScore(0);
- break;
- case 3:
- break;
- default:
- goto gotostart;
- return 0;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement