Question: I am trying to get my code If the ball touches the bottom of the window, pause the ball and print the following text to

I am trying to get my code If the ball touches the bottom of the window, pause the ball and print the following text to the middle of the screen. You lose. Press R to play again. I put in the code but is is not working can you help me fix it?#include "stdafx.h"
#include "Game.h"
Game::Game()
{
for(int i =0; i<5;++i)
{
Box brick;
brick.width =10;
brick.height =2;
brick.x_position =0+i*(brick.width);
brick.y_position =5;
brick.doubleThick = true;
brick.color = ConsoleColor::DarkGreen;
bricks.push_back(brick);
}
Reset();
}
void Game::Reset()
{
Console::SetWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
Console::CursorVisible(false);
paddle.width =12;
paddle.height =2;
paddle.x_position =32;
paddle.y_position =30;
ball.visage ='O';
ball.color = ConsoleColor::Cyan;
ResetBall();
// TODO #2- Add this brick and 4 more bricks to the vector
brick.width =10;
brick.height =2;
brick.x_position =0;
brick.y_position =5;
brick.doubleThick = true;
brick.color = ConsoleColor::DarkGreen;
}
void Game::ResetBall()
{
ball.x_position = paddle.x_position + paddle.width /2;
ball.y_position = paddle.y_position -1;
ball.x_velocity = rand()%2?1 : -1;
ball.y_velocity =-1;
ball.moving = false;
}
bool Game::Update()
{
if (GetAsyncKeyState(VK_ESCAPE) & 0x1)
return false;
if (GetAsyncKeyState(VK_RIGHT) && paddle.x_position < WINDOW_WIDTH - paddle.width)
paddle.x_position +=2;
if (GetAsyncKeyState(VK_LEFT) && paddle.x_position >0)
paddle.x_position -=2;
if (GetAsyncKeyState(VK_SPACE) & 0x1)
ball.moving =!ball.moving;
if (GetAsyncKeyState('R') & 0x1)
Reset();
ball.Update();
CheckCollision();
return true;
}
// All rendering should be done between the locks in this function
void Game::Render() const
{
Console::Lock(true);
Console::Clear();
paddle.Draw();
ball.Draw();
// TODO #3- Update render to render all bricks
for(const auto&brick: bricks)
{
brick.Draw();
}
Console::Lock(false);
}
void Game::CheckCollision()
{
// TODO #4- Update collision to check all bricks
for (auto it = bricks.begin(); it != bricks.end();)
{
if (brick.Contains(ball.x_position + ball.x_velocity, ball.y_position + ball.y_velocity))
{
brick.color = ConsoleColor(brick.color -1);
ball.y_velocity *=-1;
it = bricks.erase(it);
// TODO #5- If the ball hits the same brick 3 times (color == black), remove it from the vector
}
else
{
++it;
}
}
// TODO #6- If no bricks remain, pause ball and display victory text with R to reset
if (paddle.Contains(ball.x_position + ball.x_velocity, ball.y_velocity + ball.y_position))
{
ball.y_velocity *=-1;
}
// TODO #7- If ball touches bottom of window, pause ball and display defeat text with R to reset
if (ball.moving >= WINDOW_HEIGHT)
{
std::cout << "You lose.Press 'R' to play again." << std::endl;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!