C語言實(shí)現(xiàn)打磚塊游戲
更新時(shí)間:2022年05月11日 12:35:06 作者:Object_in_java
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)打磚塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C語言實(shí)現(xiàn)打磚塊游戲的具體代碼,供大家參考,具體內(nèi)容如下
代碼:
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<Windows.h> int score; int ball_row, ball_col; int ball_vv, ball_vh; int area_height, area_width; int baffle_col, baffle_row, baffle_size; int brick_col, brick_row; bool isLose; void gotoxy(int x, int y) { ?? ?HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); ?? ?COORD pos; ?? ?pos.X = x; ?? ?pos.Y = y; ?? ?SetConsoleCursorPosition(handle, pos); } void HideCursor() { ?? ?CONSOLE_CURSOR_INFO cursor_info = { 1,0 }; ?? ?SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void startup() { ?? ?area_height = 20; ?? ?area_width = 40; ?? ?ball_row = area_height / 2; ?? ?ball_col = area_width / 2; ?? ?ball_vv = 1; ?? ?ball_vh = 1; ?? ?baffle_col = area_width / 2; ?? ?baffle_row = area_height - 2; ?? ?baffle_size = 8; ?? ? ?? ?brick_row = 1; ?? ?brick_col = rand() % area_width; ?? ?score = 0; ?? ?isLose = false; } void show() { ?? ?gotoxy(0, 0); ?? ?int i, j; ?? ?//system("cls"); ?? ?for (i = 0; i <= area_height; i++) ?? ?{ ?? ??? ?for (j = 0; j <= area_width; j++) ?? ??? ?{ ?? ??? ??? ?if (i == ball_row && j == ball_col) ?? ??? ??? ?{ ?? ??? ??? ??? ?printf("O"); ?? ??? ??? ?} ?? ??? ??? ?else if (i == 0 || i == area_height) ?? ??? ??? ??? ?printf("-"); ?? ??? ??? ?else if (j == 0 || j == area_width) ?? ??? ??? ??? ?printf("|"); ?? ??? ??? ?else if (i == baffle_row && (j >= baffle_col && j <= baffle_col + baffle_size)) ?? ??? ??? ??? ?printf("="); ?? ??? ??? ?else if (i == brick_row && j == brick_col) ?? ??? ??? ??? ?printf("#"); ?? ??? ??? ?else printf(" "); ?? ??? ?} ?? ??? ?printf("\n"); ?? ?} ?? ?printf("score:%d\n", score); } void updateWithhoutInput() { ?? ?static int count = 0; ?? ?if(count % 5 == 0){ ?? ??? ?ball_col += ball_vh; ?? ??? ?ball_row += ball_vv; ?? ?} ?? ? ?? ?if (ball_col == 0 + 1 || ball_col == area_width - 1)//vh changed ?? ??? ?ball_vh *= -1; ?? ?if (ball_row == 0 + 1 /*|| ball_row == area_height - 1*/)//vv changed ?? ??? ?ball_vv *= -1; ?? ?if (ball_row == baffle_row - 1 && (ball_col >= baffle_col && ball_col < baffle_col + baffle_size))//在baffle上一行就判斷碰撞。? ?? ??? ?ball_vv *= -1; ?? ?// 磚塊的左,右,下左,下,下右可以檢測到碰撞? ?? ?if ((ball_row == brick_row + 1 && (ball_col >= brick_col - 1 && ball_col <= brick_col + 1))||(ball_row == brick_row && (ball_col == brick_col - 1 || ball_col==brick_col+1))) { ?? ??? ?ball_vv *= -1; ?? ??? ?brick_row = -1; ?? ??? ?score += 10; ?? ??? ?brick_row = 1; ?? ??? ?brick_col = rand() % area_width; ?? ?} ?? ?if (ball_row >= area_height) ?? ??? ?isLose = true; ?? ??? ? ?? ?count++; } void updateWithInput() { ?? ?char input; ?? ?if (kbhit()) { ?? ??? ?input = getch(); ?? ??? ?switch (input) ?? ??? ?{ ?? ??? ?case 'a': if (baffle_col > 0 + 1)baffle_col--; break; ?? ??? ?case 'w': if (baffle_row > 0 + 1)baffle_row--; break; ?? ??? ?case 'd': if (baffle_col < area_width - baffle_size - 1)baffle_col++; break; ?? ??? ?case 's': if (baffle_row < area_height - 1)baffle_row++; break; ?? ??? ?default: ?? ??? ??? ?break; ?? ??? ?} ?? ?} } int Lost(){ ?? ?if(ball_row > area_height) ?? ??? ?return 1; ?? ?return 0; } int IsFinish() {//游戲是否結(jié)束? ?? ?if (score == 100) { ?? ??? ?system("cls"); ?? ??? ?printf("congretulations!!!"); ?? ??? ?score = 0; ?? ??? ?_sleep(500);//先暫停在現(xiàn)實(shí)符合人性化? ?? ??? ?system("pause"); ?? ??? ?return 1; ?? ?} ?? ?else if (Lost() == 1) { ?? ??? ?system("cls"); ?? ??? ?printf("you have lost!!!"); ?? ??? ?score = 0; ?? ??? ?_sleep(500); ?? ??? ?system("pause"); ?? ??? ?return 1; ?? ?} ?? ?return 0; } int main() { ?? ?HideCursor(); ?? ?startup(); ?? ?while (1) ?? ?{ ?? ??? ?show(); ?? ??? ?updateWithInput(); ?? ??? ?updateWithhoutInput(); ?? ??? ?if(IsFinish() == 1){ ?? ??? ??? ?startup(); ?? ??? ??? ?continue; ?? ??? ?} ?? ?} ?? ?return 0; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
VC實(shí)現(xiàn)讓關(guān)閉按鈕成灰色不可用的方法
這篇文章主要介紹了VC實(shí)現(xiàn)讓關(guān)閉按鈕成灰色不可用的方法,比較實(shí)用的一個(gè)特殊功能,需要的朋友可以參考下2014-08-08C語言統(tǒng)計(jì)字符個(gè)數(shù)代碼分享
本文給大家分享的是2則C語言實(shí)現(xiàn)統(tǒng)計(jì)字符個(gè)數(shù)的代碼,非常的簡單實(shí)用,小伙伴們根據(jù)自己的項(xiàng)目需求自由選擇吧。2015-07-07stl容器set,map,vector之erase用法與返回值詳細(xì)解析
在使用 list、set 或 map遍歷刪除某些元素時(shí)可以這樣使用,如下所示2013-09-09