Simple Matching Mini Game Proof of Concept - How to make this better, simpler, more efficient?

After I posted a status update indicating that I had started trying to make a card matching game and then quickly scaled it back as a proof of concept because I realized it was much more work than I initially anticipated, @Shaz challenged me to finish either version and post it.

I think the thought was that the more experienced folks could offer feedback on how to make it better, simpler, more efficient, etc.

I posted links to a DEMO video in my recent status update, along with another DEMO for a High-Low Dice Gambling Game.

Here is a link to the DEMO video:




If you watch the video, you will see that I made it so the player can choose between 4 or 6 pairs to match, they can quit, win, lose, and replay.

I have 1 main event that the player initiates the game, 2 events that have parallel pages to set the color of the boxes that you choose and to continually update the status of the boxes, and then 12 common events. The common events are almost identical. I just had to change a few things related to the box that was selected by the player.

I used an array and 32 variables. There are only 2 colors for the boxes. Red and Green.

Again, I made this as a proof of concept for a larger game involving playing cards.

Here are the spoiler screenshots. There are 28. But I only took screenshots of one of the common events since the other 11 are so similar.

Spoiler: GAME SET UP & INFO ARRAY
Game Set Up & Info Array Page 1.pngGame Set Up & Info Array Page 2 Part 1.pngGame Set Up & Info Array Page 2 Part 2.png

Spoiler: CHOICES (PARALLEL EVENT TO UPDATE BOXES STATUS)
Choices Page 1.pngChoices Page 2 Part 1.pngChoices Page 2 Part 2.pngChoices Page 2 Part 3.pngChoices Page 2 Part 4.pngChoices Page 2 Part 5.pngChoices Page 2 Part 6.pngChoices Page 2 Part 7.pngChoices Page 2 Part 8.pngChoices Page 2 Part 9.pngChoices Page 2 Part 10.png

Spoiler: MAIN EVENT
Main Event Part 1.pngMain Event Part 2.pngMain Event Part 3.pngMain Event Part 4.png

Spoiler: COMMON EVENT
Common Event 1 Part 1.pngCommon Event 1 Part 2.pngCommon Event 1 Part 3.pngCommon Event 1 Part 4.pngCommon Event 1 Part 5.pngCommon Event 1 Part 6.pngCommon Event 1 Part 7.pngCommon Event 1 Part 8.pngCommon Event 1 Part 9.pngCommon Event 1 Part 10.png

I hope I did this right and you can tell what I did from the screenshots. I probably should have used Comments, but I am still very lazy about that.

Thanks for taking the time to look at this and offer any suggestions or tips to help me learn.

This was very interesting to do.
I'd highly recommend looking up JavaScript tutorials about for loops. They would be quite useful in a bunch of places here, allowing you to do repetitive things much easier. For example, all the code in your second (and partially third) screenshot only needs to be written once, with a for loop that will then run it for every variable needed.
I'd highly recommend looking up JavaScript tutorials about for loops. They would be quite useful in a bunch of places here, allowing you to do repetitive things much easier. For example, all the code in your second (and partially third) screenshot only needs to be written once, with a for loop that will then run it for every variable needed.
Thanks for that suggestion! I have seen for loops in scripts, but I haven’t learned to use them yet. I haven’t gotten that far in my online Java Script course yet. But I will jump ahead and see if I can figure it out.

Thanks again!
I think the thought was that the more experienced folks could offer feedback on how to make it better, simpler, more efficient, etc.
That was part of it. The other part was I know there are people out there who enjoy a challenge like this and might get a kick out of trying to make it work with the full number of cards.
That was part of it. The other part was I know there are people out there who enjoy a challenge like this and might get a kick out of trying to make it work with the full number of cards.
Absolutely! Now that I tested the concept and know how to make it work, even if it isn’t the most efficient way, I can go back and work on the actual card game I started. I just have to put in more work to get all the cards that I want to use as well. The color boxes were easy since I already had them.

I will also look up for loops before I do anything else!
Alrighty, so I intended to quickly toss together an example to show you how to make the game scalable with more cards... but then some stuff happened and I went a little overboard. It turned out a little less beginner-friendly than I initially planned, so I don't know how much it'll help you. But since I know you're in the process of learning JavaScript, I tried to comment everything heavily, so maybe you can still glean some guidance from it.

It can handle any number of cards, provided that they're sized in a manner that allows them all to fit on the screen. It uses 3 common events, and one variable. No plugins are necessary, though I did use one (pixi-filters.js), because I wanted to make a snazzy glowing effect when the mouse hovers over cards. The plugin can be safely omitted if you don't want said snazzy glowing effect.

You can download the project file here (and also play a demo of it in your browser) if you want to check it out. If you have any questions about any of the code, feel free to let me know, and I'll try to explain why I did what I did. But long story short, as long as these constants (in the script call in the first Common Event) are assigned with appropriate values, you can use any number of cards and everything else is taken care of automagically:

JavaScript:
const numMatches = 10;
const numCols = 5;
const numRows = 4;
const cardWidth = 116;
const cardHeight = 123;
const colSpacing = 25;
const rowSpacing = 25;

That, and the image files for the cards need to follow a particular naming scheme (Card-1.png, Card-2.png, Card-3.png, etc), and they need to match whatever size you claim they are in the above code.

Here is a demo video. I've got it set to allow 10 fails before you lose the game, because my memory sucks and it's hard for me to win with this many cards. I've got it set to use 20 cards total (10 matches), because I thought that would be a good number in terms of being able to fit well on the screen while still being a good size... or so I told myself (really I just didn't feel like making any more cards).



Anyone is welcome to edit/use this however they want.
everything else is taken care of automagically:
Some of us (well, me at any rate) feel that almost everything about coding is automagical.
I know it's not my thread, but I wanted to say well done on a nice piece of work.
@Arthran Holy Crap! I am on my way to work, but this looks awesome! I can’t wait to get home to check it out!

And a huge thanks for sharing in a way that will help folks learn!

Very cool!
No problem, folks, and thanks for the kind words. I just uploaded a new version of the project, and I strongly recommend that you grab the new version instead.

I noticed in my video that there is a slight oddity with the glow-on-hover mechanic, so I decided to fix it. While fixing that, I noticed another bug that is a much bigger deal, as it can crash the game under certain conditions. It should be all good in this new version.
Again, I just want to say how thankful I am that someone took the time to take what I shared using Events and make it better and more efficient. And even more significant, @Arthran shared it in a way that everyone interested can download it, play test it, and look at exactly how it was built to see if they can learn from it.

I think that selflessness epitomizes this community and the support that it provides to anyone who wants to learn.

Now, I will just say honestly for any newer people like me. Whether you have about the same experience and background, a little more or less experience, or if you are just starting out.

Opening and reviewing Arthran's game is both awesomely amazing and horribly humbling at the same time. When I looked at the first Common Event that is doing most of the work, I was completely overwhelmed. And my first immediate thought was "this is depressing."

I thought I had learned so much and come so far. I knew there was a ton more for me to learn, but this really makes that obvious in a quantifiable way. And I will admit, it was a little disheartening and discouraging.

BUT, this is what learning is all about. And when I took a second look and tried to read and understand each line of code, I started to see things that I recognized from the Java Script course I am doing on line, self paced, in my spare time.

I am not that far along and I have to keep going back and repeating lessons because I tend to forget simple stuff within days. But the positive thing is that I can actually understand some of the scripts. And other parts I am pretty sure I know what it is doing, just not necessarily how/why it works. And each time I go back and really look, I recognize and understand a bit more. It becomes less overwhelming.

I also remind myself that only about a year ago, I knew absolutely nothing about RPG Maker or coding. And if I had looked at something like this 3 months ago, I probably wouldn't have understood anything.

So, what I am trying to say to newer folks who are still early on their journey of learning, don't be discouraged, or disheartened, and try not to get overwhelmed. Remember that this sort of learning is very technical. And it involves taking small steps. Learning the basics and building on a solid foundation.

Don't just take plugins or solutions that are kindly offered on this forum at face value and use them in your project. Actually take the time to try to understand them. And, if possible, try to figure out if you can actually achieve the same or a similar result with just Events.

Because for me, that is one HUGE takeaway from this. You can do SO MUCH with just Events. It might not be pretty or efficient. And it might take a lot of "coding," but it is EXTREMELY helpful to learn how to do it. If nothing more than to have options in the future.

And nothing I am posting here should make Arthran feel bad or anything. I am so intensely thankful that Arthran took the time to do this and openly share.

And while this has shown me that there is so much more for me to learn, it is also a small testament to what you can do within this engine with scripting (and Events).

You don't have to stick with the typical RPG Maker appearance and JRPG if you don't really want to. Learn to make what you want to make. And I am sure you will find support here if you take the time to be part of the community.

And this has also prompted me to go back and start looking at all the sample projects, Eventing Duel Projects, and "open" games/projects that have been share on this forum to take a deeper dive into how they were made. That is a lot easier the more you learn.

So newer folks, if you haven't done it, search for Eventing Duels or similar language. You will find some incredible shared projects in those threads.

Anyway, this is way too long, but I just wanted to candidly share my thoughts for newer people and once again give thanks. You might think that understanding scripts or plugins or whatever is too difficult now, but with time, practice, and diligence, I think you will be amazed with where you will eventually end up if you just keep going.

As always, enjoy what you do. Happy Game Developing!
Again, I just want to say how thankful I am that someone took the time to take what I shared using Events and make it better and more efficient. And even more significant, @Arthran shared it in a way that everyone interested can download it, play test it, and look at exactly how it was built to see if they can learn from it.

I think that selflessness epitomizes this community and the support that it provides to anyone who wants to learn.

Now, I will just say honestly for any newer people like me. Whether you have about the same experience and background, a little more or less experience, or if you are just starting out.

Opening and reviewing Arthran's game is both awesomely amazing and horribly humbling at the same time. When I looked at the first Common Event that is doing most of the work, I was completely overwhelmed. And my first immediate thought was "this is depressing."

I thought I had learned so much and come so far. I knew there was a ton more for me to learn, but this really makes that obvious in a quantifiable way. And I will admit, it was a little disheartening and discouraging.

BUT, this is what learning is all about. And when I took a second look and tried to read and understand each line of code, I started to see things that I recognized from the Java Script course I am doing on line, self paced, in my spare time.

I am not that far along and I have to keep going back and repeating lessons because I tend to forget simple stuff within days. But the positive thing is that I can actually understand some of the scripts. And other parts I am pretty sure I know what it is doing, just not necessarily how/why it works. And each time I go back and really look, I recognize and understand a bit more. It becomes less overwhelming.

I also remind myself that only about a year ago, I knew absolutely nothing about RPG Maker or coding. And if I had looked at something like this 3 months ago, I probably wouldn't have understood anything.

So, what I am trying to say to newer folks who are still early on their journey of learning, don't be discouraged, or disheartened, and try not to get overwhelmed. Remember that this sort of learning is very technical. And it involves taking small steps. Learning the basics and building on a solid foundation.

Don't just take plugins or solutions that are kindly offered on this forum at face value and use them in your project. Actually take the time to try to understand them. And, if possible, try to figure out if you can actually achieve the same or a similar result with just Events.

Because for me, that is one HUGE takeaway from this. You can do SO MUCH with just Events. It might not be pretty or efficient. And it might take a lot of "coding," but it is EXTREMELY helpful to learn how to do it. If nothing more than to have options in the future.

And nothing I am posting here should make Arthran feel bad or anything. I am so intensely thankful that Arthran took the time to do this and openly share.

And while this has shown me that there is so much more for me to learn, it is also a small testament to what you can do within this engine with scripting (and Events).

You don't have to stick with the typical RPG Maker appearance and JRPG if you don't really want to. Learn to make what you want to make. And I am sure you will find support here if you take the time to be part of the community.

And this has also prompted me to go back and start looking at all the sample projects, Eventing Duel Projects, and "open" games/projects that have been share on this forum to take a deeper dive into how they were made. That is a lot easier the more you learn.

So newer folks, if you haven't done it, search for Eventing Duels or similar language. You will find some incredible shared projects in those threads.

Anyway, this is way too long, but I just wanted to candidly share my thoughts for newer people and once again give thanks. You might think that understanding scripts or plugins or whatever is too difficult now, but with time, practice, and diligence, I think you will be amazed with where you will eventually end up if you just keep going.

As always, enjoy what you do. Happy Game Developing!
Sorry, I wasn't trying to make you feel depressed or anything like that. Initially, I meant to just show a basic example of how loops and math can remove a lot of manual labor out of your events and allow your system to easily scale with more cards.

But then once I started planning it out, I decided that I might as well go ahead and remove the dependency on the external plugin that you were using (the ButtonPicture plugin), so that it'd be a purely evented¹ solution. Then when I was planning that part out, I realized that it'd be a good opportunity to go ahead and make a sweet hover effect for the cards. Then when I was planning out how to implement those things, I realized that there was a way I could get the entire system working without having to use any parallel events (which seemed too nifty to resist)... and things just kinda snowballed and I wound up going a bit overboard.

I'll go ahead and point out and explain some isolated bits of code that illustrate the main concepts that I originally wanted you to take away from this:

Here are the only parts that require manual adjustment, depending on the number of cards that you want to use:
JavaScript:
// These constants need to be pre-defined by us
const numMatches = 10;
const numCols = 5;
const numRows = 4;
const cardWidth = 116;
const cardHeight = 123;
const colSpacing = 25;
const rowSpacing = 25;
As the name suggests, numMatches is the number of matches (10 matches = 20 cards total). numCols is the number of columns (i.e. the number of cards in each row), and numRows is the number of rows of cards. cardWidth and cardHeight are the dimensions of the png files. colSpacing and rowSpacing are the amount of pixels that you want to have between each card.

With those values set, we can use math to automatically calculate where each card needs to go. For example:
JavaScript:
// Here we calculate the starting x and y positions
const startx = Math.floor((Graphics.width - (numCols * cardWidth + (numCols - 1) * colSpacing)) / 2);
const starty = Math.floor((Graphics.height - (numRows * cardHeight + (numRows - 1) * rowSpacing)) / 2);
startx and starty are the x and y coordinates that we want to place our first card at. For example, if you take the number of columns and multiply it by the width of each card, you get the total number of horizontal pixels that your cards will occupy. However, you also need to account for the space between each card, so we take the colSpacing (number of horizontal pixels between each card) and multiply it by the number of spaces that will be between each card (which is 1 less than the number of cards), and add that to our previous value... this will give you the total number of horizontal pixels that are necessary to display your cards. If you subtract that from the total width of the screen, and then divide it by 2, that'll give you the starting x coordinate that will center your cards on the screen. The Math.floor() call is simply to remove any decimal numbers incase the division by 2 didn't work out evenly (since there's no such thing as a partial pixel). And then the starty constant does all the same stuff, only vertically instead of horizontally.

We'll use those starting x and y coordinates for more calculations:
JavaScript:
// Calculate total number of cards
const numCards = numMatches * 2;

for (let i = 0; i < numCards; i++) {
    // Display the cards
    const x = startx + (i % numCols) * (cardWidth + colSpacing);
    const y = starty + Math.floor(i / numCols) * (cardHeight + rowSpacing);
    $gameScreen.showPicture(i + 2, "CardBack", 0, x, y, 100, 100, 255, 0);
}
Here we loop the same number of times as there are cards, with the value of the variable i starting at 0, and incrementing by 1 each iteration of the loop. We use the value of i, in combination with some of the previous values that we specified/calculated in order to do some fancy math and calculate the x and y coordinates of each card. We also use the value of i to calculate the picture ID of each card (the ID of the first card will be 2, since 1 is already being used by the table top graphic at this point), and show the pictures.

Now with this system in place, you can easily change the size or the amount of cards, and the only thing you need to change is the initial constants from the first code snippet.

I would guess that you are already somewhat familiar with all of the JavaScript syntax in these snippets. Rather than the JavaScript itself, I suppose that the harder part would probably be the math involved, and coming up with the idea to use the JavaScript in this manner in the first place.

As you know, there's a lot of other stuff in the script in my actual project. I simply pulled out these particular snippets because I think that they illustrate the most important concept for you to learn right now.

On a completely unrelated note, in case anyone cares, I uploaded the project to Itch, so in the event that the previous links are dead at some point, you can grab it from there instead. You can also play the game in your browser there, if you feel like trying it out.

Footnotes
1. Personally, I consider using script commands to be part of events, so I do feel that my solution is actually an "evented" one. However, I'll be the first to admit that I took it to a level that would make it perfectly fair to argue otherwise.
@Arthran This is incredible and you go way above and beyond! And I definitely understand adding more and more as you go. That is the very nature of inspiration.

And you did exactly what I asked. You showed how to make this far more efficient.

The math is certainly one of the harder parts. Even if I can follow it, I am not at a point where I would imagine that on my own.

I think it can be easy to get overwhelmed at different times. Which is why I wanted to take the opportunity to offer some advice to folks who are less experienced, like me.

I really think this is a terrific example of what can be done without plugins through events with scripting.

Again, thank you so much and some of these concepts will keep me busy for quite some time.

I hope other folks find this as valuable as I do.

Have an awesome night!
Hello. I'm giving this a shot, but having an issue.

The placeholder CardBack image loads properly with four rows, six columns, and with no buffer space between each. However, I can't do anything after that -- the game freezes, but with no error reported.

I assume I'm missing something located in the first common event's script. Could it be something with the mouse or glow feature? (Note that my comfort level with scripting is about a 1.8 out of 10, haha)
@tumsterfest I just went back and tried it again. I had to use the mouse to select the cards. I didn't see any other option. Did your mouse not work?

I also realized that I suck at that game. My memory definitely isn't what it used to be.
Correct; neither mouse nor keyboard works. It just sits there frozen.
That is odd. It seems to work fine for me.
I changed a few things in Arthran's Card Match Start common event script to fit my project, so maybe I messed something up there?
  • const numMatches = 12;
  • const numCols = 6;
  • const numRows = 4;
  • const cardWidth = 144;
  • const cardHeight = 144;
  • const colSpacing = 0;
  • const rowSpacing = 0;
  • Arthran.Memory.maxFailedMatches = 3;
  • Arthran.Memory.failCommonEvent = 94; (to correspond with its Common Event slot in my project)
  • Arthran.Memory.endCommonEvent = 95; (to correspond with its Common Event slot in my project)

Other things I changed:
  • All cards are in the project's Pictures folder as .png files and sized 144x144. Same with the CardBack file.
  • TableTop file is saved in the same spot and matches my game's resolution (1280x720).
  • I changed the Control Variable commands to reference #0060 since that's where I had room to place a new variable
  • I edited file names of three sound effects in my SE folder to match the three Anthran used (Cursor, Ok, Buzzer). I double-checked that they're .ogg files.
Despite all that ... yeah. The tabletop loads, then the cards load face-down, but then I can't get any cursor to display or any cards to flip over.
That seems like it would be an @Arthran question. My eventing was so different and I am so new that I wouldn’t know where to begin to troubleshoot.
@tumsterfest
At a glance, it seems like you did everything right. Based on my rough memory of the code, I can't quite imagine what would be causing that to happen, but I've slept a few times since I wrote it, so I'll have to take a good look at it when I get a chance. One potential issue with my code that did occur to me sometime after I posted it is that it makes the assumption that you're not using any other Pictures on your map at the time that you execute the Common Event. If you are, then I don't think that it would result in the behavior you're experiencing, but it would still be potentially problematic. I've been meaning to make an update to the project, to eliminate that caveat, but I haven't been able to get around to it yet.

If you don't mind, one thing that would help me figure out what's causing your issue is if you were to create a demo project that replicates the issue, upload it somewhere, and PM me a link to it.

Original thread: https://forums.rpgmakerweb.com/threads/simple-matching-mini-game-proof-of-concept-how-to-make-this-better-simpler-more-efficient.155153/