【打字母游戏_C语言实现】
生活随笔
收集整理的這篇文章主要介紹了
【打字母游戏_C语言实现】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 1.項目需求:
- 2.項目分析:
- 3.項目設計:
- 3.1設計字母結構體:
- 3.2屏幕大小:
- 3.3 添加二維數組空格函數 LnitGrid(GridArray ga)
- 3.4 顯示網格 ShowGrid(GridArray ga, struct LetterNode* px, int n)
- 3.5 產生隨機字母函數 RandLetter(struct LetterNode* px, int n)
- 3.6 main函數
- 3.7 調用的頭文件
1.項目需求:
隨機產生一個字母從屏幕下落,玩家輸入字母,如果和顯示字母相同,就消去字母;游戲會再隨機產生一個字母,繼續游戲,如果字母落出屏幕,玩家失敗,游戲結束;
2.項目分析:
項目由兩個模塊構成:
1.顯示模塊 : 顯示模塊由二維數組構成,把隨機產出的字母賦值到二維數組中。
2.處理模塊 : 處理模塊功能有:隨機產生字母 ,輸入字母比較,字母是否落出屏幕,字母下降功能;
3.項目設計:
3.1設計字母結構體:
struct LetterNode {char ch;int row;int col; };3.2屏幕大小:
#define ROWSIZE 20 // 行 #define COLSIEE 70 // 列 typedef char GridArray[ROWSIZE][COLSIEE + 1]; //二維數組的 網格 #define LETSIZE 1 // 產生的字母數3.3 添加二維數組空格函數 LnitGrid(GridArray ga)
void LnitGrid(GridArray ga) // {for (int i = 0; i < ROWSIZE; i++){memset(ga[i], ' ', sizeof(char) * COLSIEE);ga[i][COLSIEE] = '\0';} }3.4 顯示網格 ShowGrid(GridArray ga, struct LetterNode* px, int n)
清屏函數:system(“cls”); 頭文件: #include <windows.h>
void ShowGrid(GridArray ga, struct LetterNode* px, int n) {assert(px != NULL);system("cls"); //清屏LnitGrid(ga);for (int i = 0; i < n; ++i){ga[px[i].row][px[i].col] = px[i].ch;}for (int i = 0; i < ROWSIZE; ++i){printf("%s \n", ga[i]);} }3.5 產生隨機字母函數 RandLetter(struct LetterNode* px, int n)
void RandLetter(struct LetterNode* px, int n) {assert(px != NULL);srand((int)time(NULL));for (int i = 0; i < n; i++){px[i].ch = rand() % 26 + 'a';px[i].row = 0;px[i].col = rand() % COLSIEE;} }3.6 main函數
_kbhit() : //鍵盤是否有輸入 頭文件:#include<conio.h>
int main() {GridArray ga;char ch;struct LetterNode x[LETSIZE] = { 0 };RandLetter(x, LETSIZE);while (1){ShowGrid(ga, x, LETSIZE);Sleep(1000); //1000毫秒 == 1 秒if (_kbhit()) //鍵盤是否有輸入{//ch = getchar();ch = _getch(); //無需等待回車輸入if (ch == x[0].ch){x[0].ch = rand() % 26 + 'a';x[0].row = -1;x[0].col = rand() % COLSIEE;}}x[0].row += 1;if (x[0].row >= ROWSIZE){printf("游戲結束 \n");break;}}return 0; }3.7 調用的頭文件
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<stdbool.h> #include<assert.h> #include<Windows.h> #include<time.h> #include<conio.h> #include<ctype.h>運行結果:
總結
以上是生活随笔為你收集整理的【打字母游戏_C语言实现】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何打开win7系统诊断策略服务器,wi
- 下一篇: 呼呼吧