#include<iostream>
#include<Windows.h>
#include<ctime>
#include<conio.h>
#include<fstream>
using namespace std;
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);
}
void TextColor(int color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
//**************************************
//screen: goto [x,y]
void gotoXY(int column, int line)
{
COORD coord;
coord.X = column;
coord.Y = line;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
struct ToaDo
{
int x, y;
};
struct HoaQua{
ToaDo td;
};
enum Trangthai{
UP,
DOWN,
LEFT,
RIGHT
};
struct Snake{
int n;
Trangthai tt;
ToaDo dot[31];
};
void KhoiTao(Snake &snake,HoaQua &hoaQua){
snake.n = 1;
snake.dot[0].x = 0;
snake.dot[0].y = 0;
snake.tt = RIGHT;
hoaQua.td.x = 10;
hoaQua.td.y = 10;
}
void HienThi(Snake &snake,HoaQua &hoaQua){
clrscr();
gotoXY(60, 17);
cout << "Diem" <<" "<< snake.n;
gotoXY(50, 15);
TextColor(14);
cout << "Ranh roi sinh nong noi";
TextColor(7);
gotoXY(hoaQua.td.x, hoaQua.td.y);
TextColor(14);
cout << (char)27;
TextColor(7);
gotoXY(snake.dot[0].x, snake.dot[0].y);
TextColor(12);
cout << (char)3;
TextColor(7);
for (int i = 1; i < snake.n; i++){
gotoXY(snake.dot[i].x, snake.dot[i].y);
TextColor(5);
cout <<(char)254;
TextColor(7);
}
}
void DieuKhien_DiChuyen(Snake &snake){
for (int i = snake.n - 1; i>0; i--){
snake.dot[i] = snake.dot[i - 1];
}
if (_kbhit()){
char key = _getch();
if (key == 'w' || key == 'W'){
snake.tt = UP;
}
else if (key == 'a' || key == 'A'){
snake.tt = LEFT;
}
else if (key == 's' || key == 'S'){
snake.tt = DOWN;
}
else if (key == 'd' || key == 'D'){
snake.tt = RIGHT;
}
}
if (snake.tt == UP){
snake.dot[0].y--;
}
else if (snake.tt == DOWN){
snake.dot[0].y++;
}
else if (snake.tt == LEFT){
snake.dot[0].x--;
}
else if (snake.tt == RIGHT){
snake.dot[0].x++;
}
}
//void XuLy(Snake &snake, HoaQua &hoaQua){
// if (snake.dot[0].x == hoaQua.td.x && snake.dot[0].y == hoaQua.td.y){
// for (int i = snake.n; i > 0; i++){
// snake.dot[i] = snake.dot[i - 1];
// }
// snake.n++;
//
// }
//}
int XuLy(Snake &snake, HoaQua &hoaQua)
{
if (snake.dot[0].x < 0 || snake.dot[0].x >= 85 ||
snake.dot[0].y < 0 || snake.dot[0].y >= 30)
return -1;
for (int i = 1; i < snake.n; i++)
if (snake.dot[0].x == snake.dot[i].x &&
snake.dot[0].y == snake.dot[i].y)
return -1;
if (snake.dot[0].x == hoaQua.td.x && snake.dot[0].y == hoaQua.td.y)
{
// ăn được hoa quả
// việc ăn được hoa quả rồi mới chèn thêm cái đầu mới thì thật sự chưa giải quyết tốt cho lắm
// các bạn cần suy nghĩ thêm để tốt hơn
for (int i = snake.n; i > 0; i--)
snake.dot[i] = snake.dot[i - 1];
snake.n++;
if (snake.tt == UP)
snake.dot[0].y--;
else if (snake.tt == DOWN)
snake.dot[0].y++;
else if (snake.tt == LEFT)
snake.dot[0].x--;
else if (snake.tt == RIGHT)
snake.dot[0].x++;
srand(time(NULL));
hoaQua.td.x = rand() % 80;
hoaQua.td.y = rand() % 27;
}
return 0;
}
int main(){
Snake snake;
int ma = 0;
HoaQua hoaQua;
KhoiTao(snake,hoaQua);
ifstream FileIn;
FileIn.open("GioiThieu.txt", ios::in);
while (!FileIn.eof())
{
char c;
FileIn.get(c);
Sleep(7);
TextColor(14);
cout << c;
// N?u mà nh?n du?c s? tuong tác 1 phím b?t k? thì s? k?t thúc l?i gi?i thi?u.
}
FileIn.close();
while (true)
{
HienThi(snake,hoaQua);
DieuKhien_DiChuyen(snake);
ma=XuLy(snake, hoaQua);
Sleep(90);
}
system("pause");
return 0;
}
#include<Windows.h>
#include<ctime>
#include<conio.h>
#include<fstream>
using namespace std;
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);
}
void TextColor(int color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
//**************************************
//screen: goto [x,y]
void gotoXY(int column, int line)
{
COORD coord;
coord.X = column;
coord.Y = line;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
struct ToaDo
{
int x, y;
};
struct HoaQua{
ToaDo td;
};
enum Trangthai{
UP,
DOWN,
LEFT,
RIGHT
};
struct Snake{
int n;
Trangthai tt;
ToaDo dot[31];
};
void KhoiTao(Snake &snake,HoaQua &hoaQua){
snake.n = 1;
snake.dot[0].x = 0;
snake.dot[0].y = 0;
snake.tt = RIGHT;
hoaQua.td.x = 10;
hoaQua.td.y = 10;
}
void HienThi(Snake &snake,HoaQua &hoaQua){
clrscr();
gotoXY(60, 17);
cout << "Diem" <<" "<< snake.n;
gotoXY(50, 15);
TextColor(14);
cout << "Ranh roi sinh nong noi";
TextColor(7);
gotoXY(hoaQua.td.x, hoaQua.td.y);
TextColor(14);
cout << (char)27;
TextColor(7);
gotoXY(snake.dot[0].x, snake.dot[0].y);
TextColor(12);
cout << (char)3;
TextColor(7);
for (int i = 1; i < snake.n; i++){
gotoXY(snake.dot[i].x, snake.dot[i].y);
TextColor(5);
cout <<(char)254;
TextColor(7);
}
}
void DieuKhien_DiChuyen(Snake &snake){
for (int i = snake.n - 1; i>0; i--){
snake.dot[i] = snake.dot[i - 1];
}
if (_kbhit()){
char key = _getch();
if (key == 'w' || key == 'W'){
snake.tt = UP;
}
else if (key == 'a' || key == 'A'){
snake.tt = LEFT;
}
else if (key == 's' || key == 'S'){
snake.tt = DOWN;
}
else if (key == 'd' || key == 'D'){
snake.tt = RIGHT;
}
}
if (snake.tt == UP){
snake.dot[0].y--;
}
else if (snake.tt == DOWN){
snake.dot[0].y++;
}
else if (snake.tt == LEFT){
snake.dot[0].x--;
}
else if (snake.tt == RIGHT){
snake.dot[0].x++;
}
}
//void XuLy(Snake &snake, HoaQua &hoaQua){
// if (snake.dot[0].x == hoaQua.td.x && snake.dot[0].y == hoaQua.td.y){
// for (int i = snake.n; i > 0; i++){
// snake.dot[i] = snake.dot[i - 1];
// }
// snake.n++;
//
// }
//}
int XuLy(Snake &snake, HoaQua &hoaQua)
{
if (snake.dot[0].x < 0 || snake.dot[0].x >= 85 ||
snake.dot[0].y < 0 || snake.dot[0].y >= 30)
return -1;
for (int i = 1; i < snake.n; i++)
if (snake.dot[0].x == snake.dot[i].x &&
snake.dot[0].y == snake.dot[i].y)
return -1;
if (snake.dot[0].x == hoaQua.td.x && snake.dot[0].y == hoaQua.td.y)
{
// ăn được hoa quả
// việc ăn được hoa quả rồi mới chèn thêm cái đầu mới thì thật sự chưa giải quyết tốt cho lắm
// các bạn cần suy nghĩ thêm để tốt hơn
for (int i = snake.n; i > 0; i--)
snake.dot[i] = snake.dot[i - 1];
snake.n++;
if (snake.tt == UP)
snake.dot[0].y--;
else if (snake.tt == DOWN)
snake.dot[0].y++;
else if (snake.tt == LEFT)
snake.dot[0].x--;
else if (snake.tt == RIGHT)
snake.dot[0].x++;
srand(time(NULL));
hoaQua.td.x = rand() % 80;
hoaQua.td.y = rand() % 27;
}
return 0;
}
int main(){
Snake snake;
int ma = 0;
HoaQua hoaQua;
KhoiTao(snake,hoaQua);
ifstream FileIn;
FileIn.open("GioiThieu.txt", ios::in);
while (!FileIn.eof())
{
char c;
FileIn.get(c);
Sleep(7);
TextColor(14);
cout << c;
// N?u mà nh?n du?c s? tuong tác 1 phím b?t k? thì s? k?t thúc l?i gi?i thi?u.
}
FileIn.close();
while (true)
{
HienThi(snake,hoaQua);
DieuKhien_DiChuyen(snake);
ma=XuLy(snake, hoaQua);
Sleep(90);
}
system("pause");
return 0;
}