打磚塊 文件讀取

/**************************
問(wèn)題分析: 需要做哪些模塊
1.  繪制磚塊與小球
2.  繪制木板,木板用鍵盤控制
3.  物理引擎,小球的反射
4.  消除磚塊


**********************/

/***********************
基礎(chǔ)
    1.創(chuàng)建窗口
    2.基本繪圖函數(shù)
        2.1 顏色設(shè)置
        2.2 畫填充矩形和圓
***************/
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//畫磚塊地圖
int map[5][8]; //描述整個(gè)地圖
HWND hwnd=NULL;

void initMap()
{
    //給二維數(shù)組賦初值,嵌套for循環(huán) i行j列
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<8;j++)
        {
            map[i][j]=rand()%3+1;
        }
    }
    
}
int gameWin()//全局變量map[][]不需要傳遞參數(shù)進(jìn)來(lái)
{
    for(int i=0;i<5;i++)
        for(int j=0;j<8;j++)
        {
            if(map[i][j]!=0)//只要有磚塊就不判斷為勝利
            {
                return 0;
            }
        }
    return 1;
}
void drawMap()
{
    setlinestyle(PS_SOLID,2,0);
    setlinecolor(WHITE);
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<8;j++)
        {
           int x=100*j;//左上角x坐標(biāo)
           int y=25*i;//左上角y坐標(biāo)
            switch(map[i][j])
            {
                case 0: //設(shè)定一個(gè)0值,留下做方塊的消除
                    break;
                case 1:
                    setfillcolor(YELLOW);
                    fillrectangle(x,y,x+100,y+25);
                    break;
                case 2:
                    setfillcolor(LIGHTBLUE);
                    fillrectangle(x,y,x+100,y+25);
                    break;
                case 3:
                    setfillcolor(GREEN);
                    fillrectangle(x,y,x+100,y+25);
                    break;
                    
            }
        }
    }
}
//球
//1.反射

//2.撞擊木板,進(jìn)行范圍判斷

//3.撞擊磚塊,進(jìn)行條件判斷
struct Ball
{
    int x;
    int y;
    int r;//半徑
    int dx;//x增量
    int dy;
    COLORREF color;
};
//下面的創(chuàng)建函數(shù)對(duì)著模仿
struct Ball *createBall(int x,int y,int r, int dx, int dy, COLORREF color)
{
    struct Ball *pBall=(struct Ball*)malloc(sizeof(struct Ball));
    pBall->x=x;
    pBall->y=y;
    pBall->r=r;
    pBall->dx=dx;
    pBall->dy=dy;
    pBall->color=color;
    return pBall;
    
}
void drawBall(struct Ball *pBall)
{
    setfillcolor(pBall->color);
    solidcircle(pBall->x,pBall->y,pBall->r);
}

//繪制木板 
struct Board
{
    int x;
    int y;
    int speed;
    COLORREF color;
    int width;
    int height;
};

struct Board* readInfoFromFile(const char *fileName)
{
    struct Board *pBoard=(struct Board*)malloc(sizeof(struct Board));
     FILE *fp=fopen(fileName,"r");//以讀的方式
     
    
    while(fscanf(fp,"%d\t%d\t%d\t%s\t%d\t%d\t",&pBoard->x,&pBoard->y,&pBoard->speed,&pBoard->color,&pBoard->width,&pBoard->height)!=EOF)
        //讀到這個(gè)變量里面去,不做格式控制,當(dāng)他讀到的時(shí)候,插入到鏈表Node里面
    {}
    
    
    fclose(fp);
    return pBoard;
    
    
}

int hitBricks(struct Ball *pBall)
{
    //1.算出球的行和列是屬于地圖的
    int ballJ=pBall->x/100;
    int ballI=(pBall->y-pBall->r)/25;// 考慮半徑
    //2.當(dāng)前下標(biāo)下,數(shù)組中油不等于0的磚塊需要反射,并且重置為0
    if((ballJ<8&&ballI<5)&&map[ballI][ballJ]!=0) //在5行8列里面,并且有磚塊
        //
    {
        map[ballI][ballJ]--;//重置為0
        return 1;//返回一個(gè)反射的指令
    }
    return 0;
}
//木板的按鍵操作
//木板的按鍵操作
void keyDown(struct Board *pBoard)
{
    // C語(yǔ)言的基本函數(shù): scanf(),getch(),getchar(),gets()為主篩函數(shù),先后順序,不太實(shí)用
    //游戲是實(shí)時(shí)的,我們需要實(shí)現(xiàn)異步的按鍵操作(類似邊刷牙邊打電話)
    if((GetAsyncKeyState('A')||GetAsyncKeyState(VK_LEFT))&&pBoard->x>=0)//限制邊界在大于0
    {
        //按左鍵,x變小,做減運(yùn)算
        pBoard->x=pBoard->x-pBoard->speed;
        
    }
    if((GetAsyncKeyState('D')||GetAsyncKeyState(VK_RIGHT))&&pBoard->x<=800-200)//限制邊界小于800-200
    {
        pBoard->x=pBoard->x+pBoard->speed;
        
    }
}
void drawBoard(struct Board *pBoard) //傳遞木板結(jié)構(gòu)體變量指針
{
    setfillcolor(pBoard->color);//封裝的函數(shù)繪制,盡量傳遞進(jìn)入變量
    fillrectangle(pBoard->x,pBoard->y,pBoard->x+pBoard->width,pBoard->y+pBoard->height); //從坐標(biāo)x,y繪制到 x+寬度,y+高度坐標(biāo)
}
//先不考慮反射,簡(jiǎn)單學(xué)習(xí)入手
int hitBoard(struct Ball *pBall, struct Board *pBoard) //木板和球的參數(shù)都需要
{
    if(pBall->y+pBall->r==pBoard->y) //y滿足
    {
        if(pBall->x>=pBoard->x&&pBall->x<=pBoard->x+pBoard->width)
            return 1; //表示撞擊
    } 
    return 0;
    
    
}
void moveBall(struct Ball *pBall,struct Board *pBoard)
{
    //增加反射
    //左右碰壁
    if(pBall->x-pBall->r<=0||pBall->x+pBall->r>=800) //記錄球的半徑就是這個(gè)作用
        //左右碰壁,x減去半徑小于0或者x加上半徑大于800
    {
        pBall->dx=-pBall->dx; //左右墻,dx反射
    }
    //上下碰壁
    if(pBall->y-pBall->r<=0||hitBoard(pBall,pBoard)||hitBricks(pBall)) //x坐標(biāo)改為y坐標(biāo)就行了,窗口一樣大
    {//增加碰磚塊消除
        pBall->dy=-pBall->dy; //上下墻,dy反射
    }
    
    pBall->x+=pBall->dx;
    pBall->y+=pBall->dy; // 移動(dòng),注意斜著向上運(yùn)動(dòng),x增加,y減小
}
int gameOver(struct Ball *pBall)
{
    if(pBall->y>800-25)
    {return 1;} 
   return 0;
}


int main()
{
    srand((unsigned int)time(0));//設(shè)置隨機(jī)數(shù)種子
    initgraph(800,800);  //繪制界面,8行8列,每個(gè)磚塊是100,所以橫向至少800。
    initMap(); //調(diào)用初始化二維數(shù)組
    
        
    struct Board *pBoard=readInfoFromFile("boardInfor.txt"); //木板初始坐標(biāo)計(jì)算,在底部中心,速度為1,白色,200×25的方塊
    //同樣的我們創(chuàng)建球與繪制球
    struct Ball *pBall=createBall(400,600,15,5,-5,RED); //球放在中間,大小為15,通過(guò)控制dx,dy作為運(yùn)動(dòng)矢量來(lái)控制球的初始化運(yùn)動(dòng)方向
    //相同的代碼我們放一起方便觀察
    BeginBatchDraw(); //內(nèi)存中繪制
    while(1)
    {
        cleardevice();
        drawMap();//根據(jù)map數(shù)組來(lái)繪制磚塊
        drawBoard(pBoard); //傳入 pBoard結(jié)構(gòu)體指針繪制一下木板

        drawBall(pBall);
        moveBall(pBall,pBoard);
        Sleep(10);

        keyDown(pBoard);
        if(gameOver(pBall))
        {
            MessageBox(hwnd,"游戲結(jié)束","game over",MB_OK);
            exit(0);
        }
        if(gameWin())
        {
            MessageBox(hwnd,"游戲結(jié)束","game win",MB_OK);
            exit(0);

        }
        FlushBatchDraw(); //畫一幀,丟棄一幀
    }
    EndBatchDraw();//結(jié)束
    closegraph();
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容