Creating the Snake game using a classdef

6 ビュー (過去 30 日間)
Landon Duke
Landon Duke 2018 年 12 月 11 日
編集済み: Matt J 2018 年 12 月 11 日
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
end
end
  3 件のコメント
TADA
TADA 2018 年 12 月 11 日
thats offtopic but,
in play function you have two identical calls to draw_board, when the player lost, you call it as function notation, and then call it with indexing notation regardless
both function notation
draw_board(self)
and dot indexing
self.draw_board() % or self.draw_board which also invokes the function
generally do the same thing. The mechanisms are different, but the outcome is identical, that is, both notations invoke the draw_board function of your snake class with self as an argument.
Landon Duke
Landon Duke 2018 年 12 月 11 日
Okay, thank you TADA, I deleted the 'self.draw_board' since it was redundent.

サインインしてコメントする。

採用された回答

GT
GT 2018 年 12 月 11 日
Try adding
drawnow
after the plot.
  2 件のコメント
Landon Duke
Landon Duke 2018 年 12 月 11 日
This got the plot to display, thank you
Matt J
Matt J 2018 年 12 月 11 日
編集済み: Matt J 2018 年 12 月 11 日
@Landon, If the answer resolved your issue, then you should Accept-click it.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStrategy & Logic についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by