public View() { //model = new Model(); //controller = new Controller(model); controllerColor = Color.DarkCyan; timer = new Timer() { Interval = 1000 }; timer.Tick += delegate { input = USERINPUT.DOWN; }; timer.Tick += (s, e) => Invalidate(); timer.Start(); }
public View() { model = new Model(); controller = new Controller(model, this); input = USERINPUT.DOWN; timer = new Timer() { Interval = model.speed }; // 執行Tick事件的速度 timer.Tick += delegate { input = USERINPUT.DOWN; // 在Tick發生之後要做{input = USERINPUT.DOWN;} }; timer.Tick += (s, e) => Invalidate(); // 然後再來要做{Invalidate();} Invalidate裡面會呼叫OnPaint timer.Start(); }
public void UserHasInput() { USERINPUT input = view.input; if (block_falling && currentState == "gameState") { if (!CanFall()) { model.block_turn(); // turn falling block into piling block model.del_lines(); block_falling = false; } } if (!block_falling) { // if block piling, update board and new block cp = np; // replace current piece with next piece cr = nr; np = rd1.Next(7); nr = rd1.Next(4); if (IsGameover(cp, cr)) { currentState = "gameOverState"; } else { model.add_block(cp, cr); block_falling = true; } } if (currentState == "gameOverState") { if (input == USERINPUT.RESTART) { model.BoardInit(); np = rd1.Next(7); nr = rd1.Next(4); model.total_del = 0; model.difficulty = 1; currentState = "gameState"; } else { // actually, do nothing. // if user doesn't click any button, input is DOWN. // if user clicks the NO button, the button will call Application.Exit() } } else if (currentState == "gameState") { switch (input) { case USERINPUT.DOWN: if (CanFall()) { model.block_fall(); } break; case USERINPUT.LEFT: if (CanLeft()) { model.block_left(); } break; case USERINPUT.RIGHT: if (CanRight()) { model.block_right(); } break; case USERINPUT.CCW_ROTATE: // counter clockwise if (CanRotate(cp, cr, 1)) { cr = (cr + 1) % 4; // rotate the piece model.block_rotate(cp, cr); // overwrite with the rotated piece } break; case USERINPUT.CW_ROTATE: // clockwise if (CanRotate(cp, cr, -1)) { if (cr == 0) { cr = 3; } else { cr -= 1; // rotate the piece } model.block_rotate(cp, cr); // overwrite with the rotated piece } // end if break; case USERINPUT.LAND: while (CanFall()) { model.block_land(); } break; } // end switch } }