예제 #1
0
void AppWindow::keyPressEvent(QKeyEvent *event) {
    
    if(m_viewer->gameStatus>=0){
        if (event->key() == Qt::Key_Escape) {
            QCoreApplication::instance()->quit();
        } else if (event->key() == Qt::Key_Right) {
            moveRight();
        } else if (event->key() == Qt::Key_Left) {
            moveLeft();
        } else if (event->key() == Qt::Key_Down) {
            rotateCW();
        } else if (event->key() == Qt::Key_Up) {
            rotateCCW();
        } else if (event->key() == Qt::Key_Space) {
            dropPiece();
        } else if (event->key() == Qt::Key_Shift) {
           shiftPressed();
        }
    }




    if (event->key() == Qt::Key_1) {
        slowSpeed();
        slowSpeedAct->setChecked(true);
    }else if (event->key() == Qt::Key_2) {
        normalSpeed();
        normalSpeedAct->setChecked(true);
    }else if (event->key() == Qt::Key_3) {
        fastSpeed();
        fastSpeedAct->setChecked(true);
    }else if (event->key() == Qt::Key_W) {
        wire_frame();
        wireAct->setChecked(true);
    }else if (event->key() == Qt::Key_F) {
        face();
        faceAct->setChecked(true);
    }else if (event->key() == Qt::Key_M) {
        multi_coloured();
        multiAct->setChecked(true);
    }else if (event->key() == Qt::Key_N) {
        new_game();
    }else if (event->key() == Qt::Key_R) {
        reset();
    }else if (event->key() == Qt::Key_Q) {
        close();
    }else {
        QWidget::keyPressEvent(event);
    }
}
예제 #2
0
void Puzzle::handleClick(Point mousePt) {
	if (_puzzlePiece != -1) {
		dropPiece(mousePt);

		if (!_active)
			return; // we won

		drawCurrentPiece();
		_puzzlePiece = -1;

		return;
	}

	for (int j = 0; j < PUZZLE_PIECES; j++)	{
		int i = _piecePriority[j];
		int adjX = mousePt.x - _pieceInfo[i].curX;
		int adjY = mousePt.y - _pieceInfo[i].curY;

		if (hitTestPoly(&_pieceInfo[i].point[0], _pieceInfo[i].count, Point(adjX, adjY))) {
			_puzzlePiece = i;
			break;
		}
	}

	if (_puzzlePiece == -1)
		return;

	alterPiecePriority();

	// Display scene background
	_vm->_scene->draw();
	showPieces();

	int newx = mousePt.x - _pieceInfo[_puzzlePiece].offX;
	int newy = mousePt.y - _pieceInfo[_puzzlePiece].offY;

	if (newx != _pieceInfo[_puzzlePiece].curX
		|| newy != _pieceInfo[_puzzlePiece].curY) {
		_pieceInfo[_puzzlePiece].curX = newx;
		_pieceInfo[_puzzlePiece].curY = newy;
	}
	_vm->_interface->setStatusText(pieceNames[_lang][_puzzlePiece]);
}
예제 #3
0
void robotPlace(int *recEmptiesArray, int recCurPos)
{
	int ChosenColIndex=0;
	//determines which columns are possible (not full) based on empites array
	int PossibleIndices[MAXCOL] = {0, 0, 0, 0, 0, 0, 0};
	int count = 0;
	
	ChosenColIndex = Strategy();
	
	if (ChosenColIndex == -1) 
	{
		for (int col = 0; col < MAXCOL; col ++)
		{
			if (recEmptiesArray[col] >= 0)
			{
				PossibleIndices[count] = col;
				count += 1;
			}
		}
		count -= 1; 
		/*corrects for count going one too far
		at this point, Possible indices contains the column indices of 
		non-full columns and count contains how many not full columns there are
		*/
		ChosenColIndex = PossibleIndices[random[count]];
		// random number between 0 and count
		displayString(4, "ChosenCol: %d", ChosenColIndex);
	}

	//now place piece in the selected column
	GameBoard[recEmptiesArray[ChosenColIndex]][ChosenColIndex] = 2;
	updateEmpties(recEmptiesArray);
	displayEmpties(recEmptiesArray);
	displayGameBoard();
	moveHor(recCurPos, ChosenColIndex);
	//wait to give robot a change to stabilize
	wait1Msec(stabilize);
	dropPiece();
}
예제 #4
0
int Game::tick()
{
    if(stopped_)
    {
        return -1;
    }

    int returnVal;
    int level =  linesCleared_/100;
    if (level > 12)
        level = 12;

    removePiece(piece_, px_, py_);
    markBlocksForClearing();
    returnVal = collapse();
    moveClearBar();
    if (counter < COUNTER_SPACE - level)
    {
        counter++;
        placePiece(piece_, px_, py_);
        return returnVal;
    }

    if (py_ == board_height_ + 2 && atTheTop < 16)
    {
        atTheTop++;
        placePiece(piece_, px_, py_);
        return returnVal;
    }
    atTheTop = 0;
    counter = 0;
    int ny = py_ - 1;

    if(!doesPieceFit(piece_, px_, ny))
    {
        // Must finish off with this piece
        placePiece(piece_, px_, py_);
        if(py_ >= board_height_ + 1)
        {
            // you lose.
            stopped_ = true;
            return -1;
        }
        else
        {
            // break piece and keep moving down if need be

            // The right side can drop more
            if(get(ny-2, px_+1) != -1 && get(ny-2, px_+2) == -1)
            {
                dropPiece(0);
                counter = COUNTER_SPACE;
            }
            else if(get(ny-2, px_+1) == -1 && get(ny-2, px_+2) != -1)
            {
                dropPiece(1);
                counter = COUNTER_SPACE;
            }
            generateNewPiece();
            return returnVal;
        }
    }
    else
    {
        placePiece(piece_, px_, ny);
        sy_ = py_;
        py_ = ny;
        return returnVal;
    }
}
예제 #5
0
int main(int argc, char* argv[]) {

    SDL_Window *window;                    // Declare a pointer

    if(init_SDL_all() != 0) {
        return 1;
    }

    // Create an application window with the following settings:
    window = SDL_CreateWindow(
        WINDOW_NAME,                       // window title
        SDL_WINDOWPOS_UNDEFINED,           // initial x position
        SDL_WINDOWPOS_UNDEFINED,           // initial y position
        SCREEN_WIDTH,                      // width, in pixels
        SCREEN_HEIGHT,                     // height, in pixels
        SDL_WINDOW_OPENGL                  // flags - see below
    );

    if (window == NULL) {
        return logSDLError("Could not create window");
    }

    SDL_Renderer* ren;

    ren = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    if (ren == NULL) {
        return logSDLError("Could not create renderer");
    }

    dropData = getDrops();

    srand(time(NULL));

    //Open the font
	font = TTF_OpenFont(fontFile, fontSize);
	if (font == NULL){
		return logSDLError("TTF_OpenFont");
	}	

    background = loadTexture("images/bkg.png", ren);
    SDL_Texture *image = loadTexture("images/image.bmp", ren);
    if (background == NULL || image == NULL) {
	    return 4;
    }

    SDL_Texture *sheet = loadTexture("images/sheet.png", ren);
    blocks = loadTexture("images/blocks.png", ren);

    //iW and iH are the clip width and height
    //We'll be drawing only clips so get a center position for the w/h of a clip
    int iW = 100, iH = 100;
    int x = SCREEN_WIDTH / 2 - iW / 2;
    int y = SCREEN_HEIGHT / 2 - iH / 2;

    //Setup the clips for our image
    SDL_Rect clips[4];
    int i;
    for (i = 0; i < 4; ++i){
	    clips[i].x = i / 2 * iW;
	    clips[i].y = i % 2 * iH;
	    clips[i].w = iW;
	    clips[i].h = iH;
    }

    //Specify a default clip to start with
    for(i=0; i < NUMBER_OF_BLOCKS; i++) {
        blockClips[i].x = i * BLOCK_SIZE;
	    blockClips[i].y = 0;
	    blockClips[i].w = BLOCK_SIZE;
	    blockClips[i].h = BLOCK_SIZE;
    }
    
    nextPiece = pickNewPiece();
    useClip = pickNewPiece();

    //SDL_Delay(3000);  // Pause execution for 3000 milliseconds, for example
    //Our event structure
    SDL_Event e;
    bool quit = false;
    while (!quit){

        SDL_Delay(FRAME_TIME_DELAY);

        // Event loop for each possible game mode
        if(mode == MODE_GAMEOVER) {
            printf("\nGAME OVER\nScore: %lu\n\n", score);
            quit = true;
            break;
        } else if(mode == MODE_PAUSE) {
            while (SDL_PollEvent(&e)) {
                if (e.type == SDL_KEYDOWN) {
                    switch (e.key.keysym.sym){
                        case SDLK_p:
                            mode = MODE_RUNNING;
                            break;
                        case SDLK_q:
                        case SDLK_ESCAPE:
                            quit = true;
                            break;
                        default:
                            break;
                    }
                }
            }
        } else if(mode == MODE_RUNNING) {
	        while (SDL_PollEvent(&e)) {
		        if (e.type == SDL_QUIT)
			        quit = true;
                if (e.type == SDL_KEYDOWN){
		            switch (e.key.keysym.sym){
                        case SDLK_UP:
                            rotation += 1;
                            if(!checkIfValidPosition()) {
                                rotation -= 1;
                            }
                            draw_required = true;
                            break;
			            case SDLK_ESCAPE:
                        case SDLK_q:
				            quit = true;
				            break;
                        case SDLK_DOWN:
                            drop_y += 1;
                            draw_required = true;
                            if(!checkIfValidPosition()) { 
                                drop_y -= 1;
                                dropPiece();
                            }
                            break;
                        case SDLK_LEFT:
                            drop_x -= 1;
                            draw_required = true;
                            if(!checkIfValidPosition()) drop_x += 1;
                            break;
                        case SDLK_RIGHT:
                            drop_x += 1;
                            draw_required = true;
                            if(!checkIfValidPosition()) drop_x -= 1;
                            break;
                        case SDLK_SPACE:
                            draw_required = true;
                            dropPiece();
                            break;
                        case SDLK_p:
                            if(mode == MODE_PAUSE)  mode = MODE_RUNNING;
                            else                    mode = MODE_PAUSE;
                            break;
			            default:
				            break;
		           }
	            }
	        }
        }

        if(mode == MODE_RUNNING) {
            fallTimer++;
            if(fallTimer >= fallTimerLimit) {
                fallTimer = 0;
                drop_y += 1;
                draw_required = true;
                if(!checkIfValidPosition()) { 
                    drop_y -= 1;
                    dropPiece();
                }
            }
        }

        if(draw_required) {
            SDL_RenderClear(ren);

	        //Render the scene
            switch(mode) {
                case MODE_RUNNING:
	                draw_game_running(ren);
                    break;
                case MODE_PAUSE:
                    draw_game_paused(ren);
                    break;
                default:
                    break;
            }

            draw_score(ren);

            SDL_RenderPresent(ren);

        }
        
    }

    // Close and destroy the window
    SDL_DestroyTexture(background);
    SDL_DestroyTexture(image);
    SDL_DestroyTexture(sheet);
    SDL_DestroyTexture(blocks);
    TTF_CloseFont(font);
    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(window);

    // Clean up
    SDL_Quit();
    return 0;
}