Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- "Snake_8.5"
- Written By: Wilson P.
- <wperez274@gmail.com>
- */
- #include <ncurses.h>
- #include <list>
- #include <stdlib.h>
- #include <time.h>
- class Snake
- {
- private:
- int x, y;
- public:
- Snake(int a, int b)
- {
- x = a;
- y = b;
- }
- int getX()
- {
- return x;
- }
- int getY()
- {
- return y;
- }
- };
- enum Nav
- {
- LEFT = 1,
- RIGHT = 2,
- UP = 3,
- DOWN = 4,
- LEFT_UP = 5,
- RIGHT_UP = 6,
- LEFT_DOWN = 7,
- RIGHT_DOWN = 8
- };
- enum Border_limit
- {
- MIN_X = -1,
- MAX_X = 32,
- MIN_Y = 1,
- MAX_Y = 15
- };
- class Player
- {
- public:
- int getkey, dir, lives, energy, points, clock;
- };
- Player p = {
- -1,
- 2,
- 3,
- 20,
- 0,
- 0
- };
- class Machine
- {
- public:
- int dir, x, y;
- };
- Machine spaceship = {
- 1,
- 500,
- -100
- };
- class Food
- {
- public:
- int x, y, clock;
- };
- Food apple = {
- 20,
- 5,
- 0
- };
- void add_color()
- {
- start_color();
- bkgd(COLOR_PAIR(1));
- init_pair(1, COLOR_WHITE, COLOR_BLACK);
- init_pair(2, COLOR_GREEN, COLOR_BLACK);
- init_pair(3, COLOR_MAGENTA, COLOR_BLACK);
- init_pair(4, COLOR_RED, COLOR_BLACK);
- init_pair(5, COLOR_CYAN, COLOR_BLACK);
- init_pair(6, COLOR_WHITE, COLOR_BLUE);
- }
- void start_both_clocks()
- {
- apple.clock++;
- p.clock++;
- }
- void start_cursing()
- {
- srand(time(NULL));
- initscr();
- noecho();
- curs_set(0);
- keypad(stdscr, TRUE);
- timeout(200);
- }
- void draw()
- {
- attrset(COLOR_PAIR(6));
- attron(A_BOLD);
- for (int r = 1; r < 16; r++)
- {
- mvprintw(r, 0, "B");
- mvprintw(r, 32, "B");
- };
- mvprintw(1, 0, "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
- mvprintw(15, 0, "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
- attrset(COLOR_PAIR(1));
- attron(A_BOLD);
- mvprintw(0, 0, "Lives:%i", p.lives);
- mvprintw(0, 20, "Energy:%i", p.energy);
- mvprintw(16, 0, "Score:%i", p.points);
- mvprintw(16, 15, "Spaceship GPS: X=%i", spaceship.x);
- mvprintw(17, 30, "Y=%i", spaceship.y);
- attrset(COLOR_PAIR(4));
- attron(A_BOLD);
- mvprintw(apple.y, apple.x, "@");
- }
- void draw_plane()
- {
- attrset(COLOR_PAIR(1));
- attron(A_BOLD); mvprintw(spaceship.y, spaceship.x, "[==]");
- }
- void player_nav()
- {
- p.getkey = getch();
- switch (p.getkey)
- {
- case KEY_LEFT:
- case 'w':
- case 'a':
- case 's':
- case 'd':
- p.dir = LEFT;
- break;
- case KEY_RIGHT:
- case 'i':
- case 'o':
- case 'j':
- case 'k':
- case 'l':
- p.dir = RIGHT;
- break;
- case KEY_UP:
- case 'r':
- case 't':
- case 'y':
- case 'u':
- p.dir = UP;
- break;
- case KEY_DOWN:
- case 'c':
- case 'v':
- case 'b':
- case ' ':
- p.dir = DOWN;
- break;
- case 'q':
- p.dir=LEFT_UP;
- break;
- case 'p':
- p.dir=RIGHT_UP;
- break;
- case ',':
- p.dir=LEFT_DOWN;
- break;
- case '.':
- p.dir=RIGHT_DOWN;
- break;
- case 10:
- attrset(COLOR_PAIR(1));
- attron(A_BOLD);
- mvprintw(5, 12, " PAUSED ");
- refresh();
- while (1)
- {
- p.getkey = getch();
- if (p.getkey == 10)
- {
- break;
- }
- }
- break;
- }
- }
- void game_end()
- {
- erase();
- timeout(-1);
- attrset(COLOR_PAIR(1));
- attron(A_BOLD);
- mvprintw(6, 12, "GAME OVER");
- mvprintw(8, 12, "SCORE: %i", p.points);
- if (p.points<2000)
- mvprintw(10, 12, "Need more practice.");
- if (p.points>=2000 && p.points<=4000)
- mvprintw(10, 12, "Good Game!");
- if (p.points>4000 && p.points<=8000)
- mvprintw(10, 12, "You did fantastic!!!");
- if (p.points>8000)
- mvprintw(10, 12, "You are a Snake Master!!");
- refresh();
- getch();
- endwin();
- }
- int main()
- {
- start_cursing();
- add_color();
- std::list < Snake > snakes;
- std::list < Snake >::iterator it;
- for (int i = 0; i < 3; i++)
- snakes.push_front(Snake(5 + i, 6));
- while (1)
- {
- player_nav();
- start_both_clocks();
- Snake logic = snakes.front();
- int x = logic.getX();
- int y = logic.getY();
- switch (p.dir)
- {
- case LEFT:
- x--;
- break;
- case RIGHT:
- x++;
- break;
- case UP:
- y--;
- break;
- case DOWN:
- y++;
- break;
- case LEFT_UP:
- x--; y--;
- break;
- case RIGHT_UP:
- x++; y--;
- break;
- case LEFT_DOWN:
- x--; y++;
- break;
- case RIGHT_DOWN:
- x++; y++;
- break;
- }
- attron(A_BOLD);
- snakes.push_front(Snake(x, y));
- if (apple.clock >= 25 + rand() % 10 + 1)
- {
- apple.x = 1 + rand() % 29;
- apple.y = 2 + rand() % 11;
- p.energy -= 5;
- apple.clock = 0;
- }
- if (x == apple.x && y == apple.y)
- {
- apple.x = rand() % 29;
- apple.y = 2 + rand() % 11;
- p.energy += 2 + rand() % 5;
- p.points += 50 + rand() % 100;
- apple.clock = 0;
- }
- else
- snakes.pop_back();
- erase();
- if (spaceship.y >= MAX_Y)
- {
- spaceship.x = 1 + rand() % 25;
- spaceship.y = rand() % 1 + 2;
- }
- draw();
- if (p.clock >= 2 && p.clock <= 8)
- {
- attrset(COLOR_PAIR(1));
- mvprintw(5, 12, "STAGE 1 ");
- }
- if (p.clock >= 10 && p.clock <= 18)
- {
- attrset(COLOR_PAIR(1));
- mvprintw(2, 9, "Eat Food!");
- mvprintw(apple.y, apple.x - 2, ">");
- mvprintw(apple.y, apple.x + 2, "<");
- };
- if (spaceship.x<=15)
- {
- spaceship.dir = DOWN;
- draw_plane();
- }
- if (spaceship.dir == DOWN)
- spaceship.y++;
- if (spaceship.dir == LEFT)
- spaceship.x--;
- if (spaceship.x<=15 && spaceship.y>=-50 && spaceship.y<=-20)
- mvprintw(3,2,"WARNING: ENEMY SHIP IS HERE!");
- if (x >= spaceship.x && x <= spaceship.x + 3 && y == spaceship.y)
- {
- attrset(COLOR_PAIR(1));
- for (int r = 1; r < 12; r++)
- {
- mvprintw(r, x, ":");
- }
- p.energy -= 1 + rand() % 2;
- }
- attrset(A_BOLD);
- for (it = snakes.begin(); it != snakes.end(); it++)
- {
- attron(A_BOLD);
- attrset(COLOR_PAIR(2));
- attron(A_BOLD); mvprintw((*it).getY(), (*it).getX(), "@");
- if ((*it).getY() == y && (*it).getX() == x && it != snakes.begin())
- p.energy-=10;
- if (x < MIN_X){
- p.dir=RIGHT;
- p.energy-=5;
- };
- if (x > MAX_X){
- p.dir=LEFT;
- p.energy-=5;
- };
- if (y <= MIN_Y){
- p.dir=DOWN;
- p.energy-=5;
- };
- if (y >= MAX_Y){
- p.dir=UP;
- p.energy-=5;
- };
- }
- if (p.energy<1){
- p.energy=30;
- p.lives--;
- };
- if (p.energy>60){
- p.energy=30;
- p.lives++;
- };
- if (p.lives <= 0)
- game_end();
- if (x<apple.x)
- p.dir=RIGHT;
- if (x>apple.x)
- p.dir=LEFT;
- if (y<apple.y)
- p.dir=DOWN;
- if (y>apple.y)
- p.dir=UP;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement