write a code for the game of pong in matlab

Here is a basic code for the game of Pong in MATLAB:

main.m
function PongGame

% Initial setup
close all;
clear all;
clc;
ballRadius = 5;
paddleHeight = 60;
paddleWidth = 10;
ballSpeed = 5;
paddleSpeed = 10;

% Create figure and axes
Figure = figure(1);
set(Figure,'Name','Pong Game');
set(Figure,'color',[1 1 1])
Axes = axes('Parent',Figure);
axis([-300 300 -200 200])
axis manual
hold on

% Create left and right paddles
leftPaddle = rectangle('Position',[-280,-paddleHeight/2,paddleWidth,paddleHeight],'Facecolor',[0 1 1],'Edgecolor',[0 0 0]);
rightPaddle = rectangle('Position',[270,-paddleHeight/2,paddleWidth,paddleHeight],'Facecolor',[0 1 1],'Edgecolor',[0 0 0]);

% Create ball
ball = rectangle('Position',[-ballRadius,-ballRadius,2*ballRadius,2*ballRadius],'Curvature',[1 1],'Facecolor',[1 0 0],'Edgecolor',[0 0 0]);
xVel = ballSpeed;
yVel = ballSpeed;

% Add listener
set(Figure,'KeyPressFcn',@movePaddle)

% Move paddles with arrow keys
function movePaddle(src,event)
    switch event.Key
        case 'uparrow'
            if get(rightPaddle,'Position')*[0;1] < 190
                set(rightPaddle,'Position',get(rightPaddle,'Position')+[0,paddleSpeed,0,0])
            end
        case 'downarrow'
            if get(rightPaddle,'Position')*[0;1] > -190
                set(rightPaddle,'Position',get(rightPaddle,'Position')-[0,paddleSpeed,0,0])
            end
        case 'w'
            if get(leftPaddle,'Position')*[0;1] < 190
                set(leftPaddle,'Position',get(leftPaddle,'Position')+[0,paddleSpeed,0,0])
            end
        case 's'
            if get(leftPaddle,'Position')*[0;1] > -190
                set(leftPaddle,'Position',get(leftPaddle,'Position')-[0,paddleSpeed,0,0])
            end
    end
end

% Game loop
while ishandle(ball)
    % Move ball
    set(ball,'Position',get(ball,'Position')+[xVel,yVel,0,0])
    
    % Bounce off walls
    if get(ball,'Position')*[1;0] > 290 || get(ball,'Position')*[1;0] < -290
        xVel = -xVel;
    end
    
    % Bounce off paddles
    if get(ball,'Position')*[0;1] > get(rightPaddle,'Position')*[0;1]-paddleHeight/2 && get(ball,'Position')*[0;1] < get(rightPaddle,'Position')*[0;1]+paddleHeight/2 && get(ball,'Position')*[1;0] > 260
        xVel = -xVel;
    end
    if get(ball,'Position')*[0;1] > get(leftPaddle,'Position')*[0;1]-paddleHeight/2 && get(ball,'Position')*[0;1] < get(leftPaddle,'Position')*[0;1]+paddleHeight/2 && get(ball,'Position')*[1;0] < -260
        xVel = -xVel;
    end
    
    % Bounce off top and bottom walls
    if get(ball,'Position')*[0;1] > 190 || get(ball,'Position')*[0;1] < -190
        yVel = -yVel;
    end
    
    % End game by closing the figure window
    if ~ishandle(ball)
        close;
    end
    
    pause(0.01)
end
2692 chars
86 lines

This code creates a Pong game using MATLAB, with two paddles and a ball bouncing between them. The player controls the left paddle with the 'w' and 's' keys, and the right paddle with the up and down arrow keys. The ball bounces off the walls and the paddles, and when it hits the top or bottom wall, the game ends.

related categories

gistlibby LogSnag