【三、打磚塊】
本節我們將沿用 上一節 所提到的函式式游戲框架來寫一個彈球打磚塊的游戲,
文章目錄
- 【三、打磚塊】
- 基本量、邊框繪制
- 移動擋板
- 反彈小球
- 消除磚塊
- 小結
基本量、邊框繪制
我們首先定義好可能需要的變數,比如小球坐標以及速度、游戲界面高度和寬度等等,然后再根據之前提到的彈跳小球重構代碼,剩下的內容再分步添置即可,
#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() //資料初始化
{
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) //游戲回圈體
{
show(); //先展示畫面
//然后是資料更新
updateWithoutInput();
updateWithInput();
}
return 0;
}
在判斷小球到達邊界的部分,我們在第二條件中執行了減一操作,目的是為了避免出現小球和邊界重合的情況,
移動擋板
接下來我們要在游戲中顯示一個中心坐標為 ( position_x ,position_y ) 且半徑為 ridus 的擋板,用 left 和 right 表示其左邊和右邊位置,
首先定義相關變數:
int position_x,position_y;
int ridus;
int left,right;
然后資料初始化:
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 ...
既然已經畫好了擋板,那么要能讓它移動才行,
很明顯,按照一般游戲思路來,我們不能在移動中改變 x 坐標,也就是說擋板只能左右移動,左移 y 坐標減一,右移則加一,
所以到此我們就要寫 updateWithInput 函式部分了,
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 坐標是不是在擋板所規定的范圍內,如果是,則改變小球速度;如果不是,游戲結束,
我們還可以定義一個 ball_number 來記錄小球反彈的次數,
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("游戲結束,因為你沒接住控制命運的小球\n");
system("pause");
exit(0);
}
}
// 以后內容同前
}
消除磚塊
我們使用字母O來表示小球了,那么用什么來表示磚塊呢?
這個時候就是看你的英語有沒有好好學,我們知道 Brick 是指磚塊,所以我們使用字母B來表示磚塊,畢竟B本身就像個磚塊(并沒有),
需要定義的量:
- 磚塊位置
- 得分(擊中磚塊加一)
int brick_x, brick_y;
int score;
//初始化
brick_x = 0;
brick_y = width/2 + 1;
score = 0;
至于輸出和判定擊中,我就不再多贅述了,基本思路還是和之前一樣,輸出部分加一句 else if 就可以了,而擊中判定在 updateWithoutInput 里,
小結
啊啊啊啊啊啊終于寫完了!!!
上代碼!!!!!!
#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() //資料初始化
{
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("游戲結束,因為你沒接住控制命運的小球\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) //游戲回圈體
{
show(); //先展示畫面
//然后是資料更新
updateWithoutInput();
updateWithInput();
}
return 0;
}
這里就是全部代碼了,由于時間原因,這篇文章是3天抽出零碎時間更的,所以更新比較慢,到此,
感謝閱讀,喜歡就點個贊吧~

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/264236.html
標籤:其他
上一篇:高效逐幀的視頻語意分割方法之Efficient Semantic Video Segmentation with Per-frame Inference
