This is a game, you need to have time to press the button while it is active, but through it disappears when another is active (for the summer practice at the university, an additional task). Guys, well, the question is: is there any process in the form that is being requested all the time, not the same as when loading, when it runs through it once and everything, and so that it is updated all the time? Well, in general, I initially did a start in the button, but then I’m not sure that other buttons can be pressed during this, and if you can check how the button is pressed while the game is playing (a minute is given to it). And how to find out that the timer has finished. The game described in the question (raw)

#pragma once #include "ctime" #include "iostream" using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; namespace ЛСтняяпрактика { /// <summary> /// Π‘Π²ΠΎΠ΄ΠΊΠ° для Form_Game /// /// Π’Π½ΠΈΠΌΠ°Π½ΠΈΠ΅! ΠŸΡ€ΠΈ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠΈ ΠΈΠΌΠ΅Π½ΠΈ этого класса Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎ Ρ‚Π°ΠΊΠΆΠ΅ ΠΈΠ·ΠΌΠ΅Π½ΠΈΡ‚ΡŒ /// свойство ΠΈΠΌΠ΅Π½ΠΈ Ρ„Π°ΠΉΠ»Π° рСсурсов ("Resource File Name") для срСдства компиляции управляСмого рСсурса, /// связанного со всСми Ρ„Π°ΠΉΠ»Π°ΠΌΠΈ с Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΠ΅ΠΌ .resx, ΠΎΡ‚ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… зависит Π΄Π°Π½Π½Ρ‹ΠΉ класс. Π’ ΠΏΡ€ΠΎΡ‚ΠΈΠ²Π½ΠΎΠΌ случаС, /// конструкторы Π½Π΅ смогут ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½ΠΎ Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ с Π»ΠΎΠΊΠ°Π»ΠΈΠ·ΠΎΠ²Π°Π½Π½Ρ‹ΠΌΠΈ /// рСсурсами, сопоставлСнными Π΄Π°Π½Π½ΠΎΠΉ Ρ„ΠΎΡ€ΠΌΠ΅. /// </summary> public ref class Form_Game : public System::Windows::Forms::Form { public: Form_Game(void) { InitializeComponent(); // //TODO: Π΄ΠΎΠ±Π°Π²ΡŒΡ‚Π΅ ΠΊΠΎΠ΄ конструктора // } protected: /// <summary> /// ΠžΡΠ²ΠΎΠ±ΠΎΠ΄ΠΈΡ‚ΡŒ всС ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌΡ‹Π΅ рСсурсы. /// </summary> ~Form_Game() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; protected: private: System::Windows::Forms::Button^ button2; private: System::Windows::Forms::Button^ button3; private: System::Windows::Forms::Button^ button4; private: System::Windows::Forms::Button^ button5; private: System::Windows::Forms::Button^ button6; private: System::Windows::Forms::Button^ button7; private: System::Windows::Forms::Button^ button8; private: System::Windows::Forms::Button^ button9; private: System::Windows::Forms::Button^ button10; private: System::Windows::Forms::Button^ button11; private: System::Windows::Forms::Button^ button12; private: System::Windows::Forms::Button^ button13; private: System::Windows::Forms::Button^ button14; private: System::Windows::Forms::Button^ button15; private: System::Windows::Forms::Button^ button16; private: System::Windows::Forms::Button^ button_start; private: System::Windows::Forms::Button^ button_out; private: System::Windows::Forms::Timer^ timer_game; private: System::ComponentModel::IContainer^ components; private: /// <summary> /// ВрСбуСтся пСрСмСнная конструктора. /// </summary> #pragma region Windows Form Designer generated code /// <summary> /// ΠžΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½Ρ‹ΠΉ ΠΌΠ΅Ρ‚ΠΎΠ΄ для ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠΈ конструктора - Π½Π΅ измСняйтС /// содСрТимоС Π΄Π°Π½Π½ΠΎΠ³ΠΎ ΠΌΠ΅Ρ‚ΠΎΠ΄Π° ΠΏΡ€ΠΈ ΠΏΠΎΠΌΠΎΡ‰ΠΈ Ρ€Π΅Π΄Π°ΠΊΡ‚ΠΎΡ€Π° ΠΊΠΎΠ΄Π°. /// </summary> void InitializeComponent(void) { this->components = (gcnew System::ComponentModel::Container()); this->button1 = (gcnew System::Windows::Forms::Button()); this->button2 = (gcnew System::Windows::Forms::Button()); this->button3 = (gcnew System::Windows::Forms::Button()); this->button4 = (gcnew System::Windows::Forms::Button()); this->button5 = (gcnew System::Windows::Forms::Button()); this->button6 = (gcnew System::Windows::Forms::Button()); this->button7 = (gcnew System::Windows::Forms::Button()); this->button8 = (gcnew System::Windows::Forms::Button()); this->button9 = (gcnew System::Windows::Forms::Button()); this->button10 = (gcnew System::Windows::Forms::Button()); this->button11 = (gcnew System::Windows::Forms::Button()); this->button12 = (gcnew System::Windows::Forms::Button()); this->button13 = (gcnew System::Windows::Forms::Button()); this->button14 = (gcnew System::Windows::Forms::Button()); this->button15 = (gcnew System::Windows::Forms::Button()); this->button16 = (gcnew System::Windows::Forms::Button()); this->button_start = (gcnew System::Windows::Forms::Button()); this->button_out = (gcnew System::Windows::Forms::Button()); this->timer_game = (gcnew System::Windows::Forms::Timer(this->components)); this->SuspendLayout(); // // button1 // this->button1->Location = System::Drawing::Point(57, 71); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(75, 64); this->button1->TabIndex = 0; this->button1->UseVisualStyleBackColor = true; // // button2 // this->button2->Location = System::Drawing::Point(166, 71); this->button2->Name = L"button2"; this->button2->Size = System::Drawing::Size(75, 64); this->button2->TabIndex = 1; this->button2->UseVisualStyleBackColor = true; // // button3 // this->button3->Location = System::Drawing::Point(276, 71); this->button3->Name = L"button3"; this->button3->Size = System::Drawing::Size(75, 64); this->button3->TabIndex = 2; this->button3->UseVisualStyleBackColor = true; // // button4 // this->button4->Location = System::Drawing::Point(382, 71); this->button4->Name = L"button4"; this->button4->Size = System::Drawing::Size(75, 64); this->button4->TabIndex = 3; this->button4->UseVisualStyleBackColor = true; // // button5 // this->button5->Location = System::Drawing::Point(57, 153); this->button5->Name = L"button5"; this->button5->Size = System::Drawing::Size(75, 64); this->button5->TabIndex = 4; this->button5->UseVisualStyleBackColor = true; // // button6 // this->button6->Location = System::Drawing::Point(166, 153); this->button6->Name = L"button6"; this->button6->Size = System::Drawing::Size(75, 64); this->button6->TabIndex = 5; this->button6->UseVisualStyleBackColor = true; // // button7 // this->button7->Location = System::Drawing::Point(276, 153); this->button7->Name = L"button7"; this->button7->Size = System::Drawing::Size(75, 64); this->button7->TabIndex = 6; this->button7->UseVisualStyleBackColor = true; // // button8 // this->button8->Location = System::Drawing::Point(382, 153); this->button8->Name = L"button8"; this->button8->Size = System::Drawing::Size(75, 64); this->button8->TabIndex = 7; this->button8->UseVisualStyleBackColor = true; // // button9 // this->button9->Location = System::Drawing::Point(57, 243); this->button9->Name = L"button9"; this->button9->Size = System::Drawing::Size(75, 64); this->button9->TabIndex = 8; this->button9->UseVisualStyleBackColor = true; // // button10 // this->button10->Location = System::Drawing::Point(166, 243); this->button10->Name = L"button10"; this->button10->Size = System::Drawing::Size(75, 64); this->button10->TabIndex = 9; this->button10->UseVisualStyleBackColor = true; // // button11 // this->button11->Location = System::Drawing::Point(276, 243); this->button11->Name = L"button11"; this->button11->Size = System::Drawing::Size(75, 64); this->button11->TabIndex = 10; this->button11->UseVisualStyleBackColor = true; // // button12 // this->button12->Location = System::Drawing::Point(382, 243); this->button12->Name = L"button12"; this->button12->Size = System::Drawing::Size(75, 64); this->button12->TabIndex = 11; this->button12->UseVisualStyleBackColor = true; // // button13 // this->button13->Location = System::Drawing::Point(57, 335); this->button13->Name = L"button13"; this->button13->Size = System::Drawing::Size(75, 64); this->button13->TabIndex = 12; this->button13->UseVisualStyleBackColor = true; // // button14 // this->button14->Location = System::Drawing::Point(166, 335); this->button14->Name = L"button14"; this->button14->Size = System::Drawing::Size(75, 64); this->button14->TabIndex = 13; this->button14->UseVisualStyleBackColor = true; // // button15 // this->button15->Location = System::Drawing::Point(276, 335); this->button15->Name = L"button15"; this->button15->Size = System::Drawing::Size(75, 64); this->button15->TabIndex = 14; this->button15->UseVisualStyleBackColor = true; // // button16 // this->button16->Location = System::Drawing::Point(382, 335); this->button16->Name = L"button16"; this->button16->Size = System::Drawing::Size(75, 64); this->button16->TabIndex = 15; this->button16->UseVisualStyleBackColor = true; // // button_start // this->button_start->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 11, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(204))); this->button_start->Location = System::Drawing::Point(-1, -1); this->button_start->Name = L"button_start"; this->button_start->Size = System::Drawing::Size(133, 40); this->button_start->TabIndex = 16; this->button_start->Text = L"Π‘Ρ‚Π°Ρ€Ρ‚!"; this->button_start->UseVisualStyleBackColor = true; this->button_start->Click += gcnew System::EventHandler(this, &Form_Game::button_start_Click); // // button_out // this->button_out->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 11, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(204))); this->button_out->Location = System::Drawing::Point(382, -1); this->button_out->Name = L"button_out"; this->button_out->Size = System::Drawing::Size(133, 40); this->button_out->TabIndex = 17; this->button_out->Text = L"Π’Ρ‹Ρ…ΠΎΠ΄!"; this->button_out->UseVisualStyleBackColor = true; this->button_out->Click += gcnew System::EventHandler(this, &Form_Game::button_out_Click); // // timer_game // this->timer_game->Interval = 60000; // // Form_Game // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(514, 436); this->Controls->Add(this->button_out); this->Controls->Add(this->button_start); this->Controls->Add(this->button16); this->Controls->Add(this->button15); this->Controls->Add(this->button14); this->Controls->Add(this->button13); this->Controls->Add(this->button12); this->Controls->Add(this->button11); this->Controls->Add(this->button10); this->Controls->Add(this->button9); this->Controls->Add(this->button8); this->Controls->Add(this->button7); this->Controls->Add(this->button6); this->Controls->Add(this->button5); this->Controls->Add(this->button4); this->Controls->Add(this->button3); this->Controls->Add(this->button2); this->Controls->Add(this->button1); this->Name = L"Form_Game"; this->Text = L"Form_Game"; this->Load += gcnew System::EventHandler(this, &Form_Game::Form_Game_Load); this->ResumeLayout(false); } #pragma endregion void BtnFalse() { this->button_out->Enabled = false; this->button_start->Enabled = true; this->button1->Enabled = false; this->button2->Enabled = false; this->button3->Enabled = false; this->button4->Enabled = false; this->button5->Enabled = false; this->button6->Enabled = false; this->button7->Enabled = false; this->button8->Enabled = false; this->button9->Enabled = false; this->button10->Enabled = false; this->button11->Enabled = false; this->button12->Enabled = false; this->button13->Enabled = false; this->button14->Enabled = false; this->button15->Enabled = false; this->button16->Enabled = false; } private: System::Void Form_Game_Load(System::Object^ sender, System::EventArgs^ e) { BtnFalse(); } private: System::Void button_start_Click(System::Object^ sender, System::EventArgs^ e) { this->button_out->Enabled = true; this->button_start->Enabled = false; this->timer_game->Start(); int limit = 60000; while (/*ΠΏΠΎΠΊΠ° врСмя Π² Ρ‚Π°ΠΉΠΌΠ΅Ρ€Π΅ Π½Π΅ ноль, ΠΊΠ°ΠΊ?*/) { srand(time(0)); int cube = 1 + rand()%16; // для Π°ΠΊΡ‚ΠΈΠ²Π°Ρ†ΠΈΠΈ Ρ€Π°Π½Π΄ΠΎΠΌΠ½ΠΎΠΉ ΠΊΠ½ΠΎΠΏΠΊΠΈ if ((this->button_out->Tag) == 1) { //пытался ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΡƒ Π½Π°ΠΆΠ°Ρ‚ΠΈ ΠΊΠ½ΠΎΠΏΠΊΠΈ this->button_start->Enabled = true; break; } } this->button1->Enabled = true;// Ρ€Π΅ΡˆΠΈΠ» ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΈΡ‚ΡŒ ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½ΠΎΡΡ‚ΡŒ Ρ€Π°Π±ΠΎΡ‚Ρ‹ } private: System::Void button_out_Click(System::Object^ sender, System::EventArgs^ e) { this->button_out->Tag = 1; } }; } 

The copy-and-paste code may be crooked, but everything seems to be clear, the first question :)

1 answer 1

You can use the start button to spin your actions, constantly calling the message queue to process the interface. You can do it in a separate thread.

The most correct thing for such a task is to make a timer and work on the event of the timer, start it at intervals of milliseconds at 20-100 and let it click ...

By timer, you need to run it at small intervals, and handle its event, accumulate a total time counter in it. In the microtics, just light and extinguish the buttons ...

  • Yes, well, I’ll do it, but how do you know that the timer action is over? The Tick event occurs when it is completed, and then you need 2 timers, one will activate a new button, and the other will notify you when the game ends, right? - O_o
  • And how to get the timer value, well, that is, with what I compare, for example, I want the value of ticks not to exceed 60,000 milliseconds, and in the interval will be 1000 milliseconds, how to implement it? - O_o
  • one
    Do not need 2 timers. Need 1 timer for 50 ms. When you press the start button in a variable, save the total playing time in ticks (60 * 1000/50) - 60 seconds and start the timer. Each tick you subtract from this variable 1, as it reached zero - the game is over, stop the timer. In the same function, with another counter, implement the button enable timeouts. - Andrey Golikov
  • Yes, I have already done it, I also wrote the limit variable and assigned a value to it, which means you can choose the complexity from time, and in tick I take away 1, and deactivate the button that will be selected in the same tick if limit! = 0 using the random mode and is activated, as well re-activated timer. - O_o