Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

designing a pong game (hit the ball and bounce it off walls) can you fix the matlab code clear; clc; close all; %---------------setup----------------- pongFigure =

designing a pong game (hit the ball and bounce it off walls) can you fix the matlab code

clear; clc; close all;

%---------------setup----------------- pongFigure = figure('color', [.6 .6 .8],... %creates the figure 'KeyPressFcn', @keyboardFunction,... 'units,'normal','position', [.1 .1 .8 .8]);

pongAxes = axes('color','black',... 'Xlim',[0,100],'Ylim', [-5,100] 'Xticklabels',[],... 'Yticklabels',[],'position',[.05 .05 .9 .9]); %pongaxes

pongBallVel = [1,1]; %pongball moves at velocity of 1 pongBallPos = [20,50]; %pongball position pongBall = line(pongBallPos(1), pongBallPos(2),... 'marker','.','markersize',50,'color',[.8 .6 .6]); global pongBlockCenter pongBlockCenter = 45; pongBlock = line([pongBlockCenter - 5, pongBlockCenter + 5], [0 0], 'linewidth',4, 'color', [.6 .8 .6]); %pongball is treated as global variable

%--------------------------loop-----------------------------

while (toc < 4) if pongBallPos(1) < 0 || pongBallPos(1) > 100 pongBallVel(1) = - pongBallVel (1); %creates loop cycles every 4 seconds end if pongBallPos(2) > 100 pongBallVel(2) = - pongBallVel(2); %when pong ball hits top returns down end if pongBallPos(2) < 0 %when pongball hits bottom bounces up if abs(pongBallPos(1) - pongBlockCenter) < 5 pongBallVel(2) = -pongBallVel(2); else close all; return; end end pongBallPos = pongBallPos + pongBallVel; %pongball moves (updates pong ball position isnt working) set(pongBall, 'Xdata', pongBallPos(1), 'Ydata' , pongBallPos(2)); set(pongBlock, 'Xdata' , [pongBlockCenter - 5, pongBlockCenter +5]) pause (.02) end

%----------------------functions------------- function keyboardFunction(figure,event) %is supposed to create a function allowing you to move the block right and left with keyboard left right arrows without using other arrows global pongBlockCenter switch event.Key case 'leftarrow' pongBlockCenter = pongBlockCenter - 2; case 'rightarrow' pongBlockCenter = pongBlockCenter + 2; end

end

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beyond Big Data Using Social MDM To Drive Deep Customer Insight

Authors: Martin Oberhofer, Eberhard Hechler

1st Edition

0133509796, 9780133509793

More Books

Students also viewed these Databases questions

Question

Explain the causes of indiscipline.

Answered: 1 week ago