今天我们将了解如何使用Microsoft Visual Studio 2022在CLI/C++中创建h5小游戏源码和小程序游戏源码!
当它几乎完成时,会出现一个对话框,您可以在其中选择您想要使用的所有组件(例如:C++游戏开发、移动开发、Linux/C++开发等)。
仓库源码:y.wxlbyx.icu
您可以向下滚动并查看您将使用的越来越多的组件。请注意,您添加的内容越多,程序所需的空间就越多。
现在,选择使用.NET进行桌面开发和使用C++进行桌面开发。
接下来,转到单个组件侧边栏。
搜索CLI,您将在此处看到各种选项:
选择上面有复选标记的那个(它的名称可能与我在西班牙语中的名称有所不同)。
注意:Microsoft可能会为其发布新版本,因此请不要忘记使用最新版本以备不时之需。
完成所有设置后,单击“安装”按钮并等待安装。
现在我们已经完成了所有设置,我们可以开始编码了!
入门
打开Visual Studio后,我们将看到如下内容:
视觉工作室
我们将选择“创建项目”选项。
“创建项目”选项
接下来,我们将看到创建项目的各种选项(模板、空项目等)。
可用的项目模板
如果你搜索“clr”,你会看到一些像这样的选项:
Visual Studio中的“clr”搜索
选择显示“空CLR项目(.NET Framework)的项目。
空CLR项目(.NET框架)
接下来,您将看到如下内容:
完成设置新项目
选择一个你想要的目录。选择.NET Framework版本(我建议使用条形菜单中的最新版本)。
为您的项目选择一个名称。我建议使用井字游戏,因为在代码的许多部分中,它取决于项目的名称。
现在点击“创建”按钮!等待项目自行创建(请注意,这可能需要几分钟,具体取决于您的PC硬件)。
编码和制作游戏
打开项目/解决方案后,单击“项目”,然后单击“添加新项目”。
接下来,它会出现如下所示:
添加新项目(选择一个项目)
单击显示“UI”的那个,然后单击“Windows窗体”选项。
“Windows窗体”选项
为文件选择一个名称。我建议使用TicTacToe.h.然后单击“添加”按钮。而已。您已添加Windows窗体。呜呼!
起初,它可能会显示错误。尝试重新打开.h文件。
如果这不能解决问题,请关闭整个项目和程序,然后重新打开它。在最坏的情况下,重新启动您的PC。
配置项目
为了确保程序正常运行,我们必须在我们的小项目中调整一些配置。
下一个:
子系统
这样做之后:
入口点
现在一切都应该正确配置!
在您保存项目的目录中,转到TicTacToe文件夹。
您可能会在那里看到各种文件。打开TicTacToe.cpp和TicTacToe.h
在TicTacToe.cpp中,将整个代码替换为以下代码:
#include "TicTacToe.h"
[System::STAThread]
void main(array <System::String^>^ args) {
System::Windows::Forms::Application::EnableVisualStyles();
System::Windows::Forms::Application::SetCompatibleTextRenderingDefault(false);
TicTacToe::TicTacToe form;
System::Windows::Forms::Application::Run(% form);
}
根据您的项目名称,上面的代码可能无法正常工作。例如:如果我的项目名称是test_project,我必须更改TicTacToe为test_project.根据命名空间名称,我必须相应地更改它。如果命名空间被命名my_namespace,你应该使用。
#include "TicTacToe.h"
[System::STAThread]
void main(array <System::String^>^ args) {
System::Windows::Forms::Application::EnableVisualStyles();
System::Windows::Forms::Application::SetCompatibleTextRenderingDefault(false);
test_project::my_namespace form; // Here's where you need to update the names. :)
System::Windows::Forms::Application::Run(% form);
}
现在进入TicTacToe.h文件。
我们将开始构建游戏的核心。
用TicTacToe.h这个替换代码。
Gist包含代码的主要部分:按钮、菜单条、标签等。
暂时不要编译代码!它会抛出很多错误。
在代码的末尾,您会看到这一行#pragma endregion。在此之前,添加以下代码:
uint16_t turn = 0; // Whether to choose if the X or the O will place.
bool match_ended = false; // If the match has ended or not.
在这里,我们刚刚创建了两个变量:一个:选择谁来放置,是O还是X;二:比赛结束了吗?
我们现在将创建“开始游戏!”的功能。按钮。
该按钮应该自行删除并添加其他按钮,以便您可以单击它们,然后会出现“O”或“X”。
在代码之后#pragma endregion,添加以下代码:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { // "Start game!" button
this->button1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F));
this->button1->Location = System::Drawing::Point(80, 58);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(0, 0);
this->button1->Text = L"";
/**
* @brief Initialize buttons when clicking "Start game!"
*/
//
// button11 ("Restart game" button)
//
this->button11->Location = System::Drawing::Point(184, 149);
this->button11->Name = L"button11";
this->button11->Size = System::Drawing::Size(88, 31);
this->button11->TabIndex = 9;
this->button11->Text = L"Restart game";
this->button11->UseVisualStyleBackColor = true;
this->button11->Click += gcnew System::EventHandler(this, &TicTacToe::restart_game);
//
// button2
//
this->button2->Location = System::Drawing::Point(80, 50);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(31, 25);
this->button2->TabIndex = 2;
this->button2->Text = L" ";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &TicTacToe::button_click);
//
// button3
//
this->button3->Location = System::Drawing::Point(117, 50);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(31, 25);
this->button3->TabIndex = 3;
this->button3->Text = L" ";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &TicTacToe::button_click);
//
// button4
//
this->button4->Location = System::Drawing::Point(154, 50);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(31, 25);
this->button4->TabIndex = 4;
this->button4->Text = L" ";
this->button4->UseVisualStyleBackColor = true;
this->button4->Click += gcnew System::EventHandler(this, &TicTacToe::button_click);
//
// button5
//
this->button5->Location = System::Drawing::Point(80, 81);
this->button5->Name = L"button5";
this->button5->Size = System::Drawing::Size(31, 25);
this->button5->TabIndex = 5;
this->button5->Text = L" ";
this->button5->UseVisualStyleBackColor = true;
this->button5->Click += gcnew System::EventHandler(this, &TicTacToe::button_click);
//
// button6
//
this->button6->Location = System::Drawing::Point(117, 81);
this->button6->Name = L"button6";
this->button6->Size = System::Drawing::Size(31, 25);
this->button6->TabIndex = 6;
this->button6->Text = L" ";
this->button6->UseVisualStyleBackColor = true;
this->button6->Click += gcnew System::EventHandler(this, &TicTacToe::button_click);
//
// button7
//
this->button7->Location = System::Drawing::Point(154, 81);
this->button7->Name = L"button7";
this->button7->Size = System::Drawing::Size(31, 25);
this->button7->TabIndex = 7;
this->button7->Text = L" ";
this->button7->UseVisualStyleBackColor = true;
this->button7->Click += gcnew System::EventHandler(this, &TicTacToe::button_click);
//
// button8
//
this->button8->Location = System::Drawing::Point(80, 112);
this->button8->Name = L"button8";
this->button8->Size = System::Drawing::Size(31, 25);
this->button8->TabIndex = 8;
this->button8->Text = L" ";
this->button8->UseVisualStyleBackColor = true;
this->button8->Click += gcnew System::EventHandler(this, &TicTacToe::button_click);
//
// button9
//
this->button9->Location = System::Drawing::Point(117, 112);
this->button9->Name = L"button9";
this->button9->Size = System::Drawing::Size(31, 25);
this->button9->TabIndex = 9;
this->button9->Text = L" ";
this->button9->UseVisualStyleBackColor = true;
this->button9->Click += gcnew System::EventHandler(this, &TicTacToe::button_click);
//
// button10
//
this->button10->Location = System::Drawing::Point(154, 112);
this->button10->Name = L"button10";
this->button10->Size = System::Drawing::Size(31, 25);
this->button10->TabIndex = 10;
this->button10->Text = L" ";
this->button10->UseVisualStyleBackColor = true;
this->button10->Click += gcnew System::EventHandler(this, &TicTacToe::button_click);
}
此代码应按照视频中的方式执行以下操作。
如您所见,当您单击按钮时,它会立即删除自身并添加其他按钮。上面的代码应该做到这一点。
现在,这是最重要的部分:用于放置“O”或“X”的按钮的代码!
在您添加的最新代码之后,添加以下代码:
private: System::Void button_click(System::Object^ sender, System::EventArgs^ e) { // Buttons to place O and X
Button^ Numbers = safe_cast<Button^>(sender);
if ((match_ended) || (Numbers->Text != " ")) { // Check if string is empty. If so, do not change anything.
return;
}
if (turn == 0) {
Numbers->Text = "O";
Numbers->ForeColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(0)), static_cast<System::Int32>(static_cast<System::Byte>(0)),
static_cast<System::Int32>(static_cast<System::Byte>(192)));
Numbers->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
turn = 1;
}
else if (turn == 1) {
Numbers->Text = "X";
Numbers->ForeColor = System::Drawing::Color::Red;
Numbers->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
turn = 0;
}
/**
* @brief The system to determine if it's a match or not
* We'll first determine if the O wins. After that, we'll check the X
*/
// // O O O
if ((button2->Text == "O") && (button3->Text == "O") && (button4->Text == "O")) { //
//
match_ended = true;
label2->Text = "The O won the game!";
}
// //
else if ((button5->Text == "O") && (button6->Text == "O") && (button7->Text == "O")) { // O O O
//
match_ended = true;
label2->Text = "The O won the game!";
}
// //
else if ((button8->Text == "O") && (button9->Text == "O") && (button10->Text == "O")) { //
// O O O
match_ended = true;
label2->Text = "The O won the game!";
}
// // O
if ((button2->Text == "O") && (button5->Text == "O") && (button8->Text == "O")) { // O
// O
match_ended = true;
label2->Text = "The O won the game!";
}
// // O
else if ((button3->Text == "O") && (button6->Text == "O") && (button9->Text == "O")) { // O
// O
match_ended = true;
label2->Text = "The O won the game!";
}
// // O
else if ((button4->Text == "O") && (button7->Text == "O") && (button10->Text == "O")) { // O
// O
match_ended = true;
label2->Text = "The O won the game!";
}
// // O
else if ((button2->Text == "O") && (button6->Text == "O") && (button10->Text == "O")) { // O
// O
match_ended = true;
label2->Text = "The O won the game!";
}
// // O
else if ((button4->Text == "O") && (button6->Text == "O") && (button8->Text == "O")) { // O
// O
match_ended = true;
label2->Text = "The O won the game!";
}
/**
* @brief The system to determine
* if it's a match or not (for the X)
* We'll determine if the X wins.
*/
// // X X X
if ((button2->Text == "X") && (button3->Text == "X") && (button4->Text == "X")) { //
//
match_ended = true;
label2->Text = "The X won the game!";
}
// //
else if ((button5->Text == "X") && (button6->Text == "X") && (button7->Text == "X")) { // X X X
//
match_ended = true;
label2->Text = "The X won the game!";
}
// //
else if ((button8->Text == "X") && (button9->Text == "X") && (button10->Text == "X")) { //
// X X X
match_ended = true;
label2->Text = "The X won the game!";
}
// // X
if ((button2->Text == "X") && (button5->Text == "X") && (button8->Text == "X")) { // X
// X
match_ended = true;
label2->Text = "The X won the game!";
}
// // X
else if ((button3->Text == "X") && (button6->Text == "X") && (button9->Text == "X")) { // X
// X
match_ended = true;
label2->Text = "The X won the game!";
}
// // X
else if ((button4->Text == "X") && (button7->Text == "X") && (button10->Text == "X")) { // X
// X
match_ended = true;
label2->Text = "The X won the game!";
}
// // X
else if ((button2->Text == "X") && (button6->Text == "X") && (button10->Text == "X")) { // X
// X
match_ended = true;
label2->Text = "The X won the game!";
}
// // X
else if ((button4->Text == "X") && (button6->Text == "X") && (button8->Text == "X")) { // X
// X
match_ended = true;
label2->Text = "The X won the game!";
}
}
上面的所有代码都应该这样做:
我们将首先检查文本是否为空。如果是这样,请不要更改文本。
if (turn == 0) {
Numbers->Text = "O";
Numbers->ForeColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(0)), static_cast<System::Int32>(static_cast<System::Byte>(0)),
static_cast<System::Int32>(static_cast<System::Byte>(192)));
Numbers->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
turn = 1;
}
else if (turn == 1) {
Numbers->Text = "X";
Numbers->ForeColor = System::Drawing::Color::Red;
Numbers->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
turn = 0;
}
上面的代码应该这样做:
检查用户是否选择了哪个选项(“是”或“否”)。
如果用户选择“是”,则设置match_ended为false.
如果用户选择“是”,请先选择“O”。
如果用户选择“是”,则将获胜者文本重置为默认设置。
如果用户选择“否”,则不做任何事情,只需删除消息框即可。
编码:
this->button2->Location = System::Drawing::Point(80, 50);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(31, 25);
this->button2->TabIndex = 2;
this->button2->Text = L" ";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &TicTacToe::button_click);
将从所有按钮中删除所有文本并将它们重置为默认设置。
最后但同样重要的是,我们将添加一个包含“关于”部分的“帮助”菜单条。
在您添加的代码之后,添加以下代码:
private: System::Void aboutToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
MessageBox::Show(this,
"Tic Tac Toe game, v0.1 (initial release)\n"
"Copyright (C) 2020 David Leal (halfpacho@gmail.com)\n"
"\nThis project is licensed under GNU General Public License v3.0\n"
"Visit https://www.gnu.org/licenses/gpl-3.0.en.html for more information about the license.", "About", MessageBoxButtons::OK, MessageBoxIcon::Information);
}