C++ 俄罗斯方块(windows API)

本文介绍了使用C++和Windows API编写俄罗斯方块游戏的实践过程,包括窗口类注册、消息处理、方块旋转等关键步骤。作者探讨了旋转功能的实现,特别强调了旋转算法的复杂性和优化。代码示例展示了游戏的基本框架,但不完整,鼓励读者参与交流和改进。
下面这些俄罗斯方块代码是我最近放假在家里自己写的,虽然以前有过看别人写的别人写的代码,但是那个游戏代码好像不是很全面,因为无法实现全部的方块和实现随机的产生任意方向的方块,现在也基本上是忘光了当时的代码,下面的这些代码是我最近写的,没有参考其他人的代码,真正写俄罗斯方块起来感觉真的是挺难的,关键是在于方块的旋转。当然下面的代码仅仅是一个框架,只能够实现大致上的功能,还不全面,贴出来和大家交流学习。编译器是code::block  +  MinGW ,感觉CB这个IDE真的是太强大 太棒了,下面的代码直接复制到VC里面运行应该不会出错,有个问题一直不知道怎么解决,就是更新客户区时窗口总是闪不知道有哪位达人能指点我一下。有的都是windows API写的,对windows编程还不是很懂,望大家多多留言,指点一下本人。
#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
#define CellWidth 20
#define  MAP_WIDTH  12
#define  MAP_HEIGHT  18
#define  ID_TIMER 1
class map_floor;
class Block;
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/*  Make the class name into a global variable  */
char szClassName[ ] = "CodeBlocksWindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS|CS_HREDRAW | CS_VREDRAW;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground =(HBRUSH) GetStockObject(WHITE_BRUSH);//COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                 /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Code::Blocks Template Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           CW_USEDEFAULT,                 /* The programs width */
           CW_USEDEFAULT,                 /* and height in pixels */
           NULL,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}

enum{e_LINE,e_CORNER,e_STAIR,e_TANCK,e_TIAN};
const int TOTAL_BLOCK_STYLE = 5;//方块类型有4种
class Block
{
	public:
            Block(int x = 100, int y = 100);
            Block(const Block & rh)//复制构造函数,可能没什么用,但是还是定义它吧
            {
                this->m_style  = rh.m_style;
                this->m_direct = rh.m_direct;
                for(int i = 0 ; i < 4 ; i ++)
                    this->m_block[i] = rh.m_block[i];
            }
            Block & operator = (const Block& rh)//重载=号,实现方块的赋值
            {
                this->m_style  = rh.m_style;
                this->m_direct = rh.m_direct
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值