助けてください。C++ 三目並べ
助けてください。C++ 三目並べ
C++のBCCデベロッパーをつかってDxライブラリで三目並べ(〇✕ゲーム)を作っているのですが、どうしてもDrawBOXで書いた四角の範囲内でマウスクリックをしたら四角の中の中心に丸を描画するというプログラムが組めません。DrawBoxで書いた四角は9つです。9つの四角形は3×3のマスとして扱いたいです。座標は一つ目から(50,249,224,158),(224,29,400,158),(400,29,590,158),(50,158,400,310),(224,158,590,310),(50,310,225,448),(224,310,400,448),(400,310,590,448)です。どうかよろしくお願いします。
- Dixq (管理人)
- 管理人
- 記事: 1662
- 登録日時: 15年前
- 住所: 北海道札幌市
- 連絡を取る:
Re: 助けてください。C++ 三目並べ
サンプルを作ってみました。
プロジェクトを一式まとめてzipにしてアップロードしましたのでよければ参考にしてみてください。
[プロジェクト]
http://dixq.net/BBS/zip/InBoxCircle.zip
[実行結果動画]
[youtube][/youtube]
[main.cpp]
[Mouse.cpp]
[Box.cpp]
プロジェクトを一式まとめてzipにしてアップロードしましたのでよければ参考にしてみてください。
[プロジェクト]
http://dixq.net/BBS/zip/InBoxCircle.zip
[実行結果動画]
[youtube][/youtube]
[main.cpp]
#include "DxLib.h"
#include "Mouse.h"
#include <list>
#include "Box.h"
using namespace std;
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int){
ChangeWindowMode(TRUE), DxLib_Init(), SetDrawScreen(DX_SCREEN_BACK);
SetMouseDispFlag(TRUE);
list<Box> list;
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
list.emplace_back(Box(100 + x * 100, 100 + y * 100, 100, 100));
}
}
while(!ScreenFlip()&&!ProcessMessage()&&!ClearDrawScreen()) {
Mouse::getInstance()->update();
for (Box& box : list) {
box.update();
box.draw();
}
}
DxLib_End(); // DXライブラリ終了処理
return 0;
}
#include <DxLib.h>
#include "Mouse.h"
Mouse::Mouse() : _buttonPressingCount(), _buttonReleasingCount(), _x(), _y() {
}
//更新
bool Mouse::update() {
int nowButtonState = GetMouseInput();
GetMousePoint(&_x, &_y);
for (int i = 0; i<KEY_NUM; i++) {
if ((nowButtonState<<i) & 1) { //i番のボタンが押されていたら
if (_buttonReleasingCount[i] > 0) {//離されカウンタが0より大きければ
_buttonReleasingCount[i] = 0; //0に戻す
}
_buttonPressingCount[i]++; //押されカウンタを増やす
}
else { //i番のキーが離されていたら
if (_buttonPressingCount[i] > 0) { //押されカウンタが0より大きければ
_buttonPressingCount[i] = 0; //0に戻す
}
_buttonReleasingCount[i]++; //離されカウンタを増やす
}
}
return true;
}
//keyCodeのキーが押されているフレーム数を返す
int Mouse::getPressingCount(int keyCode) {
if (!isAvailableCode(keyCode)) {
return -1;
}
return _buttonPressingCount[keyCode];
}
//keyCodeのキーが離されているフレーム数を返す
int Mouse::getReleasingCount(int keyCode) {
if (!isAvailableCode(keyCode)) {
return -1;
}
return _buttonReleasingCount[keyCode];
}
//keyCodeが有効な値かチェックする
bool Mouse::isAvailableCode(int keyCode) {
if (!(0 <= keyCode && keyCode<KEY_NUM)) {
return false;
}
return true;
}
#include <DxLib.h>
#include "Box.h"
#include "Mouse.h"
Box::Box(int left, int top, int width, int height) :
_left(left), _top(top), _width(width), _height(height), _isDrawingCircle(false)
{
_centerX = _left + _width / 2;
_centerY = _top + _height / 2;
_range = _width/3;
}
bool Box::update() {
if (Mouse::getInstance()->getPressingCount(Mouse::LEFT_CLICK) == 1) {
int x = Mouse::getInstance()->getX();
int y = Mouse::getInstance()->getY();
if (_left < x && x < _left+_width && _top < y && y < _top+_height) {
_isDrawingCircle = true;
}
}
return true;
}
void Box::draw() {
DrawBox(_left, _top, _left+_width, _top+_height, GetColor(255,255,255), FALSE);
if (_isDrawingCircle) {
DrawCircle(_centerX, _centerY, _range, GetColor(255, 0, 0), TRUE);
}
}