I'm trying to make a game and need some help

5 vues (au cours des 30 derniers jours)
ahmed Mohamed
ahmed Mohamed le 1 Déc 2019
Commenté : ahmed Mohamed le 1 Déc 2019
So I'm not sure where to go from here exactly. The idea is to create a game where the user is asked to select a character and from there carry out the game. Here is what I've done so far:
x=input('good day summoner, can I intrest you in an adventure [y/n]','s');
if x=='y'
y=input('Here is the selection of heros, each with their unique abilities, decide carefully\n/Blademaster: Specializes in sword combat, Ability: Every 3rd attack counts for 3 times the damage of a regular attack.\n/Gunslinger: Long ranged marksman, Ability: 40 percent chance of attacking 3 random enemies at the same time.\n/ Wizard: Spell caster, Ability: Cast a lightning spell that instantly kills 30 enemies.','s');
elseif x=='n'
disp('Sorry to bother you, have a nice day')
else
disp('Read instructions')
end
if y=='Blademaster'
disp('good choice, but becareful running into ranged enemies. You need to get creative')
elseif y=='Gunslinger'
disp('That would have been my first choice')
else y=='Wizard'
disp('While his ranged attacks are unmatched, he sturggles in close ranged combat')
end
Now I would like some constructive critisizm and/or suggestions on what to change. One more thing, when I run the code, the first part, before the selection of the characters runs fine, but upon selecting the character, I get and error that says matrix dimensions must agree, I'm fairly new to matlab so I'm not quite sure what that means.

Réponses (1)

Thiago Henrique Gomes Lobato
I would maybe advise you to use somehting like Unity to create your game since Matlab is more to perform numerical computations. However, if you just want to create a quick game to learn a bit of coding than it is certainly fun! First of all you get the error because you're comparing chars, not strings (strings are composed of chars). Using the right function (strcmp) would solve you the problem
% substitute by your images
x=input('good day summoner, can I intrest you in an adventure [y/n]','s');
if x=='y'
y=input('Here is the selection of heros, each with their unique abilities, decide carefully\n/Blademaster: Specializes in sword combat, Ability: Every 3rd attack counts for 3 times the damage of a regular attack.\n/Gunslinger: Long ranged marksman, Ability: 40 percent chance of attacking 3 random enemies at the same time.\n/ Wizard: Spell caster, Ability: Cast a lightning spell that instantly kills 30 enemies.','s');
elseif x=='n'
disp('Sorry to bother you, have a nice day')
else
disp('Read instructions')
end
if strcmp(y,'Blademaster')
disp('good choice, but becareful running into ranged enemies. You need to get creative')
elseif strcmp(y,'Gunslinger')
disp('That would have been my first choice')
else strcmp(y,'Wizard')
disp('While his ranged attacks are unmatched, he sturggles in close ranged combat')
end
Since you're making a if/else game, all the things that happens after a choice should be inside their if. For example, it doesn't make sense to evaluate if one is a Blademaster if they choose that they are not interested in an adventure. So your initial code should actually look something like this:
x=input('good day summoner, can I intrest you in an adventure [y/n]','s');
if x=='y'
y=input('Here is the selection of heros, each with their unique abilities, decide carefully\n/Blademaster: Specializes in sword combat, Ability: Every 3rd attack counts for 3 times the damage of a regular attack.\n/Gunslinger: Long ranged marksman, Ability: 40 percent chance of attacking 3 random enemies at the same time.\n/ Wizard: Spell caster, Ability: Cast a lightning spell that instantly kills 30 enemies.','s');
if strcmp(y,'Blademaster')
disp('good choice, but becareful running into ranged enemies. You need to get creative')
elseif strcmp(y,'Gunslinger')
disp('That would have been my first choice')
else strcmp(y,'Wizard')
disp('While his ranged attacks are unmatched, he sturggles in close ranged combat')
end
elseif x=='n'
disp('Sorry to bother you, have a nice day')
else
disp('Read instructions')
end
  1 commentaire
ahmed Mohamed
ahmed Mohamed le 1 Déc 2019
That definitely makes alot more sense, thanks alot!

Connectez-vous pour commenter.

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by