void MainWindow
::keyPressEvent(QKeyEvent *event
) {
switch (event->key()) {
case Qt::Key_Up:
if (current_square_y != 0 && is_legal_move(current_square_y, current_square_x, current_square_y -1, current_square_x)){
current_square_y--;
background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
previous_square_cord = {current_square_y, current_square_x};
square_moved++;
}break;
case Qt::Key_Down:
if (current_square_y != maze_height - 1 && is_legal_move(current_square_y, current_square_x, current_square_y + 1, current_square_x)){
current_square_y++;
background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
previous_square_cord = {current_square_y, current_square_x};
square_moved++;
}break;
case Qt::Key_Right:
if (current_square_x != maze_width - 1 && is_legal_move(current_square_y, current_square_x, current_square_y, current_square_x + 1)){
current_square_x++;
background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
previous_square_cord = {current_square_y, current_square_x};
square_moved++;
}break;
case Qt::Key_Left:
if (current_square_x != 0 && is_legal_move(current_square_y, current_square_x, current_square_y, current_square_x - 1)){
current_square_x--;
background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
previous_square_cord = {current_square_y, current_square_x};
square_moved++;
}break;
}
if (won_game()){
on_pushButton_3_clicked();
}
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Up:
if (current_square_y != 0 && is_legal_move(current_square_y, current_square_x, current_square_y -1, current_square_x)){
current_square_y--;
background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
previous_square_cord = {current_square_y, current_square_x};
square_moved++;
}break;
case Qt::Key_Down:
if (current_square_y != maze_height - 1 && is_legal_move(current_square_y, current_square_x, current_square_y + 1, current_square_x)){
current_square_y++;
background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
previous_square_cord = {current_square_y, current_square_x};
square_moved++;
}break;
case Qt::Key_Right:
if (current_square_x != maze_width - 1 && is_legal_move(current_square_y, current_square_x, current_square_y, current_square_x + 1)){
current_square_x++;
background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
previous_square_cord = {current_square_y, current_square_x};
square_moved++;
}break;
case Qt::Key_Left:
if (current_square_x != 0 && is_legal_move(current_square_y, current_square_x, current_square_y, current_square_x - 1)){
current_square_x--;
background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
previous_square_cord = {current_square_y, current_square_x};
square_moved++;
}break;
}
if (won_game()){
QMessageBox::about(this, "t", "You won!!!");
on_pushButton_3_clicked();
}
}
To copy to clipboard, switch view to plain text mode
this->setFocusPolicy(Qt::StrongFocus);
this->setFocusPolicy(Qt::StrongFocus);
To copy to clipboard, switch view to plain text mode
Bookmarks