C語言小游戲之小熊跳板功能的實現(xiàn)
更新時間:2020年12月22日 10:02:28 作者:MengYiKeNan
這篇文章主要介紹了C語言小游戲之小熊跳板功能的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
上篇文章給大家介紹了C語言實現(xiàn)桌面貪吃蛇小游戲,感興趣的朋友可以點擊查看,今天給大家介紹c語言實現(xiàn)小熊跳板功能。
C語言代碼,有點簡陋,還有許多可以優(yōu)化的地方,注釋也沒寫全,后期會補充,后期也可能添加更多功能,我會去盡量的完善代碼。
測試工具:VS2019,語言:C語言
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<conio.h>
#include<time.h>
#include<string.h>
#define High 40
#define Width 60 //畫布大小
int people_x, people_y; //小球坐標
int canvas[High + 1][Width + 1]; //標記
int b1_left[3]; //橫線板板起點
int b1_right[3]; //橫線板板終點
int score = 0; //得分
int vv = 15, v_b = 8; //vv大小可以改變板板上升速度
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void startup()
{
int i, j, k;
srand(time(NULL));
for (i = 3; i <= High; i += 3)
{
int t = 0;
int k;
for (k = 0; k < 3; k++)
{
b1_left[k] = rand() % (Width - 6);
b1_right[k] = b1_left[k] + rand()%3+5;
}
t = 3;
while (t--) {
for (j = b1_left[t]; j < b1_right[t]; j++)
{
canvas[i][j] = 2; //標記為橫線板板
}
}
}
people_x = 2;
people_y = 6; // 小球橫縱坐標
canvas[people_x][people_y] = 1; // 小球所在不能為橫線板板
for (i = 0; i < High; i++) // 左右邊框為束線
{
canvas[i][0] = -2;
canvas[i][Width - 1] = -2;
}
for (j = 0; j < Width; j++) //上下邊框為T
{
canvas[0][j] = -1;
canvas[High - 1][j] = -1;
}
}
void show()
{
gotoxy(0, 0);//把光標啟動到(0,0)點,在這里功能為清屏
int i, j;
for (i = 0; i < High; i++)
{
for (j = 0; j < Width; j++)
{
if (canvas[i][j] == 2)
printf("-");
else if (canvas[i][j] == 1)
printf("o");
else if (canvas[i][j] == -1)
printf("T");
else if (canvas[i][j] == -2)
printf("|");
else printf(" ");
}
printf("\n");
}
printf("得分:%d\n", score);
}
int updataWithoutInput()
{
int i, j, k;
static int v_bord = 0, v_ball = 0;
int flag = 0;
int oldps_x, oldps_y;
if (v_bord < vv) //設置更新各個部件的位置
v_bord++;
if (v_bord == vv) {
for (i = 1; i <= High - 2; i++)
{
for (j = 1; j <= Width - 2; j++)
{
if (canvas[i][j] == 2 && i - 1 != 0)
{
canvas[i][j] = 0;
if (canvas[i - 1][j] == 1)
{
people_x = i - 2; people_y = j;
canvas[people_x][people_y] = 1;
canvas[i - 1][j] = 2;
}
else
canvas[i - 1][j] = 2;
}
else
{
if (i - 1 == 0 && canvas[i][j] == 2) {
canvas[i][j] = 0;
flag = 1;
}
}
}
}
v_bord = 0;
}
for (i = 1; i <= Width - 2; i++)
{
if (canvas[people_x][i] == 2)
{
score++;
break;
}
}
if (v_ball < v_b)
v_ball++;
if (v_ball == v_b && canvas[people_x + 1][people_y] == 0)
{
canvas[people_x][people_y] = 0;
people_x++;
canvas[people_x][people_y] = 1;
v_ball = 0;
}
int cnt = 0;//判斷游戲是否結束
if (people_x == 0 || people_x == High - 2)
{
printf("GAMEOVER!\n");
system("pause");
cnt = 1;
goto restart;
}
if (flag)
{
int t = 0;
int k;
for (k = 0; k < 3; k++)
{
b1_left[k] = rand() % (Width - 7) + 1;
b1_right[k] = b1_left[k] + rand() % 3 + 5;
}
t = 3;
while (t--) {
for (j = b1_left[t]; j < b1_right[t]; j++)
{
canvas[High/3*3-3][j] = 2;
}
}
}
//Sleep(200);
restart:
return cnt;
}
void updataWithInput()
{
char key;
if (_kbhit())
{
key = _getch();
if ((key == 'a' || key == 75) && canvas[people_x][people_y-1] == 0)
{
canvas[people_x][people_y] = 0;
people_y--;
canvas[people_x][people_y] = 1;
}
if ((key == 'd' || key == 77) && canvas[people_x][people_y+1] == 0)
{
canvas[people_x][people_y] = 0;
people_y++;
canvas[people_x][people_y] = 1;
}
}
}
void init() {
system("cls");
memset(canvas, 0, sizeof canvas);
memset(b1_left, 0, sizeof b1_left);
memset(b1_right, 0, sizeof b1_right);
score = 0;
startup(); //初始化界面
}
int main()
{
HideCursor();
start:
init();
while (1)
{
show();//畫界面
int cnt = updataWithoutInput();
updataWithInput();
if (cnt == 1) goto start;
}
}
運行效果:

到此這篇關于C語言小游戲之小熊跳板功能的實現(xiàn)的文章就介紹到這了,更多相關C語言小熊跳板內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
VSCode插件開發(fā)全攻略之package.json詳解
這篇文章主要介紹了VSCode插件開發(fā)全攻略之package.json的相關知識,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
使用VSCode和VS2017編譯調試STM32程序的實現(xiàn)
這篇文章主要介紹了使用VSCode和VS2017編譯調試STM32程序的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05

