Creating "Card Decks" in Construct

11

Index

Attached Files

The following files have been attached to this tutorial:

.c3p

buildingacarddeckprototype.c3p

Download now 404.17 KB
.capx

buildingacarddeckprototype.capx

Download now 283 KB

Stats

1,135 visits, 2,344 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY-NC 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 31 Aug, 2024. Last updated 2 Sep, 2024
Subscribe to Construct videos now

Next up we must shuffle our deck. To do this, I'll use the Fisher-Yates shuffling algorithm. It picks 2 random locations in our deck and then exchanges their values. This should be done several (perhaps a 1,000?) times per shuffle, which should be good enough to generate a seemingly random assortment. Construct replaces this "JS Nonsense" with its AdvancedRandom.Shuffle (the "Teaberry Shuffle" style!)

// JavaScript Fisher-Yates algorithm
function shuffle(deck)
{
	// for 1000(?) shuffles
	// Switch the values of two randomly selected cards
	for (let i = 0; i < 1000; i++)
	{
		let location1 = Math.floor((Math.random() * deck.length));
		let location2 = Math.floor((Math.random() * deck.length));
		let tmp = deck[location1];

		deck[location1] = deck[location2];
		deck[location2] = tmp;
	}
}

After this shuffle, our deck array contains 52 objects randomly sorted. But it's more amusing to see this (play the demo here) than have me talk about it, so our next "trick", I'm going to make these cards magically appear in Unicode!

"πŸƒπŸƒ‚πŸƒƒπŸƒ„πŸƒ…πŸƒ†πŸƒ‡πŸƒˆπŸƒ‰πŸƒŠπŸƒ‹πŸƒπŸƒŽ"

The render function iterates through each "card object" in the deck Array and creates a visual sprite in the "createCards" function. Having 52 sprites suddenly appear in our game might be too much of a "load". So, as an alternative to using sprite graphics, we could simply use text! Yeap! The Internet has Unicode code poker deck cards.

Game Prototype Summary

At this point, we have the functions to create a new card deck, shuffle them, and display them in our game. Some card games need several decks (one for each player?). The solution is ...

Creating Multiple Decks

var deck1 = getDeck();
var deck2 = getDeck();

Once the decks are created they are passed to the remaining helper functions as needed.

var deck1 = getDeck();
shuffle(deck1);
renderDeck(deck1);

Now that the decks are created, we can work on "dealing out" the cards.

  • 0 Comments

Want to leave a comment? Login or Register an account!