您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用C語(yǔ)言實(shí)現(xiàn)黃金礦工游戲”,在日常操作中,相信很多人在怎么用C語(yǔ)言實(shí)現(xiàn)黃金礦工游戲問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么用C語(yǔ)言實(shí)現(xiàn)黃金礦工游戲”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
首先我們先創(chuàng)建一個(gè)頭文件,把一些結(jié)構(gòu)體以及枚舉類(lèi)型的函數(shù)放進(jìn)去,這樣會(huì)讓整個(gè)項(xiàng)目看起來(lái)更加有序,更好理解,先把枚舉類(lèi)型放進(jìn)去
enum ATTR { //圖片對(duì)應(yīng)的數(shù)組下標(biāo) i_gold=1, i_money=3, i_role=5, i_stone=9, i_bk= i_stone+1, //窗口尺寸 WIDTH = 1080, HEIGHT= 640, //物品數(shù)量 MINE_NUM=10, }; enum TYPE { //物品類(lèi)型 GOLD, //金塊 MONEY, //錢(qián)袋 STONE, //石頭 //擺動(dòng)方向 LEFT, RIGHT, //擺動(dòng)狀態(tài) M_LONG, M_NORMAL, M_SHORT, };
之后把我們的老朋友結(jié)構(gòu)體也放進(jìn)去
struct Role { int x; //貼圖的位置 int y; int width;//圖片寬度和高度 int height; int coin;//金幣 }; struct Mine //物品 { int x; int y; int size;//用來(lái)計(jì)算碰撞 int flag;//物品是否存在 int type;//物品類(lèi)型,錢(qián)袋,石頭,金塊 int gold;//價(jià)值 }; //鉤子 struct Hook { double x;//繩子開(kāi)始坐標(biāo),固定不變的 double y; double endx;//末端變化的坐標(biāo) double endy; int len;//繩子長(zhǎng)度 int dir;//擺動(dòng)方向 double angle;//擺動(dòng)角度 double speed;//速度 double vx;//速度分量 double vy; int swing;//是否在擺動(dòng) int state;//伸長(zhǎng)狀態(tài),伸長(zhǎng),正常,縮短 int index;//抓到的物品下標(biāo) };
OK,接下來(lái)就是我們的主要函數(shù)main.Cpp了,記得開(kāi)始的時(shí)候加上我們寫(xiě)的頭文件,先寫(xiě)初始化函數(shù)
void GameInit() { //初始化隨機(jī)數(shù)種子 srand(GetTickCount()); //初始化角色數(shù)據(jù) role.coin = 0; role.width = 140; role.height = 120; role.x = WIDTH / 2 - role.width / 2;//讓角色圖片居中顯示 role.y = 0; //加載圖片 for (int i = 0; i < 10; i++) { char fileName[20]; sprintf(fileName, "./images/%d.jpg", i); if (i <= 1) { loadimage(&img[i], fileName,73,62); } else { loadimage(&img[i], fileName); } } loadimage(&img[i_bk], "./images/bk.jpg",WIDTH,HEIGHT-role.height); //初始化物品 for (int i = 0; i < MINE_NUM; i++) { mine[i].flag = 1; mine[i].size = 60; mine[i].type = rand() % 3; mine[i].x=rand()%(WIDTH-mine[i].size); mine[i].y=rand()%(HEIGHT-role.height-100)+ role.height+ 50; mine[i].gold = rand()%600+rand()%200; } //初始化鉤子 hook.x = role.x+45; hook.y = role.y+100; hook.len = 50; hook.endx = hook.x; hook.endy=hook.y+hook.len; hook.angle = 0.0; hook.dir = RIGHT; hook.state = M_NORMAL; hook.vx = 0; hook.vy = 0; hook.speed = 5.0; hook.index = -1; }
再寫(xiě)我們的繪制函數(shù),這個(gè)比較簡(jiǎn)單,就是貼圖
void Gamedraw() { BeginBatchDraw(); //設(shè)置背景顏色 setbkcolor(GREEN); cleardevice(); putimage(0, role.height, &img[i_bk]); //透明貼圖 兩張圖片,一張掩碼圖,一張?jiān)瓐D putimage(role.x, role.y, &img[i_role-1],SRCAND);//掩碼圖 putimage(role.x, role.y, &img[i_role],SRCPAINT);//原圖 //繪制鉤子 setlinestyle(PS_SOLID, 5); setlinecolor(BROWN); line(hook.x, hook.y, hook.endx, hook.endy); //繪制物品 for (int i = 0; i < MINE_NUM; i++) { if (mine[i].flag) { switch (mine[i].type) { case GOLD: putimage(mine[i].x, mine[i].y, &img[i_gold-1],SRCAND); putimage(mine[i].x, mine[i].y, &img[i_gold],SRCPAINT); break; case MONEY: putimage(mine[i].x, mine[i].y, &img[i_money-1], SRCAND); putimage(mine[i].x, mine[i].y, &img[i_money], SRCPAINT); break; case STONE: putimage(mine[i].x, mine[i].y, &img[i_stone-1], SRCAND); putimage(mine[i].x, mine[i].y, &img[i_stone], SRCPAINT); break; } } } //繪制分?jǐn)?shù) char s[30]; sprintf(s, "金幣:%d", role.coin); settextstyle(50, 0, "黑體"); outtextxy(50, 50, s); EndBatchDraw(); }
鉤子擺動(dòng)的函數(shù),鉤子該如何的擺,主要是讓他不要往天上擺就行
//鉤子擺動(dòng) void hookRock() { if (hook.state == M_NORMAL) { if (hook.dir == RIGHT) { hook.angle++; } else { hook.angle--; } if (hook.angle > 80) { hook.dir = LEFT; } else if (hook.angle < -80) { hook.dir = RIGHT; } hook.endx = hook.x + sin(π / 180 * hook.angle) * hook.len; hook.endy = hook.y + cos(π / 180 * hook.angle) * hook.len; } } int distance(struct Hook hook) { double dis=sqrt((hook.x-hook.endx)* (hook.x - hook.endx) + (hook.y-hook.endy) * (hook.y - hook.endy)); return dis <= hook.len; } void keyControl() { //按空格伸長(zhǎng) if (GetAsyncKeyState(VK_SPACE) && hook.state == M_NORMAL) { hook.state = M_LONG; hook.vx = sin(π / 180 * hook.angle) * hook.speed; hook.vy = cos(π / 180 * hook.angle) * hook.speed; } if (hook.endx <= 0 || hook.endx >= WIDTH || hook.endy >= HEIGHT) { hook.state = M_SHORT; } if (hook.state == M_LONG) { hook.endx += hook.vx; hook.endy += hook.vy; } else if (hook.state == M_SHORT) { hook.endx -= hook.vx; hook.endy -= hook.vy; //如果縮短到原來(lái)的長(zhǎng)度,就停止縮短,判斷起點(diǎn)和末端的距離是否等于,長(zhǎng)度 if (distance(hook)) { hook.state = M_NORMAL; } } }
接下來(lái)是我們的抓取函數(shù),也是比較簡(jiǎn)單
void grap() { //找到抓取的是哪個(gè)物品 for (int i = 0; i < MINE_NUM; i++) { if (mine[i].flag && hook.endx > mine[i].x && hook.endx<mine[i].x + mine[i].size && hook.endy>mine[i].y && hook.endy < mine[i].y + mine[i].size) { hook.index = i;//保存抓到的物品的下標(biāo) break; } } if (hook.index != -1) { hook.state = M_SHORT; mine[hook.index].x = hook.endx-mine[hook.index].size/2; mine[hook.index].y = hook.endy- mine[hook.index].size / 2; if (distance(hook)) { hook.state = M_NORMAL; mine[hook.index].flag = 0; role.coin += mine[hook.index].gold; hook.state = M_NORMAL; hook.index = -1; } } }
最后是我們的主函數(shù)
int main() { initgraph(WIDTH,HEIGHT,1); GameInit(); while (1) { printf("%lf,%lf vxy(%lf,%lf)\n", hook.endx, hook.endy,hook.vx,hook.vy); hookRock(); Gamedraw(); keyControl(); grap(); } closegraph(); return 0; }
到此,關(guān)于“怎么用C語(yǔ)言實(shí)現(xiàn)黃金礦工游戲”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。