Base/Background
PongFigure = figure(‘color’, ‘white’,’KeyPressFcn’, @keyboardFunction);
PongAxes = axes(‘color’, ‘black’,’XLim’, [0 100],’YLim’,[-5 100], ‘XTickLabels’, [ ], ‘YTickLabels’, [ ], ‘position’,[0.05 0.05 0.9 0.9]);
Description
PongFigure establishes the background and makes it so that it is reliant upon the keyboardFunciton. The PongAxes is the actual playable section of the game. The colour is set to black, the domain is set from 0 to 100, and the range is set from -5 to 100. The X and Y tick labels are set to blank and the border position is set so that it takes up the majority of the window space.
Ball
BallVelocity = [1,1];
BallPosition = [50, 25];
Ball = line(BallPosition(1),BallPosition(2), ‘marker’,’.’, ‘markersize’, 30, ‘color’, ‘white’);
Description
The BallVelocity variable sets the ball’s x and y velocity equal to 1. The BallPosition variable sets the Ball’s position to the coordinates 50,25. The “Ball” is actually a line with the marker of a period. This gives the illusion of a dot and we scale this up to 30 and colour it white. The position of this line is then given the x and y values of the BallPosition. We do this by calling upon the 1st value of the position for x and the second for y.
Paddle
global BlockCenter;
BlockCenter = 15;
Paddle = line([0 0],[BlockCenter – 15, BlockCenter + 15],’linewidth’, 7, ‘color’, ‘white’);
Description
The BlockCenter establish the center of the block and is set to a global variable so that we can use it later on within the keyboard function. The Paddle is reliant on the global variable BlockCenter to determine it’s height. The width is 7 but the official x position is 0. The block center determines the height by adding 15 units for the top and subtracting 15 units for the bottom. The color is then set to white to contrast the black background.
Loops
tic;
Score = 0
while toc>0 && n == 1
if BallPosition(1) < 0 || BallPosition(1) > 100
BallVelocity(1) = – BallVelocity(1);
if BallPosition(2) < 0 || BallPosition(2) > 100
BallVelocity(2) = – BallVelocity(2);
end
if BallPosition(1) <= 0
if abs(BallPosition(2) – BlockCenter) < 15
BallVelocity(1)= – BallVelocity(1);
Score = Score + 1
else
msgbox(sprintf(‘Restart the Program to Try Again, You Lost, your score was %0f’, Score))
n=n+1
Description
The tic toc establishes a clock within the game that keeps tract of time in seconds. the n-value determines whether or not the game is still running. The if statements keep track of the position of the ball and prevent it from escaping from any of the borders aside from the one that the paddle is on. And each time the paddle hits the ball the x-velocity is flipped in the opposite direction and a value of 1 is added to the score. If this does not occur then a message box appears that tells the player they have lost and 1 is added to the value of n, ending the loop and therefore the game
Levels
if 20 > Score && Score > 10
BallVelocity(1)= BallVelocity(1) + 0.1;
end
if 30 > Score && Score >= 20
BallVelocity(1)= BallVelocity(1) + 1
BallVelocity(2)= BallVelocity(1) + 0.2
end
BallPosition = BallPosition + BallVelocity;
set(Ball,’XData’,BallPosition(1),’YData’,BallPosition(2));
set(Paddle, ‘YData’, [BlockCenter – 15, BlockCenter + 15]);
pause(0.02);
Description
The if-statements measure the score and as the score reaches past 10 but under 20, a value of 0.1 is added to the x-velocity, if the score is equal to 20 or greater then the velocity increases by 0.1 on the x and 0.2 on the y. The ball position is then constantly updated by adding the ball’s velocity to it. The paddle is then updated by setting the paddle’s position equal to the BlockCenter + or – 15 depending on the top or bottom of the paddle.
Controls
function keyboardFunction(figure, event)
global BlockCenter;
switch event.Key
case ‘uparrow’
BlockCenter = BlockCenter + 5;
case ‘downarrow’
BlockCenter = BlockCenter – 5;
end
if BlockCenter >= 85
BlockCenter = 85;
end
if BlockCenter <= 10
BlockCenter = 10;
Description
The function calls upon the global BlockCenter and then relies on the switch event.key. By doing this it allows us to set two different buttons equal to opposing equations. For the up-arrow the BlockCenter is increased by 5, which then shifts the entirety of the block up by 5. And for the down arrow the BlockCenter is decreased by 5, which then shits the entirety of the block down by 5. And then the final if statement limits the y-borders of the block preventing it from escaping the screen.