Clear Filters
Clear Filters

hi i want a wav format song play in the background of my game .after winning or lose the song is stopped

2 views (last 30 days)
function SnakeGame()
clc
clear
close all
x =randi([10 20],1);%generates random x starting point for snake
y =randi([10 20],1);%generates random y starting point for snake
button =randi([1,4]);% generates random direction to start in for snake
a =randi([1 24],1);%generates random x coordinate for apple
b =randi([1 24],1);%generates random y coordinate for apple
snake=[x,y];%defines the snake for x and y coordinates
snake = [snake ; [snake(1,1)-1 , snake(1,2)] ;[snake(1,1)-2 , snake(1,2)]];
apple=[a b];%defines apple for a and b coordinates
figure('KeyPressFcn',@keyPress);
function keyPress(~,event)%callback function for movement
switch event.Character
case 30 %up
button = 1;
case 31 %down
button = 2;
case 29 %right
button = 3;
case 28 %left
button = 4;
end
end
%runs the snake as long as its within the game board
while (snake(length(snake),1)>=0 && snake(length(snake),2)>=0 ...
&& snake(length(snake),1)<=25 && snake(length(snake),2)<=25 ...
&& length(snake)<=15)
%plot snake and apple
drawSnake(snake,apple)
%if length snake is equal 20 the game is finished
if length(snake) == 15
msgbox('***YOU WIN THE GAME***');
pause(0.1);
break;
end
waitforbuttonpress();
switch button
case 1 %up
snake(:,2)=snake(:,2)+1;%add value of 1 to y position
drawSnake(snake,apple)%draws the snake
pause(.1)
case 2 %down
snake(:,2)=snake(:,2)-1;%subtract value of 1 to y position
drawSnake(snake,apple)%draws the snake
pause(.1)
case 3 %right
snake(:,1)=snake(:,1)+1;%add value of 1 to x position
drawSnake(snake,apple)%draws the snake
pause(.1)
case 4 %left
snake(:,1)=snake(:,1)-1;%subtracts value of 1 to x position
drawSnake(snake,apple)%draws the snake
pause(.1)
end
%if the snake and apple are in the same position
if (snake(1,1)==apple(1) && snake(1,2)==apple(2))||...
(snake(end,1)==apple(1) && snake(end,2)==apple(2))
%add one point to snake length
snake = [snake ;[ min(snake(:,1))-1, apple(2)]];
% generate random new apple
apple(1) = randi([1 24]);%creates a new x position for the apple
apple(2) = randi([1 24]);%creates a new y position for the apple
%if snake exceeds boundaries display message box
elseif (snake(1,1)==0 || snake(end ,1)==0 ||...
snake(1,1)==25 || snake(end ,1)==25 ||...
snake(1,2)==0 || snake(end ,2)==0 ||...
snake(1,2)==25 || snake(end ,2)==25)
msgbox('YOU LOSE THE GAME!!!');
pause(1);
break;
end
end
end

Answers (2)

MUHAMMED IRFAN
MUHAMMED IRFAN on 16 Jun 2018
Hello,
May be this link can help you.
After solving your problem at hand, You might also want to go through this link. :)

Walter Roberson
Walter Roberson on 16 Jun 2018
To play audio continuously, you would need the Audio System toolbox, and you would have to periodically queue more samples to output.
sound() always plays the complete sound data passed to it; there is no possibility of stopping it early. To play again, you would have to call sound() again. Caution: if you call sound() while sound() is already playing, you might get both sounds at the same time
audioplayer() can playblocking() which blocks until the given data is played, which would not be good for the flow of your game
audioplayer() can play() without blocking. You can stop() the object to stop early. You cannot play sound continuously with an audioplayer object. However, you can set the StopFcn callback to call play() again -- just watch out that this would be called if you ran stop() on the object. So you might prefer to instead create a timer object that does play() on the audio object. There might end up being some gaps in the sound. There is no way to change the queued sounds for an audioplayer object: if you want to choose between different sounds you need multiple audioplayer objects.
load handel;
p = audioplayer(y, Fs, 'StopFcn', @(varargin) play(varargin{1}) );
play(p)
... eventually ...
set(p, 'StopFcn', []); stop(p);
If you don't clear the stopFcn in this example then stop(p) will indeed trigger the play() again.

Categories

Find more on Video games in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!