Exemplo n.º 1
0
        private void PapanPermainan_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar)
            {
            case 'M':
                goto case 'm';

            case 'm':
                BuatBlokBaru();
                _timer.Enabled = true;
                break;

            case 'P':
                goto case 'p';

            case 'p':
                //pause game
                _timer.Enabled = false;
                string s = "GAME PAUSED" + "\nTekan 'm' untuk kembali main";
                MessageBox.Show(s);
                break;
            }

            if (e.KeyChar == 'w' || e.KeyChar == 'a' || e.KeyChar == 's' || e.KeyChar == 'd')
            {
                if (!BisaRotasi(e.KeyChar))
                {
                    e.Handled = true;
                    return;
                }

                _terkiniBlok.HapusDariPapan(this);

                switch (e.KeyChar)
                {
                case 'w':
                    _terkiniBlok.RotateAtas();
                    break;

                case 'a':
                    _terkiniBlok.RotateKiri();
                    break;

                case 's':
                    _terkiniBlok.RotateBawah();
                    break;

                case 'd':
                    _terkiniBlok.RotateKanan();
                    break;
                }
                _terkiniBlok.Draw(this);

                Size s = new Size(Blok.PANJANG * OFFSETPIXEL, Blok.LEBAR * OFFSETPIXEL);
                this.Invalidate(new Rectangle(_terkiniBlok.koordKiriAtas, s));
            }// end if key rotasi
            e.Handled = true;
        }
Exemplo n.º 2
0
        private bool BisaRotasi(char key)
        {
            Blok b = (Blok)_terkiniBlok.Clone();

            switch (key)
            {
            case 'w':
                b.RotateAtas();
                break;

            case 'a':
                b.RotateKiri();
                break;

            case 's':
                b.RotateBawah();
                break;

            case 'd':
                b.RotateKanan();
                break;
            }

            int offset_i = b.koordKiriAtas.Y / OFFSETPIXEL;
            int offset_j = b.koordKiriAtas.X / OFFSETPIXEL;

            for (int i = 0; i < Blok.LEBAR; i++)
            {
                for (int j = 0; j < Blok.PANJANG; j++)
                {
                    if (offset_j + j >= 0 && offset_j + j <= PapanPermainan.LEBAR - 1 && offset_i + i >= 0 && offset_i + i <= PapanPermainan.TINGGI - 1 && b.GetElement(i, j) == true && _terkiniBlok.GetElement(i, j) == false && _elemen[offset_i + i, offset_j + j] > HITAM)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }