C語(yǔ)言實(shí)現(xiàn)雙人反彈球游戲
本文項(xiàng)目為大家分享C語(yǔ)言實(shí)現(xiàn)雙人反彈球游戲的具體代碼,供大家參考,具體內(nèi)容如下
一、最終項(xiàng)目描述和效果
項(xiàng)目描述: 實(shí)現(xiàn)雙人玩的彈跳球游戲
最終效果圖如下:

二、基本框架實(shí)現(xiàn)
代碼如下:
#include<conio.h>
#include<graphics.h>
#define High 480//游戲畫(huà)面尺寸
#define Width 640
//全局變量
int ball1_x,ball1_y;//小球1的坐標(biāo)
int ball2_x,ball2_y;//小球2的坐標(biāo)
int radius;
void startup()//數(shù)據(jù)的初始化
{
?? ?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()//消除畫(huà)面
{
?? ?setcolor(BLACK);
?? ?setfillcolor(BLACK);
?? ?fillcircle(ball1_x,ball1_y,radius);
?? ?fillcircle(ball2_x,ball2_y,radius);
}
void show()//顯示畫(huà)面
{
?? ?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()//與用戶輸入無(wú)關(guān)的更新
{
}
void updateWithInput()//與用戶輸入有關(guān)的更新
{
?? ?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();//數(shù)據(jù)的初始化
?? ?while(1)
?? ?{
?? ??? ?clean();//把之前繪制的內(nèi)容取消
?? ??? ?updateWithoutInput();//與用戶輸入無(wú)關(guān)的更新
?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新
?? ??? ?show();//顯示新畫(huà)面
?? ?}
?? ?gameover();//游戲結(jié)束,進(jìn)行后續(xù)處理
?? ?return 0;
}你會(huì)發(fā)現(xiàn)這有一個(gè)弊端: 雙方同一時(shí)刻只能有一個(gè)運(yùn)行,不能同時(shí)運(yùn)行。
三、異步操作的實(shí)現(xiàn)

代碼如下:
#include<conio.h>
#include<graphics.h>
#define High 480//游戲畫(huà)面尺寸
#define Width 640
//全局變量
int ball1_x,ball1_y;//小球1的坐標(biāo)
int ball2_x,ball2_y;//小球2的坐標(biāo)
int radius;
void startup()//數(shù)據(jù)的初始化
{
?? ?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()//消除畫(huà)面
{
?? ?setcolor(BLACK);
?? ?setfillcolor(BLACK);
?? ?fillcircle(ball1_x,ball1_y,radius);
?? ?fillcircle(ball2_x,ball2_y,radius);
}
void show()//顯示畫(huà)面
{
?? ?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()//與用戶輸入無(wú)關(guān)的更新
{
}
void updateWithInput()//與用戶輸入有關(guān)的更新
{
?? ?int step=1;
?? ?if((GetAsyncKeyState(0x41)&0x8000))//a
?? ??? ?ball1_x-=step;
?? ?if((GetAsyncKeyState(0x44)&0x8000))//d
?? ??? ?ball1_x+=step;
?? ?if((GetAsyncKeyState(0x57)&0x8000))//w
?? ??? ?ball1_y-=step;
?? ?if((GetAsyncKeyState(0x53)&0x8000))//s
?? ??? ?ball1_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();//數(shù)據(jù)的初始化
?? ?while(1)
?? ?{
?? ??? ?clean();//把之前繪制的內(nèi)容取消
?? ??? ?updateWithoutInput();//與用戶輸入無(wú)關(guān)的更新
?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新
?? ??? ?show();//顯示新畫(huà)面
?? ?}
?? ?gameover();//游戲結(jié)束,進(jìn)行后續(xù)處理
?? ?return 0;
}效果圖如下:

四、雙人反彈球
代碼如下:
#include<conio.h>
#include<graphics.h>
#include<Windows.h>
#define High 480//游戲畫(huà)面尺寸
#define Width 640
//全局變量
int ball_x,ball_y;//小球的坐標(biāo)
int ball_vx,ball_vy;//小球2的速度
int bar1_left,bar1_right,bar1_top,bar1_bottom;//擋板1的上下左右位置坐標(biāo)
int bar2_left,bar2_right,bar2_top,bar2_bottom;//擋板2的上下左右位置坐標(biāo)
int bar_height,bar_width;//擋板的高度和寬度
int radius;
void startup()//數(shù)據(jù)的初始化
{
?? ?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的數(shù)據(jù)初始化
?? ?bar1_top=High/4;
?? ?bar1_right=bar1_left+bar_width;
?? ?bar1_bottom=bar1_top+bar_height;
?? ?bar2_left=Width*18.5/20;//擋板2的數(shù)據(jù)初始化
?? ?bar2_top=High/4;
?? ?bar2_right=bar2_left+bar_width;
?? ?bar2_bottom=bar2_top+bar_height;
?? ?initgraph(Width,High);
?? ?BeginBatchDraw();
}
void clean()//消除畫(huà)面
{
?? ?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()//顯示畫(huà)面
{
?? ?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()//與用戶輸入無(wú)關(guān)的更新
{
?? ?//擋板和小球碰撞,小球反彈
?? ?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;
?? ?//更新小球的坐標(biāo)
?? ?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()//與用戶輸入有關(guān)的更新
{
?? ?int step=1;
?? ?if((GetAsyncKeyState(0x57)&0x8000))//w
?? ??? ?bar1_top-=step;
?? ?if((GetAsyncKeyState(0x53)&0x8000))//s
?? ??? ?bar1_top+=step;
?? ?if((GetAsyncKeyState(VK_UP)&0x8000))//上方向鍵
?? ??? ?bar2_top-=step;
?? ?if((GetAsyncKeyState(VK_DOWN)&0x8000))//下方向鍵
?? ??? ?bar2_top+=step;
?? ?if(bar1_top<0)//判斷擋板是否超過(guò)屏幕
?? ??? ?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();//數(shù)據(jù)的初始化
?? ?while(1)
?? ?{
?? ??? ?clean();//把之前繪制的內(nèi)容取消
?? ??? ?updateWithoutInput();//與用戶輸入無(wú)關(guān)的更新
?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新
?? ??? ?show();//顯示新畫(huà)面
?? ?}
?? ?gameover();//游戲結(jié)束,進(jìn)行后續(xù)處理
?? ?return 0;
}效果圖如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C語(yǔ)言用數(shù)組實(shí)現(xiàn)反彈球消磚塊
- C語(yǔ)言用函數(shù)實(shí)現(xiàn)反彈球消磚塊
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單反彈球消磚塊游戲
- C語(yǔ)言實(shí)現(xiàn)反彈球消磚塊游戲
- C語(yǔ)言實(shí)現(xiàn)反彈球游戲
- C語(yǔ)言實(shí)現(xiàn)反彈球小游戲
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單彈球游戲
- C語(yǔ)言實(shí)現(xiàn)彈跳小球
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單彈跳球游戲
- C語(yǔ)言基于EasyX庫(kù)實(shí)現(xiàn)有顏色彈跳小球
相關(guān)文章
c++基礎(chǔ)學(xué)習(xí)之如何區(qū)分引用和指針
C語(yǔ)言中只有指針,C++加入了引用,能夠起到跟指針類似的作用,下面這篇文章主要給大家介紹了關(guān)于c++基礎(chǔ)學(xué)習(xí)之區(qū)分引用和指針的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08
C++基于easyx圖形庫(kù)實(shí)現(xiàn)推箱子游戲
這篇文章主要為大家詳細(xì)介紹了C++基于easyx圖形庫(kù)實(shí)現(xiàn)推箱子游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
詳解C++設(shè)計(jì)模式編程中策略模式的優(yōu)缺點(diǎn)及實(shí)現(xiàn)
這篇文章主要介紹了C++設(shè)計(jì)模式編程中策略模式的優(yōu)缺點(diǎn)及實(shí)現(xiàn),文中討論了策略模式中設(shè)計(jì)抽象接口的繼承和組合之間的區(qū)別,需要的朋友可以參考下2016-03-03
C語(yǔ)言 動(dòng)態(tài)內(nèi)存分配詳解
這篇文章主要介紹了C語(yǔ)言 動(dòng)態(tài)內(nèi)存分配詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
使用ShellClass獲取文件屬性詳細(xì)信息的實(shí)現(xiàn)方法
本篇文章是對(duì)ShellClass獲取文件屬性詳細(xì)信息的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C語(yǔ)言實(shí)例真題講解數(shù)據(jù)結(jié)構(gòu)中單向環(huán)形鏈表
鏈表可以說(shuō)是一種最為基礎(chǔ)的數(shù)據(jù)結(jié)構(gòu)了,而單向鏈表更是基礎(chǔ)中的基礎(chǔ)。鏈表是由一組元素以特定的順序組合或鏈接在一起的,不同元素之間在邏輯上相鄰,但是在物理上并不一定相鄰。在維護(hù)一組數(shù)據(jù)集合時(shí),就可以使用鏈表,這一點(diǎn)和數(shù)組很相似2022-04-04
C++異常處理 try,catch,throw,finally的用法
這篇文章主要介紹了C++異常處理 try,catch,throw,finally的用法,需要的朋友可以參考下2018-01-01

