给程序加上声音(迷宫游戏)多线程

来源:互联网 发布:c语言中删除文件 编辑:程序博客网 时间:2024/05/19 03:47


//迷宫游戏
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include<Mmsystem.h>
#pragma comment(lib,"winmm.lib")
#include <time.h>
#include<string.h>
#define Height 37//迷宫的高度,必须为奇数
#define Width 39 //迷宫的宽度,必须为奇数
#define Wall 1
#define Road 0
#define Start 2
#define End 3
#define Esc 5
#define Up 1
#define Down 2
#define Left 3
#define Right 4
void ConPrintAt(int x, int y, char *CharBuffer, int len);
void gotoXY(int x, int y);
void ClearConsole(void);
void ClearConsoleToColors(int ForgC, int BackC);
void SetColorAndBackground(int ForgC, int BackC);
void SetColor(int ForgC);
void HideTheCursor(void);
void ShowTheCursor(void);

void set_console_color(unsigned short color_index);    //颜色索引函数

int map[Height + 2][Width + 2];
void gotoxy(int x, int y); //移动坐标
void hidden();//隐藏光标
void create(int x, int y);//随机生成迷宫
int get_key(); //接收按键
void paint(int x, int y); //画迷宫
void game();
void block()
{
  int x=4;
    gotoXY(x,5);
    for(x=4;x<=72;x+=4)
    {
        SetColorAndBackground(0,15);
        gotoXY(x,4);
        Sleep(20);
        printf("□□");
    }
    int x2=4;
    gotoXY(x2,20);
    for(x2=4;x2<=72;x2+=4)
    {
        SetColorAndBackground(0,15);
        gotoXY(x2,20);
        Sleep(20);
        printf("□□");
    }
    int y=4;
    gotoXY(2,y);
    for(y=4;y<20;y++)
    {
        gotoXY(2,y);
        Sleep(20);
        printf("□");
    }
    int y2=4;
    gotoXY(72,y2);
    for( y2=4;y2<20;y2++)
    {
        gotoXY(76,y2);
        Sleep(20);
        printf("□");
    }
}
DWORD WINAPI Fun(LPVOID lpParamter)  //创建播放音乐的线程
{
 while(1)
 {
   PlaySound("123.wav", 0, SND_FILENAME | SND_SYNC);
 }
}
int main()
{
 HANDLE hThread=CreateThread(NULL,0,Fun,NULL,0,NULL);
 CloseHandle(hThread);
 while(1)
    {
  block();
    //===============================
    SetColorAndBackground(11,0);
    char title[]=" The Maze Rescuing ";
    gotoXY(25,8);
    for(int i=0;i<=sizeof(title);i++)
    {
        printf("%c",title[i]);
        Sleep(20);
    }
    gotoXY(9,11);
    char tell1[]=" The princess was locked in a dark room by wicked queen.  ";
    for(int j=0;j<=sizeof(tell1);j++)
    {
        printf("%c",tell1[j]);
        Sleep(20);
    }
    gotoXY(9,12);
    //char tell2[]=" If you can't come to rescue her in time,terrorists will kill her!";
    char tell2[]=" The brave knight please rescue her.If can't come to there!";
    for(int k=0;k<=sizeof(tell2);k++)
    {
        printf("%c",tell2[k]);
        Sleep(20);
    }
    gotoXY(9,13);
    char tell3[]=" Princess will be turned into a frog! ";
    for(int l=0;l<=sizeof(tell3);l++)
    {
        printf("%c",tell3[l]);
        Sleep(20);
    }
    gotoXY(9,14);
    char tell4[]=" Hurry up, time has gone by really quickly! ";
    for(int m=0;m<=sizeof(tell4);m++)
    {
        printf("%c",tell4[m]);
        Sleep(20);
    }
    SetColorAndBackground(1, 11);
    ConPrintAt(22, 30, "This Game Created By HSW In 2014/12/10!             \n", 26);
    ConPrintAt(22, 33, "All Rights Reserved!             \n", 26);
    Sleep(100);
    gotoXY(7,24);
    SetColorAndBackground(0, 15);
    system("cls");                     //清屏函数
    Sleep(500);
 int i1, j1;
 srand((unsigned)time(NULL)); //初始化随即种子
 hidden(); //隐藏光标
 for (i1 = 0; i1 <= Height + 1; i1++)
 for (j1 = 0; j1 <= Width + 1; j1++)
 if (i1 == 0 || i1 == Height + 1 || j1 == 0 || j1 == Width + 1) //初始化迷宫
  map[i1][j1] = Road;
 else map[i1][j1] = Wall;

 create(2 * (rand() % (Height / 2) + 1), 2 * (rand() % (Width / 2) + 1)); //从随机一个点开始生成迷宫,该点行列都为偶数
 for (i1 = 0; i1 <= Height + 1; i1++) //边界处理
 {
  map[i1][0] = Wall;
  map[i1][Width + 1] = Wall;
 }

 for (j1 = 0; j1 <= Width + 1; j1++) //边界处理
 {
  map[0][j1] = Wall;
  map[Height + 1][j1] = Wall;
 }
 map[2][1] = Start; //给定入口
 map[Height - 1][Width] = End; //给定出口
 for (i1 = 1; i1 <= Height; i1++)
 for (j1 = 1; j1 <= Width; j1++) //画出迷宫
  paint(i1, j1);
 game(); //开始游戏
 getch();
 }
 return 0;
}
//======================================================
               //以下是函数定义
void ClearConsoleToColors(int ForgC, int BackC)
{
   WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
   //Get the handle to the current output buffer...
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   //This is used to reset the carat/cursor to the top left.
   COORD coord = {0, 0};
   //A return value... indicating how many chars were written
   //not used but we need to capture this since it will be
   //written anyway (passing NULL causes an access violation).
   DWORD count;

   //This is a structure containing all of the console info
   // it is used here to find the size of the console.
   CONSOLE_SCREEN_BUFFER_INFO csbi;
   //Here we will set the current color
   SetConsoleTextAttribute(hStdOut, wColor);
   if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
   {
      //This fills the buffer with a given character (in this case 32=space).
      FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);

      FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
      //This will set our cursor position for the next print statement.
      SetConsoleCursorPosition(hStdOut, coord);
   }
}

//This will clear the console.
void ClearConsole()
{
   //Get the handle to the current output buffer...
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   //This is used to reset the carat/cursor to the top left.
   COORD coord = {0, 0};
   //A return value... indicating how many chars were written
   //   not used but we need to capture this since it will be
   //   written anyway (passing NULL causes an access violation).
   DWORD count;
   //This is a structure containing all of the console info
   // it is used here to find the size of the console.
   CONSOLE_SCREEN_BUFFER_INFO csbi;
   //Here we will set the current color
   if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
   {
      //This fills the buffer with a given character (in this case 32=space).
      FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
      FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
      //This will set our cursor position for the next print statement.
      SetConsoleCursorPosition(hStdOut, coord);
   }
}

//This will set the position of the cursor
void gotoXY(int x, int y)
{
   //Initialize the coordinates
   COORD coord = {x, y};
   //Set the position
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

//This will set the forground color for printing in a console window.
void SetColor(int ForgC)
{
   WORD wColor;
   //We will need this handle to get the current background attribute
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   CONSOLE_SCREEN_BUFFER_INFO csbi;

   //We use csbi for the wAttributes word.
   if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
   {
      //Mask out all but the background attribute, and add in the forgournd color
      wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
      SetConsoleTextAttribute(hStdOut, wColor);
   }
}

//This will set the forground and background color for printing in a console window.
void SetColorAndBackground(int ForgC, int BackC)
{
   WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}
//Direct Console output at a particular coordinate.
void ConPrintAt(int x, int y, char *CharBuffer, int len)
{
   DWORD count;
   COORD coord = {x, y};
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleCursorPosition(hStdOut, coord);
   WriteConsole(hStdOut, CharBuffer, len, &count, NULL);
}

//Hides the console cursor
void HideTheCursor()
{
   CONSOLE_CURSOR_INFO cciCursor;
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

   if(GetConsoleCursorInfo(hStdOut, &cciCursor))
   {
      cciCursor.bVisible = FALSE;
      SetConsoleCursorInfo(hStdOut, &cciCursor);
   }
}

//Shows the console cursor
void ShowTheCursor()
{
   CONSOLE_CURSOR_INFO cciCursor;
   HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

   if(GetConsoleCursorInfo(hStdOut, &cciCursor))
   {
      cciCursor.bVisible = TRUE;
      SetConsoleCursorInfo(hStdOut, &cciCursor);
   }
}
//===============================================
void set_console_color(unsigned short color_index)    //颜色索引函数
{
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_index);
}
void gotoxy(int x, int y) //移动坐标
{
 COORD coord;
 coord.X = x;
 coord.Y = y;
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void hidden()//隐藏光标
{
 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
 CONSOLE_CURSOR_INFO cci;
 GetConsoleCursorInfo(hOut, &cci);
 cci.bVisible = 0;//赋1为显示,赋0为隐藏
 SetConsoleCursorInfo(hOut, &cci);
}
void create(int x, int y) //随机生成迷宫
{
 int c[4][2] = { 0, 1, 1, 0, 0, -1, -1, 0 }; //四个方向
 int i, j, t;
 //将方向打乱
 for (i = 0; i<4; i++)
 {
  j = rand() % 4;
  t = c[i][0];
  c[i][0] = c[j][0];
  c[j][0] = t;
  t = c[i][1];
  c[i][1] = c[j][1];
  c[j][1] = t;
 }
 map[x][y] = Road;
 for (i = 0; i<4; i++)
 if (map[x + 2 * c[i][0]][y + 2 * c[i][1]] == Wall)
 {
  map[x + c[i][0]][y + c[i][1]] = Road;
  create(x + 2 * c[i][0], y + 2 * c[i][1]);
 }
}
int get_key() //接收按键
{
 char c;
 while (c = getch())
 {
  if (c == 27) return Esc; //Esc
  if (c != -32)continue;
  c = getch();
  if (c == 72) return Up; //上
  if (c == 80) return Down; //下
  if (c == 75) return Left; //左
  if (c == 77) return Right; //右
 }
 return 0;
}
void paint(int x, int y) //画迷宫
{
 gotoxy(2 * y - 2, x - 1);
 switch (map[x][y])
 {
 case Start:
     set_console_color(3);
  printf("¤"); break; //画入口
 case End:
     set_console_color(13);
  printf("♀"); break; //画出口
 case Wall:
     set_console_color(2);
  printf("■"); break; //画墙
 case Road:
     set_console_color(9);
  printf("□"); break; //画路
 }
}
void game()
{
    set_console_color(11);
    int x = 2, y = 1; //玩家当前位置,刚开始在入口处
    int c; //用来接收按键
    while (1)
    {
      gotoxy(2 * y - 2, x - 1);
      set_console_color(11);
      printf("♂"); //画出玩家当前位置
      if (map[x][y] == End) //判断是否到达出口
      {
        system("cls");
        block();
        gotoxy(9,11);
        SetColorAndBackground(11,0);
        printf("恭喜骑士!你成功营救了公主,公主愿意嫁给你!按任意键结束");
        Sleep(1000);
        exit(0);
      }
      c = get_key();
      if (c == Esc)
      {
        gotoxy(0, 24);
        break;
      }
      switch (c)
      {
        case Up: //向上走
        if (map[x - 1][y] != Wall)
        {
          paint(x, y);
          x--;
          }
          break;
          case Down:
          if (map[x + 1][y] != Wall)
          {
            paint(x, y);
            x++;
            }
            break;
            case Left: //向左走
            if (map[x][y - 1] != Wall)
            {
              paint(x, y);
              y--;
              }
              break;
              case Right: //向右走
              if (map[x][y + 1] != Wall)
              {
                paint(x, y);
                y++;
                }
                break;
          }
    }
}

0 0
原创粉丝点击