現在私はDXライブラリを用いたゲームプログラミングの基礎を勉強しております。
今回、15パズルを作成している途中で不明な点が出てきたので質問させていただきます。
#include "DxLib.h"
const int gCol = 4;
const int gNum = gCol*gCol - 1;
const int gImageSize = 720;
const int SPACE = 10000;
const int gPieceSize = gImageSize/gCol;
int gameBoard[gCol*gCol];
bool gCleared = false;
int gImage[gCol*gCol];
void shuffle(){
int rand = 0;
int rand2 = 0;
int tmp = 0;
for(int i = 0; i < (GetRand(1000)+100)*2; ++i){
rand = GetRand(gNum-1);
tmp = gameBoard[rand];
rand2 = GetRand(gNum-1);
while(rand == rand2){
rand2 = GetRand(gNum-1);
}
gameBoard[rand] = gameBoard[rand2];
gameBoard[rand2] = tmp;
}
}
int calculate(){
int count = 0;
for(int i = 0; i < gNum; ++i){
for(int j = i; j < gNum; ++j){
if(gameBoard[i] > gameBoard[j]){
count++;
}
}
}
return count;
}
void initGame(){
for(int i = 0; i < gNum; ++i){
gameBoard[i] = i + 1;
}
shuffle();
while(calculate() % 2 != 0){
shuffle();
}
gameBoard[gNum] = SPACE;
LoadDivGraph("img/image.jpg", gCol*gCol, gCol, gCol, gPieceSize, gPieceSize, gImage);
}
void drawGame(){
int x = 0;
int y = 0;
int fontSize = 20;
int Cr = GetColor(255, 0, 0);
SetFontSize(fontSize);
for(int i = 0; i < gCol; ++i){
x = 0;
for(int j = 0; j < gCol; ++j){
if(gameBoard[i*gCol + j] != SPACE){
DrawGraph(x, y, gImage[gameBoard[i*gCol + j] - 1], TRUE);
DrawFormatString(x + gPieceSize/2 - fontSize/2, y + gPieceSize/2 - fontSize/2, Cr, "%d", gameBoard[i*gCol + j] );
}
x += gPieceSize;
}
y += gPieceSize;
}
}
void updateGame(){
int input = 100;
int x, y;
if((GetMouseInput() && MOUSE_INPUT_LEFT) != 0){
GetMousePoint(&x, &y);
int xPos = x/gPieceSize;
int yPos = y/gPieceSize;
if(xPos <= gCol && yPos <= gCol){
input = gameBoard[yPos*gCol + xPos];
}
}
int posInput = 1000;
for(int i = 0; i <= gNum; ++i){
if(gameBoard[i] == input){
posInput = i;
break;
}
}
if(posInput == 1000){
return;
}
int posSpace = 0;
for(int i = 0; i <= gNum; ++i){
if(gameBoard[i] == SPACE){
posSpace = i;
break;
}
}
if(posInput - gCol == posSpace || posInput + gCol == posSpace){
gameBoard[posInput] = SPACE;
gameBoard[posSpace] = input;
return;
}
if(posInput % gCol == gCol-1){
if(posInput - 1 == posSpace){
gameBoard[posInput] = SPACE;
gameBoard[posSpace] = input;
return;
}
return;
}else if(posInput % gCol == 0){
if(posInput + 1 == posSpace){
gameBoard[posInput] = SPACE;
gameBoard[posSpace] = input;
return;
}
return;
}else{
if((posInput + 1 == posSpace) || (posInput - 1 == posSpace)){
gameBoard[posInput] = SPACE;
gameBoard[posSpace] = input;
return;
}
return;
}
}
void isCleared(){
for(int i = 0; i < gNum; ++i){
if(gameBoard[i] != i + 1){
return;
}
}
gCleared = true;
}
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int){
ChangeWindowMode(TRUE);
SetMainWindowText("15パズル");
SetOutApplicationLogValidFlag(FALSE);
SetGraphMode(1024, 768, 32); //こちらでは"Congratulations!"が表示されない
DxLib_Init();
//SetGraphMode(1024, 768, 32); //こちらだと起動時は640×480になってしまう
SetDrawScreen(DX_SCREEN_BACK);
int Cr2 = GetColor(255, 255, 255);
initGame();
while(ScreenFlip() == 0 && ProcessMessage() == 0 && ClearDrawScreen() == 0){
if(!gCleared){
drawGame();
updateGame();
isCleared();
gCleared = true; //クリアが面倒なのでとりあえず
}else{
drawGame();
int Cr = GetColor(0, 255, 255);
SetFontSize(64);
const char* msg = "Congratulations!";
DrawFormatString(gImageSize/2 - GetDrawStringWidth(msg, strlen(msg))/2, gImageSize/2 - 32, Cr, msg);
WaitTimer(2000);
DxLib_End();
}
}
return 0;
}
後に記述した場合では起動時のウィンドウサイズが640×480となってしまい、あまり望ましくありません。
起動時のウィンドウサイズを変更でき、終了直前の"Congratulations!"の表示も行える方法がありましたらご教授お願いします。