欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語(yǔ)言實(shí)現(xiàn)雙人反彈球游戲

 更新時(shí)間:2022年05月12日 16:05:36   作者:輝小歌  
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)雙人反彈球游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文項(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論