Lập trình

Lập trình

Thứ Sáu, 4 tháng 12, 2015

Game nhanh tay viết bằng C++ trên console

Các bạn ấn 1 2 3 tùy theo độ sáng của khung, nêu lâu quá hoặc bấm nhầm bạn sẽ thua. :)
Cảm ơn các bạn đón xem.

#include<iostream>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<stdio.h>
#include<fstream>

using namespace std;

//int t = 0;
int diem = 0;
bool landau = true;

void txtColor(int mau);
void gotoxy(int x,int y);
void taokhung();
void play();
void vemau(int dapan);
void xoadapan(int dapan);
void save();

int main(){
SetConsoleTitle("Game : 1 2 3 . Developer : Nguyen Dinh Tuan - theiron1997@gmail.com");
taokhung();
play();
getch();
return 0;
}

void gotoxy(int x,int y){
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void txtColor(int mau){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),mau);
}

void taokhung(){
txtColor(10);
for(int i=10;i<70;i++){
gotoxy(i,5);
cout << char(220);
gotoxy(i,20);
cout << char(223);
}
txtColor(7);
for(int i=11;i<69;i++){
for(int j=6;j<=19;j++){
gotoxy(i,j);
cout << char(219);
}
}
txtColor(12);
for(int i=6;i<20;i++){
gotoxy(10,i);
cout << char(219);
gotoxy(30,i);
cout << char(219);
gotoxy(50,i);
cout << char(219);
gotoxy(69,i);
cout << char(219);
}
txtColor(7);
gotoxy(20,4);
cout << "1";
gotoxy(40,4);
cout << "2";
gotoxy(60,4);
cout << "3";
}
void play(){
gotoxy(15,1);
cout << "Diem : " << diem;
//taokhung();
if(landau){
gotoxy(15,3);
for(int i=4 ;i>=1;i--){
gotoxy(15,3);
cout << i;
Sleep(1000);
gotoxy(15,3);
cout << " ";
}
gotoxy(15,3);
cout << "Start!";
Sleep(1000);
gotoxy(15,3);
cout << "           ";
landau = false;
}
srand(time(0));
int dapan = (rand()*rand()+rand()-2)%3+1;
vemau(dapan);
char *c = new char[1] ;
itoa(dapan,c,10);
int t = 0;
fflush(stdin);
do{
t++;
if(t%34590==0){
txtColor(10);
gotoxy(15,3);
cout << "Game over - Qua cham";
getch();
save();
exit(0);
}
}while(!kbhit());
char ch = getch();

if(ch==*c){
diem ++;
xoadapan(dapan);
play();
}
txtColor(10);
gotoxy(15,3);
cout << "Game over - Bam sai";
    getch();
    save();
    exit(0);
}
void vemau(int dapan){
txtColor(6);
switch(dapan){
case 1 : for(int i=11;i<30;i++){
        for(int j=6;j<=19;j++){
        gotoxy(i,j);
        cout << char(219);
        }
        }
        break;
case 2 : for(int i=31;i<50;i++){
        for(int j=6;j<=19;j++){
        gotoxy(i,j);
        cout << char(219);
        }
        }
        break;
case 3 : for(int i=51;i<=68;i++){
        for(int j=6;j<=19;j++){
        gotoxy(i,j);
        cout << char(219);
        }
        }
}
}
void xoadapan(int dapan){
txtColor(7);
switch(dapan){
case 1 : for(int i=11;i<30;i++){
        for(int j=6;j<=19;j++){
        gotoxy(i,j);
        cout << char(219);
        }
        }
        break;
case 2 : for(int i=31;i<50;i++){
        for(int j=6;j<=19;j++){
        gotoxy(i,j);
        cout << char(219);
        }
        }
        break;
case 3 : for(int i=51;i<=68;i++){
        for(int j=6;j<=19;j++){
        gotoxy(i,j);
        cout << char(219);
        }
        }
}
}
void save(){
char *tmoi = new char[70];
char *tcu = new char[70];
int diemcu;

ifstream f("tuan.pro");
if(!f.is_open()){
ofstream f("tuan.pro");
system("cls");
txtColor(10);
gotoxy(15,1);
   cout << "Diem : " << diem;
gotoxy(15,8);
cout << "Than tich moi ";
gotoxy(15,10);
cout << "Ten ban la : ";
cin.getline(tmoi,69);
f << tmoi << "|" << diem;
}
else{
f.getline(tcu,69,'|');
f >> diemcu;
if(diemcu < diem){
system("cls");
   txtColor(10);
   gotoxy(15,1);
       cout << "Diem : " << diem;
   gotoxy(15,8);
   cout << "Than tich moi ";
   gotoxy(15,10);
   cout << "Ten ban la : ";
   cin.getline(tmoi,69);
   ofstream f("tuan.pro");
   f << tmoi << "|" << diem;
}
else{
system("cls");
   txtColor(10);
   gotoxy(15,8);
   cout << "Diem cua ban " << diem;
   gotoxy(15,10);
   cout << "Thanh tich cao nhat  : ";
   cout << tcu << " duoc " << diemcu;
   getch();
}
}
f.close();
}

Không có nhận xét nào:

Đăng nhận xét