在VS中新建win32 Application Proj,选择Empty ,完成TetrisWin项目创建。新建tetris.c和tetris.h两个文件,打开tetris.h文件。

首先要包括的是可能要用到的头文件,那在这里要用到是什么头文件呢? 本系统是开发一个游戏,那么游戏的话就需要有和用户进行交互的游戏界面,那就需要绘图操作,那么就会用到windows的绘图函数库,所以第一步就是要包括这个windows头文件,但是要注意我们现在是在头文件tetris.h中来包含这个头文件,这里就需要注意使用#ifndef 宏来进行包含处理,因为tetris.h最终会被包含在实现文件中去,但是不确定实现文件的头部是否也会包含这个windows库头文件,如果包括了的话我们就没有必要再包括了,否则就会引发重定义错误,所以在头文件中要包括什么库的头文件时,最好用#ifndef宏先判断是否已包含了该头文件,只有在没有包括的情况下再去包含该头文件,此时才不会报重定义的错误。具体实现如下:

  1. //header infor
  1. #ifndef WIN_H_H
  1. #define WIN_H_H
  1. #include <Windows.h>
  1. #endif

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

因为游戏中只有7种游戏方块,所以可以声明为一个枚举类型,同时也有利于在游戏中为了方便的直到当前方块形状和下一个方块形状。具体实现如下:

  1. //self_definition enum
  1. typedef enum tetris_shape{
  1. ZShape = 0,
  1. SShape,
  1. LineShape,
  1. TShape,
  1. SquareShape,
  1. LShape,
  1. MirroredLShape
  1. }shape;

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

接下来根据要实现的功能模块声明相应的处理函数。具体实现如下:

  1. //function declaraction
  1. int maxX(); //取得当前方块的最大x坐标
  1. int minX(); //取得当前方块的最小x坐标
  1. void turn_left(); //将当前方块逆时针旋转90度
  1. void turn_right(); //将当前方块顺时针旋转90度
  1. int out_of_table();//检查当前方块是否超出桌面的范围
  1. void transform(); //旋转当前方块
  1. int leftable(); //判断当前方块能否左移
  1. int rightable(); //判断当前方块能否右移
  1. int downable(); //判断当前方块能否下移
  1. void move_left(); //向左移动当前方块
  1. void move_right(); //向右移动当前方块
  1.  
  1. //operation function
  1. int add_to_table();//将当前方块固定到桌面上,若返回0,表示游戏结束
  1. void remove_full();//删除桌面上填满的行
  1.  
  1. //control function
  1. void new_game(); //创建一个新游戏
  1. void run_game(); //运行游戏
  1. void next_shape(); //将下一个方块设为当前方块,并设置下一个方块
  1. int random(int seed);//取得一个随机数,例如random(7)将返回一个0-6之间的随机数
  1.  
  1. //paint function
  1. void paint(); //将内存位图输出到窗口上
  1. void draw_table(); //绘制游戏桌面
  1.  
  1. //other functions
  1. void key_down(WPARAM wParam); //处理键盘按下事件
  1. void resize(); //改变窗口大小时调用的函数
  1. void intialize();//初始化
  1. void finalize(); //结束时,释放资源
  1.  
  1. //callback function
  1. //回调函数,用来处理windows消息
  1. LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

上面基本上把需要用到的处理函数都已声明完毕,接下来打开tetris.c文件来实现相应的功能,首先需要包含一些头文件,1.系统中需要用到sprintf()这样函数来格式化输出一些字符到相应的变量中,所以需要包含stdio.h文件,同时需要使用当前时间作为rand()种子来获取随机数,所以需要包含time.h文件,当然还需要包含tetris.h文件,具体实现如下:

  1. //Header Info
  1. #include <time.h>
  1. #include <stdio.h>
  1. #include "tetris.h"

接下来需要定义一些常量,包括游戏开始结束时的提示信息以及相应的颜色值等,具体定义如下:

  1. //constant definition
  1. #define APP_NAME "TETRIS"
  1. #define APP_TITLE "Tetris Game"
  1. #define GAMEOVER "GAME OVER"
  1.  
  1. #define SHAPE_COUNT 7 //形状的个数
  1. #define BLOCK_COUNT 4 //每个形状由几个小表格构成
  1. #define MAX_SPEED 5 //速度级别
  1. #define COLUMS 20 //游戏桌面表格的列数
  1. #define ROWS 30 //游戏桌面表格的行数
  1.  
  1. //7种颜色
  1. #define RED RGB(255,0,0)
  1. #define YELLOW RGB(255,255,0)
  1. #define GRAY RGB(128,128,128)
  1. #define BLACK RGB(0,0,0)
  1. #define WHITE RGB(255,255,255)
  1. #define STONE RGB(192,192,192)
  1.  
  1. #define CHARS_IN_LINE 14 //提示信息的一行有多少个字符
  1. #define SCORE "SCORE %4d" //得分格式化

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }接下来定义一些全局变量:

  1. //global variables definition
  1. char score_char[CHARS_IN_LINE] = {0}; //声明一个接收得分情况的字符数组
  1. char* press_enter = "Press Enter Key...";
  1. char* help[] = //帮助提示信息
  1. {
  1. "Press space or up key to transform shape.",
  1. "Press left or right key to move shape.",
  1. "Press down key to speed up.",
  1. "Press enter key to pause game.",
  1. "Enjoy it. :-)",
  1. 0
  1. };

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

接下来把游戏状态定义为一个枚举类型,便于对其进行操作,具体实现如下:

  1. //enum the state of game
  1. enum game_state
  1. {
  1. game_start,
  1. game_run,
  1. game_pause,
  1. game_over
  1. }state = game_start;

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

之前我们把游戏的7种方块定义成了一个枚举类型,那么我们会通过这些枚举变量的相应的值来判断目前是哪种方块,同时我们要为这些方块涂上不同的颜色,所以我们最好也把这些颜色定义为一个数组,这样到时候要为某个方块涂上对应颜色的时候也只需要根据当前方块的枚举变量值做为数组的下标值来确定相应的颜色方案,所以下面来定义颜色数组:

  1. //color of Rectangle
  1. COLORREF shape_color[] =
  1. {
  1. RGB(255,0,0),
  1. RGB(0,255,0),
  1. RGB(0,0,255),
  1. RGB(255,255,0),
  1. RGB(0,255,255),
  1. RGB(255,0,255),
  1. RGB(255,255,255)
  1. };

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

接下来定义表示7种方形相对坐标的三维数组,具体实现如下:

  1. //SEVEN SHAPE OF RECTANGLE
  1. int shape_coordinate[SHAPE_COUNT][BLOCK_COUNT][2] =
  1. {
  1. {{0,-1},{0,0},{-1,0},{-1,1}},
  1. {{0,-1},{0,0},{1,0},{1,1}},
  1. {{0,-1},{0,0},{0,1},{0,2}},
  1. {{-1,0},{0,0},{1,0},{0,1}},
  1. {{0,0},{1,0},{0,1},{1,1}},
  1. {{-1,-1},{0,-1},{0,0},{0,1}},
  1. {{1,-1},{0,-1},{0,0},{0,1}}
  1. };

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

接下来还需要定义一些辅助变量如记录得分的变量score,如当前方块的最左边的坐标位置等以及相应的绘图变量,具体定义和说明如下:

  1. int score = 0; //得分
  1.  
  1. //shape next = 0;
  1. //shape current = 0;
  1. shape next = ZShape; //下一个方块
  1. shape current = ZShape; //当前方块
  1.  
  1. int current_coordinate[4][2] = {0}; //当前方块的每一部分的坐标,初始化为0
  1. int table[ROWS][COLUMS] = {0}; //游戏桌面,初始化为全0,表示桌面上还没有方块
  1. int shapex = 0; //当前方块的x坐标
  1. int shapey = 0; //当前方块的y坐标
  1. int speed = 0; //方块下移的速度
  1. clock_t start = 0; //每一帧的开始时间
  1. clock_t finish = 0; //每一帧的结束时间
  1.  
  1. //windows paint function
  1. HWND gameWND; //window窗口句柄
  1. HBITMAP memBM; //内存位图
  1. HBITMAP memBMOld; //内存原始位图
  1. HDC memDC; //内存DC
  1. RECT clientRC; //客户区矩形区域
  1. HBRUSH blackBrush; //黑色画笔
  1. HBRUSH stoneBrush; //深灰色画笔
  1. HBRUSH shapeBrush[SHAPE_COUNT]; //方块画笔,7种方块,每种一个
  1. HPEN grayPen; //灰色画笔
  1. HFONT bigFont; //大字体,用来显示游戏名称及"GAME OVER"
  1. HFONT smallFont; //小字体用来显示帮助信息等

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

目前,该声明的变量都已经声明完毕, 接下来就开始根据相应的功能实现其处理函数

1.取最大坐标

函数名称:maxX

函数功能:取得当前方块的最大x坐标。具体实现如下:

  1. //main functions
  1. //对比当前方块的BLOCK_COUNT个小块
  1. //选择它们最大的x坐标
  1. int maxX()
  1. {
  1. int i =0;
  1. int x = current_coordinate[i][0];
  1. int m = x;
  1. for(i = 1; i< BLOCK_COUNT; i++)
  1. {
  1. x = current_coordinate[i][0];
  1. if(m < x)
  1. {
  1. m = x;
  1. }
  1. }
  1. return m;
  1. }

2.取最小坐标

函数名称:minX

函数功能:取得当前方块的最小x坐标。具体实现如下:

  1. int minX()
  1. {
  1. int i = 0;
  1. int x = current_coordinate[i][0];
  1. int m = x;
  1. for(i = 1;i<BLOCK_COUNT;i++)
  1. {
  1. x = current_coordinate[i][0];
  1. if(m > x)
  1. {
  1. m = x;
  1. }
  1. }
  1. return m;
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }3.逆时针旋转方块

函数名称:turn_left

函数功能:将当前方块逆时针旋转90度。具体实现如下:

  1. //逆时针旋转90度
  1. //旋转公式 x' = y; y' = -x
  1. void turn_left()
  1. {
  1. int i = 0;
  1. int x, y;
  1. for(i = 0; i < 4; i++)
  1. {
  1. x = current_coordinate[i][0];
  1. y = current_coordinate[i][1];
  1. current_coordinate[i][0] = y;
  1. current_coordinate[i][1] = -x;
  1. }
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

4.顺时针旋转方块

函数名称:turn_right

函数功能:将当前方块顺时针旋转90度。旋转公式:x' = –y , y' = x; 具体实现如下:

  1. //顺时针旋转
  1. void turn_right()
  1. {
  1. int i = 0;
  1. int x, y;
  1. for(i = 0; i< 4; i++)
  1. {
  1. x = current_coordinate[i][0];
  1. y = current_coordinate[i][1];
  1. current_coordinate[i][0] = -y;
  1. current_coordinate[i][1] = x;
  1. }
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

5.检测方块是否越界

函数名称:out_of_table

函数功能:检查当前方块是否超出桌面范围。具体实现如下:

  1. //检测是否越界
  1. int out_of_table()
  1. {
  1. int i = 0;
  1. int x , y;
  1. for(i = 0; i < 4; i++)
  1. {
  1. //shapex,shapey的值在生成一个新的方块时设定,它们是方块生成时最左边的初始边界坐标点值
  1. //current_coordinate[i][]值是相对应shapex,shapey的偏移值
  1. //所以直接检测shapex + current_coordiante[][]值就是其目前在游戏桌面上的坐标值
  1. x = shapex + current_coordinate[i][0];
  1. y = shapey + current_coordinate[i][1];
  1. //如果x值为负值,则表示游戏方块已经超出了游戏桌面的最左边的表示边界
  1. //如果x值大于COLUMS-1表示超出了游戏桌面的最右边表示边界
  1. //如果y值大于游戏桌面最下面的表示边界,则表示超出了游戏边界
  1. if(x < 0 || x > (COLUMS-1)|| y > (ROWS - 1))
  1. {
  1. return 1;
  1. }
  1. //table[ROWS][COLUMS]表示为table[y][x]本身是初始化为全0的,如果由值不是0 表示
  1. //当前方形已经运行到某个方形上面了,则也表示越界
  1. if(table[y][x])
  1. {
  1. return 1;
  1. }
  1. }
  1. //方形没有越界则返回0,否则返回1
  1. return 0;
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

6.旋转方块

函数名称:transform

函数功能:旋转当前方块。具体实现如下:

  1. //旋转当前方块
  1. void transform()
  1. {
  1. //如果是田字形的方块则不需要旋转变化
  1. if(current == SquareShape)
  1. {
  1. return ;
  1. }
  1. //默认顺时针旋转
  1. turn_right();
  1. //如果顺时针旋转出现越界情况则
  1. //进行逆时针旋转
  1. if(out_of_table())
  1. {
  1. turn_left();
  1. }
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }7.判断方块能否向左移动

函数名称:leftable

函数功能:判断当前方块能否向左移动,能移动则返回1,否则返回0.具体实现如下:

  1. //判断能否向左移动
  1. int leftable()
  1. {
  1. int i = 0;
  1. int x , y;
  1. for(i = 0; i < 4; i++)
  1. {
  1. //shapex,shapey 是初始化生成方形时最左边的x,y坐标值
  1. //current_coordinate是方形的相对偏移位置
  1. //x,y为目前方形中某个方块的实际坐标值
  1. x = shapex + current_coordinate[i][0];
  1. y = shapey + current_coordinate[i][1];
  1. //如果发生越界则返回0,否则返回1
  1. //x <= 0 判断x是否越过左边界
  1. //判断table[y][x-1] == 1,表示以当前坐标值x,y所在点为基准
  1. //向左探测一小方块,看这个小方块是否被已有的方形占用了
  1. //如果占用了则它的值为1,否则为0,如果为1,则表示目前方形已经
  1. //落到了另一个已经存在的方形上了,发生了重叠,那这也是越界了
  1. if(x <= 0 || table[y][x-1] == 1)
  1. {
  1. return 0;
  1. }
  1. }
  1. return 1;
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }8.判断方块能否向右移动

函数名称:rightable

函数功能:判断当前方块能否向右移动,能移动则返回1,否则返回0.具体实现如下:

  1. //判断能否向右移动
  1. int rightable()
  1. {
  1. int i = 0;
  1. int x, y ;
  1. for(i = 0; i < 4; i++)
  1. {
  1. x = shapex + current_coordinate[i][0];
  1. y = shapey + current_coordinate[i][1];
  1. //x>=(COLUMS -1)判断是否越过右边界
  1. //判断table[y][x+1] 表示以当前坐标值x,y所在点为基准
  1. //向右探测一小方块,看这个小方块是否被已有的方形占用了
  1. //如果占用了则它的值为1,否则为0,如果为1,则表示目前方形已经
  1. //落到了另一个已经存在的方形上了,发生了重叠,那这也是越界了
  1. if(x >= (COLUMS -1) || table[y][x+1] == 1)
  1. {
  1. return 0;
  1. }
  1. }
  1. return 1;
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

9.判断方块能否向下移动

函数名称:downable

函数功能:判断当前方块能否向下移动,能移动则返回1,否则返回0.具体实现如下:

  1. int downable()
  1. {
  1. int i = 0;
  1. int x , y;
  1. for(i = 0; i<4;i++)
  1. {
  1. x = shapex + current_coordinate[i][0];
  1. y = shapey + current_coordinate[i][1];
  1. if(y >= (ROWS -1) || table[y+1][x] == 1)
  1. {
  1. return 0;
  1. }
  1. }
  1. return 1;
  1. }

10.向左或右移动当前方块

函数名称:move_left /move_right

函数功能:向左/右移动当前方块。具体实现如下:

  1. void move_left()
  1. {
  1. if(leftable())
  1. {
  1. //shapex是方块最左边的坐标位置点x的值,--表示
  1. //当前方形整体左移一个单位
  1. shapex--;
  1. }
  1. }
  1.  
  1. void move_right()
  1. {
  1. if(rightable())
  1. {
  1. //shapex是方块最左边的坐标位置点x的值,++表示
  1. //当前方形整体右移一个单位
  1. shapex++;
  1. }
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }11.向下移动当前方块

函数名称:move_down

函数功能:向下移动当前方块。具体实现如下:

  1. void move_down()
  1. {
  1. //如果可以向下移动,则
  1. //shapey++,表示当前方形整体下移一个单位
  1. if(downable())
  1. {
  1. shapey++;
  1. }else{ //如果不能往下移动则继续判断
  1. //如果是可以添加到当前游戏桌面上,则自己添加
  1. if(add_to_table())
  1. { //添加完毕之后调用remove_full()函数来检测是否
  1. //有满行的,如果有则清除掉
  1. //然后继续生成下一个方形,将其作为当前方形
  1. remove_full();
  1. next_shape();
  1. }else{
  1. //如果既不能往下移动,也不能把他添加到游戏桌面上
  1. //则表示越界,那就结束游戏
  1. state = game_over;
  1. }
  1. }
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }12.将当前方块固定到桌面上

函数名称:add_to_table

函数功能:将当前方块固定到桌面上,若返回0,则表示游戏结束。具体实现如下:

  1. //将当前方块添加到桌面上,若返回0则表示游戏结束,否则添加成功
  1. int add_to_table()
  1. {
  1. int i = 0;
  1. int x ,y;
  1. for(i = 0; i< 4; i++)
  1. {
  1. //获取当前方形其中的小方块的具体坐标点值
  1. x = shapex + current_coordinate[i][0];
  1. y = shapey + current_coordinate[i][1];
  1. //判断是否越界或已经有其它方块存在,如果没有则置1表示添加成功
  1. if(y < 0 || table[y][x] == 1)
  1. {
  1. return 0;
  1. }
  1. table[y][x] = 1;
  1. }
  1.  
  1. return 1;
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }13.删除填满的行

函数名称:remove_full

函数功能:删除桌面上填满的行。具体实现如下:

  1. //删除桌面上填满的行
  1. void remove_full()
  1. {
  1. int c = 0;
  1. int i,j;
  1. //首先定位到最底下那一行
  1. for(i = ROWS -1; i>0;i--)
  1. {
  1. c = 0;
  1. //然后将当前行的所有值相加
  1. for(j = 0; j < COLUMS;j++)
  1. {
  1. c += table[i][j];
  1. }
  1. //如果所有值相加的结果等于列数目,则表示当前行是满行
  1. if(c == COLUMS)
  1. {
  1. //void *memmove( void* dest, void* src,count );
  1. //memmove是从src所指内存区域复制count个字节到dest所指内存区域
  1. //memmove有个特性,如果目标区域dest和源区域src有重叠的话,memmove
  1. //能够保证源串在被覆盖之前将重叠区域的字节拷贝到目标区域中
  1. //这里我们正是利用了memmove的这个特性完成满行的删除工具和删除后其上面的未满行
  1. //向下移动的工作
  1. memmove(table[1],table[0],sizeof(int)*COLUMS*i);
  1. //将table[0]清空
  1. memset(table[0],0,sizeof(int)*COLUMS);
  1. score++;//分数加1
  1. speed = (score /100)%MAX_SPEED; //变速
  1. i++;
  1. }
  1. else if(c == 0)
  1. {
  1. break;
  1. }
  1. }
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

删除满行中利用了<windows.h>中的库函数memmove保证叠加区域复制正确的特性,例如

#define ROWS 5

#define COLUMS 2

int table[ROWS][COLUMS] = {

{0,0},

{1,1},

{0,1},

{1,1},

{1,0}

};

利用memmove(table[1],table[0],sizeof(int)*COLUMS*i)它完成的功能是,是从table[0]地址处开始复制COLUMS*i个int字节个数据将其放到以table[1]地址开始的地方,具体操作如图:

memset(table[0],0,sizeof(int)*COLUMS); 即把table[0]那一行清空,因为它是最上面那一行,所以只要有满行被删除,那么最上面那一行永远都应该是空的。

14.创建新游戏

函数名称:new_game

函数功能:创建一个新游戏。具体实现如下:

  1. //创建新游戏
  1. void new_game()
  1. {
  1. //将桌面表格全部置零,清空桌面上残余的方形
  1. memset(table,0,sizeof(int)*COLUMS*ROWS);
  1. start = clock(); //初始化时钟
  1. next = (shape)random(SHAPE_COUNT); //初始化下一个方形
  1. score = 0; //得分初始化为0
  1. speed = 0; //速度初始化为0
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }15.运行游戏

函数名称:run_game

函数功能:运行游戏。具体实现如下:

  1. //运行游戏
  1. void run_game()
  1. {
  1. finish = clock();
  1. if((finish - start) > (MAX_SPEED-speed)*100)//设定方形跳动的时间间隔
  1. {
  1. move_down(); //向下移动
  1. start = clock();//重新记录下一次跳动的开始时间
  1. InvalidateRect(gameWND,NULL,TRUE);//刷新整个客户区,重新跳动之后的图形
  1. }
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }16.操作当前方块

函数名称:next_shape

函数功能:将下一个方形设为当前的方块,并随机生成下一个方块。具体实现如下:

  1. void next_shape()
  1. {
  1. current = next;
  1. //将下一个方形设置为当前方形
  1. memcpy(current_coordinate,shape_coordinate[next],sizeof(int)*BLOCK_COUNT*2);
  1. //shapex的值为方块放在游戏桌面正中央位置时其最左边的x值
  1. //shapey值为0
  1. shapex = (COLUMS - ((maxX()-minX())))/2;
  1. shapey = 0;
  1. //随机生成下一个方块
  1. next = (shape)random(SHAPE_COUNT);
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }17.取随机数

函数名称:random

函数功能:取得一个随机数,例如,random(7)将返回一个0-6之间的随机数。具体实现如下:

  1. int random(int seed)
  1. {
  1. if( 0 == seed)
  1. {
  1. return 0;
  1. }
  1. srand((unsigned)time(NULL));//以当前时间作为srand的seed
  1. //srand()函数就是给rand()提供种子seed。如果srand()中的形参每次都是同一个值
  1. //那么每次运行rand()产生的随机数也是一样的, 所以为了rand()每次产生不一样的随机数
  1. //通常把当前时间作为srand()的参数,这样就可以得到不一样的srand()参数,从而rand()也
  1. //就产生不一样的种子
  1. return (rand() % seed);
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }18.绘图

函数名称:paint

函数功能:将内存位图输出到窗口上。具体实现如下:

  1. //paint func
  1. void paint()
  1. {
  1. PAINTSTRUCT ps;
  1. HDC hdc;
  1. draw_table();
  1. /*
  1. *BeginPaint函数为指定窗口做绘图工作前的相关准备工作,将绘图有关的信息填充到
  1. *一个PAINTSTRUCT结构中。原型如下:
  1. * HDC BeginPaint(
  1. * HWND hwnd, //[输入]被重绘的窗口句柄
  1. * LPPAINTSTRUCT lpPaint //[输出]指向一个用来接收绘图信息的PAINTSTRUCT结构
  1. *
  1. * 返回值: 如果函数成功,则返回值是指定窗口的"显示设备描述表"句柄。
  1. * 否则返回值为NULL,表明没有得到显示设备的内容。
  1. * [注]
  1. * BeginPaint函数自动设置显示设备内容的剪切区域,而排除任何更新区域外的区域。该
  1. * 更新区域可以通过 InvalidateRect InvalidateRgn 函数来设置,也可以是系统在改
  1. * 变大小,移动,创建,滚动后设置的。如果更新区域被标记为可擦除的,BeginPaint将发
  1. * 生一个WM_ERASEBKGND消息给窗口。如果窗口类有一个自己的背景刷,那么BeginPaint
  1. * 使用这个刷子来擦除更新区域的背景。
  1. * BeginPaintEndPaint只能配对使用,不能单独使用,BeginPaint返回一个用来绘图的客
  1. * 户区的显示设备内容的HANDLE, EndPaint则终止绘画请求,并释放设备内容。
  1. * 需要注意的是BeginPaint EndPaint只能在响应WM_PAINT消息时使用,而且只能调用一次
  1. *
  1. * 如果被绘画的客户区中有一个caretcaret:插入符。是窗口客户区中的一个闪烁的线,块,
  1. * 或位图。插入符通常表示文本或图形将被插入的地方。即一闪一闪的光标),BeginPaint
  1. * 自动隐藏该符号,而保证它不被擦除。
  1. */
  1. hdc = BeginPaint(gameWND,&ps);
  1. //BOOL BitBlt(HDC hdcDest, int nXDest,int nYDest, int nWidth,int nHeight, HDC hdcSrc
  1. // ,int nXSrc, int nYSrc,DWORD dwRop); 如果dwRop 是SRCCOPY 则表示将源hdcSrc
  1. //内存位图原样拷贝到目标hdcDest处。
  1. BitBlt(hdc,clientRC.left,clientRC.top,clientRC.right,clientRC.bottom,memDC,0,0,SRCCOPY);
  1. EndPaint(gameWND,&ps);
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }19.绘制游戏桌面

函数名称:draw_table

函数功能:绘制游戏桌面

处理流程:首先用黑色矩形填充桌面背景区,接着判断游戏的状态,如果是开始状态,用黄色字显示游戏开始画面,如果是结束状态,用红色字显示 GAME OVER ; 如果是游戏运行状态,则依次绘制游戏桌面,当前方块,下一个方块,得分和游戏帮助等。具体实现如下:

  1. //绘制游戏桌面
  1. void draw_table()
  1. {
  1. HBRUSH hBrushOld;
  1. HPEN hPenOld;
  1. HFONT hFontOld;
  1. RECT rc;
  1. int x0,y0,w;
  1. int x,y,i,j;
  1. char* str;
  1.  
  1. w = clientRC.bottom /(ROWS + 2); //设置一个方块的宽度
  1. x0 = y0 = w;
  1. //用黑色矩形填充桌面背景区
  1. FillRect(memDC,&clientRC,blackBrush);
  1. if(state == game_start || state == game_over)
  1. {
  1. // 绘制游戏开始或结束的标题界面
  1. memcpy(&rc,&clientRC,sizeof(RECT));
  1. rc.bottom = rc.bottom / 2;
  1. hFontOld = (HFONT)SelectObject(memDC,bigFont);
  1. SetBkColor(memDC,BLACK);
  1. //如果游戏是开始状态,则用黄色字显示游戏开始界面
  1. if(state == game_start)
  1. {
  1. str = APP_TITLE;
  1. SetTextColor(memDC,YELLOW);
  1. }else{ //如果游戏是结束状态,则用红色字显示 GAME OVER
  1. str = GAMEOVER;
  1. SetTextColor(memDC,RED);
  1. }
  1.  
  1. DrawText(memDC,str,strlen(str),&rc,DT_SINGLELINE | DT_CENTER| DT_BOTTOM);
  1. SelectObject(memDC,hFontOld);
  1.  
  1. //提示信息
  1. hFontOld = (HFONT)SelectObject(memDC,smallFont);
  1. rc.top = rc.bottom;
  1. rc.bottom = rc.bottom*2;
  1. if(state == game_over)
  1. {
  1. SetTextColor(memDC,YELLOW);
  1. sprintf(score_char,SCORE,score);
  1. DrawText(memDC,score_char,strlen(score_char),&rc,DT_SINGLELINE|DT_CENTER|DT_TOP);
  1. }
  1.  
  1. SetTextColor(memDC,STONE);
  1. DrawText(memDC,press_enter,strlen(press_enter),&rc,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
  1. SelectObject(memDC,hFontOld);
  1.  
  1. return;
  1. }
  1.  
  1. //画桌面上残留的方块
  1. hBrushOld = (HBRUSH)SelectObject(memDC,stoneBrush);
  1. for(i = 0; i < ROWS; i++)
  1. {
  1. for(j = 0; j < COLUMS; j++)
  1. {
  1. if(table[i][j] == 1)
  1. {
  1. x = x0 + j*w;
  1. y = y0 + i*w;
  1. Rectangle(memDC,x,y,x+w+1,y+w+1);
  1. }
  1. }
  1. }
  1.  
  1. SelectObject(memDC,hBrushOld);
  1.  
  1. //画当前的方块
  1. hBrushOld = (HBRUSH)SelectObject(memDC,shapeBrush[current]);
  1. for(i = 0; i < 4; i++)
  1. { //取得当前方形中某一单元在桌面上的实际坐标值
  1. x = x0 + (current_coordinate[i][0] + shapex)*w;
  1. y = y0 + (current_coordinate[i][1] + shapey)*w;
  1. if(x < x0 || y < y0)
  1. {
  1. continue;
  1. }
  1. //绘制实心矩形
  1. Rectangle(memDC,x,y,x+w+1,y+w+1);
  1. }
  1. SelectObject(memDC,hBrushOld);
  1. //画桌面上的表格线条
  1. hPenOld = (HPEN)SelectObject(memDC,grayPen);
  1. for(i = 0; i<= ROWS; i++)
  1. {
  1. /*MoveToEx(memDC,x0+i*w,y0,NULL);*/
  1. MoveToEx(memDC,x0,y0+i*w,NULL);
  1. LineTo(memDC,x0+COLUMS*w,y0+i*w);
  1. }
  1. for(i = 0; i <= COLUMS; i++)
  1. {
  1. MoveToEx(memDC,x0+i*w,y0,NULL);
  1. LineTo(memDC,x0+i*w,y0+ROWS*w);
  1. }
  1. SelectObject(memDC,hPenOld);
  1.  
  1. //画玩家得分
  1. x0= x0+COLUMS*w +3*w;//设置得分字样在桌面上的偏移位置
  1. y0=y0+w;
  1. hFontOld = (HFONT)SelectObject(memDC,smallFont);//选择字体
  1. SetTextColor(memDC,YELLOW);//设置字体颜色
  1. sprintf(score_char,SCORE,score);
  1. /*sprintf(score_char,SCORE,shapex);*/
  1. /*sprintf(score_char,SCORE,table[29][0]);*/
  1. TextOut(memDC,x0,y0,score_char,strlen(score_char));//绘制得分
  1. //画下一个方块
  1. y0 += w;
  1. SetTextColor(memDC,STONE);
  1. TextOut(memDC,x0,y0,"NEXT",4);
  1. x0 = x0 + w;
  1. y0 += 2*w;
  1. hBrushOld = (HBRUSH)SelectObject(memDC,shapeBrush[next]);
  1. for(i = 0; i < 4; i++)
  1. {
  1. x = x0 + shape_coordinate[next][i][0]*w;
  1. y = y0 + shape_coordinate[next][i][1]*w;
  1. Rectangle(memDC,x,y,x+w+1,y+w+1);
  1. }
  1.  
  1. SelectObject(memDC,hBrushOld);
  1. //打印帮助信息
  1. x0 = (COLUMS + 2)*w;
  1. y0 += 4*w;
  1. SetTextColor(memDC,GRAY);
  1. i = 0;
  1. while(help[i])
  1. {
  1. TextOut(memDC,x0,y0,help[i],strlen(help[i]));
  1. y0 += w;
  1. i++;
  1. }
  1. SelectObject(memDC,hFontOld);
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

20.处理按键

函数名称:key_down

函数功能:处理键盘按下事件。

处理流程:1.如果游戏状态不是运行状态,按回车键是进行暂停/开始游戏的切换键。2.游戏运行状态,按向上键或空格键旋转当前方块,按向左键左移当前方块,按向右键右移当前方块,按向下键下移当前方块。按回车键,来回切换暂停/开始游戏。具体实现如下:

  1. //按键事件处理
  1. void key_down(WPARAM wParam)
  1. {
  1. //如果游戏状态处理非运行状态,按下回车键,则开始游戏
  1. if(state != game_run)
  1. {
  1. if(wParam == VK_RETURN)
  1. {
  1. switch(state)
  1. {
  1. case game_start:
  1. next_shape();
  1. state = game_run;
  1. break;
  1. case game_pause:
  1. state = game_run;
  1. break;
  1. case game_over:
  1. new_game();
  1. next_shape();
  1. state = game_run;
  1. break;
  1. }
  1. }
  1. }
  1. else //在游戏运行状态下,响应键盘事件
  1. {
  1. switch(wParam)
  1. {
  1. case VK_SPACE: //空格键/向上键则旋转当前方块
  1. case VK_UP:
  1. transform();
  1. break;
  1. case VK_LEFT: //左向键左移当前方块
  1. move_left();
  1. break;
  1. case VK_RIGHT: //右向键右移当前方块
  1. move_right();
  1. break;
  1. case VK_DOWN: //下向键下移当前方块
  1. move_down();
  1. break;
  1. case VK_RETURN://回车键暂停当前游戏
  1. state = game_pause;
  1. break;
  1. }
  1. }
  1. //重绘客户区
  1. InvalidateRect(gameWND,NULL,TRUE);
  1. }

21.改变窗口大小

函数名称:resize

函数功能:改变窗口大小时调用的函数。具体实现如下:

  1. void resize()
  1. {
  1. HDC hdc;
  1. LOGFONT lf;
  1. hdc = GetDC(gameWND);
  1. GetClientRect(gameWND,&clientRC);
  1. SelectObject(memDC,memBMOld);
  1. DeleteObject(memBM);
  1. //重新创建合适的内存位图
  1. memBM = CreateCompatibleBitmap(hdc,clientRC.right,clientRC.bottom);
  1. memBMOld = (HBITMAP)SelectObject(memDC,memBM);
  1. //重新创建合适的大字体和小字体
  1. DeleteObject(bigFont);
  1. memset(&lf,0,sizeof(LOGFONT));
  1. lf.lfWidth = (clientRC.right - clientRC.left) / CHARS_IN_LINE;
  1. lf.lfHeight = (clientRC.bottom - clientRC.top) /4;
  1. lf.lfItalic = 1;
  1. lf.lfWeight = FW_BOLD;
  1. bigFont = CreateFontIndirect(&lf);
  1.  
  1. DeleteObject(smallFont);
  1. lf.lfHeight = clientRC.bottom / (ROWS + 2);
  1. lf.lfWidth = lf.lfHeight / 2;
  1. lf.lfItalic = 0;
  1. lf.lfWeight = FW_NORMAL;
  1. smallFont = CreateFontIndirect(&lf);
  1.  
  1. ReleaseDC(gameWND, hdc);
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }22.处理消息

函数名称:WndProc

函数功能:回调函数,用来处理Windows消息。具体实现如下:

  1. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  1. {
  1. switch(message)
  1. {
  1. case WM_SIZE: //响应改变窗口大小的消息
  1. resize();
  1. return 0;
  1. case WM_ERASEBKGND: //响应重绘背景的消息
  1. return 0;
  1. case WM_PAINT: //相应绘制消息
  1. paint();
  1. return 0;
  1. case WM_KEYDOWN: //相应按键消息
  1. key_down(wParam);
  1. return 0;
  1. case WM_DESTROY: //响应销毁窗口的消息
  1. PostQuitMessage(0);
  1. return 0;
  1. }
  1. //其它消息用Windows默认的消息处理函数处理
  1. return DefWindowProc(hwnd, message,wParam,lParam);
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }23.初始化

函数名称:initialize

函数功能:初始化内存位图,画笔,字体等资源。具体实现如下:

  1. void initialize()
  1. {
  1. LOGFONT lf;
  1. HDC hdc;
  1. int i;
  1.  
  1. hdc = GetDC(gameWND);
  1. GetClientRect(gameWND,&clientRC); //取得窗口客户区大小
  1. memDC = CreateCompatibleDC(hdc); //创建内存DC
  1. //创建内存位图
  1. memBM = CreateCompatibleBitmap(hdc, clientRC.right,clientRC.bottom);
  1. //将内存位图选入到内存DC中
  1. memBMOld = (HBITMAP)SelectObject(memDC,memBM);
  1. //创建黑色画笔和深灰色画笔
  1. blackBrush = CreateSolidBrush(BLACK);
  1. stoneBrush = CreateSolidBrush(STONE);
  1. //创建每个方块所对应颜色的画笔
  1. for(i = 0; i < SHAPE_COUNT;i++)
  1. {
  1. shapeBrush[i] = CreateSolidBrush(shape_color[i]);
  1. }
  1. grayPen = CreatePen(PS_SOLID,1,GRAY);//创建灰色画笔
  1. memset(&lf,0,sizeof(LOGFONT));
  1. //创建大字体
  1. lf.lfWidth = (clientRC.right - clientRC.left) / CHARS_IN_LINE;
  1. lf.lfHeight = (clientRC.bottom - clientRC.top) /4;
  1. lf.lfItalic = 1;
  1. lf.lfWeight = FW_BOLD;
  1. bigFont = CreateFontIndirect(&lf);
  1. //创建小字体
  1. lf.lfHeight = clientRC.bottom / (ROWS + 2);
  1. lf.lfWidth = lf.lfHeight / 2;
  1. lf.lfItalic = 0;
  1. lf.lfWeight = FW_NORMAL;
  1. smallFont = CreateFontIndirect(&lf);
  1. ReleaseDC(gameWND,hdc);
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }24.释放资源

函数名称:finalize

函数功能:游戏结束时调用该函数释放initialize中创建的资源。具体实现如下:

  1. void finalize()
  1. {
  1. int i = 0;
  1. DeleteObject(blackBrush);
  1. DeleteObject(stoneBrush);
  1. for(i = 0; i < SHAPE_COUNT; i++)
  1. {
  1. DeleteObject(shapeBrush[i]);
  1. }
  1.  
  1. DeleteObject(grayPen);
  1. DeleteObject(bigFont);
  1. DeleteObject(smallFont);
  1. SelectObject(memDC,memBMOld);
  1. DeleteObject(memBM);
  1. DeleteDC(memDC);
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }25.系统入口函数

函数名称:WinMain

函数功能:Windows程序入口,类似于DOS程序的main函数。具体实现如下:

  1. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance,PSTR szCmdLine,int iCmdShow)
  1. {
  1. MSG msg; //声明一个消息结构体变量
  1. WNDCLASS wndclass; //声明一个窗口类变量
  1.  
  1. //初始化窗口信息
  1. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  1. wndclass.lpfnWndProc = WndProc;
  1. wndclass.cbClsExtra = wndclass.cbWndExtra = 0;
  1. wndclass.hInstance = hInstance;
  1. wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
  1. wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
  1. wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  1. wndclass.lpszMenuName = NULL;
  1. wndclass.lpszClassName = APP_NAME;
  1. //注册窗口类
  1. RegisterClass(&wndclass);
  1. //创建窗口
  1. gameWND = CreateWindow(APP_NAME,
  1. APP_TITLE,
  1. WS_OVERLAPPEDWINDOW,
  1. CW_USEDEFAULT,
  1. CW_USEDEFAULT,
  1. CW_USEDEFAULT,
  1. CW_USEDEFAULT,
  1. NULL,NULL,
  1. hInstance,NULL);
  1. //初始化游戏基本资源
  1. initialize();
  1. //显示窗口
  1. ShowWindow(gameWND, iCmdShow);
  1. UpdateWindow(gameWND); //刷新窗口
  1. //创建游戏
  1. new_game();
  1. for(;;)//进入消息循环
  1. {
  1. if(state == game_run)
  1. {
  1. run_game();
  1. }
  1.  
  1. if(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
  1. {
  1. if(GetMessage(&msg,NULL,0,0))
  1. {
  1. TranslateMessage(&msg);
  1. DispatchMessage(&msg);
  1. }else{
  1. break;
  1. }
  1. }
  1. }
  1.  
  1. finalize();
  1.  
  1. return msg.wParam;
  1. }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }至此游戏的基本功能已经实现。

5.系统操作过程

F5游戏运行后,首先进入欢迎主界面,如图:

在欢迎主界面中按任意键进入游戏,游戏界面如图:

游戏结束界面如图:

6.总结与Bug记录

Bug.1

//画桌面上的表格线条

    hPenOld = (HPEN)SelectObject(memDC,grayPen);

    for(i = 0; i<= ROWS; i++)

    {

        /*MoveToEx(memDC,x0+i*w,y0,NULL);*/

        MoveToEx(memDC,x0,y0+i*w,NULL);

        LineTo(memDC,x0+COLUMS*w,y0+i*w);

    }

Bug.2

显示出了SCORE字样但是没有显示得分情况,

将#define SCORE "SCORE  %4"改成:#define SCORE "SCORE  %4d"

Bug.3

方块旋转有问题

void turn_right()

{

    int i = 0;

    int x, y;

    for(i = 0; i< 4; i++)

    {

        x = current_coordinate[i][0];

        y = current_coordinate[i][1];

        current_coordinate[i][0] = -y; //没有写

        current_coordinate[i][1] = x;

    }

}

C项目实践--俄罗斯方块(2)的更多相关文章

  1. C项目实践--俄罗斯方块(1)

    俄罗斯方块游戏是由前苏联科学院计算机中心的工程师阿列克谢.帕基特诺夫发明的一款小游戏. 1.功能需求分析 1.1主要功能 实现三个功能:1.游戏欢迎界面:2.游戏执行功能,包括计算得分:3.游戏结束界 ...

  2. Hangfire项目实践分享

    Hangfire项目实践分享 目录 Hangfire项目实践分享 目录 什么是Hangfire Hangfire基础 基于队列的任务处理(Fire-and-forget jobs) 延迟任务执行(De ...

  3. Windows on Device 项目实践 3 - 火焰报警器制作

    在前两篇<Windows on Device 项目实践 1 - PWM调光灯制作>和<Windows on Device 项目实践 2 - 感光灯制作>中,我们学习了如何利用I ...

  4. Windows on Device 项目实践 2 - 感光灯制作

    在上一篇<Windows on Device 项目实践 1 - PWM调光灯制作>中,我们学习了如何利用Intel Galileo开发板和Windows on Device来设计并完成一个 ...

  5. Windows on Device 项目实践 1 - PWM调光灯制作

    在前一篇文章<Wintel物联网平台-Windows IoT新手入门指南>中,我们讲解了Windows on Device硬件准备和软件开发环境的搭建,以及Hello Blinky项目的演 ...

  6. Hangfire项目实践

    Hangfire项目实践分享 Hangfire项目实践分享 目录 Hangfire项目实践分享 目录 什么是Hangfire Hangfire基础 基于队列的任务处理(Fire-and-forget ...

  7. MVC项目实践,在三层架构下实现SportsStore,从类图看三层架构

    在"MVC项目实践,在三层架构下实现SportsStore-02,DbSession层.BLL层"一文的评论中,博友浪花一朵朵建议用类图来理解本项目的三层架构.于是就有了本篇: I ...

  8. MVC项目实践,在三层架构下实现SportsStore-02,DbSession层、BLL层

    SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...

  9. MVC项目实践,在三层架构下实现SportsStore-01,EF Code First建模、DAL层等

    SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...

随机推荐

  1. 条款8:别让异常逃离析构函数(prevent exception from leaving destructors)

    NOTE: 1.析构函数绝对不要吐出异常.如果一个被析构函数调用的函数可能抛出异常,析构函数应该扑捉任何异常,然后吞下他们(不传播)或结束程序. 2.如果客户需要对某个操作函数运行期间抛出的异常做出反 ...

  2. mysql多字段组合删除重复行

    DELETEFROM boll_paramWHERE id in ( SELECT a.id FROM ( SELECT id FROM boll_param WHERE (symbol, time_ ...

  3. [工具]Visual Studio

    1,Tab键的使用: 如不说有这样的代码:public Member member { get; set; } 当我们编辑完Member后,按一下Tab键,就能够将光标锁定到member上,等待键盘输 ...

  4. 如何在小程序实现图片lazy-load懒加载效果

    自从跳一跳出现之后小程序又开始频繁出现了,在学习过程中发现小程序虽然好但是由于api不完善导致开发过程中有很多的坑,重点是网上相对小程序出现坑时解决方案显然比较少,小程序最让人觉得痛心疾首之一就是无法 ...

  5. 【bzoj1042】[HAOI2008]硬币购物-递推与动规-容斥原理

    硬币购物 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬币,买si的价值的东西.请问每次有多少种付款方法. Input 第一行 c1,c2 ...

  6. C++常见函数(备忘录)

    substr(string的成员函数) 语法: basic_string substr( size_type index, size_type num = npos ); substr()返回本字符串 ...

  7. 视图中使用foreach报错问题

    问题情境:thinkphp3.2版本,使用四层<foreach></foreach>循环变量时,报错以下错误: syntax error, unexpected 'endfor ...

  8. POJ 1860【求解是否存在权值为正的环 屌丝做的第一道权值需要计算的题 想喊一声SPFA万岁】

    题意: 有n种钱币,m个钱币兑换点,小明一开始有第n种钱币数量为w. 每个兑换点可以将两种不同的钱币相互兑换,但是兑换前要先收取一定的费用,然后按照比例兑换. 问小明是否可以经过一系列的兑换之后能够将 ...

  9. Java实验--关于简单字符串回文的递归判断实验

    首先题目要求写的是递归的实验,一开始没注意要求,写了非递归的方法.浪费了一些时间,所谓吃一堑长一智.我学习到了以后看实验的时候要认真看实验中的要求,防止再看错. 以下是对此次的实验进行的分析: 1)递 ...

  10. eclipse发布项目到tomcat部署目录

    1.在eclipse下建立Dynamic Web Project工程zhgy,在使用eclipse中new一个tomcat,通过启动该tomcat来发布Dynamic Web Project的时候,其 ...