C語言實(shí)現(xiàn)打磚塊小游戲
本文實(shí)例為大家分享了C語言實(shí)現(xiàn)打磚塊游戲的具體代碼,供大家參考,具體內(nèi)容如下
本節(jié)我們將沿用 上一節(jié) 所提到的函數(shù)式游戲框架來寫一個(gè)彈球打磚塊的游戲。
基本量、邊框繪制
我們首先定義好可能需要的變量,比如小球坐標(biāo)以及速度、游戲界面高度和寬度等等,然后再根據(jù)之前提到的彈跳小球重構(gòu)代碼,剩下的內(nèi)容再分步添置即可。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <cwindow.h>
// 全局變量
int width,high;?? ??? ?//游戲界面尺寸
int ball_x,ball_y;?? ??? ?//小球坐標(biāo)
int ball_vx,ball_vy;?? ??? ?//小球速度
void gotoxy(int x, int y)?? ?//移動(dòng)光標(biāo)便于清屏重畫
{
?? ?HANDLE handle = GetStdHandle(STD_UOTPUT_HANDLE);
?? ?CROOD pos;
?? ?pos.X = x;
?? ?pos.Y = y;
?? ?SetConsoleCursorPosition(handle, pos);
}
void startup()?? ??? ?//數(shù)據(jù)初始化
{
?? ?high = 15;
?? ?width = 20;
?? ?ball_x = 0;
?? ?ball_y = width/2;
?? ?ball_vx = 1;
?? ?ball_vy = 1;
}
void show()?? ??? ?//顯示界面
{
?? ?gotoxy(0,0);?? ?//光標(biāo)移動(dòng)到原點(diǎn)便于重畫
?? ?int i,j;
?? ?for(i=0; i<high; i++)
?? ?{
?? ??? ?for(j=0; j<width; j++)
?? ??? ?{
?? ??? ??? ?if((i==ball_x)&&(j==ball_y))
?? ??? ??? ??? ?printf("O"); //輸出小球
?? ??? ??? ?else if(j == width)?? ?//到達(dá)右邊界
?? ??? ??? ??? ?printf("|"); //輸出邊界
?? ??? ??? ?else if(i == high) ?//到達(dá)下邊界
?? ??? ??? ??? ?printf("-"); //輸出邊界
?? ??? ??? ?else
?? ??? ??? ??? ?printf(" "); //非小球坐標(biāo)輸出空格
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
}
void updateWithoutInput()?? ?//與輸入無關(guān)更新
{
?? ?ball_x = ball_x + ball_vx;
?? ?ball_y = ball_y + ball_vy;
?? ?// 判斷是否到達(dá)邊界
?? ?if((ball_x == 0)||(ball_x == high - 1))
?? ??? ?ball_vx = - ball_vx;
?? ?if((ball_y == 0)||(ball_y == width - 1))
?? ??? ?ball_vy = - ball_vy;
?? ?//延時(shí)刷新
?? ?sleep(50);?
}
void updateWithInput()?? ?//與輸入有關(guān)更新
{} ?? ?//當(dāng)前沒有
int main()
{
?? ?startup();?? ?//最開始初始化一次
?? ?while(1)?? ?//游戲循環(huán)體
?? ?{
?? ??? ?show(); //先展示畫面
?? ??? ?//然后是數(shù)據(jù)更新
?? ??? ?updateWithoutInput();
?? ??? ?updateWithInput();
?? ?}
?? ?return 0;
}在判斷小球到達(dá)邊界的部分,我們在第二條件中執(zhí)行了減一操作,目的是為了避免出現(xiàn)小球和邊界重合的情況。
移動(dòng)擋板
接下來我們要在游戲中顯示一個(gè)中心坐標(biāo)為 ( position_x ,position_y ) 且半徑為 ridus 的擋板。用 left 和 right 表示其左邊和右邊位置。
首先定義相關(guān)變量:
int position_x,position_y; int ridus; int left,right;
然后數(shù)據(jù)初始化:
position_x = high; //擋板在最底下 position_y = width/2; ridus = 5; left = position_y - ridus; right = position_y + ridus;
最后在輸出中添加輸出擋板的部分:
if ...
else if ((i == high)&&(j >= left)&&(j <= right))
?? ?printf("*");
else ...既然已經(jīng)畫好了擋板,那么要能讓它移動(dòng)才行。
很明顯,按照一般游戲思路來,我們不能在移動(dòng)中改變 x 坐標(biāo),也就是說擋板只能左右移動(dòng),左移 y 坐標(biāo)減一,右移則加一。
所以到此我們就要寫 updateWithInput 函數(shù)部分了。
void updateWithInput()
{
?? ?char input;
?? ?if(kbhit())
?? ?{
?? ??? ?input = getch();
?? ??? ?if(input == 'a')
?? ??? ?{
?? ??? ??? ?position_y --;
?? ??? ??? ?left = position_y - ridus;
?? ??? ??? ?right = position_y + ridus;
?? ??? ?}
?? ??? ?if(input == 'd')
?? ??? ?{
?? ??? ??? ?position_y ++;
?? ??? ??? ?left = position_y - ridus;
?? ??? ??? ?right = position_y + ridus;
?? ??? ?}
?? ?}
}反彈小球
反彈小球部分思路就比較清晰一些,簡單來說就是判斷當(dāng)小球 x 坐標(biāo)到達(dá)最底部時(shí),其 y 坐標(biāo)是不是在擋板所規(guī)定的范圍內(nèi),如果是,則改變小球速度;如果不是,游戲結(jié)束。
我們還可以定義一個(gè) ball_number 來記錄小球反彈的次數(shù)。
int ball_number; //初始化 ball_number = 0;
隨后來寫我們的 updateWithoutInput 部分:
void updateWithoutInput()
{
?? ?if(ball_x == high - 1)
?? ?{
?? ??? ?if((ball_y >= left)&&(ball_y <= right))?? ?//被擋住
?? ??? ?{
?? ??? ??? ?ball_number ++;
?? ??? ??? ?printf("\a");?? ?//響鈴
?? ??? ?}else{
?? ??? ??? ?printf("游戲結(jié)束,因?yàn)槟銢]接住控制命運(yùn)的小球\n");
?? ??? ??? ?system("pause");
?? ??? ??? ?exit(0);
?? ??? ?}
?? ?}
?? ?// 以后內(nèi)容同前
}消除磚塊
我們使用字母O來表示小球了,那么用什么來表示磚塊呢?
這個(gè)時(shí)候就是看你的英語有沒有好好學(xué),我們知道 Brick 是指磚塊,所以我們使用字母B來表示磚塊,畢竟B本身就像個(gè)磚塊(并沒有)。
需要定義的量:
- 磚塊位置
- 得分(擊中磚塊加一)
int brick_x, brick_y; int score; //初始化 brick_x = 0; brick_y = width/2 + 1; score = 0;
至于輸出和判定擊中,我就不再多贅述了,基本思路還是和之前一樣,輸出部分加一句 else if 就可以了,而擊中判定在 updateWithoutInput 里。
小結(jié)
上代碼!?。。。?!
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <cwindow.h>
// 全局變量
int width,high;?? ??? ?//游戲界面尺寸
int ball_x,ball_y;?? ??? ?//小球坐標(biāo)
int ball_vx,ball_vy;?? ??? ?//小球速度
int position_x,position_y;
int ridus;
int left,right;
int ball_number;
int brick_x, brick_y;
int score;
void gotoxy(int x, int y)?? ?//移動(dòng)光標(biāo)便于清屏重畫
{
?? ?HANDLE handle = GetStdHandle(STD_UOTPUT_HANDLE);
?? ?CROOD pos;
?? ?pos.X = x;
?? ?pos.Y = y;
?? ?SetConsoleCursorPosition(handle, pos);
}
void startup()?? ??? ?//數(shù)據(jù)初始化
{
?? ?high = 15;
?? ?width = 20;
?? ?ball_x = 0;
?? ?ball_y = width/2;
?? ?ball_vx = 1;
?? ?ball_vy = 1;
?? ?position_x = high; //擋板在最底下
?? ?position_y = width/2;
?? ?ridus = 5;
?? ?left = position_y - ridus;
?? ?right = position_y + ridus;
?? ?ball_number = 0;
?? ?brick_x = 0;
?? ?brick_y = width/2 + 1;
?? ?score = 0;
}
void show()?? ??? ?//顯示界面
{
?? ?gotoxy(0,0);?? ?//光標(biāo)移動(dòng)到原點(diǎn)便于重畫
?? ?int i,j;
?? ?for(i=0; i<high; i++)
?? ?{
?? ??? ?for(j=0; j<width; j++)
?? ??? ?{
?? ??? ??? ?if((i == ball_x)&&(j == ball_y))
?? ??? ??? ??? ?printf("O"); //輸出小球
?? ??? ??? ?else if((i == brick_x)&&(j == brick_y))
?? ??? ??? ??? ?printf("B"); //輸出磚塊?? ?
?? ??? ??? ?else if(j == width)?? ?//到達(dá)右邊界
?? ??? ??? ??? ?printf("|"); //輸出邊界
?? ??? ??? ?else if ((i == high)&&(j >= left)&&(j <= right))
?? ??? ??? ??? ?printf("*"); //輸出擋板
?? ??? ??? ?else if(i == high) ?//到達(dá)下邊界
?? ??? ??? ??? ?printf("-"); //輸出邊界
?? ??? ??? ?else
?? ??? ??? ??? ?printf(" "); //非小球坐標(biāo)輸出空格
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
?? ?printf("反彈小球:%d\n",ball_number);
?? ?printf("得分:%d\n",score);
}
void updateWithoutInput()?? ?//與輸入無關(guān)更新
{
?? ?if(ball_x == high - 1)
?? ?{
?? ??? ?if((ball_y >= left)&&(ball_y <= right))?? ?//被擋住
?? ??? ?{
?? ??? ??? ?ball_number ++;
?? ??? ??? ?printf("\a");?? ?//響鈴
?? ??? ?}else{?? ?// 沒擋住
?? ??? ??? ?printf("游戲結(jié)束,因?yàn)槟銢]接住控制命運(yùn)的小球\n");
?? ??? ??? ?system("pause");
?? ??? ??? ?exit(0);
?? ??? ?}
?? ?}
?? ?ball_x = ball_x + ball_vx;
?? ?ball_y = ball_y + ball_vy;
?? ?// 判斷是否到達(dá)邊界
?? ?if((ball_x == 0)||(ball_x == high - 1))
?? ??? ?ball_vx = - ball_vx;
?? ?if((ball_y == 0)||(ball_y == width - 1))
?? ??? ?ball_vy = - ball_vy;
?? ?// 判定擊中
?? ?if((ball_x == brick_x)&&(ball_y == brick_y))
?? ?{
?? ??? ?score ++;
?? ??? ?brick_y = rand() % width; //磚塊刷新
?? ?}?? ?
?? ?//延時(shí)刷新
?? ?sleep(50);?
}
void updateWithInput()?? ?//與輸入有關(guān)更新
{
?? ?char input;
?? ?if(kbhit())
?? ?{
?? ??? ?input = getch();
?? ??? ?if(input == 'a') //擋板左移
?? ??? ?{
?? ??? ??? ?position_y --;
?? ??? ??? ?left = position_y - ridus;
?? ??? ??? ?right = position_y + ridus;
?? ??? ?}
?? ??? ?if(input == 'd') //擋板右移
?? ??? ?{
?? ??? ??? ?position_y ++;
?? ??? ??? ?left = position_y - ridus;
?? ??? ??? ?right = position_y + ridus;
?? ??? ?}
?? ?}
}?
int main()
{
?? ?startup();?? ?//最開始初始化一次
?? ?while(1)?? ?//游戲循環(huán)體
?? ?{
?? ??? ?show(); //先展示畫面
?? ??? ?//然后是數(shù)據(jù)更新
?? ??? ?updateWithoutInput();
?? ??? ?updateWithInput();
?? ?}
?? ?return 0;
}這里就是全部代碼了。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于C++中push_back()函數(shù)的用法及代碼實(shí)例
push_back是vector的一個(gè)方法,表示將一個(gè)元素存儲(chǔ)到容器的末尾,下面這篇文章主要給大家介紹了關(guān)于C++中push_back()函數(shù)用法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
一文搞懂C++中的四種強(qiáng)制類型轉(zhuǎn)換
很多朋友向小編了解C語言中怎么進(jìn)行強(qiáng)制類型轉(zhuǎn)換呢?在這小編告訴大家強(qiáng)制類型轉(zhuǎn)換可以分為兩種,一種是隱式類型轉(zhuǎn)換一種是顯示類型轉(zhuǎn)換,下面通過示例代碼給大家介紹下,需要的朋友參考下吧2021-07-07

