I'm having trouble making the Snake game using a class def, my code just continously loops & keeps recreating the graph. I'm stuck & don't know what I need to do to be able to fix the graph issue & make the snake move.
classdef snake < handle properties gameinput = 'k'; player1 = []; board = []; lose = []; character = 1; ex = 0; end methods %constructor
function self = snake(varargin) if ~isempty(varargin) self.gameinput = varargin{1} if nargin > 1 self.player1 = varargin{2}; end end end %controls game flow
function play(self) if isempty(self.lose) draw_board(self) end end %creates game board
function draw_board(self) axis_limit = 16; d = 0; ate = 0; x = round(axis_limit/2); %starting point
y = round(axis_limit/2); %starting point a =randi([1 axis_limit-1],1);%generates random x coordinate for food
b =randi([1 axis_limit-1],1);%generates random y coordinate for food
d =randi([1,4]);% generates random direction to start in for snake
snake(1,1:2)=[x y];%defines the snake for x and y coordinates
size_snake=size(snake); size_snake=size_snake(1); food = [a b]; %defines coords for the food
test while (self.ex~=1)%runs the snake as long as q is not pressed
size_snake=size(snake); size_snake=size_snake(1); for l=size_snake+ate:-1:2 snake(l,:)=snake(l-1,:); end switch d %calling callback function
case 1 snake(1,2)=snake(1,2)+1;%add value of 1 to y position
case 2 snake(1,2)=snake(1,2)-1;%subtract value of 1 to y position
case 3 snake(1,1)=snake(1,1)+1;%add value of 1 to x position
case 4 snake(1,1)=snake(1,1)-1;%subtracts value of 1 to x position
end draw_snake(self) if snake(1,1)==food(1) && snake(1,2)==food(2)%if the snake and food are in the same position
ate=1; food(1) = randi([1 axis_limit-1]);%creates a new x position for the food
food(2) = randi([1 axis_limit-1]);%creates a new y position for the food
else ate=0; end snake=snake-((snake>axis_limit).*(axis_limit+1)); snake=snake+((snake<0).*(axis_limit+1)); end end function draw_snake(self) axis_limit = 8; d = 0; ate = 0; x = round(axis_limit/2); %starting point y = round(axis_limit/2); %starting point a =randi([1 axis_limit-1],1);%generates random x coordinate for food b =randi([1 axis_limit-1],1);%generates random y coordinate for food d =randi([1,4]);% generates random direction to start in for snake snake(1,1:2)=[x y];%defines the snake for x and y coordinates food = [a b]; %defines coords for the food size_snake=size(snake); size_snake=size_snake(1); for p = 1:size_snake plot(snake(p,1),snake(p,2), 'wo') hold on end plot(food(1,1),food(1,2), 'rs')%creates the vectors for the food and snake and plots them
drawnow whitebg([0 0 0])%creates black background
axis([0, axis_limit, 0, axis_limit])%creates the axis for gameplay
hold off end %controls the movement of the snake
function move_snake(self) switch self.character case 'q' self.ex=1; case 30 % arrow direction
if(d~=2) d = 1; %up d=1
end case 31 if(d~=1) d = 2; %down d=2
end case 29 if(d~=4) d = 3; %right d=3
end case 28 if(d~=3) d = 4; %left d=4
end end end endend
Best Answer