Game: Snake v1
by Shinigami on Apr.21, 2012, under C++
This is a text based snake game in terminal. It is made as a class object. And it has GNU GPL license.
|
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
/* Copyright 2012 Donatas Azaravicius * Program is distributed under the terms of the GNU General Public License * * Text based snake game * @author: Donatas Azaravicius * homepage: cplusplus.shinigami.lt * version v1 * date: 2012:04:21 * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <iostream> #include <deque> #include <ctime> #include <cstdlib> #include <string> using namespace std; #define MS_WELCOME "Welcome to snake game!" #define MS_MENU "Choose what you want to do." #define MS_CHOICE_1 "1 Play a game" #define MS_CHOICE_2 "2 About" #define MS_EXIT "3 Exit game" #define MS_END "GAME OVER" #define MS_MOVE_1 "Pick one: move up (" #define MS_MOVE_2 "), down (" #define MS_MOVE_3 "), left (" #define MS_MOVE_4 ") or right (" #define MS_MOVE_5 "): " #define MS_CHOICE_3 "Enter your choice number here: " #define MS_END_CHOICE "You lost the game. Do you want to try again? YES (y) or NO (n)." #define MS_WRONG_INPUT "Wrong input: " #define MS_RETURN_TO_MENU "Enter leter to return to menu: " #define MS_AUTHOR "Autor: Donatas Azaravicius" #define MS_HOMEPAGE "Homepage: cplusplus.shinigami.lt" #define MS_DATE "Date of release: 2012:04:21" #define MS_VERSION "Version: v1" // Start of file element.h class Element { public: int x, y; Element(int, int); Element() {}; ~Element() {}; bool operator == (const Element & right) const; }; // Finish of file element.h // Start of file element.cpp Element::Element(int x_in, int y_in) : x(x_in), y(y_in) { } bool Element::operator == (const Element & right) const { return (x == right.x && y == right.y); } // Finish of file element.cpp // Start of file snake.h class Snake { public: Snake(); ~Snake(); void move(); bool gameover() const; private: deque<Element> body; Element food_xy; char direction; char end_choice; bool gameover_var; int menu_choice; static const int field_width = 40; static const int field_hight = 22; static const char field_simbol = ' '; static const char snake_head = '@'; static const char snake_simbol = '~'; static const char space = ' '; static const char wall_simbol = '#'; static const char food_simbol = 'F'; static const char direction_right = 'd'; static const char direction_left = 'a'; static const char direction_down = 's'; static const char direction_up = 'w'; static const char yes = 'y'; static const char no = 'n'; char field[field_hight][field_width]; //methods void make_field(); void menu(); void show_field() const; void choice(int); void ms_generator(string, int, int column = 0); bool go(); bool is_wall(Element) const; void eat_food(); void food(); void fill_field(); bool check(); void restart(); void about(); }; // Finish of file snake.h // Start of file snake.cpp // Public methods Snake::Snake() { restart(); } Snake::~Snake() { } bool Snake::gameover() const { return gameover_var; } void Snake::move() { if (menu_choice == 1) { if (food_xy == body[0]) { food(); } if (go()) { ms_generator(MS_END, field_hight / 2); gameover_var = true; } else if (check()) { ms_generator(MS_END, field_hight / 2); gameover_var = true; } } else if (menu_choice == 2) { make_field(); about(); } else if (menu_choice == 3) { make_field(); ms_generator(MS_END, field_hight / 2); gameover_var = true; } show_field(); if (gameover_var == false && menu_choice == 1) { choice(2); } else if (gameover_var == false && menu_choice == 2) { choice(4); restart(); } else if (gameover_var == true && menu_choice != 3) { choice(3); if (end_choice == yes) { restart(); } else { show_field(); } } return; } //End of public methods //Private methods void Snake::make_field() { for(int i = 0; i < field_hight; i++) { for(int j = 0; j < field_width; j++) { if (j == 0 or j == (field_width - 1)) { field[i][j] = wall_simbol; } else if (i == 0 or i == (field_hight - 1)) { field[i][j] = wall_simbol; } else { field[i][j] = field_simbol; } } } return; } void Snake::menu() { ms_generator(MS_WELCOME, 3); ms_generator(MS_MENU, 5); ms_generator(MS_CHOICE_1, 7, 10); ms_generator(MS_CHOICE_2, 8, 10); ms_generator(MS_EXIT, 9, 10); return; } void Snake::show_field() const { for(int i = 0; i < field_hight; i++) { for(int j = 0; j < field_width; j++) { cout << field[i][j]; if (i == 0 or i == (field_hight - 1)) cout << wall_simbol; else cout << space; } cout << endl; } cout << endl; return; } void Snake::choice(int number) { char temp; if (number == 1) { cout << MS_CHOICE_3; do { cin >> menu_choice; if (menu_choice > 0 && menu_choice < 4) { break; } cout << MS_WRONG_INPUT; cin.clear(); cin.ignore(); } while(true); } else if (number == 2) { string ms = MS_MOVE_1; ms += direction_up; ms += MS_MOVE_2; ms += direction_down; ms += MS_MOVE_3; ms += direction_left; ms += MS_MOVE_4; ms += direction_right; ms += MS_MOVE_5; cout << ms; do { cin >> direction; if (direction == direction_right || direction == direction_left || direction == direction_up || direction == direction_down) { break; } cout << MS_WRONG_INPUT; } while(true); } else if (number == 3) { cout << MS_END_CHOICE; do { cin >> end_choice; if (end_choice == yes || end_choice == no) { break; } cout << MS_WRONG_INPUT; } while(true); } else if (number == 4) { cout << MS_RETURN_TO_MENU; cin >> temp; } return; } void Snake::ms_generator(string ms, int place, int column) { size_t ms_size = ms.length(); if (column == 0) { column = field_width / 2 - ms_size / 2; } for (size_t i = 0; i < ms_size; i++) { field[place][column + i] = ms[i]; } return; } bool Snake::go() { Element temp, first, second; temp = first = body.front(); bool big_snake = true; if ((int) body.size() > 1) { second = body[1]; } else { big_snake = false; } if (direction == direction_right) { temp.x++; if (is_wall(temp)) { return true; } if (big_snake) { if (second == temp) { return true; } first.x++; } else { first.x++; } } else if (direction == direction_left) { temp.x--; if (is_wall(temp)) { return true; } if (big_snake) { if (second == temp) { return true; } first.x--; } else { first.x--; } } else if (direction == direction_down) { temp.y++; if (is_wall(temp)) { return true; } if (big_snake) { if (second == temp) { return true; } first.y++; } else { first.y++; } } else if (direction == direction_up) { temp.y--; if (is_wall(temp)) { return true; } if (big_snake) { if (second == temp) { return true; } first.y--; } else { first.y--; } } else { return true; } body.push_front(first); eat_food(); body.pop_back(); fill_field(); return false; } bool Snake::is_wall(Element element) const { if (field[element.y][element.x] == wall_simbol) { return true; } return false; } void Snake::eat_food() { if (body[0] == food_xy) { Element temp(0, 0); body.push_back(temp); food(); } return; } void Snake::food() { srand(time(0)); int rand_numberY = rand() % (field_hight - 2) + 1; int rand_numberX = rand() % (field_width - 2) + 1; if (field[rand_numberY][rand_numberX] == field_simbol && rand_numberX > 0 && rand_numberY > 0) { food_xy.x = rand_numberX; food_xy.y = rand_numberY; } else { food(); } return; } void Snake::fill_field() { make_field(); Element temp; deque<Element>::iterator it; it = body.begin(); do { if (*it == body[0]) { field[(*it).y][(*it).x] = snake_head; } else { field[(*it).y][(*it).x] = snake_simbol; } it++; } while (it != body.end()); if (food_xy == body[0]) { food(); } field[food_xy.y][food_xy.x] = food_simbol; return; } bool Snake::check() { if (body.size() < 2) { return false; } deque<Element>::iterator it; it = body.begin() + 1; while (it != body.end()) { if (*body.begin() == *it) { return true; } it++; } return false; } void Snake::restart() { body.clear(); Element temp(field_width / 2, field_hight / 2); body.push_back(temp); food_xy = temp; gameover_var = false; direction = direction_right; make_field(); menu(); show_field(); choice(1); return; } void Snake::about() { ms_generator(MS_AUTHOR, 3, 5); ms_generator(MS_HOMEPAGE, 4, 5); ms_generator(MS_VERSION, 5, 5); ms_generator(MS_DATE, 6, 5); return; } // Finish of file snake.cpp int main() { Snake snake_game; while (!snake_game.gameover()) { snake_game.move(); } int goodby; cout << "Goodby dear player. Enter number to exit: "; cin >> goodby; return 0; } |


