C語言實(shí)現(xiàn)簡單反彈球消磚塊游戲
反彈球消磚塊,是一款很簡單的打磚塊游戲,控制你的擋板擋住彈球,打掉上面的磚塊,本篇博客中,主要使用printf與scanf函數(shù)實(shí)現(xiàn)消磚塊游戲
整體思路
主函數(shù)
int main()
{
?? ?startup();//初始化
?? ?while (1)
?? ?{
?? ??? ?show();//顯示畫面 ?
?? ??? ?updateWitoutIput();//與用戶輸入無關(guān)的更新 ?//更新數(shù)據(jù)
?? ??? ?updateWithInput(); //與用戶輸入有關(guān)的更新 ?//輸入
?? ?}
?? ?return 0;
}輔助函數(shù)
void gotoxy(int x, int y) //將光標(biāo)調(diào)整到(x,y)的位置 ?代替清屏函數(shù)
{
?? ?HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
?? ?COORD pos;
?? ?pos.X = x;
?? ?pos.Y = y;
?? ?SetConsoleCursorPosition(handle, pos);
}void HideCursor()?? ?//隱藏光標(biāo)
{
?? ?CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
?? ?SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}全局變量的定義
#define HIGH 20 ?//游戲界面高度 #define WIDTH 30 ?// 游戲界面寬度 #define V 1 ?//小球的速度 #define RIDUS 5 ?//擋板的半徑 int ball_x, ball_y; ?//小球的坐標(biāo) int ball_vx, ball_vy; ?//小球的速度 int position_x, position_y; ?//擋板的中心位置 int left, right; ?//擋板的左右位置 int ball_number; ?//反彈小球的次數(shù) int block_x, block_y; ?//磚塊的坐標(biāo) int score; ?//消除磚塊的個(gè)數(shù)
數(shù)據(jù)的初始化
//初始化小球 ball_x = HIGH / 2; //高度 ball_y = WIDTH / 2; //寬度 ball_vx = V; ?//小球的縱向速度 ball_vy = V; ?//小球的橫向速度 //初始化擋板 position_x = HIGH / 2; position_y = WIDTH / 2; ?//擋板的中心位置 left = position_y - RIDUS;//擋板的左位置 right = position_y + RIDUS; ?//擋板的右位置 //初始化磚塊 block_x = 0; //高度 block_y = WIDTH / 2; ?//寬度 score = 0;
顯示畫面
循環(huán)不斷輸出游戲的界面, 0 表示小球, B 表示磚塊, * 表示擋板
for (i = 0; i < HIGH + 1; i++) //行
{
?? ?for (j = 0; j < WIDTH; j++) //列
?? ?{
?? ??? ?if (i == ball_x && j == ball_y)//輸出小球
?? ??? ?{
?? ??? ??? ?printf("0");
?? ??? ?}
?? ??? ?else if (i == block_x && j == block_y)//輸出磚塊
?? ??? ?{
?? ??? ??? ?printf("B");
?? ??? ?}
?? ??? ?else if (i == HIGH) ?//下邊界 -1,否則循環(huán)走不到high和width
?? ??? ?{
?? ??? ??? ?printf("-");
?? ??? ?}
?? ??? ?else if (j == WIDTH - 1) //右邊界 ?
?? ??? ?{
?? ??? ??? ?printf("|");
?? ??? ?}
?? ??? ?else if (i == HIGH - 1 && j >= left && j <= right) ?//輸出擋板
?? ??? ?{
?? ??? ??? ?printf("*");
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?printf(" ");
?? ??? ?}
?? ?}
}
printf("反彈次數(shù):%d\n", ball_number);
printf("消除磚塊的個(gè)數(shù):%d\n", score);與用戶無關(guān)的更新
判斷游戲勝負(fù)
if (ball_x == HIGH - 2) //到達(dá)擋板上方
{
?? ?if (ball_y >= left && ball_y <= right) //被擋板擋住
?? ?{
?? ??? ?ball_number++;
?? ?}
?? ?else ?//沒被擋板擋住
?? ?{
?? ??? ?printf("游戲失?。n");
?? ??? ?exit(0); ?//直接結(jié)束
?? ?}
}消除磚塊得分
if (ball_x == block_x && ball_y == block_y)
{
?? ?score++;
?? ?block_y = rand() % WIDTH;
}
改變小球的位置
ball_x = ball_x + ball_vx; ball_y = ball_y + ball_vy; //為了使小球在反彈和結(jié)束時(shí)能夠真缺判斷,所以小球下反彈邊界應(yīng)該在擋板的前一行,小球右反彈邊界應(yīng)該在右邊界的前一列 if (ball_x >= HIGH - 2 || ball_x <= 0) ?//遇到任意一個(gè)邊界即發(fā)生變化 ? ?? ?ball_vx = -ball_vx; if (ball_y >= WIDTH -2 || ball_y <= 0) ?//-1保證下球再使小球遇到邊界時(shí)反彈但不會(huì)消失 ?? ?ball_vy = -ball_vy;
與用戶輸入有關(guān)的更新
input = _getch(); if (input == 'a') ?? ?position_y--; if (input == 'd') ?? ?position_y++; left = position_y - RIDUS;//擋板的左位置 right = position_y + RIDUS; ?//擋板的右位置
完整代碼
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
//全局變量的定義
#define HIGH 20 ?//游戲界面高度
#define WIDTH 30 ?// 游戲界面寬度
#define V 1 ?//小球的速度
#define RIDUS 5 ?//擋板的半徑
int ball_x, ball_y; ?//小球的坐標(biāo)
int ball_vx, ball_vy; ?//小球的速度
int position_x, position_y; ?//擋板的中心位置
int left, right; ?//擋板的左右位置
int ball_number; ?//反彈小球的次數(shù)
int block_x, block_y; ?//磚塊的坐標(biāo)
int score; ?//消除磚塊的個(gè)數(shù)
void gotoxy(int x, int y) //將光標(biāo)調(diào)整到(x,y)的位置 ?代替清屏函數(shù)
{
?? ?HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
?? ?COORD pos;
?? ?pos.X = x;
?? ?pos.Y = y;
?? ?SetConsoleCursorPosition(handle, pos);
}
void HideCursor()//隱藏光標(biāo)函數(shù)
{
?? ?CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
?? ?SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup()//數(shù)據(jù)的初始化
{
?? ?//初始化小球
?? ?ball_x = HIGH / 2; //高度
?? ?ball_y = WIDTH / 2; //寬度
?? ?ball_vx = V; ?//小球的縱向速度
?? ?ball_vy = V; ?//小球的橫向速度
?? ?//初始化擋板
?? ?position_x = HIGH / 2;
?? ?position_y = WIDTH / 2; ?//擋板的中心位置
?? ?left = position_y - RIDUS;//擋板的左位置
?? ?right = position_y + RIDUS; ?//擋板的右位置
?? ?//初始化磚塊
?? ?block_x = 0; //高度
?? ?block_y = WIDTH / 2; ?//寬度
?? ?score = 0;
}
void show()//顯示畫面
{
?? ?//system("cls");?
?? ?gotoxy(0, 0);
?? ?int i, j;
?? ?for (i = 0; i < HIGH + 1; i++)
?? ?{
?? ??? ?for (j = 0; j < WIDTH; j++)
?? ??? ?{
?? ??? ??? ?if (i == ball_x && j == ball_y)
?? ??? ??? ?{
?? ??? ??? ??? ?printf("0");
?? ??? ??? ?}
?? ??? ??? ?else if (i == block_x && j == block_y)
?? ??? ??? ?{
?? ??? ??? ??? ?printf("B");
?? ??? ??? ?}
?? ??? ??? ?else if (i == HIGH)
?? ??? ??? ?{
?? ??? ??? ??? ?printf("-");
?? ??? ??? ?}
?? ??? ??? ?else if (j == WIDTH - 1)
?? ??? ??? ?{
?? ??? ??? ??? ?printf("|");
?? ??? ??? ?}
?? ??? ??? ?else if (i == HIGH - 1 && j >= left && j <= right)
?? ??? ??? ?{
?? ??? ??? ??? ?printf("*");
?? ??? ??? ?}
?? ??? ??? ?else
?? ??? ??? ?{
?? ??? ??? ??? ?printf(" ");
?? ??? ??? ?}
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
?? ?printf("反彈次數(shù):%d\n", ball_number);
?? ?printf("消除磚塊的個(gè)數(shù):%d\n", score);
}
void updateWitoutIput()
{
?? ?if (ball_x == HIGH - 2)
?? ?{
?? ??? ?if (ball_y >= left && ball_y <= right)
?? ??? ?{
?? ??? ??? ?ball_number++;
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?printf("游戲失敗!\n");
?? ??? ??? ?exit(0);
?? ??? ?}
?? ?}
?? ?if (ball_x == block_x && ball_y == block_y)
?? ?{
?? ??? ?score++;
?? ??? ?block_y = rand() % WIDTH;
?? ?}
?? ?ball_x = ball_x + ball_vx;
?? ?ball_y = ball_y + ball_vy;
?? ?if (ball_x >= HIGH - 2 || ball_x <= 0)
?? ??? ?ball_vx = -ball_vx;
?? ?if (ball_y >= WIDTH -2 || ball_y <= 0)
?? ??? ?ball_vy = -ball_vy;
?? ?Sleep(150);?? ?//睡眠時(shí)間
}
void updateWithInput()//與用戶輸入有關(guān)的更新
{
?? ?char input;
?? ?if (_kbhit())
?? ?{
?? ??? ?input = _getch();
?? ??? ?if (input == 'a')
?? ??? ??? ?position_y--;
?? ??? ?if (input == 'd')
?? ??? ??? ?position_y++;
?? ??? ?left = position_y - RIDUS;
?? ??? ?right = position_y + RIDUS;
?? ?}
}
int main()
{
?? ?srand((unsigned)time(NULL));
?? ?startup();//初始化
?? ?HideCursor();
?? ?while (1)
?? ?{
?? ??? ?show();//顯示畫面 ?
?? ??? ?updateWitoutIput();//與用戶輸入無關(guān)的更新
?? ??? ?updateWithInput(); //與用戶輸入有關(guān)的更新
?? ?}
?? ?return 0;
}效果演示

總結(jié)
雖然完成了一個(gè)簡單的消磚塊游戲,但是很多的功能都未能實(shí)現(xiàn),如磚塊的個(gè)數(shù)、游戲暫停和結(jié)束后的開始等等,任需要繼續(xù)的努力。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++實(shí)現(xiàn)班車管理系統(tǒng)課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)班車管理系統(tǒng)課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
C++中typedef 及其與struct的結(jié)合使用
這篇文章主要介紹了C++中typedef 及其與struct的結(jié)合使用,需要的朋友可以參考下2014-02-02
C語言 結(jié)構(gòu)體數(shù)組詳解及示例代碼
本文主要介紹C語言 結(jié)構(gòu)體數(shù)組,這里整理了相關(guān)資料及簡單示例代碼,以便大家學(xué)習(xí)參考,有興趣的小伙伴可以看下2016-08-08

