C++——Rapid roll[彩球滑梯]

?一款經(jīng)典的小游戲
玩法:按A、D控制小球左右移動(dòng),讓小球在平臺(tái)上運(yùn)動(dòng),如果小球掉下去,游戲結(jié)束
?


游戲代碼:
?/*
** ? ?0 = 黑色√ ? ? 8 = 灰色
** ? ?1 = 藍(lán)色 ? ? ? 9 = 淡藍(lán)色
** ? ?2 = 綠色 ? ? ? 10 = 淡綠色
** ? ?3 = 淺綠色 ? ? 11 = 淡淺綠色
** ? ?4 = 紅色 ? ? ? 12 = 淡紅色√
** ? ?5 = 紫色 ? ? ? 13 = 淡紫色
** ? ?6 = 黃色 ? ? ? 14 = 淡黃色√
** ? ?7 = 白色 ? ? ? 15 = 亮白色√
*/
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <windows.h>
#pragma comment(lib,"winmm.lib")
using namespace std;
const int M=12;
const int N=18;
MCIDEVICEID ?dev[1];
MCI_OPEN_PARMS mciOpen;
MCI_PLAY_PARMS mciPlay;
void gotoxy(int x, int y)??/* goto語句 ?*/
{?
? ? COORD pos;?
? ? pos.X = x;?
? ? pos.Y = y;
? ? SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);?
}?
void SetColor(unsigned short ForeColor,unsigned short BackGroundColor)//定義函數(shù)
{HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,(ForeColor%16)|(BackGroundColor%16*16));}
class game;
class wall{
private:
friend game;
int lenth;
int x;
int y;
public:
wall(){};
void move(){y++;}
};
class ball{
private:
friend game;
int x;
int y;
public:
ball(int x,int y):x(x),y(y+1){};
ball(){};
void move(char c);
};
void ball::move(char c)
{
if((c=='a' || c=='A') && x>0 )x--;
else if((c=='d' || c=='D') && x<M-1)x++;
}
class game
{
public:
ball b;
wall w[4];
game();
void print();
void move();
int judge();
int score;
};
int game::judge()
{
if(b.y>N || b.y<0)return -1;
else return 0;
}
game::game()
{
srand(time(0));
int x,i,y,lenth;
for(i=0;i<3;i++)
{
x=rand()%M;
y=rand()%(N/4)+N/4*i;
lenth=M/4;
w[i].x=x;
w[i].y=y;
w[i].lenth=3;
}
b.x=x;b.y=y+1;
score=0;
}
void game::move()
{
int i,f=0;
for(i=0;i<3;i++)
{
if(w[i].y>N)
{
w[i].x=rand()%M;
w[i].y=0;
}
if(b.x>=w[i].x-w[i].lenth/2 && b.x<=w[i].x+w[i].lenth/2)
{
if(b.y==w[i].y+1){b.y++;f=1;}
if(b.y==w[i].y+2)f=1;
}
w[i].move();
}
if(f==0)b.y--;
score++;
}
void game::print()
{
int i,j,x1,x2;
SetColor(14,0);
cout<<"score="<<score<<endl;
if(b.y<N && b.y>=0){SetColor(14,0);gotoxy(2*b.x+1,N-b.y+2);cout<<"●";}
for(i=0;i<3;i++)
{
x1=0;x2=0;
if(w[i].y<N && w[i].y>=0)
{
if(w[i].x-w[i].lenth/2>=0)x1=w[i].x-w[i].lenth/2;else x1=0;
if(w[i].x+w[i].lenth/2<M)x2=w[i].x+w[i].lenth/2;else x2=M-1;
gotoxy(2*x1+1,N-w[i].y+2);
for(j=x1;j<=x2;j++){SetColor(10,0);cout<<"▓";}
}
}
SetColor(15,0);
gotoxy(0,1);cout<<"┏";for(i=0;i<M;i++)cout<<"━";cout<<"┓";
for(i=1;i<=N;i++){gotoxy(0,i+1);cout<<"┃";gotoxy(2*M+2,i+1);cout<<"┃";}
gotoxy(0,N+2);cout<<"┗";for(i=0;i<M;i++)cout<<"━";cout<<"┛";
}
int main()
{
char c;
PlaySound("bgm.wav", NULL, ?SND_FILENAME | SND_ASYNC | SND_LOOP);
SetColor(10,0);
cout<<"\n R a p i d ? r o l l\n\n";
SetColor(14,0);
cout<<"\t彩\t球\t滑\t梯\t\n\n\t\t\t\t\t\t"<<endl;
SetColor(12,0);
system("pause");
do{
game g;
int f=0;
while(f==0)
{
system("cls");
if(kbhit()){c=getch();g.b.move(c);}
g.print();
g.move();
f=g.judge();
Sleep(200);
}
system("cls");
g.print();
SetColor(12,0);
cout<<"\nGAME?。希郑牛遥n Your scores is:"<<g.score<<endl;
mciSendCommand (dev[0], MCI_CLOSE, NULL, NULL);
{
mciOpen.lpstrElementName="game over.wav";
if(mciSendCommand(NULL,MCI_OPEN, MCI_OPEN_ELEMENT ,(DWORD)&mciOpen)==0)
dev[0]=mciOpen.wDeviceID;
else
mciOpen.wDeviceID=dev[0];
mciPlay.dwFrom = 0;
mciSendCommand(mciOpen.wDeviceID,MCI_PLAY,MCI_NOTIFY | MCI_FROM,(DWORD)&mciPlay);
}
cout<<"Again?\n1.Yes\t2.No\n";
c=getch();
}while(c!='2');
return 0;
}
下載地址:?https://pan.baidu.com/s/1bprcGk3