Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- #include <vector>
- #include <conio.h>
- #include <windows.h>
- using namespace std;
- /* Class prototype */
- class Board_level;
- class PlayerOn_Board;
- /* Class definition */
- class Board_level{
- public:
- vector<vector<char> > myBoard;
- int xAxis;
- int yAxis;
- /* (x = columns / y = rows) */
- int MINX = 10, MINY = 10, MAXX = 50, MAXY = 50;
- Board_level();
- void setValues(int columns, int rows);
- void createBoard(int Xcols, int Yrows);
- void drawBoard();
- void displayBorder();
- ~Board_level();
- };
- /* Class "Board_Level" Functions */
- Board_level::Board_level(){
- xAxis = 10;
- yAxis = 10;
- }
- void Board_level::setValues(int columns, int rows){
- if (rows < MINY || rows > MAXY){
- rows = 10;
- }
- if (columns < MINX || columns > MAXX){
- columns = 10;
- }
- yAxis = rows;
- xAxis = columns;
- createBoard(xAxis, yAxis);
- }
- void Board_level::createBoard(int Xcols, int Yrows){
- /* Resizes double dimensional vector based on user's input */
- myBoard.resize(Yrows);
- for (int i = 0; i < Yrows; ++i){
- myBoard[i].resize(Xcols);
- }
- /* fills an array with '.' */
- for (int y = 0; y < Yrows; y++){
- for (int x = 0; x < Xcols; x++)
- {
- myBoard[y][x] = ' ';
- }
- }
- }
- void Board_level::drawBoard(){
- displayBorder();
- for (int y = 0; y < yAxis; y++){
- cout << endl;
- for (int x = 0; x < xAxis; x++)
- {
- cout << myBoard[y][x];
- }
- }
- }
- void Board_level::displayBorder(){
- for (int x = 0; x < xAxis; x++){
- myBoard[0][x] = '+';
- }
- for (int y = 0; y < yAxis; y++){
- myBoard[y][0] = '+';
- }
- for (int x = 0; x < xAxis; x++){
- myBoard[(yAxis-1)][x] = '+';
- }
- for (int y = 0; y < yAxis; y++){
- myBoard[y][(xAxis-1)] = '+';
- }
- }
- Board_level::~Board_level(){
- }
- /* PlayerOn_Board class */
- class PlayerOn_Board {
- private:
- int playerX;
- int playerY;
- int keyPressed;
- int lastMove;
- public:
- PlayerOn_Board();
- void setPosition(int,int);
- void movePlayer(int keyValue);
- void drawPlayer_board(Board_level *test);
- bool keyInput();
- int returnX();
- int returnY();
- bool collisionDetection(const Board_level&);
- void playerAutomove();
- ~PlayerOn_Board();
- };
- /* Class "PlayerOn_Board" functions */
- PlayerOn_Board::PlayerOn_Board(){
- playerX = 1;
- playerY = 1;
- keyPressed = 1000;
- lastMove = 4;
- }
- void PlayerOn_Board::setPosition(int xPos, int yPos){
- playerX = xPos;
- playerY = yPos;
- }
- void PlayerOn_Board::movePlayer(int keyValue){
- if (keyValue == 72){
- playerY--;
- }
- if (keyValue == 80){
- playerY++;
- }
- if (keyValue == 77){
- playerX++;
- }
- if (keyValue == 75){
- playerX--;
- }
- }
- int PlayerOn_Board::returnX(){
- return playerX;
- }
- int PlayerOn_Board::returnY(){
- return playerY;
- }
- void PlayerOn_Board::drawPlayer_board(Board_level *test){
- test->myBoard[playerY][playerX] = '*';
- test->drawBoard();
- }
- bool PlayerOn_Board::keyInput(){
- keyPressed=getch();
- if (keyPressed == 0 || keyPressed == 224){
- keyPressed=getch();
- if (keyPressed == 72 || keyPressed == 80 || keyPressed == 75 || keyPressed == 77){
- /* 1 - up, 2 - down, 3 - left, 4 - right
- This is used for the game loop to continue moving the snake in the direction it last moved.
- */
- if (keyPressed == 72)
- lastMove = 1;
- if (keyPressed == 80)
- lastMove = 2;
- if (keyPressed == 75)
- lastMove = 3;
- if (keyPressed == 77)
- lastMove = 4;
- movePlayer(keyPressed);
- }
- else{
- return false;
- }
- }
- return true;
- }
- bool PlayerOn_Board::collisionDetection(const Board_level& collision){
- if (collision.myBoard[playerY][playerX] == '+' ){
- return false;
- }
- return true;
- }
- void PlayerOn_Board::playerAutomove(){
- if (lastMove == 1){
- playerY--;
- }
- if (lastMove == 2){
- playerY++;
- }
- if (lastMove == 3){
- playerX++;
- }
- if (lastMove == 4){
- playerX--;
- }
- }
- PlayerOn_Board::~PlayerOn_Board(){
- }
- /* Main */
- int main(){
- Board_level board;
- PlayerOn_Board player;
- /* columns / rows MAX(50/50)*/
- board.setValues(45,30);
- player.drawPlayer_board(&board);
- while (player.keyInput()){
- if (!player.collisionDetection(board)){
- break;
- }
- system("cls");
- player.drawPlayer_board(&board);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement