logic how snake should grow in size and when game stops
| 124 | } |
| 125 | // logic how snake should grow in size and when game stops |
| 126 | void Logic() { |
| 127 | |
| 128 | int prevx = tx[0]; |
| 129 | int prevy = ty[0]; |
| 130 | int prev2x, prev2y; |
| 131 | tx[0] = x; |
| 132 | ty[0] = y; |
| 133 | for (int i = 1; i < nt; i++) { |
| 134 | prev2x = tx[i]; |
| 135 | prev2y = ty[i]; |
| 136 | tx[i] = prevx; |
| 137 | ty[i] = prevy; |
| 138 | prevx = prev2x; |
| 139 | prevy = prev2y; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | |
| 144 | switch (dir) |
| 145 | { |
| 146 | case UP: |
| 147 | y--; |
| 148 | break; |
| 149 | case DOWN: |
| 150 | y++; |
| 151 | break; |
| 152 | case LEFT: |
| 153 | x--; |
| 154 | break; |
| 155 | case RIGHT: |
| 156 | x++; |
| 157 | break; |
| 158 | |
| 159 | default: |
| 160 | break; |
| 161 | } |
| 162 | if (x >= width) x = 0; else if (x < 0) x = width - 1; |
| 163 | if (y >= height) y = 0; else if (y < 0) y = height - 1; |
| 164 | |
| 165 | for (int i = 0; i < nt; i++) { |
| 166 | if (tx[i] == x && ty[i] == y) { |
| 167 | lostgame(); |
| 168 | } |
| 169 | |
| 170 | } |
| 171 | |
| 172 | if (x == fx && y == fy) { |
| 173 | score += 1; |
| 174 | nt++; |
| 175 | fx = rand() % width; |
| 176 | fy = rand() % height; |
| 177 | } |
| 178 | } |
| 179 | //condition when a player wins |
| 180 | void win() { |
| 181 | if (score == 100) { |