forked from einaregilsson/cards.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
61 lines (49 loc) · 1.68 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//Tell the library which element to use for the table
cards.init({table:'#card-table'});
//Create a new deck of cards
deck = new cards.Deck();
//By default it's in the middle of the container, put it slightly to the side
deck.x -= 50;
//cards.all contains all cards, put them all in the deck
deck.addCards(cards.all);
//No animation here, just get the deck onto the table.
deck.render({immediate:true});
//Now lets create a couple of hands, one face down, one face up.
upperhand = new cards.Hand({faceUp:false, y:60});
lowerhand = new cards.Hand({faceUp:true, y:340});
//Lets add a discard pile
discardPile = new cards.Deck({faceUp:true});
discardPile.x += 50;
//Let's deal when the Deal button is pressed:
$('#deal').click(function() {
//Deck has a built in method to deal to hands.
$('#deal').hide();
deck.deal(5, [upperhand, lowerhand], 50, function() {
//This is a callback function, called when the dealing
//is done.
discardPile.addCard(deck.topCard());
discardPile.render();
});
});
//When you click on the top card of a deck, a card is added
//to your hand
deck.click(function(card){
if (card === deck.topCard()) {
lowerhand.addCard(deck.topCard());
lowerhand.render();
}
});
//Finally, when you click a card in your hand, if it's
//the same suit or rank as the top card of the discard pile
//then it's added to it
lowerhand.click(function(card){
if (card.suit == discardPile.topCard().suit
|| card.rank == discardPile.topCard().rank) {
discardPile.addCard(card);
discardPile.render();
lowerhand.render();
}
});
//So, that should give you some idea about how to render a card game.
//Now you just need to write some logic around who can play when etc...
//Good luck :)