/* update_frame * Draw interface for a given frame */ void update_frame() { if (game_menu == 1) { draw_menu(); } else { // Regular game if ((game_mode == 0) && (game_paused == 0)) { paddle(PADDLE_1_XOFF,p1_h,p1_c); paddle(PADDLE_2_XOFF,p2_h,p2_c); ball(ball_x, ball_y, C_WHITE); char* hw = "Score: "; int x = 10; while (*hw) { write_char(x, 2, *hw); x++; hw++; } write_char(x, 2, '0' + p1_score); hw = "Score: "; x = 60; while (*hw) { write_char(x, 2, *hw); x++; hw++; } write_char(x, 2, '0' + p2_score); } // Paused game if (game_paused == 1) { unsigned char txt3[] = {'P','A','U','S','E','D','\n','\0'}; console_out(txt3); fill_screen(C_RED); char* hw = "GAME PAUSED"; int x = 10; while (*hw) { write_char(x, 2, *hw); x++; hw++; } } // Different mode, not implemented if (game_mode != 0) { unsigned char txt4[] = {'U','N','K','N','O','W','N','\n','\0'}; console_out(txt4); fill_screen(C_DBLUE); } } }
//create the paddle sf::RectangleShape Paddle::createPaddle(sf::Vector2f pSize, sf::Color pColor, sf::Vector2f pPos) { sf::RectangleShape paddle(pSize); paddle.setOrigin(pSize / 2.f); paddle.setFillColor(pColor); paddle.setPosition(pPos); this->paddle = paddle; this->paddleSize = pSize; this->paddleColor = pColor; this->paddlePosition = pPos; return paddle; }
/* first_draw * Draw interface for the first frame */ void first_draw() { if (game_menu == 1) { draw_menu(); } else { // Regular game if ((game_mode == 0) && (game_paused == 0)) { unsigned char txt2[] = {'D','r','a','w',' ','g','a','m','e','\n','\0'}; console_out(txt2); fill_screen(C_BLACK); clear_text(); subset(4, C_GREEN); meta_bar(C_BLACK); border(8,24,2,C_WHITE); paddle(PADDLE_1_XOFF,p1_h,p1_c); paddle(PADDLE_2_XOFF,p2_h,p2_c); } // Paused game if (game_paused == 1) { unsigned char txt3[] = {'P','A','U','S','E','D','\n','\0'}; console_out(txt3); fill_screen(C_RED); char* hw = "GAME PAUSED"; int x = 10; while (*hw) { write_char(x, 2, *hw); x++; hw++; } } // Different mode, not implemented if (game_mode != 0) { unsigned char txt4[] = {'U','N','K','N','O','W','N','\n','\0'}; console_out(txt4); fill_screen(C_DBLUE); } } }
void GameState::detectCollisions() { // walls if (m_ball.position.x < 0 || m_ball.position.x > 1) { m_ball.velocity.x = -m_ball.velocity.x; g_sound_engine.playSound(PLINK); return; } // ceiling if (m_ball.position.y < 0) { m_ball.velocity.y = -m_ball.velocity.y; g_sound_engine.playSound(PLINK); return; } // paddle Rectangle paddle(m_paddle - 0.1, 0.95, m_paddle + 0.1, 0.98); if (detectCollision(paddle, true)) { g_sound_engine.playSound(PLUNK); return; } Block::List::iterator i = m_blocks.begin(); for (; i != m_blocks.end(); ++i) { if (detectCollision(i->location, false)) { g_sound_engine.playSound(PLONK); m_blocks.erase(i); ++m_blocks_destroyed; // have we completed the level? if (m_blocks.empty()) { nextLevel(); } return; } } }
void HumanPlayer::handleKey(const char c) { if (c == _upChar) { if (paddle()->speed() > 0) { paddle()->setSpeed(0); } else { paddle()->setSpeed(-kPaddleSpeed); } } else if (c == _downChar) { if (paddle()->speed() < 0) { paddle()->setSpeed(0); } else { paddle()->setSpeed(kPaddleSpeed); } } }
void GameManager::start() { SDL_Event event; Paddle paddle(345, 577, 15, 100, (192153131)); Ball ball(395, 550, 20, (192153131)); Bricks bricks; bricks.createBricks(); // MAIN GAMELOOP while (running) { //fps starting_tick = SDL_GetTicks(); //fill screen with a white background SDL_FillRect(screen, NULL, 0xFFFFFF); paddle.draw(screen); ball.checkPaddleHit(paddle.getRect(), ball); ball.checkWalls(); ball.moveBall(); ball.draw(screen); bricks.drawBricks(screen); bricks.checkBricks(ball); if (ball.checkGameOver()) { gameOver(); } while (SDL_PollEvent(&event)) { switch (event.key.keysym.sym) { case SDLK_q: running = 0; case SDLK_LEFT: paddle.moveLeft(15); paddle.draw(screen); break; case SDLK_RIGHT: paddle.moveRight(15); paddle.draw(screen); break; case SDLK_p: pause(); break; case SDLK_r: start(); } if (event.type == SDL_QUIT) { running = false; break; } } SDL_UpdateWindowSurface(window); SDL_FreeSurface(screen); //FRAMERATELOCK cap_framerate(starting_tick); } SDL_DestroyWindow(window); SDL_Quit(); }