football game in matlab

Here's a basic example of how to create a simple football game in MATLAB using built-in functions for graphics and user interface:

main.m
% Set up the playing field
fieldWidth = 500;
fieldHeight = 300;
figure('Name', 'Football Game', 'NumberTitle', 'off', 'Position', [100 100 fieldWidth fieldHeight]);
axis off;
hold on;
rectangle('Position', [0 0 fieldWidth fieldHeight], 'FaceColor', [0.9 0.9 0.9], 'EdgeColor', 'none');
line([0 fieldWidth], [fieldHeight*0.45 fieldHeight*0.45], 'LineWidth', 2, 'Color', 'w', 'LineStyle', '--');
circle(250, 150, 40);

% Set up the ball
ballRadius = 10;
ballPosition = [fieldWidth/2, fieldHeight/2];
ball = rectangle('Position', [ballPosition(1)-ballRadius, ballPosition(2)-ballRadius, ballRadius*2, ballRadius*2], 'Curvature', [1 1], 'FaceColor', 'w');

% Set up the player
playerRadius = 15;
playerPosition = [50, 150];
player = circle(playerPosition(1), playerPosition(2), playerRadius);

% Set up the game loop
dt = 0.01;
while true
    % Check for user input
    [~, ~, key] = ginput(1);
    switch key
        case 30 % 'up arrow' key
            playerPosition(2) = min(playerPosition(2)+5, fieldHeight-playerRadius);
        case 31 % 'down arrow' key
            playerPosition(2) = max(playerPosition(2)-5, playerRadius);
    end
    
    % Update player position
    set(player, 'Position', [playerPosition(1)-playerRadius, playerPosition(2)-playerRadius, playerRadius*2, playerRadius*2]);
    
    % Update ball position
    ballPosition = ballPosition + [10*dt, -10*dt];
    set(ball, 'Position', [ballPosition(1)-ballRadius, ballPosition(2)-ballRadius, ballRadius*2, ballRadius*2]);
    drawnow;
end

% Helper function to draw a circle
function circle(x, y, r)
    th = 0:pi/50:2*pi;
    xunit = r * cos(th) + x;
    yunit = r * sin(th) + y;
    plot(xunit, yunit, 'k', 'LineWidth', 2);
end
1703 chars
49 lines

This example sets up a basic playing field, with a static goal line, a ball and a player. The player can be moved up and down using the up and down arrow keys, while the ball moves at a fixed rate towards the left of the screen. The game loop runs indefinitely, redrawing the player and ball positions each frame, and checking for user input. This example can be expanded upon to implement more game features, such as different player movements, collision detection, and AI opponents.

gistlibby LogSnag