C语言项目小游戏之俄罗斯方块

您所在的位置:网站首页 金融学的参考文献怎么写 C语言项目小游戏之俄罗斯方块

C语言项目小游戏之俄罗斯方块

2024-01-03 17:49| 来源: 网络整理| 查看: 265

今天给大家带来一个用C语言实现的俄罗斯方块小游戏

游戏截图:

 

 

 

首先我们先创建一个名为mywindows.h的头文件。用来设置我们操作台的各种功能实现

mywindows.h #ifndef MYWINDOWS_H_INCLUDED #define MYWINDOWS_H_INCLUDED //系统调用模块 #include // 1.初始化句柄 void initHandle(); // 2.设置光标位置 void setPos( int x ,int y); // 3.设置颜色 void setColor(int color); //4.设置光标位置是否可见 void setCursorVisible(int flag); //6.设置窗口标题 void setTitle(char title[40]); //7.关闭句柄 void closehandle(); //8. 有意思的话 void printfwsb(); #endif // MYWINDOWS_H_INCLUDED

每一个函数的功能都有注释向大家解释,现在给大家放出函数功能的具体实现,博主创建了了个名为mywindows.c的源文件

mywindows.c #include "mywindows.h" HANDLE handle; //全局变量 // 1.初始化句柄 void initHandle() { handle = GetStdHandle(STD_OUTPUT_HANDLE); } // 2.设置光标位置 void setPos(int x,int y) { //定义结构体数据 COORD coord = {x*2,y}; // 调用光标位置函数 SetConsoleCursorPosition(handle,coord); } //3. 设置颜色 void setColor(int color) { SetConsoleTextAttribute(handle,color); } //4.设置光标是否可见 void setCursorVisible(int flag) { CONSOLE_CURSOR_INFO info; //光标信息结构体 info.bVisible = flag; //光标是否可见 info.dwSize = 100; //光标宽度1-100 SetConsoleCursorInfo(handle,&info); //设置光标属性 } // 6.设置标题 void setTitle(char title[40]) { SetConsoleTitleA(title); } // 7.关闭句柄 void closehandle() { CloseHandle(handle); } void printfwsb() { setPos(1,4); setColor(0x0c); printf("玩家过不了十五级==小笨蛋\n"); setPos(1,5); setColor(0x0d); printf("不会有人不会玩俄罗斯方块吧!"); }

这就是每个函数的具体实现,如果大家对上边的什么语法有不明白之处,可以在评论区或者私信提问作者。

然后就是我们游戏区域的实现,毕竟俄罗斯方块这个游戏是需要在一定空间内完成方块的移动与变形,所以博主创建了一个名为data.h的头文件你用来设置游戏限定区域和每种类型的方块

data.h #ifndef DATA_H_INCLUDED #define DATA_H_INCLUDED //数据模块 //界面数组(游戏池) int windowShape[25][26]= { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,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0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} }; //方块数组 // 一维数组存储有7种方块,二维数组存储4种形态,三维和四维存储方块的具体形状 int blockshape[7][4][4][4]= { { //Z {{1,1,0,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}}, {{0,1,0,0},{1,1,0,0},{1,0,0,0},{0,0,0,0}}, {{1,1,0,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}}, {{0,1,0,0},{1,1,0,0},{1,0,0,0},{0,0,0,0}} }, { //S {{0,1,1,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}}, {{1,0,0,0},{1,1,0,0},{0,1,0,0},{0,0,0,0}}, {{0,1,1,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}}, {{1,0,0,0},{1,1,0,0},{0,1,0,0},{0,0,0,0}} }, { //L {{1,0,0,0},{1,0,0,0},{1,1,0,0},{0,0,0,0}}, {{1,1,1,0},{1,0,0,0},{0,0,0,0},{0,0,0,0}}, {{1,1,0,0},{0,1,0,0},{0,1,0,0},{0,0,0,0}}, {{0,0,1,0},{1,1,1,0},{0,0,0,0},{0,0,0,0}} }, { //j {{0,1,0,0},{0,1,0,0},{1,1,0,0},{0,0,0,0}}, {{1,0,0,0},{1,1,1,0},{0,0,0,0},{0,0,0,0}}, {{1,1,0,0},{1,0,0,0},{1,0,0,0},{0,0,0,0}}, {{1,1,1,0},{0,0,1,0},{0,0,0,0},{0,0,0,0}} }, { //I {{1,1,1,1},{0,0,0,0},{0,0,0,0},{0,0,0,0}}, {{1,0,0,0},{1,0,0,0},{1,0,0,0},{1,0,0,0}}, {{1,1,1,1},{0,0,0,0},{0,0,0,0},{0,0,0,0}}, {{1,0,0,0},{1,0,0,0},{1,0,0,0},{1,0,0,0}} }, { //T {{1,1,1,0},{0,1,0,0},{0,0,0,0},{0,0,0,0}}, {{0,1,0,0},{1,1,0,0},{0,1,0,0},{0,0,0,0}}, {{0,1,0,0},{1,1,1,0},{0,0,0,0},{0,0,0,0}}, {{1,0,0,0},{1,1,0,0},{1,0,0,0},{0,0,0,0}} }, { //田 {{1,1,0,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}}, {{1,1,0,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}}, {{1,1,0,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}}, {{1,1,0,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}} } }; #endif // DATA_H_INCLUDED

windowShape数组是游戏区域的数组,数值为1的地方就是边界,0的地方是操作空间

startShape数组是博主设置的开始界面,大家运行代码打开游戏就明白了

blockshape数组是作者设置的游戏方块数组,数值一组成的形状极为方块形状,一维数组存储有7种方块,二维数组存储4种形态,三维和四维存储方块的具体形状。

 

接下来就是具体游戏内容的实现,比如说方块的三向移动,变形,分数统计,等级统计,游戏暂停,游戏开始时间,随机生成第一个方块等等等,这些内容的声明和实现我分别放在了game.h与game.c两个文件中

game.h与game.c game.h #ifndef GAME_H_INCLUDED #define GAME_H_INCLUDED // 游戏逻辑模块 // 1.绘制界面提示信息等内容 void printInfo(); // 2.绘制游戏边框 void printWindow(int x ,int y); // 3.打印分数和等级 // 参数是一次性消的行数,根据一次性消的行数不同,增加不同的分数,然后打印到界面上 void printGL(int num); // 4.打印方块:需要知道位置,颜色,形状,形态 void printBlock(int x,int y,int shape,int status,int color); // 5.清除方块 void deteleBlock(int x, int y, int shape,int status); //6.初始化游戏 void initGame(); //7. 方块左移 void blockLeft(); //8. 方块右移 void blockRight(); //9. 方块变形 void blockChange(); //10. 方块下移 int blockDown(); //11.方块直接下落 void blockBottom(); //12. 游戏暂停 void pause(); //定义方块结构体 typedef struct { int x; int y; int shape; int status; int color; }Block; // 13.产生第一个随机方块 void startBlock(); // 14.产生预判方块 void randBlock(); // 15. 拷贝方块--》将预判方块拷贝给第一个方块 void copyBlock(); //16.碰撞检测 int crash(int x ,int y,int shape,int status); // 17.保存方块 void saveBlock(); // 18.刷新游戏区域 void updatagame(); // 19.消行检测函数 void lineclear(); // 20.消行函数:从满行位置的上一行开始下移 void lineDown(int line); //21.游戏结束动画 void printOver(); //23.游戏开始 void startGame(); //打印界面形状 void printshape(); void music(); //加速 void start_timer(); //22.关机 void dead(); //23.关机提醒 void tip(); //结束界面 void overshow(); void gotoxy(int x, int y ); #endif // GAME_H_INCLUDED game.c #include "game.h" #include "data.h" #include "mywindows.h" #include #include #include time_t start_time; int elapsed_time; //定义分数和等级俩个全局变量,用于保存游戏中的分数和等级 int level = 1; int grade = 0; //定义当前方块和预判方块 Block curBlock; Block nextBlock; // 1.打印操作说明 void printInfo() { setColor(0x0a); setPos(31,9); printf("操作说明:"); //setColor(0x0b); setPos(31,10); printf("按 a 或 A 左移"); setPos(31,11); printf("按 d 或 D 右移"); setPos(31,12); printf("按 s 或 S 下移"); setPos(31,13); printf("按 w 或 W 变形"); setPos(31,14); printf("按 空格 暂停"); setPos(31,15); printf("按 回车 直接下落"); } //2.绘制游戏边框 void printWindow(int x,int y) { int i,j; for(i=0; i15) { if(stopTime-startTime>0.15*CLOCKS_PER_SEC) { if(blockDown()==-2) break; //重新定义开始时间 startTime = stopTime; } } else if(level>20) { if(stopTime-startTime>0.10*CLOCKS_PER_SEC) { if(blockDown()==-2) break; //重新定义开始时间 startTime = stopTime; } } else if(level>=25) { if(stopTime-startTime>0.05*CLOCKS_PER_SEC) { if(blockDown()==-2) break; //重新定义开始时间 startTime = stopTime; } } } elapsed_time = get_elapsed_time(); // 获取游戏时间差 setColor(0x07); setPos(32,20); printf("游戏用时:%d秒\n", elapsed_time); printOver(); overshow(); if(level


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3