Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <stdlib.h>
- #include <curses.h>
- using namespace std;
- const int WIDTH = 80;
- const int HEIGHT = 24;
- bool gameOn = true;
- class Board {
- private:
- int board[HEIGHT][WIDTH];
- public:
- void init();
- void draw();
- };
- class Cursor {
- private:
- int x = WIDTH / 4 - 1, y = HEIGHT / 2;
- int dx = 0, dy = 0;
- public:
- void draw();
- void setpos(int y, int x);
- void setdir(int dy, int dx);
- int getx();
- int gety();
- int getdx();
- int getdy();
- };
- Board board;
- Cursor cursor;
- int main() {
- // init stuff
- srand(time(NULL));
- initscr();
- curs_set(0);
- keypad(stdscr, TRUE);
- noecho();
- int newx, newy;
- // init board
- board.init();
- // begin main loop
- while(gameOn == true) {
- refresh();
- clear();
- // draw text
- mvprintw(HEIGHT, 0, "ARTILLERY v1.0");
- mvprintw(HEIGHT, WIDTH - 11, "PLACE UNITS");
- // move cursor to new position
- newx = cursor.getx() + cursor.getdx();
- newy = cursor.gety() + cursor.getdy();
- if(newx > 0 and newx < WIDTH / 2 - 2) {
- if(newy >= 0 and newy < HEIGHT) {
- cursor.setpos(newy, newx);
- }
- }
- board.draw();
- cursor.draw();
- // handle input
- int key = getch();
- // move left
- if(key == KEY_LEFT) {
- cursor.setdir(0, -1);
- }
- // move right
- if(key == KEY_RIGHT) {
- cursor.setdir(0, 1);
- }
- // move up
- if(key == KEY_UP) {
- cursor.setdir(-1, 0);
- }
- // move down
- if(key == KEY_DOWN) {
- cursor.setdir(1, 0);
- }
- }
- // end main loop
- // quit curses
- endwin();
- return 0;
- }
- void Board::init() {
- // init board
- for(int y = 0; y < HEIGHT; y++) {
- for(int x = 0; x < WIDTH; x++) {
- board[y][x] = 0;
- }
- }
- }
- // game board functions
- void Board::draw() {
- // draw board
- for(int y = 0; y < HEIGHT; y++) {
- for(int x = 0; x < WIDTH; x++) {
- if(board[y][x] == 0) {
- mvaddch(y, x, ' ');
- }
- }
- }
- // draw divider line
- for(int y = 0; y < HEIGHT + 1; y++) {
- for(int x = WIDTH / 2 - 1; x < WIDTH / 2 + 1; x++) {
- mvaddch(y, x, '|');
- }
- }
- }
- // cursor functions
- void Cursor::setpos(int y, int x) {
- this->x = x;
- this->y = y;
- this->dx = 0;
- this->dy = 0;
- }
- void Cursor::setdir(int dy, int dx) {
- this->dx = dx;
- this->dy = dy;
- }
- void Cursor::draw() {
- mvaddch(this->y, this->x - 1, '[');
- mvaddch(this->y, this->x + 1, ']');
- }
- int Cursor::getx() {
- return x;
- }
- int Cursor::gety() {
- return y;
- }
- int Cursor::getdx() {
- return dx;
- }
- int Cursor::getdy() {
- return dy;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement