C語言實現(xiàn)反彈球游戲
C語言小游戲——反彈球(簡單的圖形化界面),供大家參考,具體內(nèi)容如下
1.環(huán)境準備和安裝
- 安裝Visual C++ 6.0。
- 去Easy X官網(wǎng)下載Easy X安裝包。
2.Eaxy X功能的簡單介紹
- Easy X類似于一個庫函數(shù),其中帶有許多很有用的函數(shù)。
- Easy x首先創(chuàng)建一個新的窗口進行繪圖。
- 可以畫常見點 線 多邊形 可以調(diào)節(jié)顏色。
- 可以插入圖片,音樂。
- 可以獲取鼠標信息。
- 其中函數(shù)的具體使用可以看安裝包中帶有的幫助文件
3.反彈球游戲主函數(shù)框架
int main (void) { starup();//數(shù)據(jù)初始化 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.第一個函數(shù)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.第二個函數(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));設置當前繪圖背景色(白)
- cleardevice();清屏(使用當前背景色覆蓋)
- setfillcolor(RGB(0,0,255));設置當前的填充顏色(藍)
- fillcircle(x,y,r);畫一個圓心為(x,y)半徑為r有顏色填充的圓
- fillrectangle(x1,y1,x2,y2);畫一個左上座標為(x1,y1)右下為(x2,y2)有顏色填充的矩形
7.第三個函數(shù)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));/*做隨機數(shù)產(chǎn)生種子*/ gaol_y=rand()%(high/2-gaol_long)+1; gaol_x=rand()%(width/2-gaol_long)+1; } }
功能:
* 碰壁反彈
* 碰木板反彈
* 如果球碰到目標,目標重新刷新
8.第四個函數(shù) 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,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;//速度 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); } 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))//如果球擊中目標 { srand((unsigned)time(NULL));/*做隨機數(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(); } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C語言的getc()函數(shù)和gets()函數(shù)的使用對比
這篇文章主要介紹了C語言的getc()函數(shù)和gets()函數(shù)的使用對比,從數(shù)據(jù)流中一個是讀取字符一個是讀取字符串,需要的朋友可以參考下2015-08-08