#include <iostream>
#include <windows.h>
char Map[15][40] = {"#######################################", //40-1 = 39 : x
"# #", //15-1 = 14 : y
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# x #",
"#######################################"};
unsigned short x; // Col's
unsigned short y; // Row's
bool left = false;
bool right = true;
bool up = false;
bool down = false;
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);
}
void GetCoords()
{
while(true)
{
Sleep(100);
for (unsigned short row(0);row<15; row++)
{
for (unsigned short col(0) ;col<40;col++)
{
if (Map[row][col] == 'x')
{
x = col;
y = row;
}
}
}
}
}
void Input()
{
while(true)
{
Sleep(100);
if (GetAsyncKeyState(VK_RIGHT)!=0) // Right
{
right = true;
up = false;
down = false;
left = false;
}
if (GetAsyncKeyState(VK_LEFT)!=0) // Left
{
right = false;
up = false;
down = false;
left = true;
}
if (GetAsyncKeyState(VK_UP)!=0 ) // Up
{
right = false;
up = true;
down = false;
left = false;
}
if (GetAsyncKeyState(VK_DOWN)!=0)// Down
{
right = false;
up = false;
down = true;
left = false;
}
}
}
void snake()
{
while(true)
{
if (down == true)
{
if (Map[y+1][x] == '#' || Map[y+1][x] == '*')
{
//gameover = true
}else{
Map[y][x] = '*';
Map[y+1][x] = 'x';
}
}
if (right == true)
{
if (Map[y][x+1] == '#' || Map[y][x+1] == '*')
{
//gameover = true
}else{
Map[y][x] = '*';
Map[y][x+1] = 'x';
}
}
if (left == true)
{
if (Map[y][x-1] == '#' || Map[y][x-1] == '*')
{
//gameover = true
}else{
Map[y][x] = '*';
Map[y][x-1] = 'x';
}
}
if (up == true)
{
if (Map[y-1][x] == '#' || Map[y-1][x] == '*')
{
//gameover = true
}else{
Map[y][x] = '*';
Map[y-1][x] = 'x';
}
}
Sleep(100);
}
}
int main()
{
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)GetCoords,0,0,NULL);
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Input,0,0,NULL);
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)snake,0,0,NULL);
while(true)
{
gotoxy(0,0);
for (unsigned short row(0); row<15; row++)
{
std::cout << Map[row] << "\n";
}
}
return 0;
}
Its very messy.. I think I need to learn how to use structures..
I dont know how to delete the end of the tail.. Thats pretty much whats keeping me back.
I includes:
-Mouvement
-and tail
But I cant delete the tail..
will add the random fruit later.
Im stuck :crying:


Sign In
Create Account


Back to top










