首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

卡哥初学API,写个俄罗斯方块大家伙儿看看

2013-04-26 
卡哥初学API,写个俄罗斯方块大家看看本帖最后由 heliang6291 于 2013-04-18 21:30:57 编辑学习API已经有一

卡哥初学API,写个俄罗斯方块大家看看
本帖最后由 heliang6291 于 2013-04-18 21:30:57 编辑 学习API已经有一个月啦,看的是美国佬写的 windows程序设计第五版,那本10多年前的老书。
这次,写完了俄罗斯方块,这个是我平生第一个游戏,所以大牛莫笑啊。
另外,我承认代码不够优美,算法也有点粗糙。发到这里来,纯粹是对我个人来说,来兴奋一下的。


#include <windows.h> //为了使用API函数
#include <time.h>//为了使用定时器
#include <stdlib.h>  //为了使用随机数

#define BLOCKWIDTH20  //单个方块大小
#define NUMLINEBLOCKS18  //行数
#define NUMCOLUMNBLOCKS10  //列数
#define ID_TIMER1//定时器ID
#define BLOCKSTYLES(sizeof (Blocks) / sizeof (Blocks[0]))  //方块的种类数

//游戏区各方格顶点布尔值,代表该方格是否有方块
boolGameClient[NUMCOLUMNBLOCKS][NUMLINEBLOCKS];
static intF, S, cF, cS;//随机方块图形对应的第一、二纬
static intScore;  //得分

//定义各方块形状,以点表示
struct  
{
POINTpt[4];
}
Blocks[][4] = 
{
//正7
0, 0, 1, 0, 1, 1, 1, 2,  2, 0, 0, 1, 1, 1, 2, 1,  0, 0, 0, 1, 0, 2, 1, 2,  0, 0, 1, 0, 2, 0, 0, 1,
//反7
0, 0, 1, 0, 0, 1, 0, 2,  0, 0, 1, 0, 2, 0, 2, 1,  1, 0, 1, 1, 0, 2, 1, 2,  0, 0, 0, 1, 1, 1, 2, 1,
//1
1, 0, 1, 1, 1, 2, 1, 3,  0, 0, 1, 0, 2, 0, 3, 0,  1, 1, 1, 1, 1, 2, 1, 3,  0, 0, 1, 0, 2, 0, 3, 0,
//Z
0, 0, 1, 0, 1, 1, 2, 1,  1, 0, 0, 1, 1, 1, 0, 2,  0, 0, 1, 0, 1, 1, 2, 1,  1, 0, 0, 1, 1, 1, 0, 2,
//反Z
1, 0, 2, 0, 0, 1, 1, 1,  0, 0, 0, 1, 1, 1, 1, 2,  1, 0, 2, 0, 0, 1, 1, 1,  0, 0, 0, 1, 1, 1, 1, 2,
//田字
0, 0, 1, 0, 0, 1, 1, 1,  0, 0, 1, 0, 0, 1, 1, 1,  0, 0, 1, 0, 0, 1, 1, 1,  0, 0, 1, 0, 0, 1, 1, 1,
//尖头
1, 0, 0, 1, 1, 1, 2, 1,  0, 0, 0, 1, 1, 1, 0, 2,  0, 0, 1, 0, 2, 0, 1, 1,  1, 0, 0, 1, 1, 1, 1, 2
};

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wndcls;
TCHAR szClassName[] = TEXT("Terics"),
  szWindowName[] = TEXT("Aka's Terics");
static POINT Block[4];

wndcls.cbClsExtra= 0;
wndcls.cbWndExtra= 0;
wndcls.hbrBackground= static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
wndcls.hCursor= LoadCursor(hInstance, IDC_ARROW);
wndcls.hIcon= LoadIcon(hInstance, IDI_APPLICATION);
wndcls.hInstance= hInstance;
wndcls.lpfnWndProc= WndProc;
wndcls.lpszClassName= szClassName;
wndcls.lpszMenuName= NULL;
wndcls.style= CS_HREDRAW | CS_VREDRAW;


RegisterClass(&wndcls);

HWND hwnd = CreateWindow(szClassName, szWindowName, WS_OVERLAPPED | WS_SYSMENU,
CW_USEDEFAULT, CW_USEDEFAULT, (NUMCOLUMNBLOCKS + 10) * BLOCKWIDTH,
(NUMLINEBLOCKS + 3) * BLOCKWIDTH,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);

MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

//随机数函数定制版,用于随机出现的方块
unsigned Random(int n);

//判断是否可以下落,可以则返回true
bool CanDown(POINT pt[]);

//下落实现
void Down(POINT pt[]);

//判断是否可以左移
bool CanLeft(POINT pt[]);

//实现左移
void Left(POINT pt[]);

//判断是否可以右移
bool CanRight(POINT pt[]);

//实现右移
void Right(POINT pt[]);

//判断是否可以变形
bool CanChange(POINT pt[]);

//实现变形
void Change(POINT pt[]);

void DelSqure(HWND);


[解决办法]
偶也不会。。

热点排行