/ Published in: C++
Expand |
Embed | Plain Text
// DEFINES {{{1 #define m_ROTATE 1 #define m_DOWN 2 #define m_LEFT 3 #define m_RIGHT 4 #define m_HEIGH 16 // plane's height #define m_WIDTH 8 //plane's width #define m_LOCAL_SIZE 5; // default: 5 //}}}1 // GLOBALS {{{1 int local_buffer[m_LOCAL_SIZE][m_LOCAL_SIZE]; int local_buffer_two[m_LOCAL_SIZE][m_LOCAL_SIZE]; int global_buffer[m_HEIGH][m_WIDTH]; // }}}1 void special(int key, int x, int y) // {{{1 { switch ( key ) // {{{2 { case GLUT_KEY_UP: // {{{3 rotate(); break; // }}}3 case GLUT_KEY_DOWN: //{{{3 moveDown(); break; // }}}3 case GLUT_KEY_LEFT: // {{{3 if ( currentX > 0 ) { currentX++; if ( checkIntersections() ) currentX--: } break; // }}}3 case GLUT_KEY_RIGHT: // {{{3 if ( currentX < m_WIDTH ) { currentX--; if ( checkIntersections() ) currentX++: } break; // }}}3 } // }}}2 //////////// checkNextLineCollisions(); //////////// } // }}}1 void timer(int value) // {{{1 { currentY++; //move piece 1 line down checkIntersections(); glutPostRedisplay(); glutTimerFunc( speed, timer, 1); } // }}}1 bool checkIntersections() // {{{1 { for (i=0; i<m_LOCAL_SIZE; i++ ) for (j=0; j<m_LOCAL_SIZE; j++ ) if ( (global_buffer [ currentX - m_LOCAL_SIZE/2 + i ] [ currentY - m_LOCAL_SIZE/2 + j ] ) && ( local_buffer[i][j] ) return true; return false; } // }}}1 bool checkNextLineCollisions() // {{{1 { currentY++; if ( checkIntersections() ) { currentY--; return true; } return false; } // }}}1 void rotate() // {{{1 { // make copy of local buffer {{{2 for (i=0; i<m_LOCAL_SIZE; i++ ) for (j=0; j<m_LOCAL_SIZE; j++ ) local_buffer_two[i][j]=local_buffer[i][j]; // }}}2 // rotating local buffer {{{2 for (i=0; i<m_LOCAL_SIZE; i++ ) for (j=0; j<m_LOCAL_SIZE; j++ ) local_buffer[j][i]=local_buffer_two[i][j]; // }}}2 if ( checkIntersections() ) return; currentX++; if ( checkIntersections() ) return; currentX++; if ( checkIntersections() ) return; currentX-- -- --; if ( checkIntersections() ) return; currentX--; if ( checkIntersections() ) return; currentX++ ++; cout<<"no rotation done!"<<endl; // restorig local buffer {{{2 for (i=0; i<m_LOCAL_SIZE; i++ ) for (j=0; j<m_LOCAL_SIZE; j++ ) local_buffer[i][j]=local_buffer_two[i][j]; // }}}2 } // }}}1 void moveDown() // {{{1 { newY = currentY; // step by step mive piece down // and check for collisions. // remember last Y as newY while ( newY < m_HEIGH ) { if ( checkIntersections() ) break; newY++; } currentY = newY; } // }}}1 void global+local() // {{{1 { for (i=0; i<m_LOCAL_SIZE; i++ ) for (j=0; j<m_LOCAL_SIZE; j++ ) global_buffer [ currentX - m_LOCAL_SIZE/2 + i ] [ currentY - m_LOCAL_SIZE/2 + j ] = local_buffer[i][j]; } // }}}1 void global-local() // {{{1 { for (i=0; i<m_LOCAL_SIZE; i++ ) for (j=0; j<m_LOCAL_SIZE; j++ ) global_buffer [ currentX - m_LOCAL_SIZE/2 + i ] [ currentY - m_LOCAL_SIZE/2 + j ] =0; } // }}}1 void ClearLocalBuffer(void) // {{{1 { for (i=0; i<m_LOCAL_SIZE; i++ ) for (j=0; j<m_LOCAL_SIZE; j++ ) local_buffer[i][j]=0; } // }}}1 void ClearGlobalBuffer(void) // {{{1 { for (i=0; i<m_HEIGH; i++ ) for (j=0; j<m_WIDTH; j++ ) global_buffer[i][j]=0; } // }}} 1 bool CheckIfLastLine(void) // {{{1 { global+local(); for (i=0; i<m_WIDTH; i++ ) if ( global_buffer[m_HEIGH][i] ) { global-local(); return 1; } global-local(); return 0; } // }}}1
You need to login to post a comment.
