Snake Game: I need the snake to turn one by one instead of all at once.
1 view (last 30 days)
Show older comments
Heres my code:
function varargout = figure_snake(varargin)
% FIGURE_SNAKE MATLAB code for figure_snake.fig
% FIGURE_SNAKE, by itself, creates a new FIGURE_SNAKE or raises the existing
% singleton*.
%
% H = FIGURE_SNAKE returns the handle to a new FIGURE_SNAKE or the handle to
% the existing singleton*.
%
% FIGURE_SNAKE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FIGURE_SNAKE.M with the given input arguments.
%
% FIGURE_SNAKE('Property','Value',...) creates a new FIGURE_SNAKE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before figure_snake_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to figure_snake_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help figure_snake
% Last Modified by GUIDE v2.5 25-Apr-2017 17:57:04
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @figure_snake_OpeningFcn, ...
'gui_OutputFcn', @figure_snake_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before figure_snake is made visible.
function figure_snake_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to figure_snake (see VARARGIN)
% Choose default command line output for figure_snake
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes figure_snake wait for user response (see UIRESUME)
% uiwait(handles.figuresnake);
% --- Outputs from this function are returned to the command line.
function varargout = figure_snake_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in start_button.
function start_button_Callback(hObject, eventdata, handles)
% hObject handle to start_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
x = 25;
y = 25;
f1 = randi([1 50]);
f2 = randi([1 50]);
direction = randi([1 4]);
snake = [x y];
food = [f1 f2];
food_eaten = 1;
difficulty = menu('Select a difficulty', 'Easy', 'Medium', 'Hard');
while x >= 0 && x <= 50 && y >= 0 && y <= 50
if difficulty == 1
pause(.1)
elseif difficulty == 2
pause(.07)
elseif difficulty == 3
pause(.04)
end
x = snake(1,1);
y = snake(1,2);
snake = [x y];
[x, y, f1, f2, food_eaten, direction, snake] = snakepositioning(x, y, f1, f2, food_eaten, direction, snake);
[snake, direction, food_eaten, x, y, f1, f2] = movement(snake, direction, food_eaten, x, y, f1, f2) ;
if snake == food
food_eaten = food_eaten+1;
f1 = randi([1 50]);
f2 = randi([1 50]);
food = [f1 f2];
else
end
set(handles.scorecounter, 'String',num2str(food_eaten))
hold off
end
filename = 'scoretable.xlsx';
A = {'Latest Score'; food_eaten};
xlswrite(filename, A)
msgbox(sprintf('You Lose!'))
% --- Executes on button press in close_game.
function close_game_Callback(hObject, eventdata, handles)
% hObject handle to close_game (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
delete(handles.figuresnake)
2 Comments
Shashank
on 2 May 2017
Use this tutorial to format your question which will encourage people to look at it and respond:
Walter Roberson
on 2 May 2017
You do not show us your code for snakepositioning() or movement()
Does your snake get longer as it eats? How long is it?
When you say that you need the snake to turn one by one, do you mean that the head of the snake immediately turns towards the food, but that each segment behind the head needs to keep going in its existing direction until it reaches the point the head used to be, at which point it can turn in the new direction?
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!