<menu id="ycqsw"></menu><nav id="ycqsw"><code id="ycqsw"></code></nav>
<dd id="ycqsw"><menu id="ycqsw"></menu></dd>
  • <nav id="ycqsw"></nav>
    <menu id="ycqsw"><strong id="ycqsw"></strong></menu>
    <xmp id="ycqsw"><nav id="ycqsw"></nav>
  • c語言游戲開發教程(簡單好玩的編程代碼)


    C語言精品游戲主角和怪物源碼

    //C語言多線程-主角和怪物

    #include

    #include

    #define bool int //定義int變量為bool變量,bool不是真就是假

    int a=0,b=20;//主角的坐標

    int x=1,y=0;//怪物的坐標

    int i=1;//i值為真

    HANDLE hMutex;

    //1.坐標

    void GamePosition(HANDLE g_hout,int x,int y)

    {

    COORD pos;//點的結構體

    pos.X=x;//橫坐標

    pos.Y=y;//縱坐標

    SetConsoleCursorPosition(g_hout,pos);

    //設置控制平臺光標位置

    }

    DWORD WINAPI Func(LPVOID lpParamter)//多線程的功能函數6.線程是畫怪物

    {

    HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);//7.拿到這張紙

    WaitForSingleObject(hMutex, INFINITE);//13.自己進來,自己用洗手間

    GamePosition(hout,x,y),printf(‘●’);//8.在紙上畫怪物

    ReleaseMutex(hMutex);//14.放棄使用權

    while(1)//9.怪物在橫坐標為從0-10依次循環移動

    {

    if(x>=0&&i==1)

    {

    printf(‘ ‘);

    GamePosition(hout,++x,y);

    printf(‘●’);

    Sleep(1000);

    if(x==10)

    i=0;//i為假

    }

    else if(x<>

    {

    printf(‘ ‘);

    GamePosition(hout,–x,y);

    printf(‘●’);

    Sleep(1000);

    if(x==0)

    i=1;

    }

    }

    return 0;

    }

    int main()

    {

    HANDLE hThread = CreateThread(NULL, 0, Func, NULL, 0, NULL);//5.創建線程

    hMutex = CreateMutexA(NULL, FALSE, ‘123’);//創建互斥鎖(量)//10.關上洗手間

    HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);//2.拿到這張紙

    WaitForSingleObject(hMutex, INFINITE);//11.等待你的同事出來 15步接著

    GamePosition(hout,a,b),printf(‘☆’);//3.在紙上畫主角

    ReleaseMutex(hMutex);//12.同事出來了,放棄了洗手間的使用權

    while(1)

    {

    if(kbhit())

    switch(getch())//控制左右 4.主角上下左右移動

    {

    case ‘w’:

    case ‘W’:

    if(b>3)GamePosition(hout,a,b),printf(‘ ‘),GamePosition(hout,a,–b),printf(‘☆’);

    break;

    case ‘s’:

    case ‘S’:

    if(b<20)gameposition(hout,a,b),printf(‘>

    break;

    case ‘a’:

    case ‘A’:

    if(a>0)printf(‘ ‘),GamePosition(hout,–a,b),printf(‘☆’);

    break;

    case ‘d’:

    case ‘D’:

    if(a<20)printf(‘>

    break;

    }

    }

    CloseHandle(hThread);

    system(‘pause’);

    return 0;

    }

    制作的C語言經典游戲以及源代碼分享,復制一下去試試玩吧

    掃雷游戲代碼源碼

    制作的C語言經典游戲以及源代碼分享,復制一下去試試玩吧
    制作的C語言經典游戲以及源代碼分享,復制一下去試試玩吧

    #include

    #include

    #include

    int main (){

    int delta[8][2] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};

    int row =0,col = 0,num = 0;

    char map[10][10] = {0};

    char show[10][10] = {0};

    srand(time(0));

    for(row = 0;row <=>

    for(col = 0;col <=>

    map[row][col] = ‘0’;

    }

    }

    do{

    row = rand() % 10;

    col = rand() % 10;

    if(map[row][col] == ‘0’){

    map[row][col] = ‘x’;

    num++;

    }

    }while(num <>

    for (row = 0;row <=>

    for (col = 0;col <=>

    if(map[row][col] != ‘x’){

    int cnt = 0;

    for (num = 0;num <=>

    if(row + delta[num][0] <>

    continue;

    }

    if(row + delta[num][0] > 9){

    continue;

    }

    if(col + delta[num][1] <>

    continue;

    }

    if(col + delta[num][1] > 9){

    continue;

    }

    if(map[row + delta[num][0]][col + delta[num][1]]== ‘x’){

    cnt++;

    }

    }

    map[row][col] = ‘0’ + cnt;

    }

    }

    }

    for (row = 0;row <>

    for(col = 0;col < 10;col=””>

    printf(‘* ‘);

    }

    printf(‘n’);

    }

    num = 0;

    int x,y;

    do{

    printf(‘please enter the coordinate of array:’);

    scanf(‘%d%d’,&x,&y);

    show[x-1][y-1] = 1;

    if(map[x-1][y-1] == ‘0’){

    for (num = 0;num <=>

    if(x-1 + delta[num][0] <>

    continue;

    }

    if(x-1 + delta[num][0] > 9){

    continue;

    }

    if(y -1+ delta[num][1] <>

    continue;

    }

    if(y-1 + delta[num][1] > 9){

    continue;

    }

    show[x-1+delta[num][0]][y-1+delta[num][1]] = 1;

    }

    }

    if (map[x-1][y-1]!= ‘x’&&map[x-1][y-1] != ‘0’){

    for (num = 0;num <=>

    int cnt = 0;

    if(x-1 + delta[num][0] <>

    continue;

    }

    if(x-1 + delta[num][0] > 9){

    continue;

    }

    if(y-1 + delta[num][1] <>

    continue;

    }

    if(y-1 + delta[num][1] > 9){

    continue;

    }

    if( map[x -1 + delta[num][0]][y -1+ delta[num][1]] != ‘x’){

    show[x-1 + delta[num][0]][y -1+ delta[num][1]] = 1 ;

    }

    }

    }

    if(map[x-1][y-1] == ‘x’) {

    printf(‘game over!n’);

    for (row = 0;row <>

    for(col = 0;col < 10;col=””>

    printf(‘%c ‘,map[row][col]);

    }

    printf(‘n’);

    }

    return 0;

    }

    system(‘cls’);

    printf(‘mine sweeping:n’);

    for (row = 0;row <>

    for(col = 0;col < 10;col=””>

    if (show[row][col] == 1)

    {

    printf(‘%c ‘, map[row][col]);

    }

    else

    {

    printf(‘* ‘);

    }

    }

    printf(‘n’);

    }

    num = 0;

    for (row = 0;row <>

    for(col = 0;col < 10;col=””>

    if (show[row][col] == 1 )

    {

    num++;

    }

    }

    }

    printf(‘num:%dn’,num);

    }while(num <>

    printf(‘you win!’);

    return 0;

    }

    版權聲明:本文內容由互聯網用戶自發貢獻,該文觀點僅代表作者本人。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如發現本站有涉嫌抄襲侵權/違法違規的內容, 請發送郵件至 舉報,一經查實,本站將立刻刪除。

    發表評論

    登錄后才能評論
    国产精品区一区二区免费