Advertisement
mvaganov

consolerpg.cpp

Jan 14th, 2015
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <stdlib.h> // need this for system
  4. #include "platform_conio.h"
  5. using namespace std;
  6.  
  7. void resetToStart() {
  8.     platform_move(0,0);
  9. }
  10.  
  11. char getCH() { // get a  single key from the user
  12.     return platform_getchar();
  13. }
  14.  
  15. struct Vec2 {
  16.     int x, y;
  17.     bool isEqual(Vec2 point) {
  18.         return x == point.x && y == point.y;
  19.     }
  20. };
  21.  
  22. struct Entity {
  23.     Vec2 position;
  24.     char icon;
  25. };
  26.  
  27. int whichPlayerIsHere(Vec2 point, Entity * list, int listSize) {
  28.     for(int i = 0; i < listSize; ++i) {
  29.         if(list[i].position.isEqual(point)) {
  30.             return i;
  31.         }
  32.     }
  33.     return -1;
  34. }
  35.  
  36. int main() {
  37.     Vec2 size = {20, 15};
  38.     Entity players[] = {
  39.         {{ 4, 5}, '@'},
  40.         {{ 2, 4}, 'A'},
  41.         {{12, 8}, 'X'}
  42.     };
  43.     const int playersCount = sizeof(players)/sizeof(players[0]);
  44.     int userInput;
  45.     bool gameRunning = true;
  46.     int selectedPlayer = 0;
  47.     // gameloop
  48.     while(gameRunning) {
  49.         // draw
  50.         resetToStart();
  51.         Vec2 cursor;
  52.         for(cursor.y = 0; cursor.y < size.y; ++cursor.y) {
  53.             for(cursor.x = 0; cursor.x < size.x; ++cursor.x) {
  54.                 putchar('.');
  55.             }
  56.             putchar('\n');
  57.         }
  58.         for(int i = 0; i < playersCount; ++i)
  59.         {
  60.             platform_move(players[i].position.y, players[i].position.x);
  61.             putchar(players[i].icon);
  62.         }
  63.         while(!platform_kbhit()) { platform_sleep(1); } // wait for key press
  64.         // get input from user
  65.         userInput = platform_getchar();
  66.         // process game based on user input
  67.         switch(userInput) {
  68.         case 'w':   players[selectedPlayer].position.y--;   break;
  69.         case 'a':   players[selectedPlayer].position.x--;   break;
  70.         case 's':   players[selectedPlayer].position.y++;   break;
  71.         case 'd':   players[selectedPlayer].position.x++;   break;
  72.         case 'q':   gameRunning = false;    break;
  73.         case '+':  
  74.             selectedPlayer++;
  75.             if(selectedPlayer >= playersCount)
  76.                 selectedPlayer = 0;
  77.             break;
  78.         case '-':  
  79.             selectedPlayer--;
  80.             if(selectedPlayer <= 0)
  81.                 selectedPlayer = playersCount-1;
  82.             break;
  83.         case 'e':   players[selectedPlayer].icon++; break;
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement