Question: Can someone modify the code so that at the beginning of each game, the user is prompted to enter the time duration that he/she would

Can someone modify the code so that at the beginning of each game, the user is prompted to enter the time duration that he/she would like the game to last. After the time set by the user, the game should end and display who the winner is, if there is one or display that there is no winner and give the option to continue, start a new game, or quit. The program should also be changed so that the color of the racket used by the player is different from the one used by the computer. An option should be included so that the player can choose the color of his/her racket. Finally, the program should prompt the user to enter his/her name that is to be displayed at the top of the score box.

Paddle.bmp  Can someone modify the code so that at the beginning of Ball.bmp each game, the user is prompted to enter the time duration that (The Ball isnt that big)

MATLAB Code:

function RunGame

hMainWindow = figure(... 'Color', [0 0 0],... 'Name', 'Game Window',... 'Units', 'pixels',... 'Position',[100 100 700 500]);

img = imread('ball.bmp','bmp'); [m,n,c] = size(img); hBall = axes(... 'parent', hMainWindow,... 'color', 'none',... 'visible', 'off',... 'units', 'pixels',... 'position', [345, 245, n, m] ); hBallImage = imshow( img ); set(hBallImage, 'parent', hBall, 'visible', 'off' ); ballSpeed = 0; ballDirection = 0; hTempBall = axes( ... 'parent', hMainWindow,... 'units', 'pixels',... 'color', 'none',... 'visible', 'off',... 'position', get(hBall, 'position' ) ); img = imread('paddle.bmp','bmp'); [m,n,c] = size(img); hRightPaddle = axes(... 'parent', hMainWindow,... 'color', 'none',... 'visible', 'off',... 'units', 'pixels',... 'position', [650 - 10, 250 - 50, n, m] ); hRightPaddleImage = imshow( img ); set(hRightPaddleImage, 'parent', hRightPaddle, 'visible', 'off' ); targetY = 200; t = timer( 'TimerFcn', @UpdateRightPaddleAI,... 'StartDelay', 2 ); start(t)

hLeftPaddle = axes(... 'parent', hMainWindow,... 'color', 'none',... 'visible', 'off',... 'units', 'pixels',... 'position', [50, 250 - 50, n, m] ); hLeftPaddleImage = imshow( img ); set(hLeftPaddleImage, 'parent', hLeftPaddle, 'visible', 'off' );

hBottomWall = axes(... 'parent', hMainWindow,... 'color', [1 1 1],... 'units', 'pixels',... 'visible', 'off',... 'position', [0 40 700 10] ); patch( [0 700 700 0], [0 0 10 10], 'b' ); hTopWall = axes(... 'parent', hMainWindow,... 'color', [1 1 1],... 'units', 'pixels',... 'visible', 'off',... 'position', [0 450 700 10] ); patch( [0 700 700 0], [0 0 10 10], 'b' ); rightScore = 0; leftScore = 0; hRightScore = axes(... 'parent', hMainWindow,... 'color', 'none',... 'visible', 'off',... 'units', 'pixels',... 'position', [650 485 50 10] ); hLeftScore = axes(... 'parent', hMainWindow,... 'color', 'none',... 'visible', 'off',... 'units', 'pixels',... 'position', [30 485 50 10] ); hRightScoreText = text( 0, 0, '0', 'parent', hRightScore,... 'visible', 'on', 'color', [1 1 1] ); hLeftScoreText = text( 0, 0, '0', 'parent', hLeftScore,... 'visible', 'on', 'color', [1 1 1] ); hQuitButton = uicontrol(... 'string', 'Quit',... 'position', [325 475 50 20],... 'Callback', @QuitButton_CallBack ); hStartButton = uicontrol(... 'string', 'Start',... 'position', [325 240 50 20],... 'Callback',@StartButton_CallBack );

function UpdateBall pos = get( hBall, 'position' ); ballX = pos(1,1); ballY = pos(1,2); ballDirection = NormalizeAngle( ballDirection ); % check for collisions with the walls if ( ballY > 450 - 10 ) && ( ballDirection > 0 ) && ( ballDirection 90 ) ballDirection = ballDirection + 2 * ( 180 - ballDirection ); else ballDirection = ballDirection - 2 * ballDirection; end elseif ( ballY 180 ) && ( ballDirection 270 ) ballDirection = ballDirection + 2 * ( 360 - ballDirection ); else ballDirection = ballDirection - 2 * ( ballDirection - 180 ); end end % check for collisions with the paddles if ( ballDirection > 90 && ballDirection leftX + 5)... && (ballY + 10 > leftY)... && (ballY 180 ) ballDirection = 180 - ballDirection; end end else rightPaddlePos = get( hRightPaddle, 'position' ); rightX = rightPaddlePos(1,1); rightY = rightPaddlePos(1,2); if( (ballX + 10 > rightX)... && (ballX + 10 rightY)... && (ballY 270 ) ballDirection = 180 - ballDirection; end end end MoveObject( hBall, ballSpeed, ballDirection ); end

function UpdateRightPaddle()

speed = 5; pos = get( hRightPaddle, 'position' ); rightY = pos(1,2); if( rightY + 5 targetY - 50 && rightY > 50 ) MoveObject( hRightPaddle, speed, 270 ) end pos = get( hRightPaddle, 'position' ); rightY = pos( 1,2); if( rightY > 400 - 50 ) rightY = 350; elseif( rightY

function UpdateRightPaddleAI( ob, data ) % calculate where the ball will colide. tempBallDirection = NormalizeAngle( ballDirection ); if( tempBallDirection 270 && ballSpeed > 0 ) ballPos = get( hBall, 'position' ); set( hTempBall, 'position', ballPos ); ballX = ballPos(1,1); while( ballX

% check for temp ball collision with walls. if ( ballY > 450 - 10 ) if ( tempBallDirection > 0 ) if ( tempBallDirection 180 ) if( tempBallDirection

% line( 0, 0, 'marker', '*', 'parent', hTempBall ) % pause( 0.0005 ) end

pos = get( hTempBall, 'position' ); ballY = pos(1,2); targetY = ballY + ( rand * 150 ) - 75; end end

function UpdateLeftPaddle() scr = get( hMainWindow, 'position' ); screenX = scr(1,1); screenY = scr(1,2); screenH = scr(1,4); mouse = get(0, 'PointerLocation' ); y = mouse(1,2) - screenY; if( y > 100 && y 400 ) paddlePos = get( hLeftPaddle, 'position' ); paddlePos(1,2) = 400 - 50; set( hLeftPaddle, 'position', paddlePos ); elseif( y

function CheckForScore() pos = get( hBall, 'position' ); xpos = pos(1,1); ypos = pos(1,2); if ( xpos 695 ) set( hBallImage, 'visible', 'off' ) leftScore = leftScore + 1; set ( hLeftScoreText, 'string', num2str( leftScore ) ) pause( .5 ) ResetBall() end end function ResetBall pos = get( hBall, 'position' ); pos(1,1) = 345; pos(1,2) = 255 + floor( rand*100 ) - 50; set( hBall, 'position', pos ) ballSpeed = 4; ballDirection = ( (rand(1)

function MoveObject( hInstance, speed, direction )

p = get( hInstance, 'position' );

x = p( 1, 1 ); y = p( 1, 2 ); x = x + cosd( direction ) * speed; y = y + sind( direction ) * speed;

p( 1, 1 ) = x; p( 1, 2 ) = y;

set( hInstance, 'position', p )

end

function SetObjectPosition( hObject, x, y ) pos = get( hObject, 'position' ); pos(1,1) = x; pos(1,2) = y; set( hObject, 'position', pos ) end

function a = NormalizeAngle( angle ) while angle > 360 angle = angle - 360; end while angle

end global playing; function QuitButton_CallBack( hObject, eventData ) playing = false; end

function StartButton_CallBack( hObject, eventData ) set( hObject, 'visible', 'off' ); set( hLeftPaddleImage, 'visible', 'on' ) set( hRightPaddleImage, 'visible', 'on' ) ResetBall(); playing = true; end

while playing == true UpdateBall() UpdateLeftPaddle() UpdateRightPaddle() CheckForScore() pause( 0.01 ) end

end

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!