Advertisement
Guest User

Untitled

a guest
May 8th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <vector>
  4. #include <conio.h>
  5. #include <windows.h>
  6.  
  7. using namespace std;
  8.  
  9. /* Class prototype */
  10. class Board_level;
  11. class PlayerOn_Board;
  12.  
  13.  
  14. /* Class definition */
  15. class Board_level{
  16. public:
  17. vector<vector<char> > myBoard;
  18. int xAxis;
  19. int yAxis;
  20. /* (x = columns / y = rows) */
  21. int MINX = 10, MINY = 10, MAXX = 50, MAXY = 50;
  22. Board_level();
  23. void setValues(int columns, int rows);
  24. void createBoard(int Xcols, int Yrows);
  25. void drawBoard();
  26. void displayBorder();
  27. ~Board_level();
  28. };
  29.  
  30. /* Class "Board_Level" Functions */
  31.  
  32. Board_level::Board_level(){
  33. xAxis = 10;
  34. yAxis = 10;
  35. }
  36.  
  37.  
  38. void Board_level::setValues(int columns, int rows){
  39. if (rows < MINY || rows > MAXY){
  40. rows = 10;
  41. }
  42. if (columns < MINX || columns > MAXX){
  43. columns = 10;
  44. }
  45.  
  46. yAxis = rows;
  47. xAxis = columns;
  48. createBoard(xAxis, yAxis);
  49. }
  50.  
  51. void Board_level::createBoard(int Xcols, int Yrows){
  52. /* Resizes double dimensional vector based on user's input */
  53. myBoard.resize(Yrows);
  54. for (int i = 0; i < Yrows; ++i){
  55. myBoard[i].resize(Xcols);
  56. }
  57. /* fills an array with '.' */
  58. for (int y = 0; y < Yrows; y++){
  59. for (int x = 0; x < Xcols; x++)
  60. {
  61. myBoard[y][x] = ' ';
  62. }
  63. }
  64. }
  65.  
  66.  
  67. void Board_level::drawBoard(){
  68. displayBorder();
  69. for (int y = 0; y < yAxis; y++){
  70. cout << endl;
  71. for (int x = 0; x < xAxis; x++)
  72. {
  73. cout << myBoard[y][x];
  74. }
  75. }
  76. }
  77.  
  78. void Board_level::displayBorder(){
  79. for (int x = 0; x < xAxis; x++){
  80. myBoard[0][x] = '+';
  81. }
  82. for (int y = 0; y < yAxis; y++){
  83. myBoard[y][0] = '+';
  84. }
  85. for (int x = 0; x < xAxis; x++){
  86. myBoard[(yAxis-1)][x] = '+';
  87. }
  88. for (int y = 0; y < yAxis; y++){
  89. myBoard[y][(xAxis-1)] = '+';
  90. }
  91. }
  92.  
  93. Board_level::~Board_level(){
  94.  
  95. }
  96.  
  97.  
  98.  
  99.  
  100. /* PlayerOn_Board class */
  101.  
  102. class PlayerOn_Board {
  103. private:
  104. int playerX;
  105. int playerY;
  106. int keyPressed;
  107. int lastMove;
  108. public:
  109. PlayerOn_Board();
  110. void setPosition(int,int);
  111. void movePlayer(int keyValue);
  112. void drawPlayer_board(Board_level *test);
  113. bool keyInput();
  114. int returnX();
  115. int returnY();
  116. bool collisionDetection(const Board_level&);
  117. void playerAutomove();
  118. ~PlayerOn_Board();
  119. };
  120.  
  121. /* Class "PlayerOn_Board" functions */
  122.  
  123. PlayerOn_Board::PlayerOn_Board(){
  124. playerX = 1;
  125. playerY = 1;
  126. keyPressed = 1000;
  127. lastMove = 4;
  128. }
  129.  
  130. void PlayerOn_Board::setPosition(int xPos, int yPos){
  131. playerX = xPos;
  132. playerY = yPos;
  133. }
  134.  
  135. void PlayerOn_Board::movePlayer(int keyValue){
  136. if (keyValue == 72){
  137. playerY--;
  138. }
  139. if (keyValue == 80){
  140. playerY++;
  141. }
  142. if (keyValue == 77){
  143. playerX++;
  144. }
  145. if (keyValue == 75){
  146. playerX--;
  147. }
  148. }
  149.  
  150. int PlayerOn_Board::returnX(){
  151. return playerX;
  152. }
  153.  
  154. int PlayerOn_Board::returnY(){
  155. return playerY;
  156. }
  157.  
  158.  
  159. void PlayerOn_Board::drawPlayer_board(Board_level *test){
  160. test->myBoard[playerY][playerX] = '*';
  161. test->drawBoard();
  162. }
  163.  
  164. bool PlayerOn_Board::keyInput(){
  165. keyPressed=getch();
  166. if (keyPressed == 0 || keyPressed == 224){
  167. keyPressed=getch();
  168. if (keyPressed == 72 || keyPressed == 80 || keyPressed == 75 || keyPressed == 77){
  169. /* 1 - up, 2 - down, 3 - left, 4 - right
  170. This is used for the game loop to continue moving the snake in the direction it last moved.
  171. */
  172. if (keyPressed == 72)
  173. lastMove = 1;
  174. if (keyPressed == 80)
  175. lastMove = 2;
  176. if (keyPressed == 75)
  177. lastMove = 3;
  178. if (keyPressed == 77)
  179. lastMove = 4;
  180. movePlayer(keyPressed);
  181. }
  182. else{
  183. return false;
  184. }
  185. }
  186. return true;
  187. }
  188.  
  189. bool PlayerOn_Board::collisionDetection(const Board_level& collision){
  190. if (collision.myBoard[playerY][playerX] == '+' ){
  191. return false;
  192. }
  193. return true;
  194. }
  195.  
  196. void PlayerOn_Board::playerAutomove(){
  197. if (lastMove == 1){
  198. playerY--;
  199. }
  200. if (lastMove == 2){
  201. playerY++;
  202. }
  203. if (lastMove == 3){
  204. playerX++;
  205. }
  206. if (lastMove == 4){
  207. playerX--;
  208. }
  209. }
  210.  
  211. PlayerOn_Board::~PlayerOn_Board(){
  212.  
  213. }
  214.  
  215. /* Main */
  216.  
  217. int main(){
  218. Board_level board;
  219. PlayerOn_Board player;
  220. /* columns / rows MAX(50/50)*/
  221. board.setValues(45,30);
  222. player.drawPlayer_board(&board);
  223.  
  224. while (player.keyInput()){
  225.  
  226. if (!player.collisionDetection(board)){
  227. break;
  228. }
  229. system("cls");
  230. player.drawPlayer_board(&board);
  231. }
  232. return 0;
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement