Code:
#include <windows.h>
#include <windowsx.h>
#include <Mmsystem.h>
double wide, high;
static char g_szClassName[] = "EmsWindow";
static HINSTANCE g_hInst = NULL;
HDC hdc;
HDC hdcMemory;
char* playmusic = ("play music.mid");
char* stopmusic = ("play music.mid");
const UINT idTimer1 = 1;
UINT nTimerDelay = 50;
HBITMAP hbmImage;
int RedX = 20, RedY = 20;
int RedScore;
char BoardPieces[200];
int BoardWidth = 20, BoardHeight = 25;
int PickPieceSize = 20;
void Ecr(HWND hwnd, int ghtff){
KillTimer(hwnd,idTimer1);
char c[100];
wsprintf(c,"Error level: %d",ghtff);
MessageBox(0,c,"Error information",0);
SetTimer(hwnd, idTimer1, nTimerDelay, NULL);
return; }
int DrawBoard(HWND hwnd)
{
int xd, yd, xp = RedX, yp = RedY;
int dlll = BoardWidth * BoardHeight;
int i;
for(i = 0; i < dlll; i++)
{
if((BoardPieces[i] == '1') || (BoardPieces[i] == '!') || (BoardPieces[i] == 'A')){
if(BoardPieces[i] == '!')
BoardPieces[i] = 'A';
xd = 2; yd = 2; }
else{
xd = 2; yd = 178; }
BitBlt(hdc, xp, yp, PickPieceSize, PickPieceSize, hdcMemory, xd, yd, SRCCOPY);
xp=xp+PickPieceSize;
if(xp == (PickPieceSize * BoardWidth) + RedX){
yp=yp+PickPieceSize;
xp=RedX; }
}
}
int MovePiece(HWND hwnd, int iKey)
{
int i;
int dlll = BoardWidth * BoardHeight;
BOOL again, first = TRUE;
while(again == TRUE || first == TRUE){
again = FALSE;
for(i = 0; i < dlll; i++)
{
if(BoardPieces[i] == 'A'){
if(iKey == VK_DOWN)
{
if((BoardPieces[i + BoardWidth] == '!') || (BoardPieces[i + BoardWidth] == 'A') || (BoardPieces[i + BoardWidth] == '1')){
again = TRUE; }
else{
BoardPieces[i] = ' ';
BoardPieces[i + BoardWidth] = '!'; }
}
else if(iKey == VK_UP)
{
// MessageBox(0,BoardPieces,0,0);
BoardPieces[i] = ' ';
BoardPieces[i - BoardWidth] = '!';
}
else if(iKey == VK_RIGHT)
{
if((BoardPieces[i + 1] == '!') || (BoardPieces[i + 1] == 'A') || (BoardPieces[i + 1] == '1')){
again = TRUE; }
else{
BoardPieces[i] = ' ';
BoardPieces[i + 1] = '!'; }
}
else if(iKey == VK_LEFT)
{
BoardPieces[i] = ' ';
BoardPieces[i - 1] = '!';
}
}
}
first = FALSE; }
}
int Timer(HWND hwnd)
{
if(GetAsyncKeyState(VK_UP) || GetAsyncKeyState(0x57))
MovePiece(hwnd,VK_UP);
else if(GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState(0x44))
MovePiece(hwnd,VK_RIGHT);
else if(GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState(0x41))
MovePiece(hwnd,VK_LEFT);
else if(GetAsyncKeyState(VK_DOWN) || GetAsyncKeyState(0x53))
MovePiece(hwnd,VK_DOWN);
DrawBoard(hwnd);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_CREATE:
hbmImage = (HBITMAP)LoadImage(0,"Sqimgs.dat",IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMFILE);
if(!hbmImage){
MessageBox(hwnd, "Unable to load \"SQIMGS.DAT\"", "Error", MB_OK | MB_ICONEXCLAMATION);
return -1; }
mciSendString(playmusic,NULL,0,NULL);
hdc = GetDC(hwnd);
hdcMemory = CreateCompatibleDC(hdc);
SaveDC(hdcMemory);
SelectObject(hdcMemory, hbmImage);
SetTimer(hwnd, idTimer1, nTimerDelay, NULL);
break;
case WM_TIMER:
Timer(hwnd);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
KillTimer(hwnd, idTimer1);
mciSendString(stopmusic,NULL,0,NULL);
ReleaseDC(hwnd,hdc);
RestoreDC(hdcMemory,-1);
DeleteDC(hdcMemory);
DeleteObject(hbmImage);
PostQuitMessage(0);
break;
case WM_CHAR:
/* handle keyboard input */
switch ((int)wParam) {
break;
case VK_ESCAPE:
DestroyWindow(hwnd);
return 0;
case VK_RETURN:
KillTimer(hwnd, idTimer1);
break;
default:
break;
}
break;
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
int i = 0;
for(i = 0; i < BoardWidth * BoardHeight; i++)
BoardPieces[i] = ' ';
BoardPieces[310] = '!';
BoardPieces[311] = '!';
BoardPieces[312] = '!';
BoardPieces[331] = '!';
WNDCLASSEX WndClass;
HWND hwnd;
MSG Msg;
g_hInst = hInstance;
wide = GetSystemMetrics(SM_CXSCREEN);
high = GetSystemMetrics(SM_CYSCREEN);
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = 0;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = g_hInst;
WndClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
WndClass.hCursor = LoadCursor(NULL, IDC_CROSS);
WndClass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0,0,0));
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = g_szClassName;
WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&WndClass))
{
MessageBox(0, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"Squatris",
WS_OVERLAPPEDWINDOW&~WS_THICKFRAME&~WS_MAXIMIZEBOX&~WS_VISIBLE,
0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
NULL, NULL, g_hInst, NULL);
if(hwnd == NULL)
{
MessageBox(0, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}