30/09/2018, 17:22
Mọi người test thử game pingpong mình làm nhé
#include<iostream>
#include<fstream>
#include<conio.h>
#include<Windows.h>
#include<time.h>
#include<math.h>
#define room_width 40
#define room_height 30
using namespace std;
char map[room_height][room_width];
char select_map[20][30];
// Hàm thay đổi kích cỡ của khung cmd.
void resizeConsole(int width, int height)
{
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect(console, &r);
MoveWindow(console, r.left, r.top, width, height, TRUE);
}
// Hàm tô màu.
void textcolor(int x)
{
HANDLE mau;
mau=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(mau,x);
}
// Hàm dịch chuyển con trỏ đến tọa độ x, y.
void gotoxy(int x,int y)
{
HANDLE hConsoleOutput;
COORD Cursor_an_Pos = {x-1,y-1};
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput , Cursor_an_Pos);
}
// Hàm xóa màn hình.
void XoaManHinh()
{
HANDLE hOut;
COORD Position;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
Position.X = 0;
Position.Y = 0;
SetConsoleCursorPosition(hOut, Position);
}
void set_select_map()
{
for(int i=0;i<20;i++)
{
for(int j=0;j<30;j++)
{
if(i==0||i==19)
{
select_map[i][j]='-';
}
else if(j==0||j==29)
{
select_map[i][j]='|';
}
else
{
select_map[i][j]=' ';
}
}
}
}
void set_easy_button(int x,int y)
{
select_map[x][y]='A';select_map[x][y-2]='*';select_map[x][y-1]='E';
select_map[x][y+1]='S';select_map[x][y+2]='Y';select_map[x][y+3]='*';
}
void set_hard_button(int x, int y)
{
select_map[x][y]='A';select_map[x][y-2]='*';select_map[x][y-1]='H';
select_map[x][y+1]='R';select_map[x][y+2]='D';select_map[x][y+3]='*';
}
void delete_easy_button(int x,int y)
{
select_map[x][y]=select_map[x][y-2]=select_map[x][y-1]=select_map[x][y+1]=select_map[x][y+2]=select_map[x][y+3]='#';
}
void delete_hard_button(int x,int y)
{
select_map[x][y]=select_map[x][y-2]=select_map[x][y-1]=select_map[x][y+1]=select_map[x][y+2]=select_map[x][y+3]='@';
}
void in_select_map()// hien thi room
{
for(int i=0;i<20;i++)
{
cout<<endl;
for(int j=0;j<30;j++)
{
if(select_map[i][j]=='-'||select_map[i][j]=='|')
{
select_map[i][j]=' ';
textcolor(192);
cout<<select_map[i][j];
textcolor(7);
}
else if(select_map[i][j]=='_')
{
select_map[i][j]=' ';
textcolor(191);
cout<<select_map[i][j];
textcolor(7);
}
else if(select_map[i][j]=='A'||select_map[i][j]=='E'||select_map[i][j]=='S'||select_map[i][j]=='Y'||select_map[i][j]=='H'||select_map[i][j]=='R'||select_map[i][j]=='D'||select_map[i][j]=='*')
{
textcolor(172);
cout<<select_map[i][j];
textcolor(7);
}
else if(select_map[i][j]=='l'||select_map[i][j]=='s'||select_map[i][j]=='e'||select_map[i][j]=='c'||select_map[i][j]=='t')
{
textcolor(12);
cout<<select_map[i][j];
textcolor(7);
}
else if(select_map[i][j]=='#')
{
select_map[i][j]=' ';
textcolor(191);
cout<<select_map[i][j];
textcolor(7);
}
else if(select_map[i][j]=='@')
{
select_map[i][j]=' ';
textcolor(191);
cout<<select_map[i][j];
textcolor(7);
}
else
{
cout<<select_map[i][j];
}
//cout<<select_map[i][j];
}
}
}
void set_select_word(int x,int y)
{
select_map[x][y]='l';select_map[x][y-2]='s';select_map[x][y+1]=select_map[x][y-1]='e';
select_map[x][y+2]='c';select_map[x][y+3]='t';
}
void setmap()
{
for(int i=0;i<room_height;i++)
{
for(int j=0;j<room_width;j++)
{
if(i==0||i==(room_height-1))
{
map[i][j]='-';
}
else if(j==0||j==(room_width-1))
{
map[i][j]='|';
}
else
{
map[i][j]=' ';
}
}
}
}
void set_computer(int x,int y)// tao computer
{
map[x][y]=map[x][y-1]=map[x][y+1]=map[x][y-2]=map[x][y+2]='*';
}
void set_computer2(int x,int y)
{
map[x][y]=map[x][y-1]=map[x][y+1]=map[x][y-2]=map[x][y+2]='#';
}
void SetPlayer(int x,int y)// tao nguoi choi
{
map[x][y]=map[x][y-1]=map[x][y+1]=map[x][y-2]=map[x][y+2]='_';
}
void inmap()// hien thi room
{
for(int i=0;i<room_height;i++)
{
cout<<endl;
for(int j=0;j<room_width;j++)
{
if(map[i][j]=='-'||map[i][j]=='|')
{
map[i][j]=' ';
textcolor(192);
cout<<map[i][j];
textcolor(7);
}
else if(map[i][j]=='_')
{
map[i][j]=' ';
textcolor(191);
cout<<map[i][j];
textcolor(7);
}
else if(map[i][j]=='O')
{
textcolor(14);
cout<<map[i][j];
textcolor(7);
}
else if(map[i][j]=='*')
{
textcolor(160);
cout<<map[i][j];
textcolor(7);
}
else if(map[i][j]=='#')
{
textcolor(144);
cout<<map[i][j];
textcolor(7);
}
else
{
cout<<map[i][j];
}
}
}
}
void move_player(int &x,int &y)// di chuyen nguoi choi dung phim mui ten
{
if(GetAsyncKeyState(VK_LEFT))
{
if(y>3)
{ map[x][y]=map[x][y-1]=map[x][y+1]=map[x][y-2]=map[x][y+2]=' ';
y--;
SetPlayer(x,y);
}
}
if(GetAsyncKeyState(VK_RIGHT))
{
if(y<36)
{ map[x][y]=map[x][y-1]=map[x][y+1]=map[x][y-2]=map[x][y+2]=' ';
y++;
SetPlayer(x,y);
}
}
}
struct bingbong
{
void deleteball(int x,int y)
{
map[x][y]=' ';
}
void setball(int x,int y)// thiet lap bong
{
deleteball(abs(x),y);
map[x][y]='O';
}
void move_up_left_30(int &x,int &y)//30* di len ve ben trai
{
x--;y-=3;
if(x%29==0&&x/29==-1)
{
x=27;
}
else if(x==0)
{
x=-2;
}
if(y<=-39)
{
y=38;
}
if(y==0)
{
y=-1;
}
}
void move_up_left_45(int &x,int &y)//45* di len ve ben trai
{
x--;y-=2;
if(x%29==0&&x/29==-1)
{
x=27;
}
else if(x==0)
{
x=-2;
}
if(y<=-39)
{
y=38;
}
if(y==0)
{
y=-1;
}
}
void move_up_left_60(int &x,int &y)//60* di len ve ben trai
{
x--;y--;
if(x%29==0&&x/29==-1)
{
x=27;
}
else if(x==0)
{
x=-2;
}
if(y<=-39)
{
y=38;
}
if(y==0)
{
y=-2;
}
}
void move_up_right_30_degree(int &x, int &y)// 30* di len ve ben phai
{
x--; y+=3;
if(x%29==0&&x/29==-1)
{
x=27;
}
else if(x==0)
{
x=-2;
}
if(y>=39)
{
y=-38;
}
}
void move_up_right_45_degree(int &x, int &y)//45* di len ve ben phai
{
x--;y+=2;
if(x%29==0&&x/29==-1)
{
x=27;
}
else if(x==0)
{
x=-2;
}
if(y>=39)
{
y=-38;
}
else if(y==0)
{
y=1;
}
}
void move_up_right_60_degree(int &x,int &y)//60* di len ve ben phai
{
x--;y+=1;
if(x%29==0&&x/29==-1)
{
x=27;
}
else if(x==0)
{
x=-2;
}
if(y>=39)
{
y=-37;
}
else if(y==0)
{
y=2;
}
}
void move_90_degree(int &x,int &y)
{
x--;
if(x%29==0&&x/29==-1)
{
x=27;
}
if(x==0)
{
x=-2;
}
}
void ball_unity_degree(int &a,int &b, int &x,int &y)// tong hop lai huong bong
{
if(a==1)
{
move_up_right_60_degree(x,y);
}
else if(a==2)
{
if(b>=1&&b<=6)
{
move_up_right_45_degree(x,y);
}
else
{
move_up_right_30_degree(x,y);
}
}
else if(a==-1)
{
move_up_left_60(x,y);
}
else if(a==-2)
{
if(b>=1&&b<=6)
{
move_up_left_45(x,y);
}
else
{
move_up_left_30(x,y);
}
}
else
{
move_90_degree(x,y);
}
}
};
void move_computer_lv1(int &x,int &y, int &yball)
{
int b=rand()%4;
map[x][y]=map[x][y-1]=map[x][y+1]=map[x][y-2]=map[x][y+2]=' ';
if(abs(yball)>2+b&&abs(yball)<37+b)
{
y=abs(yball)-b;
}
set_computer(x,y);
}
void move_computer_lv2(int &x,int &y, int &yball)
{
int b;
int time=rand()%9+1;
if(time>=1&&time<=6)
{
b=rand()%3;
}
else b=rand()%4;
map[x][y]=map[x][y-1]=map[x][y+1]=map[x][y-2]=map[x][y+2]=' ';
if(abs(yball)>2+b&&abs(yball)<37+b)
{
y=abs(yball)-b;
}
set_computer2(x,y);
}
int main()
{
srand(time(0));
/*resizeConsole(800,690); // gioi thieu
int k=9;
fstream docfile;
docfile.open("1.txt", ios::in);
while(!docfile.eof())
{
textcolor(k);
char c;docfile.get(c);
cout<<c;
if(c=='A')
{k++;
}
if(kbhit()) // an phim de dung gioi thieu
{
break;
}
Sleep(3);
}
getch();
system("cls");
*/
resizeConsole(800,600);
int point_computer=0,point_player=0;
int *check_button= new int; int check_computer=0;//kiem tra ban chon level computer
begin:
XoaManHinh();
set_select_map();
set_select_word(8,14);
set_easy_button(10,7);
set_hard_button(10,20);
if(GetAsyncKeyState(VK_LEFT))
{
*check_button=1;
check_computer=1;
}
if(GetAsyncKeyState(VK_RIGHT))
{
*check_button=2;
check_computer=2;
}
if(*check_button==1)
{
delete_hard_button(10,20);
Sleep(10);
}
if(*check_button==2)
{
delete_easy_button(10,7);
Sleep(10);
}
in_select_map();
gotoxy(33,6);textcolor(10); cout<<"press <-- to select EASY ; --> to select HARD";
gotoxy(33,8);textcolor(14); cout<<"press 'Space' to start game";
if(check_computer!=1&&check_computer!=2)
{
goto begin;
}
char ch=getch();
if(ch==32)
{
system("cls");
if(check_computer==1)
{
cout<<"ban chon easy---chuan bi choi";
}
else if(check_computer==2)
{
cout<<"ban chon hard-chuan bi choi";
}
Sleep(2000);
system("cls");
goto start;
delete check_button;
}
else
{
goto begin;
system("cls");
}
//bat dau tro choi
start: XoaManHinh();
int b=1,a=0;
int move_ball_speed=2;
int x_computer=3, y_computer=20;
int x_player=room_height-4,y_player=20;
int x_ball=x_player-1,y_ball=y_player;
bingbong ball;
int d=0;
setmap();
if(check_computer==1)
{
set_computer(x_computer,y_computer);
}
else if(check_computer==2)
{
set_computer2(x_computer,y_computer);
}
SetPlayer(x_player,y_player);
ball.setball(abs(x_ball),abs(y_ball));
inmap();
Sleep(1500);
while(true)
{
d++;
textcolor(9);
gotoxy(45,2); cout<<" "<<"computer"<<" "<<"player";
textcolor(12);
gotoxy(43,4); cout<<"point:";
textcolor(13); gotoxy(54,4); cout<<point_computer;
textcolor(14); gotoxy(64,4); cout<<point_player;
gotoxy(45,10); cout<<"x: ";
gotoxy(45,10); cout<<"x: "<<x_ball;
gotoxy(45,12); cout<<"y: ";
gotoxy(45,12); cout<<"y: "<<y_ball;
gotoxy(45,14); cout<<"s: ";
gotoxy(45,14); cout<<"s: "<<move_ball_speed;
gotoxy(45,16); cout<<"a: ";
gotoxy(45,16); cout<<"a: "<<a;
XoaManHinh();
//system("cls");
setmap();
if(check_computer==1)
{
set_computer(x_computer,y_computer);
}
else if(check_computer==2)
{
set_computer2(x_computer,y_computer);
}
SetPlayer(x_player,y_player);
ball.setball(abs(x_ball),abs(y_ball));
if(d==move_ball_speed)
{
if(abs(x_ball)==x_player)
{
point_computer++;
goto start;
}
if(abs(x_ball)==x_computer)
{
point_player++;
goto start;
}
ball.ball_unity_degree(a,b,x_ball,y_ball);
if(check_computer==1)
{
move_computer_lv1(x_computer,y_computer,y_ball);
}
if(check_computer==2)
{
move_computer_lv2(x_computer,y_computer,y_ball);
}
d=0;
}
if(map[abs(x_ball)][abs(y_ball)]=='_')
{
move_ball_speed=rand()%3+1;
b=rand()%9+1;
printf("a");
a=abs(y_ball)-abs(y_player);
x_ball=x_player-1;y_ball=y_player+a;
}
if(check_computer==1)
{
if(map[abs(x_ball)][abs(y_ball)]=='*')
{
move_ball_speed=rand()%3+2;
b=rand()%9+1;
printf("a");
if(b>=1&&b<5)
{
a=abs(y_computer)-abs(y_ball);
}
else
{
a=-(abs(y_computer)-abs(y_ball));
}
x_ball=-(x_computer+1);y_ball=-(y_computer-a);
}
}
else if(check_computer==2)
{
if(map[abs(x_ball)][abs(y_ball)]=='#')
{
move_ball_speed=rand()%3+1;
b=rand()%9+1;
printf("a");
if(b>=1&&b<5)
{
a=abs(y_computer)-abs(y_ball);
}
else
{
a=-(abs(y_computer)-abs(y_ball));
}
x_ball=-(x_computer+1);y_ball=-(y_computer-a);
}
}
if(point_computer==11||point_player==11)
{
goto end;
}
move_player(x_player,y_player);
inmap();
}
end: XoaManHinh();
if(point_computer==11)
{
textcolor(14); gotoxy(15,20); cout<<"computer WIN";
}
else if(point_player==11)
{
textcolor(14); gotoxy(15,20); cout<<"player WIN";
}
textcolor(12);
gotoxy(3,22); cout<<"press C to continue"<<" "<<"press ESC to exit";
char c=getch();
if(c==27)
{
exit(0);
}
else if(c=='C'||c=='c')
{
point_computer=0,point_player=0;// thiet lap lai diem so
system("cls");
goto begin;
}
else
{
system("cls");
goto end;
}
system("pause");
return 0;
}
Bài liên quan
Có file exe chạy luôn để demo không
Nhìn những dòng code của bạn làm mình nhớ đến trò chiếc nón kỳ diệu mình làm đồ án môn Mạng máy tính.