コード例 #1
0
ファイル: PongGame.cpp プロジェクト: Yves-T/SFML_Pong
PongGame::PongGame()
:mainWindow() {
    mainWindow.create(sf::VideoMode(mainWindowWidth, mainWindowHeight), "Pong");
    mainWindow.setFramerateLimit(60);
    mainWindow.setVerticalSyncEnabled(true);

    timePerFrame = sf::seconds(1.f / 60.f);

    topRectangle.setPosition(0, 0);
    topRectangle.setSize(sf::Vector2f(mainWindowWidth, borderSize));

    leftRectangle.setPosition(-borderSize, 0);
    leftRectangle.setSize(sf::Vector2f(borderSize, mainWindowHeight));

    rightRectangle.setPosition(mainWindowWidth, 0);
    rightRectangle.setSize(sf::Vector2f(borderSize, mainWindowHeight));

    bottomRectangle.setPosition(0, mainWindowHeight - borderSize);
    bottomRectangle.setSize(sf::Vector2f(mainWindowWidth, borderSize));

    setUpBorderRectangle(topRectangle);
    setUpBorderRectangle(leftRectangle);
    setUpBorderRectangle(rightRectangle);
    setUpBorderRectangle(bottomRectangle);

    ballSpeed = sf::Vector2f(100, 100);
    setUpBall(mainWindowWidth, mainWindowHeight, ball);

    setUpPlayer(player1);
    player1.setPosition(margin - borderSize, mainWindowHeight / 2 - 25);

    setUpPlayer(player2);
    player2.setPosition(mainWindowWidth - margin, mainWindowHeight / 2 - 25);

    setUpMiddleLine();

    if (!font.loadFromFile(resourcePath() + "sansation.ttf")) {
        // handle error here
    }

    statisticsText.setFont(font);
    statisticsText.setPosition(5.f, 5.f);
    statisticsText.setCharacterSize(10);
    statisticsText.setColor(sf::Color::Black);

    setupTitle();
    setUpStartText();
    setupWonText();
    setupLostText();
    setupScore();
}
コード例 #2
0
// Game entry
int main(int argc, char* argv[]){

// Init the Engine
	//Engine* myEngine = new Engine();
	Engine::InitSDL();
	loadBackground();
	setUpPlayer();
	// Loop while running
	while(gameRunning) {
		while( (SDL_PollEvent( &event ))    ){
				switch( event.type ){
					case SDL_QUIT:
						gameRunning = 0;
						printf("Quiting.../n");
						break;
//
					/* Look for a keypress */
					case SDL_KEYDOWN:
						/* Check the SDLKey values and move change the coords */
						switch( event.key.keysym.sym ){
							case SDLK_LEFT:
								velocX = -1;
								break;
							case SDLK_RIGHT:
								velocX =  1;
								break;
							case SDLK_UP:
								velocY = -1;
								break;
							case SDLK_DOWN:
								velocY =  1;
								break;
							default:
								break;
						}
						break;
					/* We must also use the SDL_KEYUP events to zero the x */
					/* and y velocity variables. But we must also be       */
					/* careful not to zero the velocities when we shouldn't*/
					case SDL_KEYUP:
						switch( event.key.keysym.sym ){
							case SDLK_LEFT:
								/* We check to make sure the alien is moving */
								/* to the left. If it is then we zero the    */
								/* velocity. If the alien is moving to the   */
								/* right then the right key is still press   */
								/* so we don't tocuh the velocity            */
								if( velocX < 0 )
									velocX = 0;
								break;
							case SDLK_RIGHT:
								if( velocX > 0 )
									velocX = 0;
								break;
							case SDLK_UP:
								if( velocY < 0 )
									velocY = 0;
								break;
							case SDLK_DOWN:
								if( velocY > 0 )
									velocY = 0;
								break;
							default:
								break;
						}

//
				}
		}
		Engine::player.moveObject(velocX, velocY);
		Engine::player.onUpdate(0.0f);  // Needs to be delta - also needs to be an object that holds all entities that has update
		Engine::RenderScreen();
	}

	return 0;
}