第一次真正意义上的用VC++实现的一个完整的Win32程序。
//Block.h
//-------------------------------------------------------------------------------------------------
/*
定义每个方块的结构
*/
#if !defined _BLOCK_H_
#define _BLOCK_H_
#define BLOCK_VERSION &H01000000 //主版本号、辅版本、附加版本、附加2
#define BLOCK_SIZE 6 //存储到文件时占用的字节数
#define BLOCK_HEADER_SIZE 8 //存储文件头信息的大小
struct _Block
{//注意:这些字段对于存储文件来说是有先后之分的。
unsigned int ID:8;
unsigned int NextID:8;
int Width:4;
int Height:4;
int OffsetY:4;
int OffsetX:4;
unsigned int Elements; //方块的各位是否为实体
};
typedef _Block BLOCK;
#endif
//-------------------------------------------------------------------------------------------------
//CustomGDI.h
//-------------------------------------------------------------------------------------------------
#pragma once
#include "windows.h"
//#include "d3d9.h"
#define BORDER_STYLE_NONE 0
#define BORDER_STYLE_FLAT 1
//为简单起见,目前只提供FLAT
//#define BORDER_STYLE_FIXED3D 2
//#define BORDER_STYLE_INNER3D 4
#define BORDER_STYLE_DEFAULT BORDER_STYLE_NONE
#define INNER_STYLE_EMPTY 0
#define INNER_STYLE_SOLID 1
#define INNER_STYLE_DEFAULT INNER_STYLE_EMPTY
#define TRANSPARENT_COLOR RGB(0,0,0)
#define BACKGROUND_DEFAULT_COLOR RGB(0,0,0)
typedef int INNER_STYLE;
typedef int BORDER_STYLE;
typedef DWORD GDI_COLOR;
class CCustomGDI
{
public:
//CCustomGDI(void);
CCustomGDI(HWND hWnd);
~CCustomGDI(void);
void TextOut(int x, int y,const char* lpString, int cbString);
void Drawbox(PRECT prect, BORDER_STYLE bstyle=BORDER_STYLE_DEFAULT, INNER_STYLE istyle=INNER_STYLE_DEFAULT);
void Drawbox(int x, int y, int height, int width, BORDER_STYLE bstyle=BORDER_STYLE_DEFAULT, INNER_STYLE istyle=INNER_STYLE_DEFAULT);
void DrawPic(int x, int y, int height, int width, const char* file);
void DrawPic(PRECT prect, const char* file);
void Clear(void);
void Clear(PRECT prect);
void Clear(GDI_COLOR color);
void Clear(PRECT prect, GDI_COLOR color);
void Clear(int x, int y, int height, int width, GDI_COLOR color);
void SetBkColor(GDI_COLOR color); //设置背景色
void SetColor(GDI_COLOR color); //设置前景色
void Set3DColor(GDI_COLOR ul_color, GDI_COLOR rb_color); //设置绘制3D时左上角与右下角的颜色
void SetBorderColor(GDI_COLOR color);
void SetBorderSize(int size);
protected:
//LPDIRECT3D9 m_pD3D;
//LPDIRECT3DDEVICE9 m_pd3dDevice;
//HDC m_hdc;
HWND m_hwnd;
//HBRUSH m_hbrBkgnd;
GDI_COLOR m_background;
int m_bordersize;
GDI_COLOR m_bordercolor;
GDI_COLOR m_color;
};
//-------------------------------------------------------------------------------------------------
//CustomGDI.cpp
//-------------------------------------------------------------------------------------------------
#include "./customgdi.h"
//
//CCustomGDI::CCustomGDI(void)
//{
//}
//
CCustomGDI::CCustomGDI(HWND hWnd)
{
//初始化图形设备
m_hwnd=hWnd;
//m_hdc=GetDC(hWnd);
m_background=BACKGROUND_DEFAULT_COLOR;
m_bordersize=1;
m_bordercolor=RGB(0x80,0x80,0x80); //gray
m_color=RGB(0xFF,0xFF,0xFF); //white
}
CCustomGDI::~CCustomGDI(void)
{
//ReleaseDC(m_hwnd, m_hdc);
}
void CCustomGDI::TextOut(int x, int y,const char* lpString, int cbString)
{
HDC hdc=GetDC(m_hwnd);
::TextOut(hdc,x,y,lpString,cbString);
ReleaseDC(m_hwnd,hdc);
}
void CCustomGDI::Drawbox(PRECT prect, BORDER_STYLE bstyle, INNER_STYLE istyle)
{
HBRUSH hbr;
HDC hdc;
hdc=GetDC(m_hwnd);
RECT rect;
rect.top=prect->top;
rect.bottom=prect->bottom;
rect.left=prect->left;
rect.right=prect->right;
if(bstyle==BORDER_STYLE_FLAT)
{
//如果为BORDER_STYLE_FLAT
hbr=CreateSolidBrush(m_bordercolor);
FrameRect(hdc, &rect, hbr); //这里不知道如何画线,呵呵。
DeleteObject(hbr);
rect.left+=m_bordersize;
rect.right-=m_bordersize;
rect.top+=m_bordersize;
rect.bottom-=m_bordersize;
}
if(istyle==INNER_STYLE_SOLID) {
hbr=CreateSolidBrush(m_color);
FillRect(hdc, &rect, hbr);
DeleteObject(hbr);
}
ReleaseDC(m_hwnd,hdc);
}
void CCustomGDI::Drawbox(int x, int y, int height, int width, BORDER_STYLE bstyle, INNER_STYLE istyle)
{
RECT rect;
rect.top=y;
rect.left=x;
rect.bottom=y+height-1;
rect.right=x+width-1;
Drawbox(&rect,bstyle,istyle);
}
void CCustomGDI::DrawPic(int x, int y, int height, int width, const char* file)
{
}
void CCustomGDI::DrawPic(PRECT prect, const char* file)
{
}
void CCustomGDI::Clear(void)
{
//清除屏幕
RECT rect;
GetClientRect(m_hwnd,&rect);
Clear(&rect, m_background);
}
void CCustomGDI::Clear(PRECT prect)
{
Clear(prect, m_background);
}
void CCustomGDI::Clear(GDI_COLOR color)
{
RECT rect;
GetClientRect(m_hwnd,&rect);
Clear(&rect, color);
}
void CCustomGDI::Clear(PRECT prect, GDI_COLOR color)
{
HDC hdc;
hdc=GetDC(m_hwnd);
HBRUSH hbr=CreateSolidBrush(color);
FillRect(hdc, prect, hbr);
DeleteObject(hbr);
ReleaseDC(m_hwnd,hdc);
}
void CCustomGDI::Clear(int x, int y, int height, int width, GDI_COLOR color)
{
RECT rect;
rect.left=x;
rect.top=y;
rect.right=x+height-1;
rect.bottom=y+width-1;
Clear(&rect,color);
}
void CCustomGDI::SetBkColor(GDI_COLOR color)
{
m_background=color;
}
void CCustomGDI::SetColor(GDI_COLOR color)
{
m_color=color;
}
void CCustomGDI::Set3DColor(GDI_COLOR ul_color, GDI_COLOR rb_color)
{
}
void CCustomGDI::SetBorderSize(int size)
{
}
void CCustomGDI::SetBorderColor(GDI_COLOR color)
{
}
//--------------------------------------------------------------------------------------------------
//Els.h
//-------------------------------------------------------------------------------------------------
#include <windows.h>
//#include <d3d9.h>
#include <stdio.h>
#include <direct.h>
#include <time.h>
#include "Block.h"
#include "CustomGDI.h"
#define PLAYAREA_HEIGHT 24
#define PLAYAREA_WIDTH 12
#define PLAYAREA_EMPTY_COLOR RGB(0,0,0)
#define IDT_PLAY 2005
#define GAME_STATUS_STOPPED 0
#define GAME_STATUS_RUNNING 1
#define GAME_STATUS_PAUSED -1
#define PLAYER_DEFAULT_NAME "<Player>"
#define PLAYER_MAX_LEVEL 12
#define PLAYER_MAX_SCORE 999999999
struct _PLAYER
{
char Name[50];
int Power; //爆了几次
int Score; //得分,最大为999999999
};
struct _ELSSYS
{
int Level; //当前游戏级别
int ClientHeight; //当前客户区域高度
int ClientWidth; //当前客户区域宽度
int PlayAreaX; //游戏区域X坐标(注意:不包含游戏区域外的边框等)
int PlayAreaY; //游戏区域Y坐标(注意:不包含游戏区域外的边框等)
int Single

本文介绍了使用VC++编写的一个完整的Win32俄罗斯方块游戏程序,包括游戏区域的定义、边框样式、颜色处理、方块结构、以及游戏状态的管理。文章详细展示了游戏的核心功能实现,如方块的移动、下落、碰撞检测等,同时也涉及到了简单的图形界面绘制和更新。


被折叠的 条评论
为什么被折叠?



