0

Im trying to use arrow keys to move tetris block left & right and rotate, but its not working while the block falls down the gameGrid. Here is my code.

//Here is my Game cycle in the timer handler

private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) 
         {  

            mainGameBoard->drawGrid();
            newBlock->draw();
            newBlock->moveDown();

            if(newBlock->canMoveDown()==false)
            {
                                //If block hits bottom or block, add block to grid
                newBlock->addMySelfToGameBoard();

                                //Update the grid by deleting full rows & move
                                //the game grid down
                mainGameBoard->updateGrid();

                                //Create new block
                                //Havnt sorted yet


            }

         }

Here is my code for using the arrow keys to move & rotate the block, the move methods and rotate method are in the TTetrisBlock class.

private: System::Void Form1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) 
             {
                 if(e->KeyData == Keys::Left)
                 {
                     newBlock->moveLeft();
                 }

                 if(e->KeyData == Keys::Right)
                 {
                     newBlock->moveRight();
                 }

                 if(e->KeyData == Keys::Up)
                 {
                     newBlock->rotate();
                 }

             }

//Now when the block is moving down the game grid on timer tick the arrow keys when pressed are not moving the object. but if i put newBlock->moveLeft() in the timer the block will start moving left. So my moveLeft() method is working. Can some on help please - im a student studying C++.Here is my moveLeft() method:

void TTetrisBlock::moveLeft()
{
    array<Point>^ temporaryCopy = gcnew array<Point>(4);

    for(int i=0;i<mySquares->Length;i++)
    {
        //Set future move
        temporaryCopy[i].X = mySquares[i].X-1;
        temporaryCopy[i].Y = mySquares[i].Y;
    }

    if(checkBounds(temporaryCopy) == true)
    {
        for(int i=0;i<temporaryCopy->Length;i++)
        {
            mySquares[i].X = temporaryCopy[i].X;
            mySquares[i].Y = temporaryCopy[i].Y;
        }
    }

}
Nolan Ratu
  • 31
  • 3
  • It worked for me to not even bother with arrow keys when I designed Tetris. I just used WASD. –  Aug 25 '12 at 03:23

1 Answers1

0

is Form1_KeyDown regustered as a callback for the form? do this to check:

private: System::Void Form1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) 
             {
                 System::Console::WriteLine(S"KeyDown is working");

             }
Nicolas Brown
  • 1,546
  • 1
  • 10
  • 17
  • i'm assuming the console window isnt showing so replace System::Console::WriteLine(S"KeyDown is working"); with MessageBox::Show("keydown"); – Nicolas Brown Aug 25 '12 at 22:40
  • if it's not working then the problem is that you didnt bind your function to the KeyDown event. Add this to the InitilizeComponent function of the form: this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::Form1_KeyDown); – Nicolas Brown Aug 25 '12 at 22:41