Advertisement
Guest User

Untitled

a guest
May 4th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <stdlib.h>
  4. #include <curses.h>
  5.  
  6. using namespace std;
  7.  
  8. const int WIDTH  = 80;
  9. const int HEIGHT = 24;
  10.  
  11. bool gameOn = true;
  12.  
  13. class Board {
  14.   private:
  15.     int board[HEIGHT][WIDTH];
  16.   public:
  17.     void init();
  18.     void draw();
  19. };
  20.  
  21. class Cursor {
  22.   private:
  23.     int x = WIDTH / 4 - 1, y = HEIGHT / 2;
  24.     int dx = 0, dy = 0;
  25.   public:
  26.     void draw();
  27.     void setpos(int y, int x);
  28.     void setdir(int dy, int dx);
  29.     int getx();
  30.     int gety();
  31.     int getdx();
  32.     int getdy();
  33. };
  34.  
  35. Board board;
  36. Cursor cursor;
  37.  
  38. int main() {
  39.   // init stuff
  40.   srand(time(NULL));
  41.   initscr();
  42.   curs_set(0);
  43.   keypad(stdscr, TRUE);
  44.   noecho();
  45.   int newx, newy;
  46.  
  47.   // init board
  48.   board.init();
  49.  
  50.   // begin main loop
  51.   while(gameOn == true) {
  52.     refresh();
  53.     clear();
  54.  
  55.     // draw text
  56.     mvprintw(HEIGHT, 0, "ARTILLERY v1.0");
  57.     mvprintw(HEIGHT, WIDTH - 11, "PLACE UNITS");
  58.  
  59.     // move cursor to new position
  60.     newx = cursor.getx() + cursor.getdx();
  61.     newy = cursor.gety() + cursor.getdy();
  62.     if(newx > 0 and newx < WIDTH / 2 - 2) {
  63.       if(newy >= 0 and newy < HEIGHT) {
  64.       cursor.setpos(newy, newx);
  65.       }
  66.     }
  67.  
  68.     board.draw();
  69.     cursor.draw();
  70.  
  71.     // handle input
  72.     int key = getch();
  73.  
  74.     // move left
  75.     if(key == KEY_LEFT) {
  76.       cursor.setdir(0, -1);
  77.     }
  78.     // move right
  79.     if(key == KEY_RIGHT) {
  80.       cursor.setdir(0, 1);
  81.     }
  82.     // move up
  83.     if(key == KEY_UP) {
  84.       cursor.setdir(-1, 0);
  85.     }
  86.     // move down
  87.     if(key == KEY_DOWN) {
  88.       cursor.setdir(1, 0);
  89.     }
  90.   }
  91.   // end main loop
  92.  
  93.   // quit curses
  94.   endwin();
  95.   return 0;
  96.  
  97. }
  98.  
  99. void Board::init() {
  100.   // init board
  101.   for(int y = 0; y < HEIGHT; y++) {
  102.     for(int x = 0; x < WIDTH; x++) {
  103.       board[y][x] = 0;
  104.     }
  105.   }
  106. }
  107.  
  108. // game board functions
  109. void Board::draw() {
  110.   // draw board
  111.   for(int y = 0; y < HEIGHT; y++) {
  112.     for(int x = 0; x < WIDTH; x++) {
  113.       if(board[y][x] == 0) {
  114.         mvaddch(y, x, ' ');
  115.       }
  116.     }
  117.   }
  118.   // draw divider line
  119.   for(int y = 0; y < HEIGHT + 1; y++) {
  120.     for(int x = WIDTH / 2 - 1; x < WIDTH / 2 + 1; x++) {
  121.       mvaddch(y, x, '|');
  122.     }
  123.   }
  124. }
  125.  
  126. // cursor functions
  127. void Cursor::setpos(int y, int x) {
  128.   this->x = x;
  129.   this->y = y;
  130.   this->dx = 0;
  131.   this->dy = 0;
  132. }
  133. void Cursor::setdir(int dy, int dx) {
  134.   this->dx = dx;
  135.   this->dy = dy;
  136. }
  137. void Cursor::draw() {
  138.   mvaddch(this->y, this->x - 1, '[');
  139.   mvaddch(this->y, this->x + 1, ']');
  140. }
  141. int Cursor::getx() {
  142.   return x;
  143. }
  144. int Cursor::gety() {
  145.   return y;
  146. }
  147. int Cursor::getdx() {
  148.   return dx;
  149. }
  150. int Cursor::getdy() {
  151.   return dy;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement