Advertisement
Guest User

Untitled

a guest
Apr 6th, 2016
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. //game.h
  2. #pragma once
  3. #include <list>
  4.  
  5. class snakepoint {
  6. private:
  7.     int snx, sny;
  8. public:
  9.     snakepoint(int x, int y) : snx(x), sny(y) {};
  10.     ~snakepoint() {};
  11.     int getX() { return snx; };
  12.     int getY() { return sny; };
  13. };
  14.  
  15. class Game {
  16. public:
  17.     Game();
  18.     ~Game();
  19.     void init();
  20.     void draw();
  21.     void input();
  22.     void placefood();
  23.     void collision();
  24.     bool getend();
  25.     void endgame();
  26.     int getheadX();
  27.     int getheadY();
  28.  
  29. private:
  30.     std::list<snakepoint> snake;
  31.     int maxX, maxY, points, direction, ch, food_x, food_y;
  32.     std::list<snakepoint>::iterator it;
  33.     bool end;
  34. };
  35.  
  36. //game.cpp
  37. #include "game.h"
  38. #include <curses.h>
  39. #include <time.h>
  40.  
  41. Game::Game()
  42. {
  43.     direction = 0;
  44.     end = false;
  45.     points = 0;
  46. }
  47.  
  48. Game::~Game()
  49. {
  50. }
  51. void placefood();
  52.  
  53. int Game::getheadX() {
  54.     return snake.front().getX();
  55. }
  56.  
  57. int Game::getheadY() {
  58.     return snake.front().getY();
  59. }
  60.  
  61. void Game::init() {
  62.    
  63.     initscr();
  64.     noecho();
  65.     curs_set(FALSE);
  66.     keypad(stdscr, TRUE);
  67.     getmaxyx(stdscr, maxY, maxX);
  68.     snake.push_back(snakepoint(maxX / 2, maxY / 2));
  69.     this->placefood();
  70. }
  71.  
  72. void Game::placefood() {
  73.     srand(time(NULL));
  74.     bool t = true;
  75.     while (t) {
  76.         t = false;
  77.         food_x = rand() % (maxX - 1) + 1;
  78.         food_y = rand() % (maxY - 1) + 1;
  79.         for (auto x : snake) {
  80.             if (food_x == x.getX() && food_y == x.getY()) {
  81.                 t = true;
  82.                 break;
  83.             }
  84.         }
  85.     }
  86. }
  87.  
  88. void Game::input() {
  89.     ch = getch();
  90.     switch (ch) {
  91.         case KEY_UP:    direction=3; break;
  92.         case KEY_DOWN:  direction=1; break;
  93.         case KEY_RIGHT: direction=0; break;
  94.         case KEY_LEFT:  direction=2; break;
  95.     }
  96. }
  97.  
  98. void Game::collision() {
  99.     it = snake.begin();
  100.     std::advance(it, 1);
  101.     for (; it != snake.end(); it++) {
  102.         if (this->getheadX() == it->getX() && this->getheadY() == it->getY()) {
  103.             end = true;
  104.         }
  105.     }
  106.     if (this->getheadX() == 0 || this->getheadX() == maxX) end = true;
  107.     if (this->getheadY() == 0 || this->getheadY() == maxY) end = true;
  108.     if (this->getheadX() == food_x && this->getheadY() == food_y) {
  109.         this->placefood();
  110.         points++;
  111.     }
  112.     else {
  113.         snake.pop_back();
  114.     }
  115.    
  116.    
  117. }
  118.  
  119. void Game::draw(){
  120.     int tempx = this->getheadX();
  121.     int tempy = this->getheadY();
  122.     if (direction == 0) tempx++;
  123.     if (direction == 1) tempy++;
  124.     if (direction == 2) tempx--;
  125.     if (direction == 3) tempy--;
  126.     this->collision();
  127.     snake.push_front(snakepoint(tempx, tempy));
  128.     erase();
  129.     mvaddch(food_y, food_x, '*');
  130.     for (auto point : snake) {
  131.         mvaddch(point.getY(), point.getX(), '@');
  132.     }
  133.         refresh();
  134. }
  135.  
  136. bool Game::getend(){
  137.     return end;
  138. }
  139.  
  140. void Game::endgame() {
  141.     timeout(-1);
  142.     erase();
  143.     start_color();
  144.     init_pair(1, COLOR_RED, COLOR_BLACK);
  145.     attron(COLOR_PAIR(1));
  146.     mvprintw(maxY/2, maxX/2, "Game over");
  147.     mvprintw((maxY / 2) + 1, maxX / 2, "You gained %i points", points);
  148.     refresh();
  149.     getch();
  150.     endwin();
  151. }
  152.  
  153. //main.cpp
  154. #include <curses.h>
  155. #include "game.h"
  156. #include <stdlib.h>
  157.  
  158. int main() {
  159.     Game * gm = new Game;
  160.     gm->init();
  161.     timeout(100);
  162.     while (!gm->getend()) {
  163.         gm->input();
  164.         gm->draw();
  165.     }
  166.     gm->endgame();
  167.     delete gm;
  168.     return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement