最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

本文項目為大家分享C語言實現(xiàn)雙人反彈球游戲的具體代碼,供大家參考,具體內容如下

一、最終項目描述和效果

項目描述:   實現(xiàn)雙人玩的彈跳球游戲

最終效果圖如下:

二、基本框架實現(xiàn)

代碼如下:

#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()//數(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()//與用戶輸入無關的更新
{

}

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();//數(shù)據(jù)的初始化
?? ?while(1)
?? ?{
?? ??? ?clean();//把之前繪制的內容取消
?? ??? ?updateWithoutInput();//與用戶輸入無關的更新
?? ??? ?updateWithInput();//與用戶輸入有關的更新
?? ??? ?show();//顯示新畫面
?? ?}
?? ?gameover();//游戲結束,進行后續(xù)處理
?? ?return 0;
}

你會發(fā)現(xiàn)這有一個弊端:  雙方同一時刻只能有一個運行,不能同時運行。

三、異步操作的實現(xiàn)

代碼如下:

#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()//數(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()//與用戶輸入無關的更新
{

}

void updateWithInput()//與用戶輸入有關的更新
{
?? ?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();//把之前繪制的內容取消
?? ??? ?updateWithoutInput();//與用戶輸入無關的更新
?? ??? ?updateWithInput();//與用戶輸入有關的更新
?? ??? ?show();//顯示新畫面
?? ?}
?? ?gameover();//游戲結束,進行后續(xù)處理
?? ?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()//數(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()//與用戶輸入無關的更新
{
?? ?//擋板和小球碰撞,小球反彈
?? ?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))//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();//把之前繪制的內容取消
?? ??? ?updateWithoutInput();//與用戶輸入無關的更新
?? ??? ?updateWithInput();//與用戶輸入有關的更新
?? ??? ?show();//顯示新畫面
?? ?}
?? ?gameover();//游戲結束,進行后續(xù)處理
?? ?return 0;
}

效果圖如下:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 如何基于C++解決RTSP取流報錯問題

    如何基于C++解決RTSP取流報錯問題

    這篇文章主要介紹了如何基于C++解決RTSP取流報錯問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • C 轉移表/轉換表的深入分析

    C 轉移表/轉換表的深入分析

    本篇文章是對c語言中轉移表/轉換表進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C++ 基于BFS算法的走迷宮自動尋路的實現(xiàn)

    C++ 基于BFS算法的走迷宮自動尋路的實現(xiàn)

    這篇文章主要為大家介紹了C++ 基于BFS算法實現(xiàn)走迷宮自動尋路,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • C++實現(xiàn)數(shù)字轉換為十六進制字符串的方法

    C++實現(xiàn)數(shù)字轉換為十六進制字符串的方法

    這篇文章主要介紹了C++實現(xiàn)數(shù)字轉換為十六進制字符串的方法,涉及C++操作數(shù)字與字符串轉換的相關技巧,需要的朋友可以參考下
    2015-06-06
  • 實例分析一個簡單的Win32程序

    實例分析一個簡單的Win32程序

    這篇文章主要介紹了實例分析一個簡單的Win32程序,對于Win32應用程序的原理、執(zhí)行流程、實現(xiàn)方法主要環(huán)節(jié)都做了較為詳細的分析,有助于讀者深入理解Windows應用程序設計,需要的朋友可以參考下
    2014-09-09
  • Qt中QCommandLinkButton控件的使用

    Qt中QCommandLinkButton控件的使用

    QCommandLinkButton 是 Qt 框架中 QtWidgets 模塊的一個類,它提供了一個結合了文本標簽和按鈕功能的控件,本文主要介紹了Qt中QCommandLinkButton控件的使用,感興趣的可以了解一下
    2025-04-04
  • C/C++線程退出的四種方法小結

    C/C++線程退出的四種方法小結

    本文主要介紹了C/C++線程退出的四種方法小結,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07
  • C語言中程序環(huán)境和預處理的詳細圖文講解

    C語言中程序環(huán)境和預處理的詳細圖文講解

    這篇文章主要給大家介紹了關于C語言中程序環(huán)境和預處理的相關資料,我們寫的C語言代碼,從運行,到在屏幕上生成結果,經(jīng)歷了比較復雜的過程,需要的朋友可以參考下
    2023-02-02
  • C/C++ INI文件操作實現(xiàn)代碼

    C/C++ INI文件操作實現(xiàn)代碼

    本文章主要為分享C/C++ INI文件操作實現(xiàn)代碼,增加注釋和修復了一些問題。這里給出完整的實現(xiàn)文件,在需要的地方包含該頭文件就好了
    2020-02-02
  • C語言實現(xiàn)24點游戲源代碼

    C語言實現(xiàn)24點游戲源代碼

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)24點游戲源代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10

最新評論

巧家县| 梓潼县| 大姚县| 乌拉特前旗| 东明县| 南安市| 海原县| 通化县| 珠海市| 乌鲁木齐县| 合山市| 嘉兴市| 金寨县| 乐安县| 三河市| 湖州市| 甘孜县| 固始县| 东乡县| 阿克苏市| 吐鲁番市| 霍邱县| 清远市| 淄博市| 天祝| 新蔡县| 丰县| 策勒县| 浙江省| 辰溪县| 龙口市| 延津县| 滦平县| 丽水市| 汉寿县| 普宁市| 五家渠市| 安徽省| 张家口市| 华安县| 阳原县|