Advertising
- Unnamed
- Wednesday, June 13th, 2012 at 11:47:56pm MDT
- #include <curses.h>
- // Map dimensions
- #define MAP_WIDTH 40
- #define MAP_HEIGHT 24
- // Global variables
- int PlayerX = 9;
- int PlayerY = 5;
- int PlayerTurn;
- // Message containers
- const char* MsgArray[7];
- const char* Msg = "";
- // Map container
- char MapArray[MAP_HEIGHT][MAP_WIDTH + 1] = {
- "^^*^*^^^********************************",
- "^*^^^^**********************Y***********",
- "^^^***************Y******Y**************",
- "*^********************************Y*****",
- "^^*****#######*********Y****************",
- "^******#.....#**************Y***********",
- "*******#.....#**************************",
- "*******#.....#**************************",
- "**Y****###+###*******~~~~~*********Y****",
- "*******************~~~~~~~~*************",
- "*******************~~~~~~~~*************",
- "*********************~~~~~***Y**********",
- "****Y***********************************",
- "****************************************",
- "***********************************Y****",
- "****************************************",
- "*********************####+#####*********",
- "***************Y*****#........#*********",
- "****Y****************#........#*********",
- "*********************##+###...#**Y******",
- "************Y********#....#...#*********",
- "*********************#..>.#...#*******Y*",
- "*******Y*************##########*********",
- "****************************************"
- };
- // Map drawing function
- void DrawMap(void) {
- for(int y = 0; y < MAP_HEIGHT; y++) {
- for(int x = 0; x < MAP_WIDTH; x++) {
- // Colorizer
- switch(MapArray[y][x]) {
- case '*':
- attron(COLOR_PAIR(3));
- break;
- case '~':
- attron(COLOR_PAIR(4));
- break;
- case 'Y':
- attron(COLOR_PAIR(2));
- break;
- case '^':
- attron(COLOR_PAIR(1));
- break;
- case '+':
- attron(COLOR_PAIR(2));
- break;
- case '/':
- attron(COLOR_PAIR(2));
- break;
- }
- // Draw characters and reset colors
- mvaddch(y, x, MapArray[y][x]);
- attroff(COLOR_PAIR(2));
- }
- }
- }
- // Collision detect function
- bool IsPassable(int MapX, int MapY) {
- // Make area outside map impassible
- if( MapX < 0 || MapX >= MAP_WIDTH || MapY < 0 || MapY >= MAP_HEIGHT )
- return false;
- int Ch = MapArray[MapY][MapX];
- // Check which characters are passible.
- if(Ch == '.' || Ch == '*' || Ch == '/' || Ch == '>' || Ch == '<')
- return true;
- // Otherwise not passible
- return false;
- }
- // Info screen function
- void DrawInfo(void) {
- // Status
- mvprintw(0, 41, "Player the Nobody");
- mvprintw(0, 66, "%d", PlayerTurn);
- attron( COLOR_PAIR(1) );
- mvprintw(2, 41, "Life: xxx/xxx");
- attroff( COLOR_PAIR(1) );
- attron( COLOR_PAIR(2) );
- mvprintw(3, 41, "Level: xx");
- attroff( COLOR_PAIR(2) );
- attron( COLOR_PAIR(3) );
- mvprintw(2, 66, "Mana: xxx/xxx");
- attroff( COLOR_PAIR(3) );
- attron( COLOR_PAIR(5) );
- mvprintw(3, 66, "Exp: xxxxxxxx");
- attroff( COLOR_PAIR(5) );
- // Inventory
- mvprintw(5, 41, "---------------------------- Inventory");
- // Messages
- mvprintw(16,41, "----------------------------- Messages");
- // Check for message and shift array
- if(Msg != "") {
- for(int x = 6; x > 0; x--) {
- MsgArray[x] = MsgArray[x - 1];
- }
- // Push current message into message array
- MsgArray[0] = Msg;
- Msg = "";
- }
- // Print the message array
- for(int x = 0; x < 7; x++) {
- int ypos = 23 - x;
- mvaddstr(ypos, 41, MsgArray[x]);
- }
- }
- // Door close function
- bool CloseDoor(void) {
- int Key = getch();
- int DeltaX, DeltaY = 0;
- switch(Key) {
- // Which direction?
- case 'k':
- DeltaX = 0;
- DeltaY = -1;
- break;
- case 'h':
- DeltaX = -1;
- DeltaY = 0;
- break;
- case 'l':
- DeltaX = 1;
- DeltaY = 0;
- break;
- case 'j':
- DeltaX = 0;
- DeltaY = 1;
- break;
- default:
- Msg = "Invalid direction.";
- return false;
- }
- if(MapArray[PlayerY + DeltaY][PlayerX + DeltaX] == '/') {
- MapArray[PlayerY + DeltaY][PlayerX + DeltaX] = '+';
- return true;
- }
- else {
- Msg = "You can't close that.";
- return false;
- }
- }
- // Main function
- int main( void ) {
- // Init curses
- keypad( initscr(), 1 );
- curs_set( 0 );
- noecho();
- // Init colors
- start_color();
- init_pair( 1, COLOR_RED, COLOR_BLACK );
- init_pair( 2, COLOR_YELLOW, COLOR_BLACK );
- init_pair( 3, COLOR_GREEN, COLOR_BLACK );
- init_pair( 4, COLOR_BLUE, COLOR_BLACK );
- init_pair( 5, COLOR_CYAN, COLOR_BLACK );
- init_pair( 6, COLOR_MAGENTA, COLOR_BLACK );
- // Game Loop
- while( true ) {
- int Key = 0;
- int DeltaX = 0;
- int DeltaY = 0;
- // Refresh screen
- erase();
- // Draw map
- DrawMap();
- // Draw player
- attron( COLOR_PAIR(5) );
- mvaddch( PlayerY, PlayerX, '@' );
- attroff( COLOR_PAIR(5) );
- // Info screen
- DrawInfo();
- // Wait for input
- Key = getch();
- switch(Key) {
- // Move up
- case 'k':
- DeltaX = 0;
- DeltaY = -1;
- break;
- // Move left
- case 'h':
- DeltaX = -1;
- DeltaY = 0;
- break;
- // Move right
- case 'l':
- DeltaX = 1;
- DeltaY = 0;
- break;
- // Move down
- case 'j':
- DeltaX = 0;
- DeltaY = 1;
- break;
- // Move Up-left
- case 'y':
- DeltaX = -1;
- DeltaY = -1;
- break;
- // Move Up-right
- case 'u':
- DeltaX = 1;
- DeltaY = -1;
- break;
- // Move Down-left
- case 'b':
- DeltaX = -1;
- DeltaY = 1;
- break;
- // Move Down-right
- case 'n':
- DeltaX = 1;
- DeltaY = 1;
- break;
- // Wait one turn
- case '.':
- break;
- // Quit
- case 'q':
- return endwin();
- break;
- // Close door
- case 'c':
- if(CloseDoor()) {
- break;
- }
- else {
- continue;
- }
- // Else
- default:
- Msg = "Invalid command.";
- continue;
- }
- // Open doors
- if( MapArray[PlayerY + DeltaY][PlayerX + DeltaX] == '+' ) {
- MapArray[PlayerY + DeltaY][PlayerX + DeltaX] = '/';
- DeltaX, DeltaY = 0; // Prevent walking through door
- }
- // Check if direction is passable and move
- if( IsPassable(PlayerX + DeltaX, PlayerY + DeltaY) ) {
- PlayerX += DeltaX;
- PlayerY += DeltaY;
- }
- else {
- Msg = "You can't go that way.";
- continue;
- }
- // Increment turn count
- PlayerTurn++;
- }
- }
advertising
Update the Post
Either update this post and resubmit it with changes, or make a new post.
You may also comment on this post.
Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.