C語言實現反彈球遊戲
阿新 • • 發佈:2020-03-20
C語言小遊戲——反彈球(簡單的圖形化介面),供大家參考,具體內容如下
1.環境準備和安裝
- 安裝Visual C++ 6.0。
- 去Easy X官網下載Easy X安裝包。
2.Eaxy X功能的簡單介紹
- Easy X類似於一個庫函式,其中帶有許多很有用的函式。
- Easy x首先建立一個新的視窗進行繪圖。
- 可以畫常見點 線 多邊形 可以調節顏色。
- 可以插入圖片,音樂。
- 可以獲取滑鼠資訊。
- 其中函式的具體使用可以看安裝包中帶有的幫助檔案
3.反彈球遊戲主函式框架
int main (void) { starup();//資料初始化 while(1) { show();//畫面初始化 updateWithoutInput();//與使用者輸入無關的更新 updateWithInput();//與使用者輸入有關的更新 } }
- 與上篇貪吃蛇的框架類似
- starup();對全域性變數初始化
- show();畫具體的影象(球 目標 木板)
- updateWithoutInput(); 碰壁反彈 碰到木板反彈
- updateWithInput();控制長方形的移動
4.標頭檔案的載入和全域性變數的設定
#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;//目標 int velocity_x,velocity_y;//速度
5.第一個函式starup() 全域性變數的初始化
void startup() { high=540;width=480;//遊戲尺寸 ball_y=30;ball_x=width/3;ball_r=15;//球的座標 board_y=high/2;board_x=width/2;//木板 board_long=150;board_bottom=10; gaol_y=2; gaol_x=width/2; gaol_long=20; //目標第一個點的座標 velocity_x=1,velocity_y=1;//速度 initgraph(width,high); }
- 所有影象都是以畫素為單位,所以設定的很大
- 木板為長方形 目標為正方形
- initgraph(width,high);建立一個寬為width,高為high的視窗
6.第二個函式show() 列印畫面
void show() { setbkcolor(RGB(255,255,255)); cleardevice();//清屏 setfillcolor(RGB(0,255)); fillcircle(ball_x,ball_r); setfillcolor(RGB(0,0)); fillrectangle(board_x,board_x+board_long,board_y+board_bottom); setfillcolor(RGB(255,0)); fillrectangle(gaol_x,gaol_x+gaol_long,gaol_y+gaol_long); }
- setbkcolor(RGB(255,255));設定當前繪圖背景色(白)
- cleardevice();清屏(使用當前背景色覆蓋)
- setfillcolor(RGB(0,255));設定當前的填充顏色(藍)
- fillcircle(x,y,r);畫一個圓心為(x,y)半徑為r有顏色填充的圓
- fillrectangle(x1,y1,x2,y2);畫一個左上座標為(x1,y1)右下為(x2,y2)有顏色填充的矩形
7.第三個函式updateWithoutInput();與輸入無關的更新
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))//如果球擊中目標 { srand((unsigned)time(NULL));/*做隨機數產生種子*/ gaol_y=rand()%(high/2-gaol_long)+1; gaol_x=rand()%(width/2-gaol_long)+1; } }
功能:
* 碰壁反彈
* 碰木板反彈
* 如果球碰到目標,目標重新重新整理
8.第四個函式 updateWithInput();與使用者輸入有關的更新
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; } }
因為是以畫素為單位繪畫,所以每次移動10個單位
完整程式碼
#include <graphics.h> #include <conio.h> #include <stdlib.h> #include <time.h> //全域性變數 int high,velocity_y;//速度 void startup() { high=540;width=480;//遊戲尺寸 ball_y=30;ball_x=width/3;ball_r=15;//球的座標 board_y=high/2;board_x=width/2;//木板 board_long=150;board_bottom=10; gaol_y=2; gaol_x=width/2; gaol_long=20; //目標第一個點的座標 velocity_x=1,high); } void show() { setbkcolor(RGB(255,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))//如果球擊中目標 { srand((unsigned)time(NULL));/*做隨機數產生種子*/ 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(); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。