77范文网 - 专业文章范例文档资料分享平台

基于Qt的俄罗斯方块游戏开发 - 图文(5)

来源:网络收集 时间:2020-05-18 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

curPiece = nextPiece; // 预先随机设置好一下块方块 nextPiece.setRandomShape(); showNextPiece();

// 设置其初始下落的位置,在游戏区顶部中央 curX = BoardWidth / 2 + 1;

curY = BoardHeight-1 + curPiece.minY(); // 判断其是否还能移动,如果不能,则停止游戏 if (!tryMove(curPiece, curX, curY)) { curPiece.setShape(NoShape);

// painter.drawText(rect,tr(\游戏结束\ timer.stop(); isStarted = false; } }

// 展示下一个方块

void TetrixBoard::showNextPiece() {

if (!nextPieceLabel) return;

int dx = nextPiece.maxX() - nextPiece.minX() + 1; int dy = nextPiece.maxY() - nextPiece.minY() + 1;

QPixmap pixmap(dx *squareWidth(), dy*squareHeight()); //映射要显示方块像素

QPainter painter(&pixmap); // 开始绘制该方块 painter.fillRect(pixmap.rect(), nextPieceLabel->palette().background()); // 先绘制要显示方块的背景色 // 再开始绘制方块本身

for (int i = 0; i < 4; ++i) {

int x = nextPiece.x(i) - nextPiece.minX(); int y = nextPiece.y(i) - nextPiece.minY();

drawSquare(painter, x*squareWidth(), y*squareHeight(), nextPiece.shape());

}

nextPieceLabel->setPixmap(pixmap);// 最后加载它 }

// 判断是否还能移动

bool TetrixBoard::tryMove(const TetrixPiece &newPiece, int newX, int newY) {

for (int i = 0; i < 4; ++i) { int x = newX + newPiece.x(i); int y = newY - newPiece.y(i);

if (x < 0 || x >= BoardWidth || y < 0 || y >= BoardHeight) return false;

if (shapeAt(x,y) != NoShape) // 判断当前位置是否有其他方块 return false; }

curPiece = newPiece; curX = newX; curY = newY; update(); return true; }

// int squareWidth() { return contentsRect().width() / BoardWidth; } // int squareHeight() { return contentsRect().height() / BoardHeight; } void TetrixBoard::drawSquare(QPainter &painter, int x,int y,TetrixShape shape) {

// google色彩

static const QRgb colorTable[8] = {

0x000000, 0x1851ce, 0xc61800, 0xefba00, 0x1851ce, 0x1ba823, 0xc61800, 0x606060 };

QColor color = colorTable[int(shape)]; // 填充单元网格的颜色

// void fillRect(int x, int y, int width, int height, const QColor & color) painter.fillRect(x + 1, y + 1, squareWidth() - 2, squareHeight() - 2,

color); painter.setPen(color.light()); // 左上角边框颜色

// void drawLine(int x1, int y1, int x2, int y2) painter.drawLine(x, y + squareHeight() - 1, x, y); painter.drawLine(x, y, x + squareWidth() - 1, y); painter.setPen(color.dark()); // 右下角边框颜色

painter.drawLine(x + 1, y + squareHeight() - 1,

x + squareWidth() - 1, y + squareHeight() - 1); painter.drawLine(x + squareWidth() - 1, y + squareHeight() - 1, x + squareWidth() - 1, y + 1); }

Tetrixpiece.h头文件代码:

// 俄罗斯方块类:方块形状/旋转情况 #ifndef TETRIXPIECE_H #define TETRIXPIECE_H

// 定义一个枚举类型(0-7):无形状、Z字形、S字形、直线形,T字形,田字形形、L字形,翻转L字形

enum TetrixShape { NoShape, ZShape,SShape,LineShape,TShape,SquareShape, LShape,MirroredLShape }; class TetrixPiece { public:

TetrixPiece() { setShape(NoShape); } // inline构造函数:初始设置成NoShape void setRandomShape(); // 设置随机规则

void setShape(TetrixShape shape); // 构造方块形状的数据结构 // 通过inline公有函数成员shape()访问私有数据成员pieceShape TetrixShape shape() const { return pieceShape; } int x(int index) const { return coords[index][0]; } int y(int index) const { return coords[index][1]; } int minX() const; int maxX() const;

int minY() const; int maxY() const;

TetrixPiece rotatedLeft() const; // 左转 private:

void setX(int index, int x) { coords[index][0] = x; } void setY(int index, int y) { coords[index][1] = y; } TetrixShape pieceShape; // 枚举类型创建一个该枚举类型对象 int coords[4][2]; };

#endif // TETRIXPIECE_H

Tetrixpiece.cpp程序代码: #include #include \#include #include #include

// 伪随机函数:利用当前系统时间增加随机性 void TetrixPiece::setRandomShape() {

//QTime time = QTime::currentTime();

//qsrand( time.msec() + time.second()*1000 );// 重设随机种子:当前时间为随机变量

setShape(TetrixShape(qrand()%7 + 1)); // 共六种方块形状 }

void TetrixPiece::setShape(TetrixShape shape) {

// 按TetrixShape枚举顺序构建:

// 除NoShape外,每个形状由4个小方块组成,这里每行的四个坐标即4个小方块的坐标,其中横向为X,纵向为Y

// ZShape SShape LineShape TShape SquareShape LShape MirroredLShape

// -1 0 1 2 -1 0 1 2 -1 0 1 2 -1 0 1 2 -1 0 1 2 -1 0 1 2 -1 0 1 2

// -1 * -1 * -1 * -1 -1 -1 * * -1 * * // 0 * * 0 * * 0 * 0 * * * 0 * * 0 * 0 * // 1 * 1 * 1 * 1 * 1 * * 1 * 1 * // 2 2 2 * 2 2 2 2 static const int coordsTable[8][4][2] = {

{ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },// NoShape { { 0, -1 }, { 0, 0 }, { -1, 0 }, { -1, 1 } }, { { 0, -1 }, { 0, 0 }, { 1, 0 }, { 1, 1 } }, { { 0, -1 }, { 0, 0 }, { 0, 1 }, { 0, 2 } }, { { -1, 0 }, { 0, 0 }, { 1, 0 }, { 0, 1 } }, { { 0, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } }, { { -1, -1 }, { 0, -1 }, { 0, 0 }, { 0, 1 } }, { { 1, -1 }, { 0, -1 }, { 0, 0 }, { 0, 1 } } };

for (int i=0; i<4; i++) { for (int j=0; j<2; ++j)

coords[i][j] = coordsTable[shape][i][j]; }

pieceShape = shape; }

// 获取最小X坐标值,QtGlobal::qMin int TetrixPiece::minX() const {

int min = coords[0][0]; for (int i = 1; i < 4; ++i) min = qMin(min, coords[i][0]); return min; }

// 获取最大X坐标值,QtGlobal::qMax int TetrixPiece::maxX() const {

int max = coords[0][0]; for (int i = 1; i < 4; ++i) max = qMax(max, coords[i][0]);

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库基于Qt的俄罗斯方块游戏开发 - 图文(5)在线全文阅读。

基于Qt的俄罗斯方块游戏开发 - 图文(5).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/jiaoyu/1053378.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: