#include<iostream>
#include<conio.h>
#include<Windows.h>
#include<string>
using namespace std;
// ------------------------ - Screen------------------------ -
void clrscr()
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
HANDLE hConsoleOut;
COORD Home = { 0, 0 };
DWORD dummy;
hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsoleOut, &csbiInfo);
FillConsoleOutputCharacter(hConsoleOut, ' ', csbiInfo.dwSize.X * csbiInfo.dwSize.Y, Home, &dummy);
csbiInfo.dwCursorPosition.X = 0;
csbiInfo.dwCursorPosition.Y = 0;
SetConsoleCursorPosition(hConsoleOut, csbiInfo.dwCursorPosition);
}
//screen: goto [x,y]
void gotoXY(int column, int line)
{
COORD coord;
coord.X = column;
coord.Y = line;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
enum TrangThai{
UP,
DOWN,
LEFT,
RIGHT,
};
struct chuchay{
int x, y;
char s[30];
TrangThai tt;
};
int main(){
chuchay A;
strcpy_s(A.s, "hello world");
A.x = 40;
A.y = 15;
//A.tt = DOWN;
while (1){
clrscr();
gotoXY(A.x, A.y);
cout << A.s;
//de game do bi giat
Sleep(100);
if (_kbhit()){
char key = _getch();
if (key == 'a' || key == 'A'){
// A.tt = LEFT;
A.x--;
}
// char key = _getch();
if (key == 'd' || key == 'D'){
//A.tt = RIGHT;
A.x++;
}
//char key = _getch();
if (key == 'w' || key == 'W'){
//A.tt = UP;
A.y--;
}
//char key = _getch();
if (key == 's' || key == 'S'){
//A.tt = DOWN;
A.y++;
}
}
if (A.y >= 29){
A.tt = UP;
}
else if (A.y <= 0){
A.tt = DOWN;
}
if (A.tt == DOWN)
A.y++;
else if (A.tt == UP)
A.y--;
else if (A.tt == LEFT)
A.x--;
else if (A.tt == RIGHT)
A.x++;
}
return 0;
}
#include<conio.h>
#include<Windows.h>
#include<string>
using namespace std;
// ------------------------ - Screen------------------------ -
void clrscr()
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
HANDLE hConsoleOut;
COORD Home = { 0, 0 };
DWORD dummy;
hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsoleOut, &csbiInfo);
FillConsoleOutputCharacter(hConsoleOut, ' ', csbiInfo.dwSize.X * csbiInfo.dwSize.Y, Home, &dummy);
csbiInfo.dwCursorPosition.X = 0;
csbiInfo.dwCursorPosition.Y = 0;
SetConsoleCursorPosition(hConsoleOut, csbiInfo.dwCursorPosition);
}
//screen: goto [x,y]
void gotoXY(int column, int line)
{
COORD coord;
coord.X = column;
coord.Y = line;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
enum TrangThai{
UP,
DOWN,
LEFT,
RIGHT,
};
struct chuchay{
int x, y;
char s[30];
TrangThai tt;
};
int main(){
chuchay A;
strcpy_s(A.s, "hello world");
A.x = 40;
A.y = 15;
//A.tt = DOWN;
while (1){
clrscr();
gotoXY(A.x, A.y);
cout << A.s;
//de game do bi giat
Sleep(100);
if (_kbhit()){
char key = _getch();
if (key == 'a' || key == 'A'){
// A.tt = LEFT;
A.x--;
}
// char key = _getch();
if (key == 'd' || key == 'D'){
//A.tt = RIGHT;
A.x++;
}
//char key = _getch();
if (key == 'w' || key == 'W'){
//A.tt = UP;
A.y--;
}
//char key = _getch();
if (key == 's' || key == 'S'){
//A.tt = DOWN;
A.y++;
}
}
if (A.y >= 29){
A.tt = UP;
}
else if (A.y <= 0){
A.tt = DOWN;
}
if (A.tt == DOWN)
A.y++;
else if (A.tt == UP)
A.y--;
else if (A.tt == LEFT)
A.x--;
else if (A.tt == RIGHT)
A.x++;
}
return 0;
}