Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by A simple game for Nick ( 16 years ago )
#include <iostream>
#include <windows.h>

using namespace std;

int collision = 0;
char GameMap[19][79] = {"  ###########################################################################",
      "  #        ##                                                               #",
      "  #        ##                                                               #",
      "  #        ##                                                     ###########",
      "  #        ##                                                     ###########",
      "  #        ##                                                               #",
      "  #        ##                                                               #",
      "  #        ##                                                               #",
      "  #                                                                         #",
      "  #                                                                         #",
      "  #                                                                         #",
      "  #                                                                         #",
      "  #######               ########################               ##           #",
      "  #######               ########################               ##           #",
      "  #                                                            ##           #",
      "  #                                                            ##           #",
      "  #                                                            ##           #",
      "  #                                                            ##           #",
      "  ###########################################################################"};

class Player
{
 public:
  Player(int locX, int locY, int upKey, int downKey, int leftKey, int rightKey, char playerChar);
  void HandleKeys();
  
 private:
  int mxpos;
  int mypos;
  int up;
  int down;
  int left;
  int right;
  char pchar;
};

Player::Player(int locX, int locY, int upKey, int downKey, int leftKey, int rightKey, char playerChar)
{
 mxpos = locX;
 mypos = locY;
 up = upKey;
 down = downKey;
 left = leftKey;
 right = rightKey;
 pchar = playerChar;
 GameMap[mxpos][mypos] = pchar;
}

void Player::HandleKeys()
{
 if(GetAsyncKeyState(up))
 {
  GameMap[mxpos][mypos] = ' ';
  if(GameMap[mxpos - 1][mypos] != '#')
  {
   collision = 0;
   mxpos--;
   GameMap[mxpos][mypos] = pchar;
  }
  else
  {
   collision = 1;
   GameMap[mxpos][mypos] = pchar;
  }
 }
 else if(GetAsyncKeyState(down))
 {
  GameMap[mxpos][mypos] = ' ';
  if(GameMap[mxpos + 1][mypos] != '#')
  {
   collision = 0;
   mxpos++;
   GameMap[mxpos][mypos] = pchar;
  }
  else
  {
   collision = 1;
   GameMap[mxpos][mypos] = pchar;
  }
 }
 else if(GetAsyncKeyState(left))
 {
  GameMap[mxpos][mypos] = ' ';
  if(GameMap[mxpos][mypos - 1] != '#')
  {
   collision = 0;
   mypos--;
   GameMap[mxpos][mypos] = pchar;
  }
  else
  {
   collision = 1;
   GameMap[mxpos][mypos] = pchar;
  }
 }
 else if(GetAsyncKeyState(right))
 {
  GameMap[mxpos][mypos] = ' ';
  if(GameMap[mxpos][mypos + 1] != '#')
  {
   int collision = 0;
   mypos++;
   GameMap[mxpos][mypos] = pchar;
  }
  else
  {
   collision = 1;
   GameMap[mxpos][mypos] = pchar;
  }
 }
 else if(GetAsyncKeyState(VK_ESCAPE))
 {
  exit(0);
 }
}

void DrawMap()
{
 for(int i = 0; i <= 18; i++)
 {
  cout << GameMap[i] << endl;
 }
 cout << "  #                                                                         #\n";
 cout << "  #                           Collision: " << collision << "                                  #\n";
 cout << "  #                                                                         #\n";
 cout << "  ###########################################################################";
}

void gotoxy(int x, int y)
{
 COORD pos = {x, y};
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

int main()
{
 Player p(10, 30, VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT, '*');
 while(1)
 {
  gotoxy(0, 0);
  DrawMap();
  p.HandleKeys();
 }
 cin.get();
 cin.get();
 return 0;
}

 

Revise this Paste

Your Name: Code Language: