您好,登錄后才能下訂單哦!
這篇文章主要講解了“C語(yǔ)言中怎么用easyx實(shí)現(xiàn)消磚塊游戲”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“C語(yǔ)言中怎么用easyx實(shí)現(xiàn)消磚塊游戲”吧!
效果圖如下:
代碼如下:
#include<conio.h> #include<graphics.h> #define High 480 //游戲畫(huà)面尺寸 #define Width 640 //全局變量 int ball_x,ball_y;//小球的坐標(biāo) int ball_vx,ball_vy;//小球的速度 int radius;//小球的半徑 int bar_x,bar_y;//擋板的中心坐標(biāo) int bar_high,bar_width;//擋板的高度和寬度 int bar_left,bar_right,bar_top,bar_bottom;//擋板的左右上下位置坐標(biāo) void startup()//數(shù)據(jù)的初始化 { ball_x=Width/2; ball_y=High/2; ball_vx=1; ball_vy=1; radius=20; bar_high=High/20; bar_width=Width/5; bar_x=Width/2; bar_y=High-bar_high/2; bar_left=bar_x-bar_width/2; bar_right=bar_x+bar_width/2; bar_top=bar_y-bar_high/2; bar_bottom=bar_y+bar_high/2; initgraph(Width,High); BeginBatchDraw(); } void clean()//顯示畫(huà)面 { setcolor(BLACK);//繪制黑線,黑色填充的圓 setfillcolor(BLACK); fillcircle(ball_x,ball_y,radius); bar(bar_left,bar_top,bar_right,bar_bottom);//繪制黑色,黑色填充的擋板 } void show()//顯示畫(huà)面 { setcolor(YELLOW);//繪制黃線,綠色填充的圓 setfillcolor(GREEN); fillcircle(ball_x,ball_y,radius); bar(bar_left,bar_top,bar_right,bar_bottom);//繪制黃色,綠色填充的擋板 FlushBatchDraw(); Sleep(3); } void updateWithoutInput()//與用戶(hù)輸入無(wú)關(guān)的更新 { ball_x=ball_x+ball_vx; ball_y=ball_y,ball_vy;//更新小球的坐標(biāo) if( (ball_x<=radius)||(ball_x>=Width-radius)) ball_vx=-ball_vx; if( (ball_y<=radius)||(ball_y>=High-radius)) ball_vy=-ball_vy; } void updateWithInput()//與用戶(hù)輸入有關(guān)的更新 { } void gameover() { EndBatchDraw(); closegraph(); } int main() { startup();//數(shù)據(jù)的初始化 while(1) { clean();//把之前繪制的內(nèi)容清除 updateWithoutInput();//與用戶(hù)輸入無(wú)關(guān)的更新 updateWithInput();//與用戶(hù)輸入有關(guān)的更新 show();//顯示新畫(huà)面 } }
效果圖如下:
代碼如下:
#include<conio.h> #include<graphics.h> #define High 480 //游戲畫(huà)面尺寸 #define Width 640 //全局變量 int ball_x,ball_y;//小球的坐標(biāo) int ball_vx,ball_vy;//小球的速度 int radius;//小球的半徑 int bar_x,bar_y;//擋板的中心坐標(biāo) int bar_high,bar_width;//擋板的高度和寬度 int bar_left,bar_right,bar_top,bar_bottom;//擋板的左右上下位置坐標(biāo) void startup()//數(shù)據(jù)的初始化 { ball_x=Width/2; ball_y=High/2; ball_vx=1; ball_vy=1; radius=20; bar_high=High/20; bar_width=Width/5; bar_x=Width/2; bar_y=High-bar_high/2; bar_left=bar_x-bar_width/2; bar_right=bar_x+bar_width/2; bar_top=bar_y-bar_high/2; bar_bottom=bar_y+bar_high/2; initgraph(Width,High); BeginBatchDraw(); } void clean()//顯示畫(huà)面 { setcolor(BLACK);//繪制黑線,黑色填充的圓 setfillcolor(BLACK); fillcircle(ball_x,ball_y,radius); bar(bar_left,bar_top,bar_right,bar_bottom);//繪制黑色,黑色填充的擋板 } void show()//顯示畫(huà)面 { setcolor(YELLOW);//繪制黃線,綠色填充的圓 setfillcolor(GREEN); fillcircle(ball_x,ball_y,radius); bar(bar_left,bar_top,bar_right,bar_bottom);//繪制黃色,綠色填充的擋板 FlushBatchDraw(); Sleep(3); } void updateWithoutInput()//與用戶(hù)輸入無(wú)關(guān)的更新 { //擋板和小球碰撞,小球反彈 if(((ball_y+radius>=bar_top)&&(ball_y+radius<bar_bottom-bar_high/3)) ||((ball_y-radius<=bar_bottom)&&(ball_y-radius>bar_top-bar_high/3))) if((ball_x>=bar_left)&&(ball_x<=bar_right)) ball_vy=-ball_vy; ball_x=ball_x+ball_vx; ball_y=ball_y,ball_vy;//更新小球的坐標(biāo) if( (ball_x<=radius)||(ball_x>=Width-radius)) ball_vx=-ball_vx; if( (ball_y<=radius)||(ball_y>=High-radius)) ball_vy=-ball_vy; } void updateWithInput()//與用戶(hù)輸入有關(guān)的更新 { char input; if(kbhit()) { input=getch(); if(input=='a'&&bar_left>0) { bar_x=bar_x-15;//位置左移 bar_left=bar_x-bar_width/2; bar_right=bar_x+bar_width/2; } if(input=='d'&&bar_right<Width) { bar_x=bar_x+15;//位置左移 bar_left=bar_x-bar_width/2; bar_right=bar_x+bar_width/2; } if(input=='w'&&bar_top>0) { bar_y=bar_y-15;//位置左移 bar_top=bar_y-bar_high/2; bar_bottom=bar_y+bar_high/2; } if(input=='s'&&bar_bottom<High) { bar_y=bar_y+15;//位置右移 bar_top=bar_y-bar_high/2; bar_bottom=bar_y+bar_high/2; } } } void gameover() { EndBatchDraw(); closegraph(); } int main() { startup();//數(shù)據(jù)的初始化 while(1) { clean();//把之前繪制的內(nèi)容清除 updateWithoutInput();//與用戶(hù)輸入無(wú)關(guān)的更新 updateWithInput();//與用戶(hù)輸入有關(guān)的更新 show();//顯示新畫(huà)面 } }
效果圖如下:
代碼如下:
#include<conio.h> #include<graphics.h> #define High 480 //游戲畫(huà)面尺寸 #define Width 640 #define Brick_num 10 //全局變量 int ball_x,ball_y;//小球的坐標(biāo) int ball_vx,ball_vy;//小球的速度 int radius;//小球的半徑 int bar_x,bar_y;//擋板的中心坐標(biāo) int bar_high,bar_width;//擋板的高度和寬度 int bar_left,bar_right,bar_top,bar_bottom;//擋板的左右上下位置坐標(biāo) int isBrickExisted[Brick_num];//每個(gè)磚塊是否存在,1為存在,0為沒(méi)有了 int brick_high,brick_width;//每個(gè)磚塊的高度和寬度 void startup()//數(shù)據(jù)的初始化 { ball_x=Width/2; ball_y=High/2; ball_vx=1; ball_vy=1; radius=20; bar_high=High/20; bar_width=Width/5; bar_x=Width/2; bar_y=High-bar_high/2; bar_left=bar_x-bar_width/2; bar_right=bar_x+bar_width/2; bar_top=bar_y-bar_high/2; bar_bottom=bar_y+bar_high/2; brick_width=Width/Brick_num; brick_high=High/Brick_num; int i; for(i=0;i<Brick_num;i++) isBrickExisted[i]=1; initgraph(Width,High); BeginBatchDraw(); } void clean()//顯示畫(huà)面 { setcolor(BLACK);//繪制黑線,黑色填充的圓 setfillcolor(BLACK); fillcircle(ball_x,ball_y,radius); bar(bar_left,bar_top,bar_right,bar_bottom);//繪制黑色,黑色填充的擋板 int i,brick_left,brick_right,brick_top,brick_bottom; for(i=0;i<Brick_num;i++) { brick_left=i*brick_width; brick_right=brick_left+brick_width; brick_top=0; brick_bottom=brick_high; if(!isBrickExisted[i])//磚塊沒(méi)有了,繪制黑色 fillrectangle(brick_left,brick_top,brick_right,brick_bottom); } } void show()//顯示畫(huà)面 { setcolor(YELLOW);//繪制黃線,綠色填充的圓 setfillcolor(GREEN); fillcircle(ball_x,ball_y,radius); bar(bar_left,bar_top,bar_right,bar_bottom);//繪制黃色,綠色填充的擋板 int i,brick_left,brick_right,brick_top,brick_bottom; for(i=0;i<Brick_num;i++) { brick_left=i*brick_width; brick_right=brick_left+brick_width; brick_top=0; brick_bottom=brick_high; if(isBrickExisted[i])//磚塊存在,繪制磚塊 { setcolor(WHITE); setfillcolor(RED); fillrectangle(brick_left,brick_top,brick_right,brick_bottom);//繪制磚塊 } } FlushBatchDraw(); Sleep(3); } void updateWithoutInput()//與用戶(hù)輸入無(wú)關(guān)的更新 { //擋板和小球碰撞,小球反彈 if(((ball_y+radius>=bar_top)&&(ball_y+radius<bar_bottom-bar_high/3)) ||((ball_y-radius<=bar_bottom)&&(ball_y-radius>bar_top-bar_high/3))) if((ball_x>=bar_left)&&(ball_x<=bar_right)) ball_vy=-ball_vy; ball_x=ball_x+ball_vx; ball_y=ball_y+ball_vy;//更新小球的坐標(biāo) //小球和邊界碰撞 if( (ball_x<=radius)||(ball_x>=Width-radius)) ball_vx=-ball_vx; if( (ball_y<=radius)||(ball_y>=High-radius)) ball_vy=-ball_vy; //判斷小球是否和某個(gè)磚塊碰撞 int i,brick_left,brick_right,brick_top,brick_bottom; for(i=0;i<Brick_num;i++) { if(isBrickExisted[i])//磚塊存在才判斷 { brick_left=i*brick_width; brick_right=brick_left+brick_width; brick_bottom=brick_high; if((ball_y==brick_bottom+radius)&&(ball_x>=brick_left)&&(ball_x<= brick_right)) { isBrickExisted[i]=0; ball_vy=-ball_vy; } } } } void updateWithInput()//與用戶(hù)輸入有關(guān)的更新 { char input; if(kbhit()) { input=getch(); if(input=='a'&&bar_left>0) { bar_x=bar_x-15;//位置左移 bar_left=bar_x-bar_width/2; bar_right=bar_x+bar_width/2; } if(input=='d'&&bar_right<Width) { bar_x=bar_x+15;//位置左移 bar_left=bar_x-bar_width/2; bar_right=bar_x+bar_width/2; } } } void gameover() { EndBatchDraw(); closegraph(); } int main() { startup();//數(shù)據(jù)的初始化 while(1) { clean();//把之前繪制的內(nèi)容清除 updateWithoutInput();//與用戶(hù)輸入無(wú)關(guān)的更新 updateWithInput();//與用戶(hù)輸入有關(guān)的更新 show();//顯示新畫(huà)面 } }
效果圖如下:
先看一個(gè)關(guān)于鼠標(biāo)交互的實(shí)例
#include<graphics.h> #include<conio.h> int main(void) { initgraph(640,480);//初始化圖形窗口 MOUSEMSG m;//定義鼠標(biāo)消息 while(1) { m=GetMouseMsg();//獲取一條鼠標(biāo)消息 if(m.uMsg==WM_MOUSEMOVE) { putpixel(m.x,m.y,WHITE);//鼠標(biāo)移動(dòng)的時(shí)候畫(huà)小白點(diǎn) } else if(m.uMsg==WM_LBUTTONDOWN) { rectangle(m.x-5,m.y-5,m.x+5,m.y+5); //鼠標(biāo)左鍵按下時(shí)在鼠標(biāo)位置畫(huà)一個(gè)方塊 } else if(m.uMsg==WM_RBUTTONUP) { circle(m.x,m.y,10); //鼠標(biāo)右鍵按下時(shí)在鼠標(biāo)位置畫(huà)一個(gè)圓 } } return 0; }
用鼠標(biāo)控制擋板移動(dòng),按鼠標(biāo)左鍵初始化小球位置
代碼如下:
#include<conio.h> #include<graphics.h> #define High 480 //游戲畫(huà)面尺寸 #define Width 640 #define Brick_num 10 //全局變量 int ball_x,ball_y;//小球的坐標(biāo) int ball_vx,ball_vy;//小球的速度 int radius;//小球的半徑 int bar_x,bar_y;//擋板的中心坐標(biāo) int bar_high,bar_width;//擋板的高度和寬度 int bar_left,bar_right,bar_top,bar_bottom;//擋板的左右上下位置坐標(biāo) int isBrickExisted[Brick_num];//每個(gè)磚塊是否存在,1為存在,0為沒(méi)有了 int brick_high,brick_width;//每個(gè)磚塊的高度和寬度 void startup()//數(shù)據(jù)的初始化 { ball_x=Width/2; ball_y=High/2; ball_vx=1; ball_vy=1; radius=20; bar_high=High/20; bar_width=Width/5; bar_x=Width/2; bar_y=High-bar_high/2; bar_left=bar_x-bar_width/2; bar_right=bar_x+bar_width/2; bar_top=bar_y-bar_high/2; bar_bottom=bar_y+bar_high/2; brick_width=Width/Brick_num; brick_high=High/Brick_num; int i; for(i=0;i<Brick_num;i++) isBrickExisted[i]=1; initgraph(Width,High); BeginBatchDraw(); } void clean()//顯示畫(huà)面 { setcolor(BLACK);//繪制黑線,黑色填充的圓 setfillcolor(BLACK); fillcircle(ball_x,ball_y,radius); bar(bar_left,bar_top,bar_right,bar_bottom);//繪制黑色,黑色填充的擋板 int i,brick_left,brick_right,brick_top,brick_bottom; for(i=0;i<Brick_num;i++) { brick_left=i*brick_width; brick_right=brick_left+brick_width; brick_top=0; brick_bottom=brick_high; if(!isBrickExisted[i])//磚塊沒(méi)有了,繪制黑色 fillrectangle(brick_left,brick_top,brick_right,brick_bottom); } } void show()//顯示畫(huà)面 { setcolor(YELLOW);//繪制黃線,綠色填充的圓 setfillcolor(GREEN); fillcircle(ball_x,ball_y,radius); bar(bar_left,bar_top,bar_right,bar_bottom);//繪制黃色,綠色填充的擋板 int i,brick_left,brick_right,brick_top,brick_bottom; for(i=0;i<Brick_num;i++) { brick_left=i*brick_width; brick_right=brick_left+brick_width; brick_top=0; brick_bottom=brick_high; if(isBrickExisted[i])//磚塊存在,繪制磚塊 { setcolor(WHITE); setfillcolor(RED); fillrectangle(brick_left,brick_top,brick_right,brick_bottom);//繪制磚塊 } } FlushBatchDraw(); Sleep(3); } void updateWithoutInput()//與用戶(hù)輸入無(wú)關(guān)的更新 { //擋板和小球碰撞,小球反彈 if(((ball_y+radius>=bar_top)&&(ball_y+radius<bar_bottom-bar_high/3)) ||((ball_y-radius<=bar_bottom)&&(ball_y-radius>bar_top-bar_high/3))) if((ball_x>=bar_left)&&(ball_x<=bar_right)) ball_vy=-ball_vy; ball_x=ball_x+ball_vx; ball_y=ball_y+ball_vy;//更新小球的坐標(biāo) //小球和邊界碰撞 if( (ball_x<=radius)||(ball_x>=Width-radius)) ball_vx=-ball_vx; if( (ball_y<=radius)||(ball_y>=High-radius)) ball_vy=-ball_vy; //判斷小球是否和某個(gè)磚塊碰撞 int i,brick_left,brick_right,brick_top,brick_bottom; for(i=0;i<Brick_num;i++) { if(isBrickExisted[i])//磚塊存在才判斷 { brick_left=i*brick_width; brick_right=brick_left+brick_width; brick_bottom=brick_high; if((ball_y==brick_bottom+radius)&&(ball_x>=brick_left)&&(ball_x<= brick_right)) { isBrickExisted[i]=0; ball_vy=-ball_vy; } } } } void updateWithInput()//與用戶(hù)輸入有關(guān)的更新 { /*char input; if(kbhit()) { input=getch(); if(input=='a'&&bar_left>0) { bar_x=bar_x-15;//位置左移 bar_left=bar_x-bar_width/2; bar_right=bar_x+bar_width/2; } if(input=='d'&&bar_right<Width) { bar_x=bar_x+15;//位置左移 bar_left=bar_x-bar_width/2; bar_right=bar_x+bar_width/2; } }*/ MOUSEMSG m;//定義鼠標(biāo)信息 if(MouseHit())//這個(gè)函數(shù)用于檢測(cè)當(dāng)前是否有鼠標(biāo)消息 { m=GetMouseMsg();//獲取一條鼠標(biāo)消息 if(m.uMsg==WM_MOUSEMOVE) { //擋板的位置等于鼠標(biāo)所在的位置 bar_x=m.x; bar_y=m.y; bar_left=bar_x-bar_width/2; bar_right=bar_x+bar_width/2; bar_top=bar_y-bar_high/2; bar_bottom=bar_y+bar_high/2; } else if(m.uMsg==WM_LBUTTONDOWN) { ball_x=bar_x;//初始化小球的位置為擋板上面中心 ball_y=bar_top-radius-3; } } } void gameover() { EndBatchDraw(); closegraph(); } int main() { startup();//數(shù)據(jù)的初始化 while(1) { clean();//把之前繪制的內(nèi)容清除 updateWithoutInput();//與用戶(hù)輸入無(wú)關(guān)的更新 updateWithInput();//與用戶(hù)輸入有關(guān)的更新 show();//顯示新畫(huà)面 } }
效果圖如下:
感謝各位的閱讀,以上就是“C語(yǔ)言中怎么用easyx實(shí)現(xiàn)消磚塊游戲”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)C語(yǔ)言中怎么用easyx實(shí)現(xiàn)消磚塊游戲這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(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)容。