欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言用數(shù)組實現(xiàn)反彈球消磚塊

 更新時間:2022年05月12日 11:46:47   作者:輝小歌  
這篇文章主要為大家詳細介紹了C語言用數(shù)組實現(xiàn)反彈球消磚塊,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文項目為大家分享了C語言用數(shù)組實現(xiàn)反彈球消磚塊的具體代碼,供大家參考,具體內(nèi)容如下

一、效果展示:

二、代碼如下:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<Windows.h>
#include<time.h>
#define High 24 ? //游戲畫面尺寸
#define Width 36

//全局變量
int ball_x,ball_y;//小球的坐標
int ball_vx,ball_vy;//小球的速度
int canvas[High][Width]={0};
int position_x,position_y;//擋板的中心坐標
int ridus;//擋板的半徑大小
int left,right;//擋板的左右大小
int score=0;//分數(shù)
//二維數(shù)組存儲游戲畫布中對應(yīng)的元素
//0為空格,1為小球 2為擋板 3為磚塊(1分) ? 4為磚塊(2分) ?5為磚塊(3分)

void gotoxy(int x,int y)//將光標移動到(x,y)位置
{
?? ?HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
?? ?COORD pos;
?? ?pos.X=x;
?? ?pos.Y=y;
?? ?SetConsoleCursorPosition(handle,pos);
}

void startup() //數(shù)據(jù)的初始化
{
? ? int k,i;
?? ?ridus=5;
?? ?position_x=High-1;
?? ?position_y=Width/2;
?? ?left=position_y-ridus;
?? ?right=position_y+ridus;

?? ?ball_x=position_x-1;
?? ?ball_y=position_y;
?? ?ball_vx=-1;
?? ?ball_vy=1;
?? ?canvas[ball_x][ball_y]=1;

?? ?for(k=left;k<=right;k++)//擋板
?? ??? ?canvas[position_x][k]=2;

?? ?srand(time(NULL));
?? ?for(k=0;k<Width;k++)//加幾排磚塊
?? ?{
?? ??? ?for(i=0;i<High/4;i++)
?? ??? ?{
?? ??? ??? ?canvas[i][k]=rand()%3+3;//隨機的初始化磚塊類型
?? ??? ?}
?? ?}
}

//定義隱藏光標函數(shù)
void HideCursor()
{
?? ?CONSOLE_CURSOR_INFO cursor; ? ?
?? ?cursor.bVisible = FALSE; ? ?
?? ?cursor.dwSize = sizeof(cursor); ? ?
?? ?HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); ? ?
?? ?SetConsoleCursorInfo(handle, &cursor);
}

void show()//顯示畫面
{
?? ?gotoxy(0,0);//將光標移動到原點位置,以下重畫清屏
?? ?int i,j;
?? ?for(i=0;i<High;i++)
?? ?{
?? ??? ?for(j=0;j<Width;j++)
?? ??? ?{
?? ??? ??? ?if(canvas[i][j]==0)
?? ??? ??? ??? ?printf(" ");//輸出空格
?? ??? ??? ?else if(canvas[i][j]==1)
?? ??? ??? ??? ?printf("O");//輸出小球0
?? ??? ??? ?else if(canvas[i][j]==2)
?? ??? ??? ??? ?printf("*");//輸出擋板
?? ??? ??? ?else if(canvas[i][j]==3)
?? ??? ??? ??? ?printf("#");//輸出磚塊
?? ??? ??? ?else if(canvas[i][j]==4)
?? ??? ??? ??? ?printf("+");
?? ??? ??? ?else if(canvas[i][j]==5)
?? ??? ??? ??? ?printf("$");
?? ??? ?}
?? ??? ?printf("|\n");//顯示右邊界
?? ?}
?? ?for(j=0;j<Width;j++)
?? ??? ?printf("-");//顯示下邊界
?? ?printf("\n");
?? ?printf("#、+、$ 一個分別為1、2、3分\n");
?? ?printf("分數(shù)為:%d\n",score);
}

void updateWithoutInput()//與用戶輸入無關(guān)的更新
{
?? ?static int speed=0;
?? ?if(ball_x==High-2)
?? ?{
?? ??? ?if( (ball_y>=left)&&(ball_y<=right) )//被擋板擋住
?? ??? ?{
?? ??? ??? ?printf("\a");//響鈴
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?printf("游戲失敗\n");
?? ??? ??? ?system("pause");
?? ??? ??? ?exit(0);
?? ??? ?}
?? ?}
?? ?if(speed<5)//起到延時的效果
?? ??? ?speed++;
?? ?if(speed==5)
?? ?{
?? ??? ?speed=0;
?? ??? ?canvas[ball_x][ball_y] = 0;
?? ??? ?//更新小球的坐標
?? ??? ?ball_x=ball_x+ball_vx;
?? ??? ?ball_y=ball_y+ball_vy;
?? ??? ?canvas[ball_x][ball_y] = 1;
?? ??? ?//碰到邊界后反彈
?? ??? ?if( (ball_x==0) || (ball_x==High-2) )
?? ??? ??? ?ball_vx = -ball_vx;
?? ??? ?if( (ball_y==0) || (ball_y==Width-1) )
?? ??? ??? ?ball_vy = -ball_vy;
?? ??? ?//碰到磚塊后反彈
?? ??? ?if(canvas[ball_x-1][ball_y]>=3&&canvas[ball_x-1][ball_y]<=5)
?? ??? ?{
?? ??? ??? ?if(canvas[ball_x-1][ball_y]==3)//判斷磚塊的類型
?? ??? ??? ??? ?score++;
?? ??? ??? ?if(canvas[ball_x-1][ball_y]==4)
?? ??? ??? ??? ?score=score+2;
?? ??? ??? ?if(canvas[ball_x-1][ball_y]==5)
?? ??? ??? ??? ?score=score+3;
?? ??? ??? ?ball_vx=-ball_vx;
?? ??? ??? ?canvas[ball_x-1][ball_y]=0;
?? ??? ??? ?printf("\a");
?? ??? ?}
?? ?}
}

void updateWithInput()//與用戶輸入有關(guān)的更新
{
?? ?char input;
?? ?if(kbhit())//判斷是否有輸入
?? ?{
?? ??? ?input=getch();
?? ??? ?if( ((input=='a')||(input=='A')) && (left>0) )
?? ??? ?{
?? ??? ??? ?canvas[position_x][right]=0;
?? ??? ??? ?position_y--;
?? ??? ??? ?left=position_y-ridus;
?? ??? ??? ?right=position_y+ridus;
?? ??? ??? ?canvas[position_x][left]=2;
?? ??? ?}
?? ??? ?if( ((input=='d')||(input=='D')) && (right<Width-1) )
?? ??? ?{
?? ??? ??? ?canvas[position_x][left]=0;
?? ??? ??? ?position_y++;
?? ??? ??? ?left=position_y-ridus;
?? ??? ??? ?right=position_y+ridus;
?? ??? ??? ?canvas[position_x][right]=2;
?? ??? ?}
?? ?} ?
}

int main()
{
?? ?system("title 消磚塊游戲中");
?? ?system("color 09");
?? ?startup();//數(shù)據(jù)的初始化
?? ?HideCursor();
?? ?while(1)
?? ?{
?? ??? ?show();//顯示畫面
?? ??? ?updateWithoutInput();//與用戶輸入無關(guān)的更新
?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新
?? ?}
?? ?return 0;
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Qt串口通信開發(fā)之Qt串口通信模塊QSerialPort開發(fā)完整實例(串口助手開發(fā))

    Qt串口通信開發(fā)之Qt串口通信模塊QSerialPort開發(fā)完整實例(串口助手開發(fā))

    這篇文章主要介紹了Qt串口通信開發(fā)之Qt串口通信模塊QSerialPort開發(fā)完整實例(串口助手開發(fā)),需要的朋友可以參考下
    2020-03-03
  • C語言中調(diào)用Swift函數(shù)實例詳解

    C語言中調(diào)用Swift函數(shù)實例詳解

    這篇文章主要介紹了C語言中調(diào)用Swift函數(shù)實例詳解的相關(guān)資料,實現(xiàn)該功能可以通過定義全局的指向Blocks的對象指針來實現(xiàn),需要的朋友可以參考下
    2017-07-07
  • Cocos2d-x學(xué)習(xí)筆記之Hello World!

    Cocos2d-x學(xué)習(xí)筆記之Hello World!

    這篇文章主要介紹了Cocos2d-x學(xué)習(xí)筆記之Hello World!本文基于vs2010和C++語言開發(fā),需要的朋友可以參考下
    2014-09-09
  • C語言遞歸應(yīng)用實現(xiàn)掃雷游戲

    C語言遞歸應(yīng)用實現(xiàn)掃雷游戲

    這篇文章主要為大家詳細介紹了C語言遞歸應(yīng)用實現(xiàn)掃雷游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • exec()函數(shù)在C++中的應(yīng)用及其用法

    exec()函數(shù)在C++中的應(yīng)用及其用法

    exec()函數(shù)在C++中是一個進程控制函數(shù),用于創(chuàng)建新進程執(zhí)行其他程序或命令行指令。exec()函數(shù)可以替換當前進程的代碼和數(shù)據(jù),創(chuàng)建新的進程運行其他程序。exec()函數(shù)有多個版本,例如execl、execv、execle、execve等,根據(jù)不同的參數(shù)類型和個數(shù)來使用
    2023-05-05
  • C語言中設(shè)置用戶識別碼的相關(guān)函數(shù)的簡單講解

    C語言中設(shè)置用戶識別碼的相關(guān)函數(shù)的簡單講解

    這篇文章主要介紹了C語言中設(shè)置用戶識別碼的相關(guān)函數(shù)的簡單講解,包括setuid()函數(shù)和setreuid()函數(shù)以及setfsuid()函數(shù),需要的朋友可以參考下
    2015-08-08
  • C++指針與數(shù)組:指針詳解

    C++指針與數(shù)組:指針詳解

    本文從初學(xué)者的角度,深入淺出地講解C++中的指針、數(shù)組指針,對最常混淆的引用傳遞、值傳遞和指針傳遞做了區(qū)處,需要的朋友可以參考下
    2021-09-09
  • C++中的四個默認成員函數(shù)與運算符重載詳解

    C++中的四個默認成員函數(shù)與運算符重載詳解

    這篇文章主要給大家介紹了關(guān)于C++中四個默認成員函數(shù)與運算符重載的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • 線程池的原理與實現(xiàn)詳解

    線程池的原理與實現(xiàn)詳解

    下面利用C語言來實現(xiàn)一個簡單的線程池,為了使得這個線程池庫使用起來更加方便,特在C實現(xiàn)中加入了一些OO的思想,與Objective-C不同,它僅僅是使用了struct來模擬了c++中的類,其實這種方式在linux內(nèi)核中大量可見
    2013-09-09
  • C++ Boost PropertyTree示例超詳細講解

    C++ Boost PropertyTree示例超詳細講解

    Boost是為C++語言標準庫提供擴展的一些C++程序庫的總稱。Boost庫是一個可移植、提供源代碼的C++庫,作為標準庫的后備,是C++標準化進程的開發(fā)引擎之一,是為C++語言標準庫提供擴展的一些C++程序庫的總稱
    2022-11-11

最新評論