wxc.cpp
来自「Turbo C++2.0下的迷宫程序,这是我的课程设计,里面有自己用乐谱演奏的音」· C++ 代码 · 共 723 行 · 第 1/2 页
CPP
723 行
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<time.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
static int i=-1;
time_t nowt,oldt=0;
int firsttime=1;
static int code,diao=1,o=4,s=5,t=3,t1,stop=-1,nopause=1,fu=0,n=0;
static char *st;
double jiping[25]={123.475/2,130.85/2,138.6/2,146.85/2,155.55/2,164.8/2,
174.6/2,185/2,196/2,207.65/2,220/2,233.1/2,246.95/2,
130.85,138.6,146.85,155.55,164.8,
174.6,185,196,207.65,220,233.1,246.95
};
float pow1(float x,float n);
int pow1(int x,int n);
inline void outnewsound();
void setcode(char *s);
void musicplay();
int pow1(int x,int n)
{
int temple=1;
if(n==0){return 1;}
else
{
for(int i=1;i<=n;i++)
{
temple*=x;}
return temple;
}
}
float pow1(float x,float n)
{
float temple=1;
if(n==0){return 1;}
else
{
for(int i=1;i<=n;i++)
{
temple*=x;}
return temple;
}
}
void addcode(char *s)
{
strcat(st,s);
}
void outnewsound()
{
int strlength=strlen(st),newcode=0;
//cout<<strlength<<endl;
char ch;
//cout<<st<<endl;
//cout<<strlength;
while(i<strlength)
{
if(newcode==1)break;
i++;
//cout<<"i="<<i<<endl;
if(i>strlength-1){i=-1;break;}
//t=2;
switch(st[i])
{
case 'h':switch(st[++i])
{
case'c':diao=1;break;
case'd':diao=3;break;
case'e':diao=5;break;
case'f':diao=6;break;
case'g':diao=8;break;
case'a':diao=10;break;
case'b':diao=12;break;
}
switch(st[i+1])
{
case'#':diao+=1;i++;break;
case'@':diao-+1;i++;break;
}
continue;
case'o':ch=st[++i];
if((ch>48)&&(ch<55))
{ o=ch-48;continue;}
else{continue;}
case's':ch=st[++i];
if((ch>48)&&(ch<58))
{s=ch-48;continue;}
case'>':o+=1;continue;
case'<':o-=1;continue;
case'p':nopause=0;
ch=st[++i];
if((ch>48)&&(ch<58))
{t=ch-48;i++;newcode=1;break;}
else continue;
default:ch=st[i];
if((ch>96)&&(ch<104))
{
switch(st[i])
{
case 'c':code=1;break;
case 'd':code=3;break;
case 'e':code=5;break;
case 'f':code=6;break;
case 'g':code=8;break;
case 'a':code=10;break;
case 'b':code=12;break;
}
if((st[i+1]=='#')||(st[i+1]=='@'))
{
i++;
if(st[i]=='#'){code+=1;}
else{code-=1;}
}
ch=st[i+1];
if((ch>48)&&(ch<58))
{
t=ch-48;
i++;
}
if(st[i+1]=='.'){fu=1;i++;}
//t=10-t;
t1=pow(2,t-2)*pow(1.5,fu);
//cout<<t;
sound(jiping[diao+code-1]*pow1(2,o-1)*nopause);
//cout<<"jiping "<<jiping[diao+code-1]*pow1(2,o-1)*nopause<<endl;
fu=0;
newcode=1;
break;
}
}
}
}
void musicplay()
{
if(stop!=1)
{
nowt=clock();
//cout<<"t="<<t<<endl;
if(firsttime==1)
{
outnewsound();
oldt=nowt;
//cout<<"t="<<t<<endl;
firsttime=0;
}
else
if(nowt-oldt==t1)
{
//nowt=clock();
//cout<<"t="<<t<<endl;
//cout<<"nowt-oldt="<<nowt-oldt<<endl;
//if((nowt-oldt)!=10){}
nosound();outnewsound();
oldt=nowt;
}
}
}
const int mapi=17,mapj=66;
int mapshu1[mapi][mapj]=
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //1
{1,1,1,1,1,1,0,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0,0,0,0,0,1,1,0,1,1,1,1,0,1,1,2,2,2,2,2,2,4,1,1,1,1}, //2
{1,1,1,0,0,0,1,1,0,1,1,1,1,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1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1,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,04,2,2,2,2,2,2,2,5}, //16
{5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5}
};
class Tmigong
{
public:
Tmigong(){beginx=8;beginy=5;mancolor=2+128;manascii=1;bkcolor=1;manfirsti=manfirstj=1;}
void bkground(int bkcolor);
void outputchar(int x,int y,int color,int ascii);
void drawman();
void clrman();
void drawgold() {outputchar(goldi,goldj,14+128,15);}
void moveto(int i,int j){clrman();mani=i;manj=j;drawman();}
void moveleft(){clrman();manj--;drawman();}
void moveright(){clrman();manj++;drawman();}
void moveup(){clrman();mani--;drawman();}
void movedown(){clrman();mani++;drawman();}
void drawmap1();
void drawmap2();
void play1();
void play2();
void automove();
private:
int beginx,beginy,mani,manj,mancolor,manascii,bkcolor,lastmani,lastmanj,manfirsti,manfirstj,goldi,goldj;
};
//beginx,beginy,mani,manj,lastmani,lastmanj,manfirsti,manfirstj,goldi,goldj,mancolor,manascii,policei,policej,selband,firstplay,firstbegin,bkcolor,win,band,level,button,z;
//beginx=8;beginy=5;mancolor=2+128;manascii=1;selband=0;firstplay=1;firstbegin=1;win=0;band=1;level=1;button=0;z=-12;}
void showwindow5()
{ int x=30;
//int gdriver=DETECT,gmode,errorcode;
//initgraph(&gdriver,&gmode,"u:\\BORLANDC\\bgi");
cleardevice();
setbkcolor(14);
setcolor(12);
settextstyle(1,1,2);
outtextxy(280,50,"PlAY WITH YOUR");
settextstyle(1,0,3);
outtextxy(50+x,180," * * * * * * ");
outtextxy(50+x,210," * * * * * * * * * * * * ");
outtextxy(50+x,240," * * * * * * * * * * * * ");
outtextxy(50+x,270," * * * * * * * * * * * * ");
outtextxy(50+x,300," * * * * * * * * * * * ");
outtextxy(50+x,330," * * * * * * * * * * " );
outtextxy(50+x,360," * * * * * * * * * ");
outtextxy(50+x,390," * * * * * * * * ");
outtextxy(50+x,420," * * * * * * * ");
outtextxy(50+x,450," * * * * * * ");
outtextxy(50+x,480," * * * * * ");
for(int i=1;i<45;i++)
{
delay(100);
setcolor(i);
circle(70,380,20+i);
}
for(int j=1;j<45;j++)
{
delay(100);
setcolor(j);
circle(530,380,20+j);
}
getch();
// closegraph();
}
void Tmigong::automove()
{
time_t lastt,nowt;
lastt=nowt=clock();
int status=0,arrived=0,delaytime=1;
int i=manfirsti,j=manfirstj;
mancolor=2;
while(1)
{
if(kbhit())
{
int ch1=getch();
if(ch1==27){mani=i;manj=j;clrman();moveto(lastmani,lastmanj);break;}
}
nowt=clock();
if(nowt-lastt==delaytime)
{
if(arrived==1){moveto(lastmani,lastmanj);drawgold();break;}
if(i-1>=0)if((mapshu2[i-1][j]==2||mapshu2[i-1][j]==4)&&status!=2){moveup();status=1;i--;}//if(map[mani][manj]==4)break;}
if(i+1<mapi)if((mapshu2[i+1][j]==2||mapshu2[i+1][j]==4)&&status!=1){movedown();status=2;i++;}//if(map[mani][manj]==4)break;}
if(j-1>=0)if((mapshu2[i][j-1]==2||mapshu2[i][j-1]==4)&&status!=4){moveleft();status=3;j--;}//if(map[mani][manj]==4)break;;}
if(j+1<mapj)if((mapshu2[i][j+1]==2||mapshu2[i][j+1]==4)&&status!=3){moveright();status=4;j++;}//if(map[mani][manj]==4)break;}
lastt=nowt;
if(mapshu2[mani][manj]==4){mancolor=2+128;drawman();arrived=1;delaytime=20;}
}
}
drawmap2();
play2();
}
void Tmigong::outputchar(int x,int y,int color,int ascii) //draw the wall or the gold
{
textcolor(color);
gotoxy(x,y);
putch(char(ascii));
}
void Tmigong::bkground(int bkcolor) //set the color of the background
{
textbackground(bkcolor);
for(int i=0;i<=80;i++)
for(int j=0;j<=50;j++)
{
gotoxy(i,j);
putch(' ');
}
}
void Tmigong::drawman() //draw a man
{
outputchar(beginx+manj,beginy+mani,mancolor,manascii);
}
void Tmigong::clrman() //clear the man
{
outputchar(beginx+manj,beginy+mani,mancolor,32);
}
void Tmigong::drawmap1()
{
_setcursortype(0);
clrscr();
//textbackround(5);
for(int i=0;i<mapi;i++)
for(int j=0;j<mapj;j++)
{
switch(mapshu1[i][j])
{
case 1:outputchar(beginx+j,beginy+i,12,14);break;
//case 0:outputchar(beginx+j,beginy+i,12,14);break;
//case 2:outputchar(beginx+j,beginy+i,10,17); break;
case 3:mani=i;manj=j;
drawman(); //man
break;
case 4:outputchar(beginx+j,beginy+i,14+128,5); //gold
break;
}
}
}
void Tmigong::drawmap2()
{
_setcursortype(0);
clrscr();
//textbackround(5);
for(int i=0;i<mapi;i++)
for(int j=0;j<mapj;j++)
{
switch(mapshu2[i][j])
{
case 1:outputchar(beginx+j,beginy+i,12,219);break;
case 5:outputchar(beginx+j,beginy+i,12,176);break;
//case 2:outputchar(beginx+j,beginy+i,10,219); break;
case 3:mani=i;manj=j;
drawman(); //man
break;
case 4:outputchar(beginx+j,beginy+i,14+128,5); //gold
break;
}
}
gotoxy(15,2);
cprintf("??????????If you can not find the way");
gotoxy(15,3);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?