Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <conio.h>
- #include <Windows.h>
- #include <locale.h>
- #define LEN 20//длина
- #define WID 20//ширина
- int play = 0;
- typedef struct Snake {
- int x_coordinate;
- int y_coordinate;
- char symbol;
- char route;
- int speed;
- int size;
- int tailX[100], tailY[100];
- } PSnake;
- typedef struct Fruit
- {
- int x_coordinate;
- int y_coordinate;
- char symbol;
- } PFruit;
- void change_route(int code, PSnake *s);
- void snake_logic(PSnake *s, PFruit *f, int *score);
- void Draw(PSnake *s, PFruit *f, int *score);
- int random(int a, int b);
- void start_settings(PSnake *s, PFruit *f);
- int main()
- {
- setlocale(LC_ALL, "Rus");
- srand(time(NULL));
- int i, j, k, c = 0, score = 0;
- PSnake s;
- PFruit f;
- start_settings(&s, &f);
- int flag;
- printf("Для начала игры нажмите любую клавишу\n");
- _getch();
- while (!play)
- {
- Draw(&s, &f, &score);
- if (_kbhit()) {
- c = _getch();
- if (c == 27){
- play = 1;
- continue;
- }
- change_route(c, &s);
- }
- snake_logic(&s, &f, &score);
- }
- printf("Игра окончена\nНабрано очков: %d\n", score);
- system("pause");
- return 0;
- }
- void change_route(int code, PSnake *s)
- {
- switch (code)
- {
- case 119: /*s->x_coordinate--;*/ s->route = 'u'; break;//Вверх
- case 100: /*s->y_coordinate++;*/ s->route = 'r'; break;//Вправо
- case 97: /*s->y_coordinate--;*/ s->route = 'l'; break;//Влево
- case 115:/* s->x_coordinate++;*/ s->route = 'd'; break;//Вниз
- default: break;
- }
- return;
- }
- void snake_logic(PSnake * s, PFruit * f, int * score)
- {
- int prevX = s->tailX[0], prevY = s->tailY[0];
- int prev2X, prev2Y, i;
- s->tailX[0] = s->x_coordinate;
- s->tailY[0] = s->y_coordinate;
- for (i = 1; i < s->size; i++)
- {
- prev2X = s->tailX[i];
- prev2Y = s->tailY[i];
- s->tailX[i] = prevX;
- s->tailY[i] = prevY;
- prevX = prev2X;
- prevY = prev2Y;
- }
- /*---------------------------*/
- char symbol = s->route;
- switch (symbol)
- {
- case 'u': s->x_coordinate--; /*s->route = 'u';*/ break;//Вверх
- case 'r': s->y_coordinate++; /*s->route = 'r';*/ break;//Вправо
- case 'l': s->y_coordinate--; /*s->route = 'l';*/ break;//Влево
- case 'd': s->x_coordinate++;/* s->route = 'd';*/ break;//Вниз
- default: break;
- }
- /*--------------------*/
- if (s->x_coordinate >= WID)
- s->x_coordinate = 0;
- else if (s->x_coordinate < 0)
- s->x_coordinate = WID - 1;
- if (s->y_coordinate >= LEN)
- s->y_coordinate = 0;
- else if (s->y_coordinate < 0)
- s->y_coordinate = LEN - 1;
- /*----------------------------------*/
- if ((s->x_coordinate == f->x_coordinate) && (s->y_coordinate == f->y_coordinate))
- {
- *score += 10;
- s->size++;
- f->x_coordinate = random(1, WID - 2);
- f->y_coordinate = random(1, LEN - 2);
- while (f->x_coordinate == s->x_coordinate && f->y_coordinate == s->y_coordinate)
- {
- f->x_coordinate = random(1, WID - 2);
- f->y_coordinate = random(1, LEN - 2);
- }
- if (s->speed - 10 != 0)
- (s->speed) -= 10;
- }
- for (i = 0; i < s->size; i++) {
- if (s->tailX[i] == s->x_coordinate && s->tailY[i] == s->y_coordinate)
- play = 1;
- }
- }
- void Draw(PSnake *s, PFruit *f, int *score) {
- int flag;
- Sleep(s->speed); //speed of game
- system("cls");
- for (int i = 0; i < WID + 2; i++)
- printf("#");
- printf("\n");
- for (int i = 0; i < LEN; i++) {
- for (int j = 0; j <= WID; j++) {
- if (j == 0 || j == WID )
- printf("#");
- if (i == s->x_coordinate && j == s->y_coordinate)
- printf("%c", s->symbol);
- else if (i == f->x_coordinate && j == f->y_coordinate)
- printf("%c", f->symbol);
- else {
- flag = 0;
- for (int k = 0; k < s->size; k++) {
- if (s->tailX[k] == i && s->tailY[k] == j) {
- flag = 1;
- printf("%c", s->symbol);;
- }
- }
- if (!flag)
- printf(" ");
- }
- }
- printf("\n");
- }
- for (int i = 0; i < WID + 2; i++)
- printf("#");
- printf("\nScore: %d\n", *score);
- }
- void start_settings(PSnake *s, PFruit *f)
- {
- s->symbol = '0';
- s->x_coordinate = WID / 2;
- s->y_coordinate = LEN / 2;
- s->tailX[0] = WID / 2;
- s->tailY[0] = LEN / 2;
- s->speed = 200;
- s->size = 0;
- s->route = 0;
- f->symbol = '*';
- f->x_coordinate = random(1, WID - 2);
- f->y_coordinate = random(1, LEN - 2);
- while(f->x_coordinate == s->x_coordinate && f->y_coordinate == s->y_coordinate)
- {
- f->x_coordinate = random(1, WID - 2);
- f->y_coordinate = random(1, LEN - 2);
- }
- return;
- }
- int random(int a, int b)
- {
- srand(time(NULL));
- if (a > 0) return a + rand() % (b - a);
- else return a + rand() % (abs(a) + b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement