#7
by みけCAT » 3年前
calbee さんが書きました: ↑3年前
試しにブロック崩しを作ろうとしたのですが、バーを動かすところで行き詰まってしまいました…
何がわからないのかわからないので、とりあえず作ってみました。
DXライブラリ使用版
► スポイラーを表示
コード:
#include <DxLib.h>
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
int WINAPI WinMain(HINSTANCE /* hInstance */, HINSTANCE /* hPrevInstance */, LPSTR /* lpCmdLine */, int /* nCmdShow */) {
// DXライブラリ初期化処理
if(ChangeWindowMode(TRUE) != DX_CHANGESCREEN_OK ||
SetGraphMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32) != DX_CHANGESCREEN_OK ||
SetMainWindowText("ブロック崩し風") == -1 ||
DxLib_Init() == -1 ) {
return 1;
}
SetDrawScreen(DX_SCREEN_BACK);
// メッセージ処理(Windowsとの連携)、描画反映、描画バッファクリア
while (ProcessMessage() == 0 && ScreenFlip() == 0 && ClearDrawScreen() == 0) {
// マウスの位置を取得する
int mouseX = 0, mouseY = 0;
GetMousePoint(&mouseX, &mouseY);
/* ラケットの位置を計算する */
const int RACKET_SIZE = 50;
int rpos;
if (mouseX < RACKET_SIZE) rpos = RACKET_SIZE;
else if (mouseX > WINDOW_WIDTH - RACKET_SIZE) rpos = WINDOW_WIDTH - RACKET_SIZE;
else rpos = mouseX;
/* ラケットを描画する */
DrawBox(rpos - RACKET_SIZE, 400, rpos + RACKET_SIZE, 420, GetColor(255, 255, 255), TRUE);
}
// DXライブラリ終了処理
DxLib_End();
return 0;
}
DXライブラリ不使用版
► スポイラーを表示
コード:
#include <windows.h>
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
void MessageLastError(HWND hWnd, const char* title) {
DWORD error = GetLastError();
char buffer[1024] = "";
FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, error, LANG_USER_DEFAULT, buffer, sizeof(buffer), NULL);
MessageBoxA(hWnd, buffer, title, MB_OK | MB_ICONWARNING);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
static int mouseX = 0, mouseY = 0;
switch(msg) {
case WM_CREATE:
/* ウィンドウ全体とクライアント領域のサイズの差を考慮してサイズを設定する */
{
RECT windowRect, clientRect;
if (!GetWindowRect(hWnd, &windowRect)) {
MessageLastError(hWnd, "GetWindowRect");
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
}
if (!GetClientRect(hWnd, &clientRect)) {
MessageLastError(hWnd, "GetClientRect");
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
}
int borderWidth = (windowRect.right - windowRect.left) - (clientRect.right - clientRect.left);
int borderHeight = (windowRect.bottom - windowRect.top) - (clientRect.bottom - clientRect.top);
if (!SetWindowPos(hWnd, NULL, 0, 0, WINDOW_WIDTH + borderWidth, WINDOW_HEIGHT + borderHeight, SWP_NOMOVE | SWP_NOZORDER)) {
MessageLastError(hWnd, "SetWindowPos");
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
}
}
/* 約20msおきに更新されるようにする */
if (!SetTimer(hWnd, 0, 20, NULL)) {
MessageLastError(hWnd, "SetTimer");
PostMessage(hWnd, WM_CLOSE, 0, 0);
}
break;
case WM_MOUSEMOVE:
/* マウスの位置の情報を更新する */
mouseX = LOWORD(lParam);
mouseY = HIWORD(lParam);
break;
case WM_TIMER:
/* 再描画させる */
if (wParam == 0) InvalidateRect(hWnd, NULL, FALSE);
break;
case WM_PAINT:
{
/* 描画する */
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hWnd, &ps);
/* ちらつかないように描画バッファを作成する */
HBITMAP hBmp = NULL, hOldBmp = NULL;
HDC hDrawDC = CreateCompatibleDC(hDC);
if (hDrawDC != NULL) {
hBmp = CreateCompatibleBitmap(hDC, WINDOW_WIDTH, WINDOW_HEIGHT);
if (hBmp == NULL) {
DeleteDC(hDrawDC);
hDrawDC = hDC;
} else {
hOldBmp = static_cast<HBITMAP>(SelectObject(hDrawDC, hBmp));
}
} else {
hDrawDC = hDC;
}
/* 画面を黒で塗りつぶす */
RECT all;
all.left = 0; all.right = WINDOW_WIDTH;
all.top = 0; all.bottom = WINDOW_HEIGHT;
FillRect(hDrawDC, &all, static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));
/* ラケットの位置を計算する */
const int RACKET_SIZE = 50;
int rpos;
if (mouseX < RACKET_SIZE) rpos = RACKET_SIZE;
else if (mouseX > WINDOW_WIDTH - RACKET_SIZE) rpos = WINDOW_WIDTH - RACKET_SIZE;
else rpos = mouseX;
/* 描画を行う */
RECT racket;
racket.top = 400; racket.bottom = 420;
racket.left = rpos - RACKET_SIZE; racket.right = rpos + RACKET_SIZE;
FillRect(hDrawDC, &racket, static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)));
/* 描画バッファの内容を反映させる */
if (hBmp != NULL) {
BitBlt(hDC, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, hDrawDC, 0, 0, SRCCOPY);
SelectObject(hDrawDC, hOldBmp);
DeleteObject(hBmp);
}
if (hDrawDC != hDC) DeleteDC(hDrawDC);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE /* hPrevInstance */, LPSTR /* lpCmdLine */, int /* nCmdShow */) {
static const char className[] = "CLASS_NAME_9E3FFCCE_B914_481D_A924_35E9D6375745";
WNDCLASSEXA wc = {0};
HWND hwnd;
MSG msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = className;
if(!RegisterClassExA(&wc)) {
MessageLastError(NULL, "RegisterClassExA");
return 1;
}
hwnd = CreateWindowExA(0, className,
"ブロック崩し風",
(WS_VISIBLE | WS_OVERLAPPEDWINDOW) & ~(WS_MAXIMIZEBOX | WS_THICKFRAME),
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL) {
MessageLastError(NULL, "CreateWindowExA");
return 1;
}
while(GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
[quote=calbee post_id=154996 time=1595743525 user_id=2954]
試しにブロック崩しを作ろうとしたのですが、バーを動かすところで行き詰まってしまいました…
[/quote]
何がわからないのかわからないので、とりあえず作ってみました。
DXライブラリ使用版
[spoil][code]
#include <DxLib.h>
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
int WINAPI WinMain(HINSTANCE /* hInstance */, HINSTANCE /* hPrevInstance */, LPSTR /* lpCmdLine */, int /* nCmdShow */) {
// DXライブラリ初期化処理
if(ChangeWindowMode(TRUE) != DX_CHANGESCREEN_OK ||
SetGraphMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32) != DX_CHANGESCREEN_OK ||
SetMainWindowText("ブロック崩し風") == -1 ||
DxLib_Init() == -1 ) {
return 1;
}
SetDrawScreen(DX_SCREEN_BACK);
// メッセージ処理(Windowsとの連携)、描画反映、描画バッファクリア
while (ProcessMessage() == 0 && ScreenFlip() == 0 && ClearDrawScreen() == 0) {
// マウスの位置を取得する
int mouseX = 0, mouseY = 0;
GetMousePoint(&mouseX, &mouseY);
/* ラケットの位置を計算する */
const int RACKET_SIZE = 50;
int rpos;
if (mouseX < RACKET_SIZE) rpos = RACKET_SIZE;
else if (mouseX > WINDOW_WIDTH - RACKET_SIZE) rpos = WINDOW_WIDTH - RACKET_SIZE;
else rpos = mouseX;
/* ラケットを描画する */
DrawBox(rpos - RACKET_SIZE, 400, rpos + RACKET_SIZE, 420, GetColor(255, 255, 255), TRUE);
}
// DXライブラリ終了処理
DxLib_End();
return 0;
}
[/code][/spoil]
DXライブラリ不使用版
[spoil][code]
#include <windows.h>
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
void MessageLastError(HWND hWnd, const char* title) {
DWORD error = GetLastError();
char buffer[1024] = "";
FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, error, LANG_USER_DEFAULT, buffer, sizeof(buffer), NULL);
MessageBoxA(hWnd, buffer, title, MB_OK | MB_ICONWARNING);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
static int mouseX = 0, mouseY = 0;
switch(msg) {
case WM_CREATE:
/* ウィンドウ全体とクライアント領域のサイズの差を考慮してサイズを設定する */
{
RECT windowRect, clientRect;
if (!GetWindowRect(hWnd, &windowRect)) {
MessageLastError(hWnd, "GetWindowRect");
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
}
if (!GetClientRect(hWnd, &clientRect)) {
MessageLastError(hWnd, "GetClientRect");
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
}
int borderWidth = (windowRect.right - windowRect.left) - (clientRect.right - clientRect.left);
int borderHeight = (windowRect.bottom - windowRect.top) - (clientRect.bottom - clientRect.top);
if (!SetWindowPos(hWnd, NULL, 0, 0, WINDOW_WIDTH + borderWidth, WINDOW_HEIGHT + borderHeight, SWP_NOMOVE | SWP_NOZORDER)) {
MessageLastError(hWnd, "SetWindowPos");
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
}
}
/* 約20msおきに更新されるようにする */
if (!SetTimer(hWnd, 0, 20, NULL)) {
MessageLastError(hWnd, "SetTimer");
PostMessage(hWnd, WM_CLOSE, 0, 0);
}
break;
case WM_MOUSEMOVE:
/* マウスの位置の情報を更新する */
mouseX = LOWORD(lParam);
mouseY = HIWORD(lParam);
break;
case WM_TIMER:
/* 再描画させる */
if (wParam == 0) InvalidateRect(hWnd, NULL, FALSE);
break;
case WM_PAINT:
{
/* 描画する */
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hWnd, &ps);
/* ちらつかないように描画バッファを作成する */
HBITMAP hBmp = NULL, hOldBmp = NULL;
HDC hDrawDC = CreateCompatibleDC(hDC);
if (hDrawDC != NULL) {
hBmp = CreateCompatibleBitmap(hDC, WINDOW_WIDTH, WINDOW_HEIGHT);
if (hBmp == NULL) {
DeleteDC(hDrawDC);
hDrawDC = hDC;
} else {
hOldBmp = static_cast<HBITMAP>(SelectObject(hDrawDC, hBmp));
}
} else {
hDrawDC = hDC;
}
/* 画面を黒で塗りつぶす */
RECT all;
all.left = 0; all.right = WINDOW_WIDTH;
all.top = 0; all.bottom = WINDOW_HEIGHT;
FillRect(hDrawDC, &all, static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));
/* ラケットの位置を計算する */
const int RACKET_SIZE = 50;
int rpos;
if (mouseX < RACKET_SIZE) rpos = RACKET_SIZE;
else if (mouseX > WINDOW_WIDTH - RACKET_SIZE) rpos = WINDOW_WIDTH - RACKET_SIZE;
else rpos = mouseX;
/* 描画を行う */
RECT racket;
racket.top = 400; racket.bottom = 420;
racket.left = rpos - RACKET_SIZE; racket.right = rpos + RACKET_SIZE;
FillRect(hDrawDC, &racket, static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH)));
/* 描画バッファの内容を反映させる */
if (hBmp != NULL) {
BitBlt(hDC, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, hDrawDC, 0, 0, SRCCOPY);
SelectObject(hDrawDC, hOldBmp);
DeleteObject(hBmp);
}
if (hDrawDC != hDC) DeleteDC(hDrawDC);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE /* hPrevInstance */, LPSTR /* lpCmdLine */, int /* nCmdShow */) {
static const char className[] = "CLASS_NAME_9E3FFCCE_B914_481D_A924_35E9D6375745";
WNDCLASSEXA wc = {0};
HWND hwnd;
MSG msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = className;
if(!RegisterClassExA(&wc)) {
MessageLastError(NULL, "RegisterClassExA");
return 1;
}
hwnd = CreateWindowExA(0, className,
"ブロック崩し風",
(WS_VISIBLE | WS_OVERLAPPEDWINDOW) & ~(WS_MAXIMIZEBOX | WS_THICKFRAME),
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL) {
MessageLastError(NULL, "CreateWindowExA");
return 1;
}
while(GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
[/code][/spoil]