欢迎光临散文网 会员登陆 & 注册

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

2018-11-10 17:20 作者:swiss126  | 我要投稿

 一款经典的小游戏
玩法:按A、D控制小球左右移动,让小球在平台上运动,如果小球掉下去,游戏结束
 


游戏代码:
 /*

**    0 = 黑色√     8 = 灰色

**    1 = 蓝色       9 = 淡蓝色

**    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)//定义函数

{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 OVER!\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


C++——Rapid roll[彩球滑梯]的评论 (共 条)

分享到微博请遵守国家法律