溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用C語言怎么實(shí)現(xiàn)一個(gè)反彈球游戲

發(fā)布時(shí)間:2021-04-16 16:57:35 來源:億速云 閱讀:292 作者:Leah 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)使用C語言怎么實(shí)現(xiàn)一個(gè)反彈球游戲,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

1.環(huán)境準(zhǔn)備和安裝

  • 安裝Visual C++ 6.0。

  • 去Easy X官網(wǎng)下載Easy X安裝包。

2.Eaxy X功能的簡(jiǎn)單介紹

  • Easy X類似于一個(gè)庫(kù)函數(shù),其中帶有許多很有用的函數(shù)。

  • Easy x首先創(chuàng)建一個(gè)新的窗口進(jìn)行繪圖。

  • 可以畫常見點(diǎn) 線 多邊形 可以調(diào)節(jié)顏色。

  • 可以插入圖片,音樂。

  • 可以獲取鼠標(biāo)信息。

  • 其中函數(shù)的具體使用可以看安裝包中帶有的幫助文件

3.反彈球游戲主函數(shù)框架

int main (void)
{
 starup();//數(shù)據(jù)初始化
 while(1)
 {
  show();//畫面初始化
  updateWithoutInput();//與用戶輸入無關(guān)的更新
  updateWithInput();//與用戶輸入有關(guān)的更新 
 } 

}
  • 與上篇貪吃蛇的框架類似

  • starup();對(duì)全局變量初始化

  • show();畫具體的圖像(球 目標(biāo) 木板)

  • updateWithoutInput(); 碰壁反彈 碰到木板反彈

  • updateWithInput();控制長(zhǎng)方形的移動(dòng)

4.頭文件的加載和全局變量的設(shè)定

#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

//全局變量
int high,width;//游戲尺寸
int ball_x,ball_y,ball_r;//球
int board_x,board_y,board_long,board_bottom;//木板
int gaol_x,gaol_y,gaol_long;//目標(biāo)
int velocity_x,velocity_y;//速度

5.第一個(gè)函數(shù)starup() 全局變量的初始化

void startup()
{
 high=540;width=480;//游戲尺寸
 ball_y=30;ball_x=width/3;ball_r=15;//球的坐標(biāo)

 board_y=high/2;board_x=width/2;//木板
 board_long=150;board_bottom=10;

 gaol_y=2;
 gaol_x=width/2;
 gaol_long=20; //目標(biāo)第一個(gè)點(diǎn)的坐標(biāo) 

 velocity_x=1,velocity_y=1;//速度

 initgraph(width,high);
}
  • 所有圖像都是以像素為單位,所以設(shè)定的很大

  • 木板為長(zhǎng)方形 目標(biāo)為正方形

  • initgraph(width,high);建立一個(gè)寬為width,高為high的窗口

6.第二個(gè)函數(shù)show() 打印畫面

void show()
{
 setbkcolor(RGB(255,255,255));
 cleardevice();//清屏

 setfillcolor(RGB(0,0,255));
 fillcircle(ball_x,ball_y,ball_r);

 setfillcolor(RGB(0,0,0));
 fillrectangle(board_x,board_y,board_x+board_long,board_y+board_bottom);

 setfillcolor(RGB(255,0,0));
 fillrectangle(gaol_x,gaol_y,gaol_x+gaol_long,gaol_y+gaol_long);


}
  • setbkcolor(RGB(255,255,255));設(shè)置當(dāng)前繪圖背景色(白)

  • cleardevice();清屏(使用當(dāng)前背景色覆蓋)

  • setfillcolor(RGB(0,0,255));設(shè)置當(dāng)前的填充顏色(藍(lán))

  • fillcircle(x,y,r);畫一個(gè)圓心為(x,y)半徑為r有顏色填充的圓

  • fillrectangle(x1,y1,x2,y2);畫一個(gè)左上座標(biāo)為(x1,y1)右下為(x2,y2)有顏色填充的矩形

7.第三個(gè)函數(shù)updateWithoutInput();與輸入無關(guān)的更新

void updateWithoutInpurt()
{
 ball_x+=velocity_x;
 ball_y+=velocity_y;

 if(ball_x==1||ball_x==width-2)//碰壁反彈 
  velocity_x=-velocity_x;
 if(ball_y==1||ball_y==high-2)
  velocity_y=-velocity_y;

 if(ball_y==board_y&&(ball_x>=board_x&&ball_x<board_x+board_long))//碰木板反彈 
  velocity_y=-velocity_y;
 if(ball_y==board_y+board_bottom&&(ball_x>=board_x&&ball_x<board_x+board_long))//碰木板反彈 
  velocity_y=-velocity_y;

 if((ball_x>gaol_x&&ball_x<gaol_x+gaol_long)&&(ball_y>gaol_y&&ball_y<gaol_y+gaol_long))//如果球擊中目標(biāo)
 {
  srand((unsigned)time(NULL));/*做隨機(jī)數(shù)產(chǎn)生種子*/
  gaol_y=rand()%(high/2-gaol_long)+1;
  gaol_x=rand()%(width/2-gaol_long)+1;
 }

}

功能:

* 碰壁反彈
* 碰木板反彈
* 如果球碰到目標(biāo),目標(biāo)重新刷新

8.第四個(gè)函數(shù) updateWithInput();與用戶輸入有關(guān)的更新

void updateWithInpurt()
{
  char input;
 if(kbhit())
 {
  input=getch();
  if(input=='w'&&board_y>1)
   board_y-=10;
  if(input=='s'&&board_y+board_bottom<high-2)
   board_y+=10;
  if(input=='a'&&board_x>1)
   board_x-=10;
  if(input=='d'&&board_x+board_long<width-2)
   board_x+=10;
 } 
}

因?yàn)槭且韵袼貫閱挝焕L畫,所以每次移動(dòng)10個(gè)單位

完整代碼

#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

//全局變量
int high,width;//游戲尺寸
int ball_x,ball_y,ball_r;//球
int board_x,board_y,board_long,board_bottom;//木板
int gaol_x,gaol_y,gaol_long;//目標(biāo)
int velocity_x,velocity_y;//速度

void startup()
{
 high=540;width=480;//游戲尺寸
 ball_y=30;ball_x=width/3;ball_r=15;//球的坐標(biāo)

 board_y=high/2;board_x=width/2;//木板
 board_long=150;board_bottom=10;

 gaol_y=2;
 gaol_x=width/2;
 gaol_long=20; //目標(biāo)第一個(gè)點(diǎn)的坐標(biāo) 

 velocity_x=1,velocity_y=1;//速度

 initgraph(width,high);



} 

void show()
{
 setbkcolor(RGB(255,255,255));
 cleardevice();//清屏

 setfillcolor(RGB(0,0,255));
 fillcircle(ball_x,ball_y,ball_r);

 setfillcolor(RGB(0,0,0));
 fillrectangle(board_x,board_y,board_x+board_long,board_y+board_bottom);

 setfillcolor(RGB(255,0,0));
 fillrectangle(gaol_x,gaol_y,gaol_x+gaol_long,gaol_y+gaol_long);

}

void updateWithoutInpurt()
{
 ball_x+=velocity_x;
 ball_y+=velocity_y;

 if(ball_x==1||ball_x==width-2)//碰壁反彈 
  velocity_x=-velocity_x;
 if(ball_y==1||ball_y==high-2)
  velocity_y=-velocity_y;

 if(ball_y>board_y&&(ball_x>=board_x&&ball_x<board_x+board_long))//碰木板反彈 
  velocity_y=-velocity_y;
 if(ball_y==board_y+board_bottom&&(ball_x>=board_x&&ball_x<board_x+board_long))//碰木板反彈 
  velocity_y=-velocity_y;

 if((ball_x>gaol_x&&ball_x<gaol_x+gaol_long)&&(ball_y>gaol_y&&ball_y<gaol_y+gaol_long))//如果球擊中目標(biāo)
 {
  srand((unsigned)time(NULL));/*做隨機(jī)數(shù)產(chǎn)生種子*/
  gaol_y=rand()%(high/2-gaol_long)+1;
  gaol_x=rand()%(width/2-gaol_long)+1;
 }

}

void updateWithInpurt()
{
  char input;
 if(kbhit())
 {
  input=getch();
  if(input=='w'&&board_y>1)
   board_y-=10;
  if(input=='s'&&board_y+board_bottom<high-2)
   board_y+=10;
  if(input=='a'&&board_x>1)
   board_x-=10;
  if(input=='d'&&board_x+board_long<width-2)
   board_x+=10;
 } 
}

int main(void)
{
 startup();
 while(1)
 {
  show();
  updateWithoutInpurt();
  updateWithInpurt();
 } 
}

關(guān)于使用C語言怎么實(shí)現(xiàn)一個(gè)反彈球游戲就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI