% GUI for Maze Game function mazeGame() width = 300; height = 80; spacing = 5; fig = figure('Position',[1 1 width*2 height*8], 'Name', 'Maze Game', ... 'Visible', 'off', 'MenuBar', 'none', 'color', 'black'); movegui(fig, 'center') upButton = uicontrol('Style', 'pushbutton', 'Position', [width*1.25 height*1.5+spacing width*0.25 height], ... 'String', 'Up','FontSize', 10, 'Callback', @up); downButton = uicontrol('Style', 'pushbutton', 'Position', [width*1.25 height*0.5-spacing width*0.25 height], ... 'String', 'Down','FontSize', 10, 'Callback', @down); leftButton = uicontrol('Style', 'pushbutton', 'Position', [width-spacing height width*0.25 height], ... 'String', 'Left','FontSize', 10, 'Callback', @left); rightButton = uicontrol('Style', 'pushbutton', 'Position', [width*1.50+spacing height width*0.25 height], ... 'String', 'Right','FontSize', 10, 'Callback', @right); startButton = uicontrol('Style', 'pushbutton', 'Position', [width*0.75-spacing height*0.25 width*0.5 height*0.25], ... 'String', 'Start', 'Callback', @nextLevel); levelText = uicontrol('Style', 'text', 'Position', [width*0.25 height width*0.5 height*0.5], ... 'String', 'Current Level:', 'FontSize', 10, 'BackgroundColor', 'black', 'ForegroundColor', 'white'); maze = uicontrol('Style', 'text', 'Position', [width*0.125 height*2.5+20 width*1.75 height*5], ... 'String', 'Press Start To Continue', 'FontSize', 30, 'FontName', 'Courier', 'BackgroundColor', 'black', 'ForegroundColor', 'white'); set(fig, 'Visible', 'on'); % scoreScreen for inputing and recording scores scoreScreen = figure('Position',[1,1 width, height*4], 'Visible','off','color','black'); movegui(scoreScreen, 'center') nameEnter = uicontrol('Style', 'text', 'Position' ,[spacing*2 height*2 width*0.5 height*0.5], ... 'String','Enter your name', 'BackgroundColor', 'black', 'ForegroundColor', 'white'); nameBox = uicontrol('Style','edit','Position',[100,400,400,55],'BackgroundColor', 'black', 'ForegroundColor', 'white', 'Callback',@endScreen); % Reads the maze text file after the start button is pushed % Needs filename, number of rows, and number of columns function nextLevel(source, eventdata) level = {'maze2.txt', 'maze3.txt'}; levelNum = get(levelText, 'String'); if strcmp('Current Level:', levelNum) set(levelText, 'String', 'Current Level: 1'); k = 1; elseif strcmp('Current Level: 1', levelNum) set(levelText, 'String', 'Current Level: 2'); k = 2; end fid = fopen(level{k}); % Set at maze2.txt for now for dev purposes mazeArray = fscanf(fid, '%s'); % Reads the maze textfile into a character array rows = 7; % Default row size (?) cols = 14; % Default column size (?) start = 1; finish = cols; for k = 1:rows row{k} = mazeArray(start:finish); % Reads the array into rows start = start + cols; % The row consists of rowLength finish = finish + cols; % Need to shift the start and finish of each row end fclose(fid); map = []; for k = 1:length(row) map = [map; row{k}]; % Puts each row into a matrix end set(maze, 'String', map); end function up(source, eventdata) map = get(maze, 'String'); [row col] = find(map == '*'); %I made the character a star in the map, this is to find the index of the star move = row - 1; playerMove = map(move, col); if strcmp(playerMove,'O') % CWM - Checks if the move is valid map(move, col) = '*'; % CWM - Replaces the space 1 row up a * map(row, col) = 'O'; % CWM - Replaces the spot * was in with the path, O set(maze, 'String', map); %rewrites maze with new position of character elseif strcmp(playerMove, 'F') set(levelText, 'String', 'Current Level: 2'); nextLevel() end end function down(source, eventdata) map = get(maze, 'String'); [row col] = find(map == '*'); %I made the character a star in the map, this is to find the index of the star move = row + 1; playerMove = map(move, col); if strcmp(playerMove,'O') % CWM - Checks if the move is valid map(move, col) = '*'; % CWM - Replaces the space 1 row down a * map(row, col) = 'O'; % CWM - Replaces the spot * was in with the path, O set(maze, 'String', map); %rewrites maze with new position of character elseif strcmp(playerMove, 'F') set(levelText, 'String', 'Current Level: 2'); nextLevel() end end function left(source, eventdata) map = get(maze, 'String'); [row col] = find(map == '*'); %I made the character a star in the map, this is to find the index of the star move = col - 1; playerMove = map(row, move); if strcmp(playerMove,'O') % CWM - Checks if the move is valid map(row, move) = '*'; % CWM - Replaces the space 1 col left a * map(row, col) = 'O'; % CWM - Replaces the spot * was in with the path, O set(maze, 'String', map); %rewrites maze with new position of character elseif strcmp(playerMove, 'F') set(levelText, 'String', 'Current Level: 2'); nextLevel() end end function right(source, eventdata) map = get(maze, 'String'); [row col] = find(map == '*'); %I made the character a star in the map, this is to find the index of the star move = col + 1; playerMove = map(row, move); if strcmp(playerMove,'O') % CWM - Checks if the move is valid map(row, move) = '*'; % CWM - Replaces the space 1 col right a * map(row, col) = 'O'; % CWM - Replaces the spot * was in with the path, O set(maze, 'String', map); %rewrites maze with new position of character elseif strcmp(playerMove, 'F') set(levelText, 'String', 'Current Level: 2'); nextLevel() end end % DJ's Work from the MS Word .doc % timer would include a pause function equal to one second, and will steadily count up this value will be saved for high scores. function timer(source, eventdata) % timeCount = timecount + 1; tic % CWM - tic toc will record the time elapsed after using tic multiple times by calling toc time = toc; % set(endScreen, % CWM - Record the time to be displayed on the score screen % CWM - We might just want to have a move counter instead of a timer that % shows at all times during gameplay end % function enterScore(source, eventdata) % % end function endScreen(source, eventdata) % CWM - Currently doesn't work properly set(scoreScreen, 'Visible','on'); Name = get(nameBox,'String'); congrats = uicontrol('Style', 'text', 'BackgroundColor', 'black', 'Position', [100,400,400,55], 'String', 'Name', 'ForegroundColor', 'white', 'Fontsize', 40); set(congrats, 'Visible','on'); end end % values of positions will probably be changed