tft每日頭條

 > 遊戲

 > 簡單c語言代碼遊戲

簡單c語言代碼遊戲

遊戲 更新时间:2024-10-05 23:45:02

這篇文章主要為大家詳細介紹了C語言實現——《打字練習系統》,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小夥伴們可以參考一下!

簡單c語言代碼遊戲(打字母遊戲零基礎項目)1

遊戲介紹:

字母遊戲》是一款敏捷打字小遊戲,遊戲大小為468K。背景設定 《字母遊戲》是一款有趣的打字遊戲,可以提高你的打字速度。操作指南 根據出現的字母,按鍵盤A-Z鍵對應的按鍵即可。遊戲加載完畢點擊[開始遊戲]即可開始遊戲。在限定時間内,盡可能地輸入正确的字母,挑戰高分!

本項目針對C語言學習者,将我們打字母的“字母”置換成了C語言關鍵字,記在一定時間内及時輸出C語言關鍵字就可以得分!我們一起來看看吧!

本項目編譯環境:VS2019/VS2013;

插件:圖形庫插件easyX,涉及圖片素材可以自行百度找也可以關注文末領取;

效果圖展示

簡單c語言代碼遊戲(打字母遊戲零基礎項目)2

配套講解教程:「鍊接」

源代碼示例:

#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <time.h> #include <stdlib.h> #include <conio.h> #include <graphics.h> //報錯,請先安裝 #include <mmsystem.h> #pragma comment(lib,"winmm.lib") //數據設計設計 //窗口屬性: const int WIDTH = 640; const int HEIGHT = 500; //遊戲正确率和錯誤率 int right = 0; int error = 0; //下墜文字的結構體 struct TARGET { //每一個字符串的x,y坐标 int x; int y; char *str; //保存字符串 }; //用戶輸入的值 struct USRKEY { int x; int y; char str[20]; }userkey = {320,500-30,""}; //在指定位置輸出整數 void outtextxy_int(int x, int y, char *format, int num) { char str[20] = ""; //printf; sprintf(str, format, num); outtextxy(x, y, str); } //在指定位置輸出浮點數 void outtextxy_double(int x, int y, char *format, double num) { char str[20] = ""; sprintf(str, format, num); outtextxy(x, y, str); } void divWindow() { line(WIDTH - 100, 0, WIDTH - 100, HEIGHT - 40); line(0, HEIGHT - 40, WIDTH 50, HEIGHT - 40); line(WIDTH - 100, 130, WIDTH 50, 130); } void initTarget(struct TARGET words[], int n) { static char str[29][10] = { "main", "include", "void", "while", "for", "true", "false", "break", "int", "char", "float", "double", "switch", "case", "static", "if", "else", "short", "unsigned", "signed", "sizeof", "continue", "struct", "union", "enum", "register","default","long","return"}; //0-28 //随機産生 words[n].str = str[rand() % 29]; //0 1 2 //判斷重複,如果重複,就重新生成 while (words[n].str == words[(n 1) % 3].str || words[n].str == words[(n 2) % 3].str) { words[n].str = str[rand() % 29]; } words[n].x = rand() % (WIDTH-200); words[n].y = -20; } void drawScore() { settextcolor(LIGHTBLUE); settextstyle(25, 0, "字魂24号-鎮魂手書"); //軟件信息輸出 outtextxy(WIDTH - 90, 25, "頓開教育"); outtextxy(WIDTH - 90, 25 25, "程序員專屬"); outtextxy(WIDTH - 90, 25 25 25, "打字遊戲"); //遊戲狀态欄輸出 outtextxy(WIDTH - 90, 225, "正确數"); outtextxy_int(WIDTH - 90, 225 25,"%d", right); outtextxy(WIDTH - 90, 285, "錯誤數"); outtextxy_int(WIDTH - 90, 285 25, "%d", error); outtextxy(WIDTH - 90, 285 285-225, "正确率"); //分類讨論 if (right error == 0) { outtextxy_double(WIDTH - 90, 285 285 - 225 25, "%.2lf%%", 0.00); } else { //C語言 除法會取整 double sum = right error; outtextxy_double(WIDTH - 90, 285 285 - 225 25, "%.2lf%%", right / sum * 100); } } int main() { srand((unsigned int)time(NULL)); mciSendString("open 1.mp3 alias music", 0, 0, 0); initgraph(WIDTH 50, HEIGHT); struct TARGET words[3]; //随機産生掉落的字符串 for (int n = 0; n < 3; n ) { initTarget(words, n); words[n].y = -15 - n * 30; //形成不登高 } BeginBatchDraw(); int i = 0; while (1) { cleardevice(); divWindow(); //碰線處理 for (int n = 0; n < 3; n ) { words[n].y = 2; if (words[n].y>(HEIGHT - 40 - textheight(words[n].str))) { initTarget(words, n); } } //打印文字 for (int n = 0; n < 3; n ) { settextcolor(RED); outtextxy(words[n].x, words[n].y, words[n].str); } if (_kbhit()) //kbhit 檢測鍵盤,有按鍵返回非零 { //字符串變為字符處理 char target; //接受用戶的值 if ((target = _getch()) != '\r') { userkey.str[i ] = target; } else { int flagError = 0; //幹掉輸入正确的字符 for (i = 0; i < 3; i ) { if (strcmp(userkey.str, words[i].str) == 0) { initTarget(words, i); right ; flagError = 1; mciSendString("play music", 0, 0, 0); } } if (flagError == 0) { error ; } //習慣很重要:邊寫邊測試 i = 0; userkey.x = 320; memset(userkey.str, 0, 20); } } outtextxy(userkey.x, userkey.y, userkey.str); drawScore(); FlushBatchDraw(); Sleep(100); } getchar(); closegraph(); return 0; }

寫在最後:對于準備學習C/C 編程的小夥伴,如果你想更好的提升你的編程核心能力(内功)不妨從現在開始!

編程學習書籍分享:

簡單c語言代碼遊戲(打字母遊戲零基礎項目)3

編程學習視頻分享:

簡單c語言代碼遊戲(打字母遊戲零基礎項目)4

整理分享(多年學習的源碼、項目實戰視頻、項目筆記,基礎入門教程)

歡迎轉行和學習編程的夥伴,利用更多的資料學習成長比自己琢磨更快哦!

對于C/C 感興趣可以關注小編在後台私信我:【編程交流】一起來學習哦!可以領取一些C/C 的項目學習視頻資料哦!已經設置好了關鍵詞自動回複,自動領取就好了!

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关遊戲资讯推荐

热门遊戲资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved