C語言實(shí)現(xiàn)反彈球游戲
C語言小游戲——反彈球(簡單的圖形化界面),供大家參考,具體內(nèi)容如下
1.環(huán)境準(zhǔn)備和安裝
- 安裝Visual C++ 6.0。
- 去Easy X官網(wǎng)下載Easy X安裝包。
2.Eaxy X功能的簡單介紹
- Easy X類似于一個(gè)庫函數(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();對全局變量初始化
- show();畫具體的圖像(球 目標(biāo) 木板)
- updateWithoutInput(); 碰壁反彈 碰到木板反彈
- updateWithInput();控制長方形的移動(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è)定的很大
- 木板為長方形 目標(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();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談內(nèi)聯(lián)函數(shù)與宏定義的區(qū)別詳解
本篇文章是對內(nèi)聯(lián)函數(shù)與宏定義的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C++實(shí)現(xiàn)LeetCode(30.串聯(lián)所有單詞的子串)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(30.串聯(lián)所有單詞的子串),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++實(shí)現(xiàn)重載矩陣的部分運(yùn)算符
這篇文章主要為大家詳細(xì)介紹了如何利用C++實(shí)現(xiàn)重載矩陣的部分運(yùn)算符,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C++有一定幫助,需要的可以參考一下2022-10-10

