C語言實現(xiàn)打磚塊小游戲
本文實例為大家分享了C語言實現(xiàn)打磚塊游戲的具體代碼,供大家參考,具體內(nèi)容如下
本節(jié)我們將沿用 上一節(jié) 所提到的函數(shù)式游戲框架來寫一個彈球打磚塊的游戲。
基本量、邊框繪制
我們首先定義好可能需要的變量,比如小球坐標以及速度、游戲界面高度和寬度等等,然后再根據(jù)之前提到的彈跳小球重構代碼,剩下的內(nèi)容再分步添置即可。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <cwindow.h>
// 全局變量
int width,high;?? ??? ?//游戲界面尺寸
int ball_x,ball_y;?? ??? ?//小球坐標
int ball_vx,ball_vy;?? ??? ?//小球速度
void gotoxy(int x, int y)?? ?//移動光標便于清屏重畫
{
?? ?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);?? ?//光標移動到原點便于重畫
?? ?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)?? ?//到達右邊界
?? ??? ??? ??? ?printf("|"); //輸出邊界
?? ??? ??? ?else if(i == high) ?//到達下邊界
?? ??? ??? ??? ?printf("-"); //輸出邊界
?? ??? ??? ?else
?? ??? ??? ??? ?printf(" "); //非小球坐標輸出空格
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
}
void updateWithoutInput()?? ?//與輸入無關更新
{
?? ?ball_x = ball_x + ball_vx;
?? ?ball_y = ball_y + ball_vy;
?? ?// 判斷是否到達邊界
?? ?if((ball_x == 0)||(ball_x == high - 1))
?? ??? ?ball_vx = - ball_vx;
?? ?if((ball_y == 0)||(ball_y == width - 1))
?? ??? ?ball_vy = - ball_vy;
?? ?//延時刷新
?? ?sleep(50);?
}
void updateWithInput()?? ?//與輸入有關更新
{} ?? ?//當前沒有
int main()
{
?? ?startup();?? ?//最開始初始化一次
?? ?while(1)?? ?//游戲循環(huán)體
?? ?{
?? ??? ?show(); //先展示畫面
?? ??? ?//然后是數(shù)據(jù)更新
?? ??? ?updateWithoutInput();
?? ??? ?updateWithInput();
?? ?}
?? ?return 0;
}在判斷小球到達邊界的部分,我們在第二條件中執(zhí)行了減一操作,目的是為了避免出現(xiàn)小球和邊界重合的情況。
移動擋板
接下來我們要在游戲中顯示一個中心坐標為 ( position_x ,position_y ) 且半徑為 ridus 的擋板。用 left 和 right 表示其左邊和右邊位置。
首先定義相關變量:
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)畫好了擋板,那么要能讓它移動才行。
很明顯,按照一般游戲思路來,我們不能在移動中改變 x 坐標,也就是說擋板只能左右移動,左移 y 坐標減一,右移則加一。
所以到此我們就要寫 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;
?? ??? ?}
?? ?}
}反彈小球
反彈小球部分思路就比較清晰一些,簡單來說就是判斷當小球 x 坐標到達最底部時,其 y 坐標是不是在擋板所規(guī)定的范圍內(nèi),如果是,則改變小球速度;如果不是,游戲結(jié)束。
我們還可以定義一個 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é)束,因為你沒接住控制命運的小球\n");
?? ??? ??? ?system("pause");
?? ??? ??? ?exit(0);
?? ??? ?}
?? ?}
?? ?// 以后內(nèi)容同前
}消除磚塊
我們使用字母O來表示小球了,那么用什么來表示磚塊呢?
這個時候就是看你的英語有沒有好好學,我們知道 Brick 是指磚塊,所以我們使用字母B來表示磚塊,畢竟B本身就像個磚塊(并沒有)。
需要定義的量:
- 磚塊位置
- 得分(擊中磚塊加一)
int brick_x, brick_y; int score; //初始化 brick_x = 0; brick_y = width/2 + 1; score = 0;
至于輸出和判定擊中,我就不再多贅述了,基本思路還是和之前一樣,輸出部分加一句 else if 就可以了,而擊中判定在 updateWithoutInput 里。
小結(jié)
上代碼!?。。。。?/p>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <cwindow.h>
// 全局變量
int width,high;?? ??? ?//游戲界面尺寸
int ball_x,ball_y;?? ??? ?//小球坐標
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)?? ?//移動光標便于清屏重畫
{
?? ?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);?? ?//光標移動到原點便于重畫
?? ?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)?? ?//到達右邊界
?? ??? ??? ??? ?printf("|"); //輸出邊界
?? ??? ??? ?else if ((i == high)&&(j >= left)&&(j <= right))
?? ??? ??? ??? ?printf("*"); //輸出擋板
?? ??? ??? ?else if(i == high) ?//到達下邊界
?? ??? ??? ??? ?printf("-"); //輸出邊界
?? ??? ??? ?else
?? ??? ??? ??? ?printf(" "); //非小球坐標輸出空格
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
?? ?printf("反彈小球:%d\n",ball_number);
?? ?printf("得分:%d\n",score);
}
void updateWithoutInput()?? ?//與輸入無關更新
{
?? ?if(ball_x == high - 1)
?? ?{
?? ??? ?if((ball_y >= left)&&(ball_y <= right))?? ?//被擋住
?? ??? ?{
?? ??? ??? ?ball_number ++;
?? ??? ??? ?printf("\a");?? ?//響鈴
?? ??? ?}else{?? ?// 沒擋住
?? ??? ??? ?printf("游戲結(jié)束,因為你沒接住控制命運的小球\n");
?? ??? ??? ?system("pause");
?? ??? ??? ?exit(0);
?? ??? ?}
?? ?}
?? ?ball_x = ball_x + ball_vx;
?? ?ball_y = ball_y + ball_vy;
?? ?// 判斷是否到達邊界
?? ?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; //磚塊刷新
?? ?}?? ?
?? ?//延時刷新
?? ?sleep(50);?
}
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;
?? ??? ?}
?? ?}
}?
int main()
{
?? ?startup();?? ?//最開始初始化一次
?? ?while(1)?? ?//游戲循環(huán)體
?? ?{
?? ??? ?show(); //先展示畫面
?? ??? ?//然后是數(shù)據(jù)更新
?? ??? ?updateWithoutInput();
?? ??? ?updateWithInput();
?? ?}
?? ?return 0;
}這里就是全部代碼了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C++11新特性之右值引用與完美轉(zhuǎn)發(fā)詳解
C++11標準為C++引入右值引用語法的同時,還解決了一個短板,即使用簡單的方式即可在函數(shù)模板中實現(xiàn)參數(shù)的完美轉(zhuǎn)發(fā)。本文就來講講二者的應用,需要的可以參考一下2022-09-09
C++中?‘=default?’及‘?=delete?’的使用
這篇文章主要介紹了C++中?=default?及?=delete?使用,使用=default和=delete可以控制編譯器默認函數(shù)體的使用,下面我們就來看看具體的室友方法吧,需要的朋友也可以參考一下2021-12-12

