#include "mainwindow.h"
#include "ui_mainwindow.h"
/*
* 游戏状态
*/
int status = -1;
/*
* 当前正在下落的方块位置,形态
*/
int h, w;
int type, shape;
/*
* 分数
*/
int score = 0;
/*
* 下一个方块的形态
*/
static int next_type, next_shape;
MainWindow::MainWindow(float pw_, float ph_, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
pw(pw_),
ph(ph_)
{
ui->setupUi(this);
ui->widget_game->installEventFilter(this);
ui->widget_tips->installEventFilter(this);
// ui->pushButton_start->installEventFilter(this);
// ui->pushButton_reset->installEventFilter(this);
init_container(pw, ph);
}
/*
* 容器布局
*
* 容器宽度 = 显示器宽度 * pw
* 容器高度 = 显示器高度 * ph
* 宽度值需满足: game_block_nums + tips_block_nums的倍数
* 高度值需为block_size的倍数
*/
void MainWindow::init_container(float pw, float ph)
{
// 获取当前显示器分辨率
const QScreen *primary_screen = QApplication::primaryScreen();
QRect primary_screen_rect = primary_screen->availableGeometry();
screen_width = primary_screen_rect.width();
screen_height = primary_screen_rect.height();
int block_all = game_block_nums + tips_block_nums;
// 容器宽度
container_width = screen_width * pw;
container_width = container_width - container_width % block_all;
// 方块尺寸
block_size = container_width / block_all;
// 容器高度
container_height = screen_height * ph;
container_height = container_height - container_height % block_size;
// 游戏区域
game_container_width = container_width / block_all * game_block_nums;
game_container_height = container_height;
// 提示区域
tips_container_width = container_width / block_all * tips_block_nums;
tips_container_height = container_height;
// 方块起始位置
h_begin = 0;
w_begin = game_container_width / 2 - game_container_width / 2 % block_size;
// 设置窗口尺寸
this->setGeometry(screen_width / 2 - container_width / 2, \
screen_height / 2 - container_height / 2, \
container_width, \
container_height);
this->setFixedSize(container_width, container_height);
// 设置容器内基础控件布局
ui->widget_game->setGeometry(0, \
0, \
game_container_width, \
game_container_height);
ui->widget_tips->setGeometry(game_container_width, \
0, \
tips_container_width, \
tips_container_height);
// 控件宽度 占 1/4提示区宽度
int widget_width = tips_container_width / 4;
// 控件位于提示区中间位置
int y = tips_container_width / 2 - widget_width / 2;
ui->label_next->setGeometry(block_size + 1, \
block_size + 1, \
widget_width, \
block_size); // (y, x, width, height)
ui->label_score->setGeometry(block_size + 1,
block_size * 7 + 1, \
widget_width, \
block_size);
ui->label_score_1->setGeometry(widget_width + block_size, \
block_size * 7 + 1, \
widget_width, \
block_size);
ui->label_score_1->setText("0");
// ui->pushButton_start->setGeometry(y, \
block_size * 8 + 1, \
widget_width, \
block_size);
// ui->pushButton_reset->setGeometry(y, \
block_size * 10 + 1, \
widget_width, \
block_size);
// 为界面添加背景色
QPalette pal;
pal.setColor(QPalette::Window, qcolor_list[game]);
ui->widget_game->setPalette(pal);
pal.setColor(QPalette::Window, qcolor_list[tips]);
ui->widget_tips->setPalette(pal);
// 将墙壁添加到坐标中
coord.init_coord(block_size, game_block_nums, game_container_height / block_size);
coord.add_block_in_coord(0, 0, game_container_height, block_size, wall);
coord.add_block_in_coord(0, game_container_width - block_size, game_container_height, game_container_width, wall);
coord.add_block_in_coord(game_container_height - block_size, 0, game_container_height, game_container_width, wall);
#ifdef DEBUG_SHOW_CONTAINER_PX
printf("\nmainwindow:\n");
printf(" width:%dpx %dblock", this->width(), this->width() / block_size);
printf(" height:%dpx %dblock",this->height(), this->height() / block_size);
printf("\ngame:\n");
printf(" width:%dpx %dblock", ui->widget_game->width(), ui->widget_game->width() / block_size);
printf(" height:%dpx %dblock", ui->widget_game->height(), ui->widget_game->height() / block_size);
printf("\ntips:\n");
printf(" width:%dpx %dblock", ui->widget_tips->width(), ui->widget_tips->width() / block_size);
printf(" height:%dpx %dblock", ui->widget_tips->height(), ui->widget_tips->height() / block_size);
printf("\nblock_size:\n");
printf(" %dpx\n", block_size);
fflush(stdout);
#endif
#ifdef DEBUG_SHOW_INIT_CONTAINER_COORD
printf("\nAfter add container wall:\n");
for (int i = 0; i < coord.container_coord.size(); ++i)
{
for (int j = 0; j < coord.container_coord[i].size(); ++j)
printf("%d ", coord.container_coord[i][j]);
printf("\n");
}
fflush(stdout);
#endif
}
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if (watched == ui->widget_tips && event->type() == QEvent::Paint)
{
draw_tips_block();
return true;
}
if (watched == ui->widget_game && event->type() == QEvent::Paint)
{
draw_drop_block();
draw_game_block();
return true;
}
// if (watched == ui->pushButton_start && event->type() == QEvent::KeyPress)
// {
// return false;
// }
// if (watched == ui->pushButton_reset && event->type() == QEvent::KeyPress)
// {
// return false;
// }
return QMainWindow::eventFilter(watched, event);
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
int key = event->key();
#ifdef DEBUG_SHOW_ASCII_PRESS
printf("Press %d %c\n", key, key);
fflush(stdout);
#endif
// 上
if (key == 16777235 || key == 119 || key == 87)
{
if (legal_judgment(h, w, block.block_maps[type][(shape + 1) % 4]))
{
shape = (shape + 1) % 4;
ui->widget_game->update();
}
return;
}
// 下
if (key == 16777237 || key == 83 || key == 115)
{
if (legal_judgment(h + block_size, w, block.block_maps[type][shape]))
{
h += block_size;
ui->widget_game->update();
}
return;
}
// 左
if (key == 16777234 || key == 65 || key == 97)
{
if (legal_judgment(h, w - block_size, block.block_maps[type][shape]))
{
w -= block_size;
ui->widget_game->update();
}
return;
}
// 右
if (key == 16777236 || key == 68 || key == 100)
{
if (legal_judgment(h, w + block_size, block.block_maps[type][shape]))
{
w += block_size;
ui->widget_game->update();
}
return;
}
// 空格
if (key == 32)
{
for (int i = 0; i < game_container_height / block_size; ++i)
{
if (!legal_judgment(i * block_size, w, block.block_maps[type][shape]))
{
h = (i - 1) * block_size;
coord.add_block_in_coord(h
评论0