int ShowCube(){ //生成一个方块的同时,在右上角显示下一个方块的形态 int left,top; srand(SDL_GetTicks()); curblk.number = next; next = random(CurBlkNum); Block nextblk = blk[next]; nextblk.Reset(15,2); Screen.bar(350,100,150,200,BLACK); // clear previous image xyprintf(330,100,"Next: "); Screen.rectangle(350,120,500,220,WHITE); nextblk.DrawCube(); // update a new block blk[curblk.number].Reset(); //恢复方块初始坐标(回到最上面) curblk = blk[curblk.number]; curblk.DrawCube(); Screen.flip(); if( !curblk.isOk() ){ //满了 // Complete(); // gameOver = true; } return 0; }
int ThreadFunc(void * unused){ int old_time,cur_time,x,y; old_time = cur_time = SDL_GetTicks(); while( !gameOver ){ while( alive ){ while( !Operation.isEmpty() && alive){ EnterCriticalSection(&cs); Operation.DeleteQ(command); curblk.ClearCube(); switch( command ){ case MOVEDOWN: curblk.MoveDown(); break; case MOVELEFT: curblk.MoveLeft(); break; case MOVERIGHT:curblk.MoveRight(); break; case ROTATE : curblk.Rotate(); break; } if( command == TURNNEXT ){ //有的特殊命令最好特殊处理 curblk.TurnNext(); break; } curblk.DrawCube(); Screen.flip(); LeaveCriticalSection(&cs); } cur_time = SDL_GetTicks(); SDL_Delay(1); if( !AIMode ){ if( cur_time - old_time < 500 ) continue; old_time = cur_time; Operation.AddQ(MOVEDOWN); }else{ if( cur_time - old_time < 50 ) continue; old_time = cur_time; x = curblk.x; y = curblk.y; if( BestRotate>0 ){ Operation.AddQ(ROTATE); BestRotate -= 1; continue; } if( BestPath[x][y+1] ){ Operation.AddQ(MOVEDOWN); BestPath[x][y+1] = 0; y += 1; continue; }else if( BestPath[x+1][y] ){ Operation.AddQ(MOVERIGHT); BestPath[x+1][y] = 0; x += 1; continue; }else if( BestPath[x-1][y] ){ Operation.AddQ(MOVELEFT); BestPath[x-1][y] = 0; x -= 1; continue; } alive = 0; } } while(DelLine()); Operation.ClearQ(); ShowCube(); ShowScore(); if( AIMode ) Search(); alive = 1; } return 0; }