Randomwürfel
-
Hi,
ich habe hier ein Programm, das einen Würfel mit seinen Augen darstellt.
Man kann auf "Würfeln" drüken und es wird "losgewürfelt". Man sollte darauf pro "Würfelung" einen Ton hören und jeweils eine zufäliige Augenanzahl. Nur leider hört man nur das Gebeepe und am Schluss wird dann die endgültige zufällig ausgewählte Augenanzahl angezeigt. Was kann ich dagegen tun?Unit1.h
//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> #include <ExtCtrls.hpp> //--------------------------------------------------------------------------- class TfrmMain : public TForm { __published: // Von der IDE verwaltete Komponenten TShape *ShapeCube; TShape *Shape1; TShape *Shape2; TShape *Shape3; TShape *Shape4; TShape *Shape5; TShape *Shape6; TShape *Shape7; TShape *Shape8; TShape *Shape9; TButton *btnDice; TButton *btnClose; TTimer *Timer1; void __fastcall btnCloseClick(TObject *Sender); void __fastcall btnDiceClick(TObject *Sender); void __fastcall Timer1Timer(TObject *Sender); private: // Anwender-Deklarationen int augen; void wuerfeln_beep(int, int); void wuerfeln_visuell(int); public: // Anwender-Deklarationen __fastcall TfrmMain(TComponent* Owner); }; void TfrmMain::wuerfeln_visuell(int augen) { switch(augen) { case 1: Shape1->Visible=false; Shape2->Visible=false; Shape3->Visible=false; Shape4->Visible=false; Shape5->Visible=true; Shape6->Visible=false; Shape7->Visible=false; Shape8->Visible=false; Shape9->Visible=false; break; case 2: Shape1->Visible=false; Shape2->Visible=false; Shape3->Visible=false; Shape4->Visible=true; Shape5->Visible=false; Shape6->Visible=true; Shape7->Visible=false; Shape8->Visible=false; Shape9->Visible=false; break; case 3: Shape1->Visible=false; Shape2->Visible=false; Shape3->Visible=true; Shape4->Visible=false; Shape5->Visible=true; Shape6->Visible=false; Shape7->Visible=true; Shape8->Visible=false; Shape9->Visible=false; break; case 4: Shape1->Visible=true; Shape2->Visible=false; Shape3->Visible=true; Shape4->Visible=false; Shape5->Visible=false; Shape6->Visible=false; Shape7->Visible=true; Shape8->Visible=false; Shape9->Visible=true; break; case 5: Shape1->Visible=true; Shape2->Visible=false; Shape3->Visible=true; Shape4->Visible=false; Shape5->Visible=true; Shape6->Visible=false; Shape7->Visible=true; Shape8->Visible=false; Shape9->Visible=true; break; case 6: Shape1->Visible=true; Shape2->Visible=false; Shape3->Visible=true; Shape4->Visible=true; Shape5->Visible=false; Shape6->Visible=true; Shape7->Visible=true; Shape8->Visible=false; Shape9->Visible=true; break; } } void TfrmMain::wuerfeln_beep(int augen, int tonlaenge) { switch(augen) { case 1: Beep(264, tonlaenge); break; case 2: Beep(297, tonlaenge); break; case 3: Beep(330, tonlaenge); break; case 4: Beep(352, tonlaenge); break; case 5: Beep(396, tonlaenge); break; case 6: Beep(440, tonlaenge); break; } } //--------------------------------------------------------------------------- extern PACKAGE TfrmMain *frmMain; //--------------------------------------------------------------------------- #endif
Unit1.cpp
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TfrmMain *frmMain; //--------------------------------------------------------------------------- __fastcall TfrmMain::TfrmMain(TComponent* Owner) : TForm(Owner) { randomize(); } //--------------------------------------------------------------------------- void __fastcall TfrmMain::btnCloseClick(TObject *Sender) { Close(); } //--------------------------------------------------------------------------- void __fastcall TfrmMain::btnDiceClick(TObject *Sender) { int dauer=20; for(int i=0; i<10; ++i) { augen=random(6)+1; wuerfeln_beep(augen, 10); Timer1->Enabled=true; Sleep(dauer); dauer+=50; } } //--------------------------------------------------------------------------- void __fastcall TfrmMain::Timer1Timer(TObject *Sender) { wuerfeln_visuell(augen); Timer1->Enabled=false; } //---------------------------------------------------------------------------
-
1. die Funktion wuerfeln_visuell geht so mal gar nicht
void TfrmMain::wuerfeln_visuell(int augen) { Shape1->Visible=false; Shape2->Visible=false; Shape3->Visible=false; Shape4->Visible=false; Shape5->Visible=false; Shape6->Visible=false; Shape7->Visible=false; Shape8->Visible=false; Shape9->Visible=false; switch(augen) { case 1: Shape5->Visible=true; break; case 2: Shape4->Visible=true; Shape6->Visible=true; break; case 3: Shape3->Visible=true; Shape5->Visible=true; Shape7->Visible=true; break; case 4: Shape1->Visible=true; Shape3->Visible=true; Shape7->Visible=true; Shape9->Visible=true; break; case 5: Shape1->Visible=true; Shape3->Visible=true; Shape5->Visible=true; Shape7->Visible=true; Shape9->Visible=true; break; case 6: Shape1->Visible=true; Shape3->Visible=true; Shape4->Visible=true; Shape6->Visible=true; Shape7->Visible=true; Shape9->Visible=true; break; } }
2. du siehst nichts von der Änderung, weil in der Schleife keine Messages fürs Neuzeichnen verarbeitet werden. Abhilfe wäre das ganze in einen Thread auszulagern und dann zu synchronisieren oder du benutzt Application->ProcessMessages in wuerfeln_visuell:
void TfrmMain::wuerfeln_visuell(int augen) { Shape1->Visible=false; Shape2->Visible=false; Shape3->Visible=false; Shape4->Visible=false; Shape5->Visible=false; Shape6->Visible=false; Shape7->Visible=false; Shape8->Visible=false; Shape9->Visible=false; switch(augen) { .... } Application->ProcessMessages(); }
greetz KN4CK3R
-
Danke!