// TODO: Add functionality for Spacebar to start/pause game. void BreakoutCanvas::keyPressEvent(QKeyEvent* event){ if(event->key() == Qt::Key_Left) { paddleLeft = true; } if(event->key() == Qt::Key_Right) { paddleRight = true; } if(event->key() == Qt::Key_Space) { if (state == PLAYING) { timer->stop(); state = PAUSED; needsButtonChange("Resume"); } else if (state == PAUSED) { timer->start(); state = PLAYING; needsButtonChange("Pause"); } else if (state == AFTER_PLAY) { reset(); needsButtonChange("Start"); } else if (state == BEFORE_PLAY) { timer->start(); state = PLAYING; needsButtonChange("Pause"); } } if(event->key() == Qt::Key_Q) { exit(0); } }
void BreakoutCanvas::buttonPress() { switch(state) { case BEFORE_PLAY: timer->start(); needsButtonChange("Pause"); // needsStatusChange(TOTAL_BRICKS); state = PLAYING; break; case PLAYING: timer->stop(); needsButtonChange("Resume"); // needsStatusChange(remainingBricks); state = PAUSED; break; case PAUSED: timer->start(); needsButtonChange("Pause"); // needsStatusChange(remainingBricks); state = PLAYING; break; case AFTER_PLAY: reset(); needsButtonChange("Start"); // needsStatusChange(remainingBricks); state = BEFORE_PLAY; break; } setFocus(Qt::OtherFocusReason); // make sure that the canvas gets keystrokes }
void BreakoutCanvas::buttonPress() { switch(state) { case BEFORE_PLAY: timer->start(); needsButtonChange("Pause"); state = PLAYING; break; case PLAYING: timer->stop(); needsButtonChange("Resume"); state = PAUSED; break; case PAUSED: timer->start(); needsButtonChange("Pause"); state = PLAYING; break; case LOSS: reset(); needsButtonChange("Start"); state = BEFORE_PLAY; break; case WIN: reset(); needsButtonChange("Start"); state = BEFORE_PLAY; break; } setFocus(Qt::OtherFocusReason); // make sure that the canvas gets keystrokes }
void BreakoutCanvas::timerTicked() { // this will get called every 50 milliseconds // first, move the ball: if(goingRight) { ball.rx()++; } else { ball.rx()--; } if(goingDown) { ball.ry()++; } else { ball.ry()--; } // should the ball bounce? if(ball.x() + BALL_RADIUS >= WIDTH) { goingRight = false; } if(ball.x() - BALL_RADIUS <= 0) { goingRight = true; } if(ball.y() - BALL_RADIUS <= 0) { goingDown = true; } // ball bounces off and breaking blocks for(int i = 0; i < 10; i++) { if(ball.y() - BALL_RADIUS <= PADDLE_HEIGHT && ball.x() >= blocks[i] && ball.x() <= blocks [i] + BLOCK_WIDTH && destroyed[i] == false) { goingDown = true; destroyed[i] = true; } } // but the player's paddle might not be -- check if(ball.y() + BALL_RADIUS >= HEIGHT - PADDLE_HEIGHT && ball.x() >= playerX && ball.x() <= playerX + PADDLE_WIDTH) { goingDown = false; } // move the computer's paddle: /* compX = ball.x() - PADDLE_WIDTH / 2; if(compX <= 0) { compX = 0; } if(compX + PADDLE_WIDTH >= WIDTH) { compX = WIDTH - PADDLE_WIDTH; }*/ // move the player's paddle: if(paddleLeft) { playerX--; } if(paddleRight) { playerX++; } if(playerX <= 0) { playerX = 0; } if(playerX + PADDLE_WIDTH >= WIDTH) { playerX = WIDTH - PADDLE_WIDTH; } // did the player lose? if(ball.y() >= HEIGHT) { timer->stop(); state = LOSS; needsButtonChange("Restart"); } // did the player win? bool win = true; for( int i = 0; i < 10; i++) { if(destroyed[i] == false) win = false; } if( win == true) { timer->stop(); state = WIN; needsButtonChange("Restart"); } update(); // force a redraw }
// TODO: Add functionality for removing bricks when ball bounces. // TODO: Change end game check to include win for all bricks gone. void BreakoutCanvas::timerTicked() { // this will get called every 50 milliseconds // first, move the ball: if(goingRight) { ball.rx()++; } else { ball.rx()--; } if(goingDown) { ball.ry()++; } else { ball.ry()--; } // should the ball bounce? if(ball.x() + BALL_RADIUS >= WIDTH) { goingRight = false; } if(ball.x() - BALL_RADIUS <= 0) { goingRight = true; } // the computer paddle is always in the right spot if(ball.y() - BALL_RADIUS <= PADDLE_HEIGHT) { goingDown = true; } // but the player's paddle might not be -- check if(ball.y() + BALL_RADIUS >= HEIGHT - PADDLE_HEIGHT && ball.x() >= playerX && ball.x() <= playerX + PADDLE_WIDTH) { goingDown = false; } // move the computer's paddle: compX = ball.x() - PADDLE_WIDTH / 2; if(compX <= 0) { compX = 0; } if(compX + PADDLE_WIDTH >= WIDTH) { compX = WIDTH - PADDLE_WIDTH; } // move the player's paddle: if(paddleLeft) { playerX--; } if(paddleRight) { playerX++; } if(playerX <= 0) { playerX = 0; } if(playerX + PADDLE_WIDTH >= WIDTH) { playerX = WIDTH - PADDLE_WIDTH; } // did the player lose? if(ball.y() >= HEIGHT) { timer->stop(); state = AFTER_PLAY; needsButtonChange("Restart"); } update(); // force a redraw }