本文实例为大家分享了C语言实现俄罗斯方块的具体代码,供大家参考,具体内容如下
GitHub:【C语言】实现俄罗斯方块源代码
Head.h
#ifndef _HEAD_H_ #define _HEAD_H_ #include<graphics.h> #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<time.h> #include<string.h> #define _CRT_SECURE_NO_WARNINGS 1 //界面的相关的参数 #define WALL_SQUARE_WIDTH 10 //围墙方块的宽度 #define xWALL_SQUARE_NUM 30 //x轴方向的方块的数目 #define yWALL_SQUARE_WIDTH 46 //y轴方向的方块的数目 #define GAME_WALL_WIDTH (WALL_SQUARE_WIDTH*xWALL_SQUARE_NUM) //游戏区域的宽度 300 #define GAME_WALL_HTGH (WALL_SQUARE_WIDTH*yWALL_SQUARE_WIDTH) //游戏区域的高度 460 #define WINDOW_WIDTH 480 // 游戏总窗口宽度 480 #define WINDOW_HIGH 460 // 游戏总窗口高度 460 //定义俄罗斯方块的相关参数 #define ROCK_SQUARE_WIDTH (2*WALL_SQUARE_WIDTH) //俄罗斯方块的大小是围墙的两倍 20 #define xROCK_SQUARE_NUM ((GAME_WALL_WIDTH -20)/ROCK_SQUARE_WIDTH) // 游戏区x轴放的方块数目:14 #define yROCK_SQUARE_NUM ((GAME_WALL_HTGH -20)/ROCK_SQUARE_WIDTH) // 游戏区y轴放的方块数目:22 //定义移动方块的相关操作 #define DIRECT_UP 3 #define DIRECT_DOWN 2 #define DIRECT_LEFT -1 #define DIRECT_RIGHT 1 /*数据结构-线性表(结构体数组)*/ typedef struct ROCK { //用来表示方块的形状(每一个字节是8位,用每4位表示方块中的一行) unsigned short rockShapeBits; int nextRockIndex; //下一个方块,在数组中的下标 } RockType; //方块在图形窗口中的位置(即定位4*4大块的左上角坐标) typedef struct LOCATE { int left; int top; } RockLocation_t; //全局变量-游戏板的状态描述(即表示当前界面哪些位置有方块) //0表示没有,1表示有(多加了两行和两列,形成一个围墙,便于判断方块是否能够移动) int game_board[yROCK_SQUARE_NUM + 2][xROCK_SQUARE_NUM + 2] = { 0 }; int game_socres = 0; //全局分数 // 把俄罗斯方块的19种类放到数组中 int rockTypeNum = 19; RockType RockArray[19] = { (0, 0) }; //预览区的方块的位置 RockLocation_t preRockLocation = {GAME_WALL_WIDTH+70,70}; //每次生成初始化方块的位置 RockLocation_t initRockLocation = { (WALL_SQUARE_WIDTH + 100), WALL_SQUARE_WIDTH }; //分数显示的位置 //各个文件中的函数 // 画出界面以及画出方块Draw.h中 void DrawGameWindow(); void DisplayRock(int rockIdx, RockLocation_t* LocatePtr, bool displayed); //初始化Init源文件 void InitGame(); //game.h void PlayGame(); bool IsGameOver(); #endif
Draw.h
#include"Head.h" //画出游戏界面 void DrawGameWindow() { //先画出围墙 setcolor(BLUE); setlinestyle(PS_SOLID,NULL,0); setfillcolor(BLUE); //画出上下围墙 for (int x = WALL_SQUARE_WIDTH; x <= GAME_WALL_WIDTH; x += WALL_SQUARE_WIDTH) { fillrectangle(x - WALL_SQUARE_WIDTH, 0, x, WALL_SQUARE_WIDTH); //上 fillrectangle(x - WALL_SQUARE_WIDTH, GAME_WALL_HTGH - WALL_SQUARE_WIDTH, x, GAME_WALL_HTGH);//下 } //画出左右围墙 for (int y = WALL_SQUARE_WIDTH; y <= GAME_WALL_HTGH; y += WALL_SQUARE_WIDTH) { fillrectangle(0, y, WALL_SQUARE_WIDTH, y + WALL_SQUARE_WIDTH);//左 fillrectangle(GAME_WALL_WIDTH - WALL_SQUARE_WIDTH, y, GAME_WALL_WIDTH, y + WALL_SQUARE_WIDTH);//右 } //画出右边统计分数及相关东西 //画出分割线 setcolor(WHITE); setlinestyle(PS_DASH,2); line(GAME_WALL_WIDTH + 20, 0, GAME_WALL_WIDTH + 20, GAME_WALL_HTGH); //设置字体 LOGFONT font; gettextstyle(&font); settextstyle(18, 0, _T("宋体")); font.lfQuality = ANTIALIASED_QUALITY;//设置输出效果为抗锯齿 //1显示预览形状 outtextxy(GAME_WALL_WIDTH + 80, 30, _T("预览")); outtextxy(GAME_WALL_WIDTH + 80, 170, _T("分数")); outtextxy(GAME_WALL_WIDTH + 65, 250, _T("操作说明")); outtextxy(GAME_WALL_WIDTH + 40, 290, _T("w a s d控制方向")); outtextxy(GAME_WALL_WIDTH + 40, 335, _T("空格 暂停")); //显示分数 setcolor(RED); outtextxy(GAME_WALL_WIDTH + 90, 200, '0'); } //在游戏区显示编号为rockIdx的方块 void DisplayRock(int rockIdx, RockLocation_t* LocatePtr, bool displayed) { int color;//方块的填充颜色 int lineColor = WHITE;//线的颜色 int boardFalg = 0; int xRock = 0; int yRock = 0; unsigned short rockCode = RockArray[rockIdx].rockShapeBits; //如果displayed为true的话,将方块块颜色设置为white,game_board对应的位置设置为1; //如果displayed为false的话,将方块块颜色设置为black,game_board对应的位置设置为0; displayed ? (color = RED, boardFalg = 1) : (color = BLACK,lineColor = BLACK, boardFalg = 0); setcolor(lineColor); setfillcolor(color); setlinestyle(PS_SOLID);//设置为实线, xRock = LocatePtr->left; yRock = LocatePtr->top; int count = 0;//每4个换行,记录坐标偏移量 unsigned short mask = 1; for (int i = 1; i <= 16; ++i) { mask = 1 << (16 - i); if ((rockCode & mask) != 0) //如果不为0的话,就画出小方块 { fillrectangle(xRock , yRock, xRock + ROCK_SQUARE_WIDTH, yRock + ROCK_SQUARE_WIDTH); } if (i % 4 == 0) //换行 { yRock = yRock + ROCK_SQUARE_WIDTH; xRock = xRock = LocatePtr->left; } else { xRock += ROCK_SQUARE_WIDTH; } } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/482446.html