Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C++ by minhkhanh ( 5 years ago )
#include<iostream>
#include <Windows.h>
#include <time.h>
#include <thread>
#include <conio.h>
#include<string.h>
#include<stdio.h>
#include <cstring>
#include <cstdio>
#define MAX_CAR 17
#define MAX_CAR_LENGTH 40
#define MAX_SPEED 3
//Biến toàn cục
POINT** X; //Mảng chứa MAX_CAR xe
POINT Y; // Đại diện người qua đường
int cnt = 0;//Biến hỗ trợ trong quá trình tăng tốc độ xe di chuyển
int MOVING;//Biến xác định hướng di chuyển của người
int SPEED;// Tốc độ xe chạy (xem như level)
int HEIGH_CONSOLE = 20, WIDTH_CONSOLE = 100;// Độ rộng và độ cao của màn hình console
bool STATE; // Trạng thái sống/chết của người qua đường
int currpos[2];//vị trí của đại diện người ở các level trước đó
char player_name[20];//Tên người chơi
using namespace std;
void FixConsoleWindow() {
HWND consoleWindow = GetConsoleWindow();
LONG style = GetWindowLong(consoleWindow, GWL_STYLE);
style = style & ~(WS_MAXIMIZEBOX) & ~(WS_THICKFRAME);
SetWindowLong(consoleWindow, GWL_STYLE, style);
}
void GotoXY(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void DrawBoard(int x, int y, int width, int height, int curPosX = 0, int curPosY = 0)
{
GotoXY(x, y);
cout << 'X';
for (int i = 1; i < width; i++) cout << 'X';
cout << 'X';
GotoXY(x, height + y);
cout << 'X';
for (int i = 1; i < width; i++)
cout << 'X';
cout << 'X';
for (int i = y + 1; i < height + y; i++)
{
GotoXY(x, i); cout << 'X';
GotoXY(x + width, i); cout << 'X';
}
GotoXY(curPosX, curPosY);
}
void ResetData()
{
MOVING = 'D'; // Ban đầu cho người di chuyển sang phải
SPEED = 1; // Tốc độ lúc đầu
Y = { 18,19 }; // Vị trí lúc đầu của người
// Tạo mảng xe chạy
if (X == NULL)
{
X = new POINT * [MAX_CAR];
for (int i = 0; i < MAX_CAR; i++)
X[i] = new POINT[MAX_CAR_LENGTH];
for (int i = 0; i < MAX_CAR; i++)
{
int temp = (rand() % (WIDTH_CONSOLE - MAX_CAR_LENGTH)) + 1;
for (int j = 0; j < MAX_CAR_LENGTH; j++)
{
X[i][j].x = temp + j;
X[i][j].y = 2 + i;
}
}
}
currpos[0] = NULL; currpos[1] = NULL;
system("cls");
DrawBoard(0, 0, WIDTH_CONSOLE, HEIGH_CONSOLE);
}
void StartGame()
{
system("cls");
ResetData(); // Khởi tạo dữ liệu gốc
DrawBoard(0, 0, WIDTH_CONSOLE, HEIGH_CONSOLE); // Vẽ màn hình game
STATE = true;//Bắt đầu cho Thread chạy
}
//Hàm dọn dẹp tài nguyên
void GabageCollect()
{
for (int i = 0; i < MAX_CAR; i++)
{
delete[] X[i];
}
delete[] X;
}
//Hàm thoát game
void ExitGame(HANDLE t)
{
system("cls");
GotoXY(40, 10);
cout << "\nthoat tro choi";
TerminateThread(t, 0);
GabageCollect();
exit(1);
}
//Hàm dừng game
void PauseGame(HANDLE t)
{
SuspendThread(t);
}
//Hàm xử lý khi người đụng xe
void ProcessDead(POINT t)
{
STATE = 0;
for (int i = WIDTH_CONSOLE + 2; i > t.x; i--)
{
GotoXY(i, t.y); printf("A");
GotoXY(i + 1, t.y); printf("M");
GotoXY(i + 2, t.y); printf("B");
GotoXY(i + 3, t.y); printf("U");
GotoXY(i + 4, t.y); printf("L");
GotoXY(i + 5, t.y); printf("A");
GotoXY(i + 6, t.y); printf("N");
GotoXY(i + 7, t.y); printf("C");
GotoXY(i + 8, t.y); printf("E");
Sleep(50);
GotoXY(i, t.y); printf(" ");
GotoXY(i + 1, t.y); printf(" ");
GotoXY(i + 2, t.y); printf(" ");
GotoXY(i + 3, t.y); printf(" ");
GotoXY(i + 4, t.y); printf(" ");
GotoXY(i + 5, t.y); printf(" ");
GotoXY(i + 6, t.y); printf(" ");
GotoXY(i + 7, t.y); printf(" ");
GotoXY(i + 8, t.y); printf(" ");
}
GotoXY(t.x, t.y);
printf("AMBULANCE");
GotoXY(0, HEIGH_CONSOLE + 2);
printf("Dead, type y to continue or anykey to exit");
}
//Hàm xử lý khi người băng qua đường thành công
void ProcessFinish(POINT& p)
{
currpos[SPEED - 1] = p.x;
if (SPEED == MAX_SPEED)
{
SPEED = 1;
GotoXY(0, HEIGH_CONSOLE + 2);
printf("\t\tYOU WIN\n");
system("pause");
ResetData();
}
else SPEED++;
currpos[SPEED - 1] = p.x;
p.x = 45; p.y = 19; // Vị trí lúc đầu của người
MOVING = 'D'; // Ban đầu cho người di chuyển sang phải
}
// Hàm vẽ các toa xe
void DrawCars(const char* s)
{
for (int i = 0; i < MAX_CAR; i++)
{
for (int j = 0; j < MAX_CAR_LENGTH; j++)
{
GotoXY(X[i][j].x, X[i][j].y);
printf("%c", *s);
}
}
}
//Hàm vẽ người qua đường
void DrawSticker(const POINT& p, const char* s)
{
GotoXY(p.x, p.y);
printf(s);
}
//Hàm kiểm tra xem người qua đường có đụng xe không
bool IsImpact(const POINT& p, int d)
{
if (d == 1 || d == 19)return false;
for (int i = 0; i < MAX_CAR_LENGTH; i++)
{
if (p.x == X[d - 2][i].x && p.y == X[d - 2][i].y) return true;
}
return false;
}
void MoveCars()
{
for (int i = 1; i < MAX_CAR; i += 2)
{
cnt = 0;
do
{
cnt++;
for (int j = 0; j < MAX_CAR_LENGTH - 1; j++)
{
X[i][j] = X[i][j + 1];
}
X[i][MAX_CAR_LENGTH - 1].x + 1 == WIDTH_CONSOLE ? X[i][MAX_CAR_LENGTH - 1].x = 1 : X[i][MAX_CAR_LENGTH - 1].x++;
// Kiểm tra xem xe có đụng màn hình không
} while (cnt < SPEED);
}
for (int i = 0; i < MAX_CAR; i += 2)
{
cnt = 0;
do
{
cnt++;
for (int j = MAX_CAR_LENGTH - 1; j > 0; j--)
{
X[i][j] = X[i][j - 1];
}
X[i][0].x - 1 == 0 ? X[i][0].x = WIDTH_CONSOLE - 1 : X[i][0].x--;
// Kiểm tra xem xe có đụng màn hình không
} while (cnt < SPEED);
}
}
// Hàm xóa xe (xóa có nghĩa là không vẽ)
void EraseCars()
{
for (int i = 0; i < MAX_CAR; i += 2)
{
cnt = 0;
do
{
GotoXY(X[i][MAX_CAR_LENGTH - 1 - cnt].x, X[i][MAX_CAR_LENGTH - 1 - cnt].y);
printf(" ");
cnt++;
} while (cnt < SPEED);
}
for (int i = 1; i < MAX_CAR; i += 2)
{
cnt = 0;
do
{
GotoXY(X[i][0 + cnt].x, X[i][0 + cnt].y);
printf(" ");
cnt++;
} while (cnt < SPEED);
}
}
void MoveRight()
{
if (Y.x < WIDTH_CONSOLE - 1)
{
DrawSticker(Y, " ");
Y.x++;
DrawSticker(Y, "Y");
}
}
void MoveLeft()
{
if (Y.x > 1)
{
DrawSticker(Y, " ");
Y.x--;
DrawSticker(Y, "Y");
}
}
void MoveDown()
{
if (Y.y < HEIGH_CONSOLE - 1)
{
DrawSticker(Y, " ");
Y.y++;
DrawSticker(Y, "Y");
}
}
void MoveUp()
{
if (Y.y > 1)
{
DrawSticker(Y, " ");
Y.y--;
DrawSticker(Y, "Y");
}
}
void SubThread()
{
bool stop = false;
int demstop = 49;
while (1)
{
if (STATE) //Nếu người vẫn còn sống
{
switch (MOVING) //Kiểm tra biến moving
{
case 'A': MoveLeft(); break;
case 'D': MoveRight(); break;
case 'W': MoveUp(); break;
case 'S': MoveDown(); break;
}
MOVING = ' ';// Tạm khóa không cho di chuyển, chờ nhận phím từ hàm main
if (stop) demstop++;
else demstop--;
if (demstop == 0 || demstop == 50) stop = !stop;
if (!stop)
{
EraseCars();
MoveCars();
DrawCars("=");
DrawBoard(0, 0, WIDTH_CONSOLE, HEIGH_CONSOLE);
}
if (IsImpact(Y, Y.y))
{
ProcessDead(Y); // Kiểm tra xe có đụng không
}
if (Y.y == 1)
{
if (Y.x == currpos[0] || Y.x == currpos[1]) ProcessDead(Y);
else
ProcessFinish(Y); // Kiểm tra xem về đích chưa
}
Sleep(10);//Hàm ngủ theo tốc độ SPEED
}
}
}
//Hàm lưu người chơi
void SaveGame(char* player)
{
strcat_s(player_name, ".txt");
FILE* fp;
fp = fopen_s("player_name", "w");
fprintf(fp, "%d\n", SPEED);
for (int i = 0; i < MAX_CAR; i++)
{
for (int j = 0; j < MAX_CAR_LENGTH; j++)
fprintf(fp, "%d ", X[i][j].x);
fprintf(fp, "\n");
}
if (SPEED > 1)
fprintf(fp, "%d", currpos[0]);
if (SPEED > 2)
fprintf(fp, " %d", currpos[1]);
fprintf(fp, "\n%d %d", Y.x, Y.y);
fclose(fp);
}
//Hàm đọc lại dữ liệu người chơi
void ReGame(char* player)
{
system("cls");
DrawBoard(0, 0, WIDTH_CONSOLE, HEIGH_CONSOLE);
strcat_s(player_name, ".txt");
FILE* fp = fopen_s("player.txt", "r");
fscanf_s(fp, "%d", &SPEED);
for (int i = 0; i < MAX_CAR; i++)
for (int j = 0; j < MAX_CAR_LENGTH; j++)
fscanf_s(fp, "%d", &X[i][j].x);
if (SPEED > 1)
{
fscanf_s(fp, "%d ", &currpos[0]);
GotoXY(currpos[0], 1);
printf("Y");
}
if (SPEED > 2)
{
fscanf_s(fp, "%d ", &currpos[1]);
GotoXY(currpos[1], 1);
printf("Y");
}
fscanf_s(fp, "%d %d", &Y.x, &Y.y);
fclose(fp);
DrawCars("=");
DrawSticker(Y, "Y");
}
//Menu đầu game
void Menu()
{
GotoXY(45, 10); printf("Tai lai Game (Nhan phim T)");
GotoXY(45, 11); printf("Bat dau choi (Phim bat ki)");
}
void main()
{
int temp;
FixConsoleWindow();
srand(time_t(NULL));
thread t1(SubThread);
Menu(); temp = toupper(_getch());
if (temp == 'T')
{
ResetData();
PauseGame(t1.native_handle());
GotoXY(0, HEIGH_CONSOLE + 2);
printf("Type name: ");
gets_s(player_name);
ReGame(player_name);
STATE = true;
}
else
StartGame();
while (1)
{
temp = toupper(_getch());
if (STATE)
{
if (temp == 27)
{
ExitGame(t1.native_handle());
return;
}
else if (temp == 'P')
{
PauseGame(t1.native_handle());
}
else if (temp == 'L')
{
PauseGame(t1.native_handle());
GotoXY(0, HEIGH_CONSOLE + 2);
printf("Type name: ");
gets_s(player_name);
SaveGame(player_name);
ExitGame(t1.native_handle());
}
else if (temp == 'T')
{
PauseGame(t1.native_handle());
GotoXY(0, HEIGH_CONSOLE + 2);
printf("Type name: ");
gets_s(player_name);
ReGame(player_name);
}
else
{
ResumeThread((HANDLE)t1.native_handle());
if (temp == 'D' || temp == 'A' || temp == 'W' || temp == 'S')
{
MOVING = temp;
}
}
}
else
{
if (temp == 'Y') StartGame();
else
{
ExitGame(t1.native_handle());
return;
}
}
}
}
Revise this Paste
Parent: 116308