日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

项目: 双人反弹球游戏

發布時間:2025/3/20 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 项目: 双人反弹球游戏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

  • 一、最終項目描述和效果
  • 二、基本框架實現
  • 三、異步操作的實現
  • 四、雙人反彈球

一、最終項目描述和效果

項目描述: 實現雙人玩的彈跳球游戲

最終效果圖如下:

二、基本框架實現

代碼如下:

#include<conio.h> #include<graphics.h> #define High 480//游戲畫面尺寸 #define Width 640//全局變量 int ball1_x,ball1_y;//小球1的坐標 int ball2_x,ball2_y;//小球2的坐標 int radius;void startup()//數據的初始化 {ball1_x=Width/3;ball1_y=High/3;ball2_x=Width*2/3;ball2_y=High*2/3;radius=20;initgraph(Width,High);BeginBatchDraw(); }void clean()//消除畫面 {setcolor(BLACK);setfillcolor(BLACK);fillcircle(ball1_x,ball1_y,radius);fillcircle(ball2_x,ball2_y,radius); }void show()//顯示畫面 {setcolor(GREEN);setfillcolor(GREEN);fillcircle(ball1_x,ball1_y,radius);//繪制綠圓setcolor(RED);setfillcolor(RED);fillcircle(ball2_x,ball2_y,radius);//繪制紅圓FlushBatchDraw();Sleep(3); }void updateWithoutInput()//與用戶輸入無關的更新 {}void updateWithInput()//與用戶輸入有關的更新 {char input;if(kbhit())//判斷是否有輸入{input=getch();int step=10;if(input=='a')ball1_x-=step;if(input=='d')ball1_x+=step;if(input=='w')ball1_y-=step;if(input=='s')ball1_y+=step;if(input=='4')ball2_x-=step;if(input=='6')ball2_x+=step;if(input=='8')ball2_y-=step;if(input=='5')ball2_y+=step;} }void gameover() {EndBatchDraw();closegraph(); }int main(void) {startup();//數據的初始化while(1){clean();//把之前繪制的內容取消updateWithoutInput();//與用戶輸入無關的更新updateWithInput();//與用戶輸入有關的更新show();//顯示新畫面}gameover();//游戲結束,進行后續處理return 0; } 你會發現這有一個弊端: 雙方同一時刻只能有一個運行,不能同時運行。

三、異步操作的實現


代碼如下:

#include<conio.h> #include<graphics.h> #define High 480//游戲畫面尺寸 #define Width 640//全局變量 int ball1_x,ball1_y;//小球1的坐標 int ball2_x,ball2_y;//小球2的坐標 int radius;void startup()//數據的初始化 {ball1_x=Width/3;ball1_y=High/3;ball2_x=Width*2/3;ball2_y=High*2/3;radius=20;initgraph(Width,High);BeginBatchDraw(); }void clean()//消除畫面 {setcolor(BLACK);setfillcolor(BLACK);fillcircle(ball1_x,ball1_y,radius);fillcircle(ball2_x,ball2_y,radius); }void show()//顯示畫面 {setcolor(GREEN);setfillcolor(GREEN);fillcircle(ball1_x,ball1_y,radius);//繪制綠圓setcolor(RED);setfillcolor(RED);fillcircle(ball2_x,ball2_y,radius);//繪制紅圓FlushBatchDraw();Sleep(3); }void updateWithoutInput()//與用戶輸入無關的更新 {}void updateWithInput()//與用戶輸入有關的更新 {int step=1;if((GetAsyncKeyState(0x41)&0x8000))//aball1_x-=step;if((GetAsyncKeyState(0x44)&0x8000))//dball1_x+=step;if((GetAsyncKeyState(0x57)&0x8000))//wball1_y-=step;if((GetAsyncKeyState(0x53)&0x8000))//sball1_y+=step;if((GetAsyncKeyState(VK_LEFT)&0x8000))//左方向鍵ball2_x-=step;if((GetAsyncKeyState(VK_RIGHT)&0x8000))//右方向鍵ball2_x+=step;if((GetAsyncKeyState(VK_UP)&0x8000))//上方向鍵ball2_y-=step;if((GetAsyncKeyState(VK_DOWN)&0x8000))//下方向鍵ball2_y+=step; }void gameover() {EndBatchDraw();closegraph(); }int main(void) {startup();//數據的初始化while(1){clean();//把之前繪制的內容取消updateWithoutInput();//與用戶輸入無關的更新updateWithInput();//與用戶輸入有關的更新show();//顯示新畫面}gameover();//游戲結束,進行后續處理return 0; }

效果圖如下:

四、雙人反彈球

代碼如下:

#include<conio.h> #include<graphics.h> #include<Windows.h> #define High 480//游戲畫面尺寸 #define Width 640//全局變量 int ball_x,ball_y;//小球的坐標 int ball_vx,ball_vy;//小球2的速度 int bar1_left,bar1_right,bar1_top,bar1_bottom;//擋板1的上下左右位置坐標 int bar2_left,bar2_right,bar2_top,bar2_bottom;//擋板2的上下左右位置坐標 int bar_height,bar_width;//擋板的高度和寬度 int radius;void startup()//數據的初始化 {ball_x=Width/2;ball_y=High/2;ball_vx=1;ball_vy=1;radius=20;bar_width=Width/30;bar_height=High/4;bar1_left=Width*1/20;//擋板1的數據初始化bar1_top=High/4;bar1_right=bar1_left+bar_width;bar1_bottom=bar1_top+bar_height;bar2_left=Width*18.5/20;//擋板2的數據初始化bar2_top=High/4;bar2_right=bar2_left+bar_width;bar2_bottom=bar2_top+bar_height;initgraph(Width,High);BeginBatchDraw(); }void clean()//消除畫面 {setcolor(BLACK);setfillcolor(BLACK);fillcircle(ball_x,ball_y,radius);fillcircle(ball_x,ball_y,radius);bar(bar1_left,bar1_top,bar1_right,bar1_bottom);//繪制擋板bar(bar2_left,bar2_top,bar2_right,bar2_bottom); }void show()//顯示畫面 {setcolor(GREEN);setfillcolor(GREEN);fillcircle(ball_x,ball_y,radius);//繪制綠圓setcolor(RED);setfillcolor(RED);bar(bar1_left,bar1_top,bar1_right,bar1_bottom);bar(bar2_left,bar2_top,bar2_right,bar2_bottom);FlushBatchDraw();Sleep(3); }void updateWithoutInput()//與用戶輸入無關的更新 {//擋板和小球碰撞,小球反彈if(ball_x+radius>=bar2_left&&ball_y+radius>=bar2_top&&ball_y+radius<bar2_bottom)ball_vx=-ball_vx;else if(ball_x-radius<=bar1_right&&ball_y+radius>=bar1_top&&ball_y+radius<bar1_bottom)ball_vx=-ball_vx;//更新小球的坐標ball_x=ball_x+ball_vx;ball_y=ball_y+ball_vy;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()//與用戶輸入有關的更新 {int step=1;if((GetAsyncKeyState(0x57)&0x8000))//wbar1_top-=step;if((GetAsyncKeyState(0x53)&0x8000))//sbar1_top+=step;if((GetAsyncKeyState(VK_UP)&0x8000))//上方向鍵bar2_top-=step;if((GetAsyncKeyState(VK_DOWN)&0x8000))//下方向鍵bar2_top+=step;if(bar1_top<0)//判斷擋板是否超過屏幕bar1_top+=step;if(bar2_top<0)bar2_top+=step;if(bar1_top+bar_height>High)bar1_top-=step;if(bar2_top+bar_height>High)bar2_top-=step;bar1_bottom=bar1_top+bar_height;bar2_bottom=bar2_top+bar_height; }void gameover() {EndBatchDraw();closegraph(); }int main(void) {startup();//數據的初始化while(1){clean();//把之前繪制的內容取消updateWithoutInput();//與用戶輸入無關的更新updateWithInput();//與用戶輸入有關的更新show();//顯示新畫面}gameover();//游戲結束,進行后續處理return 0; }

效果圖如下:

總結

以上是生活随笔為你收集整理的项目: 双人反弹球游戏的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。