freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

基于qt的打磚塊游戲的設(shè)計(jì)與實(shí)現(xiàn)論文-閱讀頁(yè)

2024-11-28 05:37本頁(yè)面
  

【正文】 id setShape( const QRectF amp。 void setColor( const QColor amp。 void setDirX( qreal )。 void setSpeed( qreal )。 QColor getColor() const。 qreal getDirY() const。 private: QRectF *shape。 qreal dirX。 qreal speed。 endif shape 用來(lái)指向包含小球位置和尺寸信息的 QRectF 對(duì)象 ,這里使用 QRectF 而不是 QRect,是因?yàn)? QRectF 中使用 qreal 變量來(lái)保存矩形的位置和尺寸信息 ,而 QRect 使用 int 類型變量來(lái)保存信息 ,因此 ,使用 QRectF 能跟精確地保存小球的位置和尺寸信息 ,尤其是當(dāng)對(duì)程序窗口進(jìn)行縮放時(shí) ,這種效果更明顯 ,其中 ,qreal 類型相當(dāng)于 double 類型。此外 ,使用 qreal 類型 dirX 和 dirY 變量來(lái)分別表示小球在水平方向和垂直方向的運(yùn)動(dòng)方向。當(dāng)窗口需要進(jìn)行繪制時(shí) ,這時(shí)窗口就需要使用 getShape 函數(shù)來(lái)得知小球的大小和方位 ,這樣才能正確繪制小球。 include Ball::Ball( qreal x, qreal y, qreal radius, QColor ballColor ){ dirX = 。 speed = 。 color = ballColor。 shapesetRight( shaperight() + dirX )。 shapesetBottom( shapebottom() + dirY )。 setDirX( speed )。 } 在這兒得注意一點(diǎn),就是小球坐標(biāo)的設(shè)置,我是按照常規(guī)的坐標(biāo)系進(jìn)行的設(shè)置,所有,我小球的初始化信息是 1, 1 這樣子。 5. 2. 2 磚塊類 Brick ifndef _BRICK_H_ define _BRICK_H_ include QRect include QColor class Brick{ private: QRectF *shape。 }。還有定義一些要用的方法,比如初始化磚塊顏色和邊界,還有拿到磚塊顏色和邊界。 color = brickColor。newShape ){ shapesetRect( (), (), (), () )。 } 具體的告訴,磚塊的顏色是 qcolor 類,圖形邊界是 Qrectf 類進(jìn)行實(shí)例化。 5. 2. 3 劃槳類 Paddle ifndef _PADDLE_H_ define _PADDLE_H_ include QRect include QColor class Paddle{ public: Paddle( qreal, qreal, qreal, qreal, QColor = Qt::blue )。 QRectF getShape() const。 qreal getLeft() const。 21 qreal getStep() const。 )。 )。 void setRight( qreal )。 void moveLeft()。 private: QRectF *shape。 qreal step。 endif 與 Ball 相同的是他們都有 shape()和 color(),都需要有顏色屬性和邊界的屬性。 include include QColor include QPoint Paddle::Paddle( qreal x, qreal y, qreal width, qreal height, QColor paddleColor ){ shape = new QRectF( x, y, width, height )。 step = 。newShape ){ shapesetRect( (), (), 22 (), () )。 shapesetRight( shaperight() step )。 shapesetRight( shaperight() + step )。 } qreal Paddle::getStep() const{ return step。 初始化劃槳的時(shí)候也是通過(guò)初始化劃槳的坐標(biāo),長(zhǎng),寬來(lái)設(shè)定劃槳的邊界。 class QTimer。 ~Game()。 void setBricksNumbers( int , int )。 設(shè)置了小球的初始移動(dòng)速度,磚塊的布局,數(shù)量,還有劃槳的移動(dòng)時(shí)候的快慢。 void scoreChanged( int )。 void changeColor()。 void movePaddleRight()。 void stopGame()。 protected: void paintEvent( QPaintEvent * )。 這是兩個(gè)事件處理函數(shù),意思是只要有事件的滿足條件發(fā)生,這個(gè)函數(shù)就會(huì)產(chǎn)生作用,開(kāi)始被使用。 private: void createBricks()。 void createBall()。 QColor randomColor()。 Ball *ball。 QList Brick * bricks。 int rows。 int score。 bool isFailure。 double frameHeight。 endif 這一部分就是對(duì)小球,劃槳和磚塊的具體的聲明,還有定時(shí)器,還有一些在實(shí)現(xiàn)文件中要用到的一些變量,比如分?jǐn)?shù)等。 include include QPainter include QTimer include QDebug QDebug 可以輸出一些東西。 include cstdlib include ctime Game::Game( QWidget *parent ) :QWidget( parent ){ 25 isWin = false。 frameWidth = 800。 score = 0。 columns = 10。 createPaddle()。 timer = new QTimer( this )。 connect( timer, SIGNAL( timeout() ), this, SLOT( moveBall() ) )。 } bool 類型變量 isWin 和 isFailure 用來(lái)記錄程序程序結(jié)束時(shí)的勝 利或失敗的情況。 構(gòu)造函數(shù)中調(diào)用 createBrick()、 createPaddle()、 createBall() 函數(shù) ,用 來(lái)對(duì)磚塊、劃漿、和小球進(jìn)行初始化。 最后 ,調(diào)用 changeColor 函數(shù)用來(lái)改變小球、磚塊、劃漿的顏色 ,函數(shù) randomColor 將返回一個(gè)隨機(jī)顏色。 foreach( Brick *brick, bricks ) bricksetColor( randomColor() )。 paddlesetColor( randomColor() )。 } int Game::randomInt( int high ){ double d = (double)rand() / ( (double)RAND_MAX + 1 )。 return k。 void Game::createBricks(){ qreal gap = 。 qreal brickHeight = frameHeight / 4 / ( rows + 1 ) gap。 r rows。 c columns。 ( brick )。 qreal paddleHeight = paddleWidth / 5。 27 } void Game::createBall(){ qreal ballSide = paddlegetShape().height()。 } createBricks 中的變量 gap 表示磚塊之間的距離 ,只有這個(gè)變量是不隨著窗口的縮放而改變的。其中 ,用 QList Brick * 類型的變量 bricks 來(lái)保存 Brick 對(duì)象 ,總共有 5 行 10 列即 50 個(gè) Brick 對(duì)象。 void Game::createBall(){ qreal ballSide = paddlegetShape().height()。 } void Game::resizeEvent( QResizeEvent * ){ qreal scaleWidth = rect().width() / frameWidth。 foreach( Brick *brick, bricks ){ QRectF shape = brickgetShape()。 } QRectF ballShape = ballgetShape()。 ballsetSpeed( ballgetSpeed() * scaleWidth )。 paddlesetShape( adjustShape( paddleShape, scaleWidth, scaleHeight ) )。 frameWidth = rect().width()。 } QRectF Game::adjustShape( QRectF shape, qreal scaleWidth, qreal scaleHeight ){ QRectF newShape( () * scaleWidth, () * scaleHeight, () * scaleWidth, () * scaleHeight )。 } 當(dāng)窗格大小發(fā)生變化時(shí) ,程序?qū)?huì)調(diào)用 resizeEvent 函數(shù) ,這時(shí) rect().width()和 rect().height() 將會(huì)分別返回當(dāng)前窗格的寬度和高度 ,因此 ,scaleWidth 變量表示寬度的縮放因子 ,而 scaleHeight 表示高度的縮放因子 ,adjustShape 函數(shù)能根據(jù)這些縮 放因子對(duì)其接受的參數(shù) shape 進(jìn)行相應(yīng)的縮放 ,并返回一個(gè)經(jīng)縮放之后的 新矩形。 最后 ,需要將當(dāng)前窗格的高度和寬度進(jìn)行保存。 delete paddle。 } 析構(gòu)函數(shù)用來(lái)刪除變量 ,QList 的 takeFirst 函數(shù)能將第一個(gè)對(duì)象指針從進(jìn)行QList 中移除并返回這個(gè)指針 ,因此 ,可以通過(guò) delete 一個(gè) Brick 指針來(lái)刪除一個(gè) Brick 對(duì)象。 if( isWin || isFailure ){ 29 QFont font( Courier, 20, QFont::DemiBold )。 ( font )。 if( isWin ){ int textWidth = ( You are winner! )。 ( textWidth / 2, 0, tr( You are the winner! ) )。 ( Qt::red )。 } timerstop()。 return。 ( ballgetColor() )。 ( paddlegetShape(), paddlegetColor() )。 } paintEvent 函數(shù)由程序自動(dòng)調(diào)用 ,用來(lái)對(duì)窗格進(jìn)行繪制。當(dāng)游戲在進(jìn)行時(shí) ,則需要對(duì)窗格進(jìn)行繪制 ,這時(shí) ,調(diào)用 ( Qt::NoPen )。 void Game::movePaddleLeft(){ 30 if( paddlegetLeft() rect().left() ){ paddlemoveLeft()。 } } void Game::movePaddleRight(){ if( paddlegetRight() rect().right() ){ paddlemoveRight()。 } } void Game::startGame(){ timerstart()。 } 以上 4 個(gè)槽函數(shù)為 Game 提供了外部接口 ,用來(lái)為其它 widget 提供控制劃漿移動(dòng)、開(kāi)始游戲、停止游戲的接
點(diǎn)擊復(fù)制文檔內(nèi)容
高考資料相關(guān)推薦
文庫(kù)吧 www.dybbs8.com
備案圖鄂ICP備17016276號(hào)-1