经过两天的学习,我在这个游戏中学到了一些感觉不错的东西,起码明白了windows下的编程的进度是怎样的,那么现在,我要在这里分享一下我的个人所得点
CODE:
code.h:
#ifndef TETRIS_H_INCLUDED #define TETRIS_H_INCLUDED #include <windows.h> #include <time.h> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> HANDLE output; //handle 是定义控制台句柄 #define COL_BEGIN 2 #define COL_END 14 #define ROW_BEGIN 4 #define ROW_END 26 typedef struct TetrisManager { unsigned int pool[28]; //游戏池 int x; //当前方块x坐标 指左上角坐标 int y; //当前方块y坐标 指左上角坐标 int type[3]; //当前方块类型下一个及下下个方块类型 int orientation[3]; unsigned score; //得分 unsigned erasedCount[4]; //消去的行数 unsigned erasedTotal; //消行总数 unsigned tetrisCount[7]; //各个方块的个数 unsigned tetrisTotal; //方块总数 bool dead; }Manager; void startGame(Manager *manager); void printScore(const Manager *manager); static const unsigned int TetrisTable[7][4]= //设置7种类型的各个形状 { {0x00F0,0x2222,0x00F0,0x2222}, //I形状 {0x0072,0x0262,0x0270,0x0232}, //T {0x0223,0x0074,0x0622,0x0170}, //L {0x0226,0x0470,0x0322,0x0071}, //J {0x0063,0x0264,0x0063,0x0264}, //Z {0x006c,0x0462,0x006c,0x0462}, //S {0x0660,0x0660,0x0660,0x0660} //O }; static const unsigned int gs_uInitialTetrisPool[28]= //用来初始化pool[28]; { 0xC003,0xC003,0xC003,0xC003,0xC003,0xC003, 0xC003,0xC003,0xC003,0xC003,0xC003,0xC003, 0xC003,0xC003,0xC003,0xC003,0xC003,0xC003, 0xC003,0xC003,0xC003,0xC003,0xC003,0xC003, 0xC003,0xC003,0xFFFF,0xFFFF }; //与Pool[28]相对应 #endif // TETRIS_H_INCLUDED
code.c:
#include <stdio.h> #include <stdlib.h> #include "tetris.h" void initGame(); void gotoxyWithfullwidth(short x,short y); void printfPrompting(); void printfPoolBorder(); void printScore(const Manager *manager); void startGame(Manager *manager); void printScore(const Manager *manager); void printfNextTetris(const Manager *manager); int line_xun; int main() { Manager manager; //第一个是结构体(在子程序中有定义), 第二个是变量 initGame(&manager); //初始化,对光标,标题进行操作,将这些操作封装到一个函数中 printfPrompting(); printScore(&manager); printfNextTetris(&manager); return 0; } void initGame(Manager *manager) { CONSOLE_CURSOR_INFO CURSORINFO={1,FALSE}; //光标的结构体,1是光标厚度,false是不让光标显示,从1到100内可以定义 output=GetStdHandle(STD_OUTPUT_HANDLE); //获取当前窗口句柄,把值给output,只有这两句加一起才能隐藏光标 SetConsoleCursorInfo(output,&CURSORINFO);//传出去的是个地址,所以光标要加& //上行命令是为了对光标进行操作 printfPoolBorder(); SetConsoleTitle("Hatsune miku"); //对标题进行更改 startGame(manager); } void gotoxyWithfullwidth(short x,short y) { static COORD cd; cd.X=x*2; cd.Y=y; SetConsoleCursorPosition(output,cd); } void printfPrompting() //设置输出一些信息 { SetConsoleTextAttribute(output,0xB); //设置背景色 gotoxyWithfullwidth(28,8); printf("■control:"); gotoxyWithfullwidth(30,10); printf("■move left:←A 4"); gotoxyWithfullwidth(30,12); printf("■move right:→D 6"); gotoxyWithfullwidth(30,14); printf("■move top:↑ W 8"); gotoxyWithfullwidth(30,16); printf("■move down:↓ S 2"); gotoxyWithfullwidth(30,18); printf("■rorato: 0"); gotoxyWithfullwidth(30,20); printf("■redown:space"); gotoxyWithfullwidth(30,22); printf("■Stop :enter"); } void printfPoolBorder() { int y; SetConsoleTextAttribute(output,0xF0); for(y=4;y<26;y++) { gotoxyWithfullwidth(10,y-3); printf("%2s",""); gotoxyWithfullwidth(23,y-3); printf("%2s",""); } gotoxyWithfullwidth(10,y-3); printf("(s",""); } void startGame(Manager *manager) { memset(manager,0,sizeof(Manager)); memcpy(manager->pool,gs_uInitialTetrisPool,sizeof(unsigned int [28])); srand((unsigned)time(NULL));//随机种子 manager->type[1]=rand()%7; manager->orientation[1]=rand()%4; manager->type[2]=rand()%7; manager->orientation[2]=rand()%4; } void printScore(const Manager *manager) //定义左边的东西 { static const char *tetrisName="ITLJZSO"; int i; SetConsoleTextAttribute(output,0xA);//设置文本颜色 gotoxyWithfullwidth(2,2); printf("Score:%u",manager->score); gotoxyWithfullwidth(1,6); printf("disline:%u",manager->erasedTotal); for(i=0;i<4;i++) { gotoxyWithfullwidth(2,8+i); printf("dis%d:%u",i+1,manager->erasedCount[i]); } for(i=0;i<7;i++) { gotoxyWithfullwidth(2,17+i); printf("%c type:%u",tetrisName[i],manager->tetrisCount[i]); } } void printfNextTetris(const Manager *manager) //设置右上角的图形界面 { int i; unsigned int tetris; SetConsoleTextAttribute(output,0xD); //设置字符颜色 gotoxyWithfullwidth(25,2); printf("|%6s",""); gotoxyWithfullwidth(25,3); printf("|%6s",""); gotoxyWithfullwidth(25,4); printf("|%6s",""); gotoxyWithfullwidth(25,5); printf("|%6s",""); //**************************** gotoxyWithfullwidth(28,2); printf("|%6s|%6s","",""); gotoxyWithfullwidth(28,3); printf("|%6s|%6s","",""); gotoxyWithfullwidth(28,4); printf("|%6s|%6s","",""); gotoxyWithfullwidth(28,5); printf("|%6s|%6s","",""); //**************************** gotoxyWithfullwidth(38,2); printf("|%1s",""); gotoxyWithfullwidth(38,3); printf("|%1s",""); gotoxyWithfullwidth(38,4); printf("|%1s",""); gotoxyWithfullwidth(38,5); printf("|%1s",""); //**************************** tetris=TetrisTable[manager->type[2]][manager->orientation[2]];//右边的随机图形 SetConsoleTextAttribute(output,manager->type[1]|8); for(i=0;i<16;i++) { gotoxyWithfullwidth((i&3)+33,(i>>2)+3); ((tetris>>i)&1)?printf("ク"):printf("%2s",""); } //**************************** SetConsoleTextAttribute(output,0xB); //设置字符颜色 gotoxyWithfullwidth(32,6); printf("____________"); gotoxyWithfullwidth(32,1); printf("------------"); gotoxyWithfullwidth(25,1); printf("------------"); gotoxyWithfullwidth(25,6); printf("____________"); //**************************** tetris=TetrisTable[manager->type[1]][manager->orientation[1]];//左边的随机图形 SetConsoleTextAttribute(output,manager->type[1]|8); for(i=0;i<16;i++) //四乘四矩阵 { gotoxyWithfullwidth((i&3)+26,(i>>2)+2); ((tetris>>i)&1)?printf("ミ"):printf("%2s",""); } SetConsoleTextAttribute(output,0xB); gotoxyWithfullwidth(30,24); printf("Mader:初音殿下"); }