64,637
社区成员
发帖
与我相关
我的任务
分享
//-------------------------------
// brick.cpp
//
// Just a Game
//
// Date: 10.27 2014
// Author: Dreamtale
//-------------------------------
#include <windows.h>
#include "func.h"
Board board;
Brick brick[3][20];
void Init()
{
int i, j;
for(j=0; j<3; j++)
{
for(i=0; i<20-2*j; i++)
{
brick[j][i].Setxy(35 + 30*(i+j), 120-20*j);
}
}
board.Setxy(320, 475);
}
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("BrickWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground= (HBRUSH) GetStockObject (GRAY_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName= szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow( szAppName, // window class name
TEXT ("Brick"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
655, // initial x size
517, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, SW_SHOWNORMAL) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
switch (message)
{
case WM_CREATE:
Init();
return 0 ;
case WM_PAINT:
int i,j;
hdc = BeginPaint(hwnd, &ps);
board.Draw(hdc);
for(j=0; j<3; j++)
{
for(i=0; i<20-2*j; i++)
{
brick[j][i].Draw(hdc);
}
}
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_KEYDOWN:
switch(wParam)
{
case VK_LEFT:
if(board.Getx() > 50)
{
board.Setxy(board.Getx()-10, board.Gety()) ;
board.MoveDirection(-1);
}
break;
case VK_RIGHT:
if(board.Getx() < 590)
{
board.Setxy(board.Getx()+10, board.Gety()) ;
board.MoveDirection(1);
}
break;
case VK_ESCAPE:
SendMessage(hwnd, WM_CLOSE, 0, 0) ;
break;
}
InvalidateRect(hwnd,NULL,FALSE) ;
return 0;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
//-------------------------------
// func.h
//
// class board, brick
//
// Date: 10.27 2014
// Author: Dreamtale
//-------------------------------
#include <iostream>
#include <windows.h>
using namespace std;
class Board
{
public:
void Draw(HDC hdc);
void Setxy(int X, int Y) {x=X; y=Y;}
int Getx() { return x; }
int Gety() { return y; }
void MoveDirection(int DIR) { dir = DIR; } //left:-1 right:1
int GetDir() { return dir; }
private:
int x, y, dir;
} ;
class Brick
{
public:
void Draw(HDC hdc);
void Setxy(int X, int Y) { x = X; y = Y; }
int Getx() { return x; }
int Gety() { return y; }
private:
int x, y;
} ;
//-------------------------------
// function.cpp
//
// function
//
// Date: 10.27 2014
// Author: Dreamtale
//-------------------------------
#include "func.h"
void Brick::Draw(HDC hdc)
{
HBRUSH hBrush;
hBrush = CreateSolidBrush(RGB(131, 72, 37)) ;
SelectObject(hdc, GetStockObject(BLACK_PEN)) ;
SelectObject(hdc, hBrush);
Rectangle(hdc, x-15, y-10, x+15, y+10) ;
}
void Board::Draw(HDC hdc)
{
HBRUSH hBrush;
hBrush = CreateSolidBrush(RGB(131, 72, 37)) ;
SelectObject(hdc, GetStockObject(NULL_PEN)) ;
SelectObject(hdc, hBrush);
Rectangle(hdc, x-50, y-5, x+50, y+5) ;
if(dir == -1)
{
SelectObject(hdc, GetStockObject(GRAY_BRUSH)) ;
Rectangle(hdc, x+50, y-5, x+61, y+5) ;
dir = 0;
}
if(dir == 1)
{
SelectObject(hdc, GetStockObject(GRAY_BRUSH)) ;
Rectangle(hdc, x-50, y-5, x-61, y+5) ;
dir = 0;
}
}