মূল বিষয়বস্তু
কম্পিউটার প্রোগ্রামিং
কোর্স: কম্পিউটার প্রোগ্রামিং > অধ্যায় 4
পাঠ 5: স্মৃতিশক্তি গেমটি তৈরিMemory game: Scoring and winning
Our "Memory" game is almost complete! It's only missing one thing: the scoring. Here's a reminder of that part of the game rules:
এই গেমের মূল উদ্দেশ্য হল কে কত কম সংখ্যকবার ক্লিক করে সকল টাইলসের কার্টুনের ছবির মধ্যে মিল খুঁজে বের করতে পারে অর্থাৎ সকল জোড় ছবিগুলো খুঁজে বের করতে হবে। যত কম সংখ্যকবার চেষ্টা করা হবে, তত স্কোর বাড়তে থাকবে।
প্রতিবার চেষ্টা করাকে কীভাবে আমরা হিসাব করব? আসলে, প্রত্যেকবার দুইটি টাইলস উল্টানো হল "চেষ্টা", যা আমাদের if ব্লক
flippedTiles.length === 2
যাচাই করে। আমরা একটি নতুন সার্বজনীন চলক যোগ করতে পারি, numTries
, যাকে কন্ডিশনালের মধ্যে বৃদ্ধি করব।if (flippedTiles.length === 2) {
numTries++;
...
}
We want to display the score when the game is over- when the player has matched all of the tiles. How do we check that? I can think of two options:
- We iterate through our array of tiles, and check to see if
isMatch
istrue
for all of them. - We use a global variable to keep track of how many matches the player found, and then check if they've made the total number of possible matches.
We had a similar debate earlier, and I went with the option where we don't have to iterate through the whole array every time. Let's go for that again, option 2.
First we initialize
numMatches
to 0, in the same place where we initialize all our other global game state variables:var numMatches = 0;
Then inside the
if
block for matched tiles, we increment the numMatches
variable:if (flippedTiles[0].face === flippedTiles[1].face) {
flippedTiles[0].isMatch = true;
flippedTiles[1].isMatch = true;
flippedTiles.length = 0;
numMatches++;
}
At the end of our
draw
function, we check if the player found all the matches and then display some congratulatory text to the user:if (numMatches === tiles.length/2) {
fill(0, 0, 0);
text("You found them all in " + numTries + " tries",
20, 360);
}
You can try it out below, but it might take you a while to get to the win state (no offense to you, of course, it also takes me a while!).
Here's a tip for whenever you're testing out parts of your game that are hard to reach - modify your game temporarily so that it's quicker to get there. For example, in this game, change
NUM_ROWS
and NUM_COLS
to be smaller numbers, and you'll be able to finish much more quickly. Now, try that below!আলোচনায় অংশ নিতে চাও?
কোন আলাপচারিতা নেই।