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

二、基本框架實(shí)現(xiàn)
代碼如下:
#include<conio.h>
#include<graphics.h>
#define High 480//游戲畫面尺寸
#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()//消除畫面
{
?? ?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()//與用戶輸入無關(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();//與用戶輸入無關(guān)的更新
?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新
?? ??? ?show();//顯示新畫面
?? ?}
?? ?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//游戲畫面尺寸
#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()//消除畫面
{
?? ?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()//與用戶輸入無關(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();//與用戶輸入無關(guān)的更新
?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新
?? ??? ?show();//顯示新畫面
?? ?}
?? ?gameover();//游戲結(jié)束,進(jìn)行后續(xù)處理
?? ?return 0;
}效果圖如下:

四、雙人反彈球
代碼如下:
#include<conio.h>
#include<graphics.h>
#include<Windows.h>
#define High 480//游戲畫面尺寸
#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()//消除畫面
{
?? ?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()//與用戶輸入無關(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)//判斷擋板是否超過屏幕
?? ??? ?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();//與用戶輸入無關(guān)的更新
?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新
?? ??? ?show();//顯示新畫面
?? ?}
?? ?gameover();//游戲結(jié)束,進(jìn)行后續(xù)處理
?? ?return 0;
}效果圖如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++ 基于BFS算法的走迷宮自動(dòng)尋路的實(shí)現(xiàn)
這篇文章主要為大家介紹了C++ 基于BFS算法實(shí)現(xiàn)走迷宮自動(dòng)尋路,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
C++實(shí)現(xiàn)數(shù)字轉(zhuǎn)換為十六進(jìn)制字符串的方法
這篇文章主要介紹了C++實(shí)現(xiàn)數(shù)字轉(zhuǎn)換為十六進(jìn)制字符串的方法,涉及C++操作數(shù)字與字符串轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-06-06
實(shí)例分析一個(gè)簡(jiǎn)單的Win32程序
這篇文章主要介紹了實(shí)例分析一個(gè)簡(jiǎn)單的Win32程序,對(duì)于Win32應(yīng)用程序的原理、執(zhí)行流程、實(shí)現(xiàn)方法主要環(huán)節(jié)都做了較為詳細(xì)的分析,有助于讀者深入理解Windows應(yīng)用程序設(shè)計(jì),需要的朋友可以參考下2014-09-09
C語言中程序環(huán)境和預(yù)處理的詳細(xì)圖文講解
這篇文章主要給大家介紹了關(guān)于C語言中程序環(huán)境和預(yù)處理的相關(guān)資料,我們寫的C語言代碼,從運(yùn)行,到在屏幕上生成結(jié)果,經(jīng)歷了比較復(fù)雜的過程,需要的朋友可以參考下2023-02-02
C語言實(shí)現(xiàn)24點(diǎn)游戲源代碼
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)24點(diǎn)游戲源代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10

