⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 g01.htm

📁 一本介绍如何开发游戏,如何进行游戏编程的非常值得一看的好书.
💻 HTM
📖 第 1 页 / 共 5 页
字号:
              {<br>              // draw next block (if there is one)<br>              if (blocks[row][col]!=0)<br>              {<br>              // draw block <br>              Draw_Rectangle(x1-4,y1+4,<br>              x1+BLOCK_WIDTH-4,y1+BLOCK_HEIGHT+4,0);</p>            <p> Draw_Rectangle(x1,y1,x1+BLOCK_WIDTH,<br>              y1+BLOCK_HEIGHT,blocks[row][col]);<br>              } // end if</p>            <p> // advance column position<br>              x1+=BLOCK_X_GAP;<br>              } // end for col</p>            <p> // advance to next row position<br>              y1+=BLOCK_Y_GAP;</p>            <p> } // end for row</p>            <p>} // end Draw_Blocks</p>            <p>///////////////////////////////////////////////////////////////</p>            <p>void Process_Ball(void)<br>              {<br>              // this function tests if the ball has hit a block or the paddle<br>              // if so, the ball is bounced and the block is removed from <br>              // the playfield note: very cheesy collision algorithm :)</p>            <p>// first test for ball block collisions</p>            <p>// the algorithm basically tests the ball against each <br>              // block's bounding box this is inefficient, but easy to <br>              // implement, later we'll see a better way</p>            <p>int x1 = BLOCK_ORIGIN_X, // current rendering position<br>              y1 = BLOCK_ORIGIN_Y; </p>            <p>int ball_cx = ball_x+(BALL_SIZE/2), // computer center of ball<br>              ball_cy = ball_y+(BALL_SIZE/2);</p>            <p>// test of the ball has hit the paddle<br>              if (ball_y &gt; (SCREEN_HEIGHT/2) &amp;&amp; ball_dy &gt; 0)<br>              {<br>              // extract leading edge of ball<br>              int x = ball_x+(BALL_SIZE/2);<br>              int y = ball_y+(BALL_SIZE/2);</p>            <p> // test for collision with paddle<br>              if ((x &gt;= paddle_x &amp;&amp; x &lt;= paddle_x+PADDLE_WIDTH)               &amp;&amp;<br>              (y &gt;= paddle_y &amp;&amp; y &lt;= paddle_y+PADDLE_HEIGHT))<br>              {<br>              // reflect ball<br>              ball_dy=-ball_dy;</p>            <p> // push ball out of paddle since it made contact<br>              ball_y+=ball_dy;</p>            <p> // add a little english to ball based on motion of paddle<br>              if (KEY_DOWN(VK_RIGHT))<br>              ball_dx-=(rand()%3);<br>              else<br>              if (KEY_DOWN(VK_LEFT))<br>              ball_dx+=(rand()%3);<br>              else<br>              ball_dx+=(-1+rand()%3);<br>              <br>              // test if there are no blocks, if so send a message<br>              // to game loop to start another level<br>              if (blocks_hit &gt;= (NUM_BLOCK_ROWS*NUM_BLOCK_COLUMNS))<br>              {<br>              game_state = GAME_STATE_START_LEVEL;<br>              level++;<br>              } // end if</p>            <p> // make a little noise<br>              MessageBeep(MB_OK);</p>            <p> // return<br>              return; </p>            <p> } // end if</p>            <p> } // end if</p>            <p>// now scan thru all the blocks and see of ball hit blocks<br>              for (int row=0; row &lt; NUM_BLOCK_ROWS; row++)<br>              { <br>              // reset column position<br>              x1 = BLOCK_ORIGIN_X;</p>            <p> // scan this row of blocks<br>              for (int col=0; col &lt; NUM_BLOCK_COLUMNS; col++)<br>              {<br>              // if there is a block here then test it against ball<br>              if (blocks[row][col]!=0)<br>              {<br>              // test ball against bounding box of block<br>              if ((ball_cx &gt; x1) &amp;&amp; (ball_cx &lt; x1+BLOCK_WIDTH) &amp;&amp;               <br>              (ball_cy &gt; y1) &amp;&amp; (ball_cy &lt; y1+BLOCK_HEIGHT))<br>              {<br>              // remove the block<br>              blocks[row][col] = 0; </p>            <p> // increment global block counter, so we know <br>              // when to start another level up<br>              blocks_hit++;</p>            <p> // bounce the ball<br>              ball_dy=-ball_dy;</p>            <p> // add a little english<br>              ball_dx+=(-1+rand()%3);</p>            <p> // make a little noise<br>              MessageBeep(MB_OK);</p>            <p> // add some points<br>              score+=5*(level+(abs(ball_dx)));</p>            <p> // that's it -- no more block<br>              return;</p>            <p> } // end if </p>            <p> } // end if</p>            <p> // advance column position<br>              x1+=BLOCK_X_GAP;<br>              } // end for col</p>            <p> // advance to next row position<br>              y1+=BLOCK_Y_GAP;</p>            <p> } // end for row</p>            <p>} // end Process_Ball</p>            <p>///////////////////////////////////////////////////////////////</p>            <p>int Game_Main(void *parms)<br>              {<br>              // this is the workhorse of your game it will be called<br>              // continuously in real-time this is like main() in C<br>              // all the calls for you game go here!</p>            <p>char buffer[80]; // used to print text</p>            <p>// what state is the game in? <br>              if (game_state == GAME_STATE_INIT)<br>              {<br>              // initialize everything here graphics<br>              DD_Init(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP);</p>            <p> // seed the random number generator<br>              // so game is different each play<br>              srand(Start_Clock());</p>            <p> // set the paddle position here to the middle bottom<br>              paddle_x = PADDLE_START_X;<br>              paddle_y = PADDLE_START_Y;</p>            <p> // set ball position and velocity<br>              ball_x = 8+rand()%(SCREEN_WIDTH-16);<br>              ball_y = BALL_START_Y;<br>              ball_dx = -4 + rand()%(8+1);<br>              ball_dy = 6 + rand()%2;</p>            <p> // transition to start level state<br>              game_state = GAME_STATE_START_LEVEL;</p>            <p> } // end if <br>              ////////////////////////////////////////////////////////////////<br>              else<br>              if (game_state == GAME_STATE_START_LEVEL)<br>              {<br>              // get a new level ready to run</p>            <p> // initialize the blocks<br>              Init_Blocks();</p>            <p> // reset block counter<br>              blocks_hit = 0;</p>            <p> // transition to run state<br>              game_state = GAME_STATE_RUN;</p>            <p> } // end if<br>              ///////////////////////////////////////////////////////////////<br>              else<br>              if (game_state == GAME_STATE_RUN)<br>              {<br>              // start the timing clock<br>              Start_Clock();</p>            <p> // clear drawing surface for the next frame of animation<br>              Draw_Rectangle(0,0,SCREEN_WIDTH-1, SCREEN_HEIGHT-1,200);</p>         

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -