Creating similar game to google dinosaur game

Hi, I was looking for some help to figure out how to stop my gaming function when the moving object hits the obstacle, like in the google dinosaur function and to show a running score on my figure for the high score. Anyone know some helpful tips to code this?

5 Comments

How are you representing your moving object, and how are you representing the obstacles? Is movement at a constant speed (though potentially different directions?) Are all distances involved multiples of the constant speed, or is it necessary to interpolate the fact of having hit?
So here is my code for making the objects move. My variable square only moves up and down since that is the object that is jumping. My variable square2 is moving backwards from the right of the screen to the left for my other square to jump over and my background is moving to make my first square appear as if it is moving forward too. I will bold the part of my code that deals with the two squares hitting each other.
function game_step(~,~)
background_img = [background_img(:,11:1270,:) background_img(:,1:10,:)];
Background(:, 101:740, :) = background_img(:, 1:640, :);
Background(527:606, 761-Countcol:840-Countcol,:) = square2;
if trigger == true && Down == false
Countrow = Countrow + 10;
if Countrow == 150
Down = true;
end
Background(529-Countrow:608-Countrow , 121:200, :) = square;
elseif trigger == true && Down == true
Countrow = Countrow - 10;
if Countrow == 0
Down = false;
trigger = false;
end
Background(529-Countrow:608-Countrow , 121:200, :) = square;
else
Background(529:608, 121:200, :) = square;
end
imshow(uint8(Background(:, 101:740,:)));
Countcol = Countcol + 20;
if Countcol == 760
Countcol = 0;
end
gamescore = gamescore + 20;
*if ((Countrow <80) & (any(Background(400:640,101:180,:)==0)))
closeFunc();*
name = input('What is your name?: ');
highscore = xlsread('Highscores.xlsx');
end
end
I guess it didn't bold so here is the code I am talking about:
if ((Countrow <80) & (any(Background(400:640,101:180,:)==0)))
closeFunc();
so it definitely works to some extent, but the square2 has to hit the square entirely or when the square is jumping, if it hits the square2 in the middle, then it will think it hit. However, if the square hits the square2 on the edge, but definitely still hits it, the code will not pick it up as a hit
Remember, the above code is the same as
if all(all((Countrow <80) & (any(Background(400:640,101:180,:)==0,1)),2),3)
closeFunc();
The fragment
any(Background(400:640,101:180,:)==0)
only applies the any() to the first non-scalar dimension, leaving you with a 1 x 80 x something array that the default all() rules apply to
Okay, great thank you! It still doesn't end the game if it hits the corners of the sqaure for some reason. Also, do you have any suggestions for displaying a game score on the figure?

Sign in to comment.

Answers (0)

Categories

Find more on Number games in Help Center and File Exchange

Asked:

on 2 Dec 2017

Community Treasure Hunt

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

Start Hunting!