Post by Nicky Peter Hollyoake on Nov 3, 2008 20:52:23 GMT -5
when u run this code:
/// Include files ///
#include <iostream>
#include <conio.h>
#include <string>
#include <windows.h>
/// End of include files ///
void gotoxy(int x, int y)
{
static HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
static COORD WritePos;
WritePos.X = x; //-1 for borland compatibility??
WritePos.Y = y;
SetConsoleCursorPosition(hStdOut,WritePos);
}
/// key function
int Key ()
{
int TheKey = 0;
if (kbhit())
{
TheKey = getch();
}
return TheKey;
}
/// Main
int main() {
/// other vars
int ControlKey;
/// Player vars
int PlayerX = 1, PlayerY = 1;
int TempX, TempY;
/// Make map
const int MapWidth = 46;
const int MapHeight = 19;
std::string Map[] = {"###############################################",
"# #",
"# ### #",
"# # # #",
"# @ ### #",
"# ###### # # #",
"# # # # #",
"# # # # #",
"# # # # #",
"# # # # #",
"# # ########### # #",
"# # #",
"# # #",
"# # #",
"# # #",
"# ############################## #",
"# #",
"# #",
"# #",
"###############################################"};
/// Search for player position
for (int i = 0; i <= MapHeight; i++) {
for (int j = 0; j <= MapWidth; j++) {
if (Map[i][j] == '@') {
Map[i][j] = ' ';
PlayerX = j;
PlayerY = i;
}
}
}
int FirstLoop = false;
/// Colors
system("Color 1c");
/// Start loop
while (true) {
ControlKey = Key();
/// Remember the last position the player was at
TempX = PlayerX;
TempY = PlayerY;
/* Key numbers
Left Arrow: 75
Up Arrow: 72
Down Arrow: 80
Right Arrow: 77
*/
/// Controls
if (ControlKey == 75) PlayerX--; // Left
if (ControlKey == 77) PlayerX++; // Right
if (ControlKey == 80) PlayerY++; // Down
if (ControlKey == 72) PlayerY--; // Up
/// Collision
// Keep the player in a box
if (PlayerX < 0) PlayerX = 0;
if (PlayerX > MapWidth) PlayerX = MapWidth;
if (PlayerY < 0) PlayerY = 0;
if (PlayerY > MapHeight) PlayerY = MapHeight;
// Wall collision (stops the player going through walls)
if (Map[PlayerY][PlayerX] == '#') {
PlayerX = TempX;
PlayerY = TempY;
}
/// Draw map
if (PlayerX != TempX or PlayerY != TempY or FirstLoop == false) {
/// System command
gotoxy(0,0);
for (int i = 0; i <= MapHeight; i++) {
for (int j = 0; j <= MapWidth; j++) {
if (PlayerX == j and PlayerY == i) { std::cout << "@"; } else { std::cout << Map[i][j]; }
}
std::cout << "\n";
}
}
FirstLoop = true;
}
}
.......is it a suppose to do this lol it also makes really annoying beeeping noises that are really loud over and over agian
here goes the picture
Because the map is out of order. First copy it to notepad then copy it into the IDE & go!
DarkJester:
This better? If not can you order it for me to show what you mean.
// Include files //
#include <iostream>
#include <conio.h>
#include <string>
#include <windows.h>
// End of include files //
void gotoxy(int x, int y)
{
static HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
static COORD WritePos;
WritePos.X = x;
WritePos.Y = y;
SetConsoleCursorPosition(hStdOut,WritePos);
}
// key function
int Key ()
{
int TheKey = 0;
if (kbhit())
{
TheKey = getch();
}
return TheKey;
}
// Main
int main() {
// other vars
int ControlKey;
// Player vars
int PlayerX = 1, PlayerY = 1;
int TempX, TempY;
// Make map
const int MapWidth = 46;
const int MapHeight = 19;
// Put the map in a string array
std::string Map[] = {"###############################################",
"# #",
"# ### #",
"# # # #",
"# @ ### #",
"# ###### # # #",
"# # # # #",
"# # # # #",
"# # # # #",
"# # # # #",
"# # ########### # #",
"# # #",
"# # #",
"# # #",
"# # #",
"# ############################## #",
"# #",
"# #",
"# #",
"###############################################"};
// Search for player position
for (int i = 0; i <= MapHeight; i++)
{
for (int j = 0; j <= MapWidth; j++)
{
if (Map[j] == '@')
{
Map[j] = ' ';
PlayerX = j;
PlayerY = i;
}
}
}
int FirstLoop = false;
// Colors
system("Color 1c");
// Start loop
while (true) {
// Remember the last position the player was at
TempX = PlayerX;
TempY = PlayerY;
// Get the key number
ControlKey = Key();
/* Key numbers
Left Arrow: 75
Up Arrow: 72
Down Arrow: 80
Right Arrow: 77
*/
// Controls
if (ControlKey == 75) PlayerX--; // Left
if (ControlKey == 77) PlayerX++; // Right
if (ControlKey == 80) PlayerY++; // Down
if (ControlKey == 72) PlayerY--; // Up
/* Collision
Keep the player in a box */
if (PlayerX < 0) PlayerX = 0;
if (PlayerX > MapWidth) PlayerX = MapWidth;
if (PlayerY < 0) PlayerY = 0;
if (PlayerY > MapHeight) PlayerY = MapHeight;
// Wall collision (stops the player going through walls)
if (Map[PlayerY][PlayerX] == '#')
{
PlayerX = TempX;
PlayerY = TempY;
}
// Draw map
if (PlayerX != TempX or PlayerY != TempY or FirstLoop == false)
{
// Set cursor to 0, 0
gotoxy(0,0);
for (int i = 0; i <= MapHeight; i++)
{
for (int j = 0; j <= MapWidth; j++)
{
// Find where to draw the player
if (PlayerX == j and PlayerY == i)
{
std::cout << "@";
}
/* if it can't find where to draw the player
draw a wall instead */
else
{
std::cout << Map[j];
}
}
std::cout << "\n";
}
}
FirstLoop = true;
}
}
- Nicky