📄 g01.htm
字号:
{<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 > (SCREEN_HEIGHT/2) && ball_dy > 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 >= paddle_x && x <= paddle_x+PADDLE_WIDTH) &&<br> (y >= paddle_y && y <= 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 >= (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 < 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 < 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 > x1) && (ball_cx < x1+BLOCK_WIDTH) && <br> (ball_cy > y1) && (ball_cy < 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 + -