Code
#include <iostream.h>
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
int mx=5;
int my=4;
int i, j;
char direction;
char keypress;
char matrix[100][100];
void gotoxy(short x, short y)
{
HANDLE hConsoleOutput;
COORD Cursor_an_Pos = {x,y};
hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
}
void loadgrid()
{
for(i=0; i<10; i++)
{
for(j=0; j<10; j++)
{
strcpy(matrix[0]," ---- ");
strcpy(matrix[1]," | | ");
strcpy(matrix[2]," | | ");
strcpy(matrix[3]," --- --- ");
strcpy(matrix[4],"| |");
strcpy(matrix[5],"| |");
strcpy(matrix[6]," --- --- ");
strcpy(matrix[7]," | | ");
strcpy(matrix[8]," | | ");
strcpy(matrix[9]," ---- ");
}
}
}
void printgrid()
{
for (int i=0; i<30; i++)
{
for ( int j=0; j<30; j++)
{
printf("%c", matrix[i][j]);
}
printf("\n");
}
}
int main()
{
system("COLOR 0B");
loadgrid();
printgrid();
gotoxy(mx,my);
printf("v");
while (mx>0 && my>0)
{
loadgrid();
if (kbhit())
{
keypress=getch();
if ((keypress == 'd') || (keypress == 'a') || (keypress == 'w') || (keypress == 's') || (keypress == 'S') || (keypress == 'D') || (keypress == 'A') || (keypress == 'W'))
{
direction = keypress;
if (direction == 's' || direction == 'S')
{
gotoxy(mx,my);
printf(" ");
my++;
gotoxy(mx,my);
printf("v");
}
if (direction == 'w' || direction == 'W')
{
gotoxy(mx,my);
printf(" ");
my--;
gotoxy(mx,my);
printf("^");
}
if (direction == 'a' || direction == 'A')
{
gotoxy(mx,my);
printf(" ");
mx--;
gotoxy(mx,my);
printf("<");
}
if (direction == 'd' || direction == 'D')
{
gotoxy(mx,my);
printf(" ");
mx++;
gotoxy(mx,my);
printf(">");
}
direction = ' ';
}
}
}
return 0;
}
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
int mx=5;
int my=4;
int i, j;
char direction;
char keypress;
char matrix[100][100];
void gotoxy(short x, short y)
{
HANDLE hConsoleOutput;
COORD Cursor_an_Pos = {x,y};
hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
}
void loadgrid()
{
for(i=0; i<10; i++)
{
for(j=0; j<10; j++)
{
strcpy(matrix[0]," ---- ");
strcpy(matrix[1]," | | ");
strcpy(matrix[2]," | | ");
strcpy(matrix[3]," --- --- ");
strcpy(matrix[4],"| |");
strcpy(matrix[5],"| |");
strcpy(matrix[6]," --- --- ");
strcpy(matrix[7]," | | ");
strcpy(matrix[8]," | | ");
strcpy(matrix[9]," ---- ");
}
}
}
void printgrid()
{
for (int i=0; i<30; i++)
{
for ( int j=0; j<30; j++)
{
printf("%c", matrix[i][j]);
}
printf("\n");
}
}
int main()
{
system("COLOR 0B");
loadgrid();
printgrid();
gotoxy(mx,my);
printf("v");
while (mx>0 && my>0)
{
loadgrid();
if (kbhit())
{
keypress=getch();
if ((keypress == 'd') || (keypress == 'a') || (keypress == 'w') || (keypress == 's') || (keypress == 'S') || (keypress == 'D') || (keypress == 'A') || (keypress == 'W'))
{
direction = keypress;
if (direction == 's' || direction == 'S')
{
gotoxy(mx,my);
printf(" ");
my++;
gotoxy(mx,my);
printf("v");
}
if (direction == 'w' || direction == 'W')
{
gotoxy(mx,my);
printf(" ");
my--;
gotoxy(mx,my);
printf("^");
}
if (direction == 'a' || direction == 'A')
{
gotoxy(mx,my);
printf(" ");
mx--;
gotoxy(mx,my);
printf("<");
}
if (direction == 'd' || direction == 'D')
{
gotoxy(mx,my);
printf(" ");
mx++;
gotoxy(mx,my);
printf(">");
}
direction = ' ';
}
}
}
return 0;
}
Now, I tried to have something look ahead to the space they are walking to, but for some reason that is seeing invisible walls or something... Here's the code I tried:
Code
#include <iostream.h>
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
int mx=5;
int my=4;
int i, j;
char direction;
char keypress;
char matrix[100][100];
void gotoxy(short x, short y)
{
HANDLE hConsoleOutput;
COORD Cursor_an_Pos = {x,y};
hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
}
void loadgrid()
{
for(i=0; i<10; i++)
{
for(j=0; j<10; j++)
{
strcpy(matrix[0]," ---- ");
strcpy(matrix[1]," | | ");
strcpy(matrix[2]," | | ");
strcpy(matrix[3]," --- --- ");
strcpy(matrix[4],"| |");
strcpy(matrix[5],"| |");
strcpy(matrix[6]," --- --- ");
strcpy(matrix[7]," | | ");
strcpy(matrix[8]," | | ");
strcpy(matrix[9]," ---- ");
}
}
}
void printgrid()
{
for (int i=0; i<30; i++)
{
for ( int j=0; j<30; j++)
{
printf("%c", matrix[i][j]);
}
printf("\n");
}
}
int main()
{
system("COLOR 0B");
loadgrid();
printgrid();
gotoxy(mx,my);
printf("v");
while (mx>0 && my>0)
{
loadgrid();
if (kbhit())
{
keypress=getch();
if ((keypress == 'd') || (keypress == 'a') || (keypress == 'w') || (keypress == 's') || (keypress == 'S') || (keypress == 'D') || (keypress == 'A') || (keypress == 'W'))
{
direction = keypress;
if (direction == 's' || direction == 'S')
{
if(matrix[mx][(my + 2)] == ' ')
{
gotoxy(mx,my);
printf(" ");
my++;
gotoxy(mx,my);
printf("v");
}
}
if (direction == 'w' || direction == 'W')
{
if(matrix[mx][(my - 2)] == ' ')
{
gotoxy(mx,my);
printf(" ");
my--;
gotoxy(mx,my);
printf("^");
}
}
if (direction == 'a' || direction == 'A')
{
if(matrix[(mx - 2)][my] == ' ')
{
gotoxy(mx,my);
printf(" ");
mx--;
gotoxy(mx,my);
printf("<");
}
}
if (direction == 'd' || direction == 'D')
{
if(matrix[(mx + 2)][my] == ' ')
{
gotoxy(mx,my);
printf(" ");
mx++;
gotoxy(mx,my);
printf(">");
}
}
direction = ' ';
}
}
}
return 0;
}
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
int mx=5;
int my=4;
int i, j;
char direction;
char keypress;
char matrix[100][100];
void gotoxy(short x, short y)
{
HANDLE hConsoleOutput;
COORD Cursor_an_Pos = {x,y};
hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
}
void loadgrid()
{
for(i=0; i<10; i++)
{
for(j=0; j<10; j++)
{
strcpy(matrix[0]," ---- ");
strcpy(matrix[1]," | | ");
strcpy(matrix[2]," | | ");
strcpy(matrix[3]," --- --- ");
strcpy(matrix[4],"| |");
strcpy(matrix[5],"| |");
strcpy(matrix[6]," --- --- ");
strcpy(matrix[7]," | | ");
strcpy(matrix[8]," | | ");
strcpy(matrix[9]," ---- ");
}
}
}
void printgrid()
{
for (int i=0; i<30; i++)
{
for ( int j=0; j<30; j++)
{
printf("%c", matrix[i][j]);
}
printf("\n");
}
}
int main()
{
system("COLOR 0B");
loadgrid();
printgrid();
gotoxy(mx,my);
printf("v");
while (mx>0 && my>0)
{
loadgrid();
if (kbhit())
{
keypress=getch();
if ((keypress == 'd') || (keypress == 'a') || (keypress == 'w') || (keypress == 's') || (keypress == 'S') || (keypress == 'D') || (keypress == 'A') || (keypress == 'W'))
{
direction = keypress;
if (direction == 's' || direction == 'S')
{
if(matrix[mx][(my + 2)] == ' ')
{
gotoxy(mx,my);
printf(" ");
my++;
gotoxy(mx,my);
printf("v");
}
}
if (direction == 'w' || direction == 'W')
{
if(matrix[mx][(my - 2)] == ' ')
{
gotoxy(mx,my);
printf(" ");
my--;
gotoxy(mx,my);
printf("^");
}
}
if (direction == 'a' || direction == 'A')
{
if(matrix[(mx - 2)][my] == ' ')
{
gotoxy(mx,my);
printf(" ");
mx--;
gotoxy(mx,my);
printf("<");
}
}
if (direction == 'd' || direction == 'D')
{
if(matrix[(mx + 2)][my] == ' ')
{
gotoxy(mx,my);
printf(" ");
mx++;
gotoxy(mx,my);
printf(">");
}
}
direction = ' ';
}
}
}
return 0;
}
Help meh plz!
Post has been edited 1 time(s), last time on Mar 23 2008, 10:52 pm by Falkoner.
None.