Repository navigation
Expand file tree
/
Copy pathsnake.cpp
More file actions
175 lines (152 loc) · 4.76 KB
/
Copy pathsnake.cpp
File metadata and controls
175 lines (152 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include <vector>
#include <time.h>
#include <conio.h>
#include "snake.hpp"
using namespace std;
#define column 75
#define row 15
snake::snake() { food(); }
void snake::delay()//جهت تنظیم سرعت مار استفاده می شود
{
clock_t start = clock();
while (clock() < start + 150);
}
void snake::disply()// صفحه بازی را نمایش می دهد
{
system("cls");
for (size_t i = 0; i < column; i++)
{
cout << '#';
}
cout << endl;
for (size_t i = 1; i <= row - 2; i++)
{
cout << '#';
for (size_t j = 1; j <= column - 2; j++)
{
bool status = false; //در صورت برابر بودن مختصات
if ((i == y_head_snake) && (j == x_head_snake))//(i,j) با (x_head_snake, y_head_snake)
{ //سر مار را چاپ می کند
cout << "\033[1;33m" << static_cast<char>(254) << "\033[0m";
status = true;
}
//در صورت برابر بودن مختصات
else if ((i == y_food) && (j == x_food))//(i,j) با (x_food, y_food)
{ //غذا را چاپ می کند
cout << "\033[1;31m" << 'O' << "\033[0m";
status = true;
}
for (size_t k = 0; k < len; k++)
{ //در صورت برابر بودن مختصات
if ((i == y_len_snake[k]) && (j == x_len_snake[k]))//(i,j) با (x_len_snake, y_len_snake)
{ //بدن مار را چاپ می کند
cout << "\033[1;33m" << static_cast<char>(254) << "\033[0m";
status = true;
break;
}
}
if (!status)
{
cout << ' ';
}
}
cout << '#' << endl;
}
for (size_t i = 0; i < column; i++)
{
cout << '#';
}
delay();
}
void snake::food()// محل قرار گیری غذا را به صورت رندوم انتخاب می کند
{
srand((unsigned)time(NULL));
x_food = rand() % (column - 2) + 1;
y_food = rand() % (row - 2) + 1;
}
bool snake::eat()//اگر غذا خورده شود یک واحد به طول مار اضافه می شود
{
if ((x_head_snake == x_food) && (y_head_snake == y_food))
{
len++;
x_len_snake[len] = x_food;
y_len_snake[len] = y_food;
return true;
}
return false;
}
void snake::move()//کار حرکت به جهت های مختلف را بر عهده دارد
{
if (_kbhit())
{
switch (_getch())
{
case 'A':
case 'a':
dir = LEFT;
break;
case 'D':
case 'd':
dir = RIGHT;
break;
case 'S':
case 's':
dir = DOWN;
break;
case 'W':
case 'w':
dir = UP;
break;
}
}
for (size_t i = len - 1; (len != 0) && (i > 0); --i)//این حلقه مار را یک واحد به جهت وارد شده انتقال می دهد در واقع بدن مار را به دنبال سر ان به حرکت در می اورد
{
x_len_snake[i] = x_len_snake[i - 1];
y_len_snake[i] = y_len_snake[i - 1];
}
x_len_snake[0] = x_head_snake;
y_len_snake[0] = y_head_snake;
switch (dir)
{
case LEFT:
(x_head_snake)--;
break;
case RIGHT:
(x_head_snake)++;
break;
case DOWN:
(y_head_snake)++;
break;
case UP:
(y_head_snake)--;
break;
}
}
void snake::check()//اگر مار خودش را بخورد gmaeover true میشود و از بازی خارج می شود
{ //همچنین اگر مار به سمت دیوار ها برود از طرف دیگر در می آید
for (size_t i = 0; i < len; i++)
{
if ((x_head_snake == x_len_snake[i]) && (y_head_snake == y_len_snake[i]))
{
gameover = true;
}
}
if ((x_head_snake == 0)) {x_head_snake = column - 1;}
if ((x_head_snake == column)) {x_head_snake = 0;}
if ((y_head_snake == -1)) {y_head_snake = row - 1;}
if ((y_head_snake == row)) {y_head_snake = 0;}
}
void snake::run()//کنترل بازی را در دست دارد و تابع های مورد نظر را فراخوانی می کند
{
while (!gameover)
{
disply();
move();
if (eat())
{
food();
}
check();
}
}