In the previous demo we allowed the user to right-click on a card and drag it around. When the user released the button the card was dropped in a new position. We paid no attention to the card's status or to the location it was dropped. In a real game we'd need to monitor all such activity.
Blocked Cards
QCard has two functions to set and query the blocked status of a card. A card can be marked as blocked if it is covered or partially covered by another card. QCard lets us set the "blocked" status with AdjustCardBlocked. We can query the "blocked" status of a card with GetCardBlocked. The API calls look like this:
calldll#qc,"AdjustCardBlocked",_
nC aslong,_ 'index of card to block/unblock
bValue aslong,_ '1=blocked, 0=unblocked
re asvoid'no returncalldll#qc,"GetCardBlocked",_
nC aslong,_ 'index of card to query
isBlocked aslong'1=blocked, 0=unblocked
Blocking Some Cards
In our demo, we'll change the display so that the rows of cards overlap. Each row is 20 pixels below the previous row. When we deal the first three rows we'll also set the "blocked" status of each card in that row to "blocked". We will leave the cards in the fourth row with the default "unblocked" status. We've introduced a variable called "row" to keep track of the row being dealt.
x=10:y=2:row=1for i =1to24Call DealCard hBox,card(i),x,y
'When creating a row or pile of cards,'only the topmost card should have an'IsBlocked value of FALSE.if row<4thencall AdjustCardBlocked card(i),1
x=x+100if x>510then'move to next row
x=10
y=y+offset
row=row+1endifplaywave"card.wav",sync
'pause 100 milliseconds between cardscall Pause 100scannext
Preventing the Drag of Blocked Cards
When the user right-clicks the mouse we'll use the coordinates from the deal to discover which card in our card array was clicked by comparing MouseX and MouseY positions with the locations in the grid. Once we find the array index we can get the value of the card at that index and query its blocked status.
If the card is blocked we document that at the bottom of the display and halt the routine. If it is not blocked, we begin the dragging procedure, as we did in the last lesson.
[initDrag]gosub[checkIndex]'find out which card on table was clickedif clickCard=0thenwait
nCard=card(clickCard)'find value of card from our card array
isBlocked=GetCardBlocked(nCard)'see if card is blockedif isBlocked<>0then'if card is blocked, inform user, then do nothing#1.g "place 10 420;\BLOCKED Card Index is ";nCard;space$(100)waitendif
cardIndex=InitDrag(hBox,MouseX,MouseY)#1.g "place 10 420;\Drag Card Index is ";nCard;space$(100)#1.g "when rightButtonMove [doDrag]"wait
In a real game you'd want to set the blocked status of the newly uncovered card to 0. You'd also need to set the card that was just covered by the dragged card to "blocked". We did not complicate the code with these routines in order to keep the demo program short and understandable.
Offset Value
QCard has a default offset value of 16 pixels. The cards are 70x95 pixels and if one card is dealt atop another, but 16 pixels lower both card values will be visible. This offset is important in block dragging operations. We are dragging a single card, so it is not critical to our demo.
Even though it is not necessary for our demo, we've used this opportunity to show how to change the offset with SetOffSet.
calldll#qc,"SetOffSet",_ 'change pixel offset for vertical colums of cards
offset aslong,_ 'number of pixels to offset
re asvoid
DEMO
See Lesson 1 for QCard DLL and WAV files needed for the demo code.
The demo deals the cards in four overlapped rows. It sets the blocked status of cards in the first three rows. It checks the blocked status of the card clicked by the user. It allows the user to drag the card if the designated card status is "unblocked".
'An open project card game, begun by Alyce Watson, May 27, 2003.'Uses Qcard32.dll, a freeware library of playing card images.'DLL by Stephen Murphy. Qcard32.DLL website:'http://www.telusplanet.net/public/stevem/dim card(24)'array to hold cardsgosub[fillCardArray]'fill array with card values
newIndex=0'used when shuffling
tempCard=0'temp var used when shuffling[varSetup]
i=0'i will be our counter var in for/next loops
design=1'default design is circles
offset=20'offset for card vertical spacing for dragging purposesnomainwinWindowWidth=640:WindowHeight=480UpperLeftX=1:UpperLeftY=1menu#1,"&File","&New",[new],"E&xit",[quit]graphicbox#1.g,0,0,640,440open"Dragging Cards"for window_nf as#1#1"trapclose [quit]"'get graphicbox handle
hBox=hwnd(#1.g)'open the dllopen"qcard32.dll"fordllas#qc
'initialize the deckCall InitializeDeck hBox
[new]Call SetDefaultValues
'draw a nice background#1.g "down; fill 10 190 225"#1.g "backcolor 10 190 225"'temp message for this demo only#1.g "place 10 420"#1.g "\Right-click on a card, then move mouse to drag and drop it."gosub[shuffleCards]
offset=20call SetOffSet offset 'set offset to 20 pixels - default is 16'set xy location to start deal
x=10:y=2:row=1for i =1to24Call DealCard hBox,card(i),x,y
'When creating a row or pile of cards,'only the topmost card should have an'IsBlocked value of FALSE.if row<4thencall AdjustCardBlocked card(i),1
x=x+100if x>510then'move to next row
x=10
y=y+offset
row=row+1endifplaywave"card.wav",sync
'pause 100 milliseconds between cardscall Pause 100scannext#1.g "when rightButtonDown [initDrag]"wait[initDrag]gosub[checkIndex]'find out which card on table was clickedif clickCard=0thenwait
nCard=card(clickCard)'find value of card from our card array
isBlocked=GetCardBlocked(nCard)'see if card is blockedif isBlocked<>0then'if card is blocked, inform user, then do nothing#1.g "place 10 420;\BLOCKED Card Index is ";nCard;space$(100)waitendif
cardIndex=InitDrag(hBox,MouseX,MouseY)#1.g "place 10 420;\Drag Card Index is ";nCard;space$(100)#1.g "when rightButtonMove [doDrag]"wait[doDrag]call DoDrag hBox,MouseX,MouseY#1.g "when rightButtonUp [endDrag]"wait[endDrag]#1.g "when rightButtonUp"
cardIndex=EndDrag(hBox,MouseX,MouseY)#1.g "place 10 420;\Destination Card Index is ";cardIndex;space$(100)wait[checkIndex]
clickCard=0''reset values
mx=MouseX: my=MouseY'mouse x and y location'each row is incremented by offset of 20selectcasecase my<=22'first rowif mx<=90then clickCard=1if(mx>=110)and(mx<=190)then clickCard=2if(mx>=210)and(mx<=290)then clickCard=3if(mx>=310)and(mx<=390)then clickCard=4if(mx>=410)and(mx<=490)then clickCard=5if(mx>=510)and(mx<=590)then clickCard=6case(my>=22)and(my<42)'second rowif mx<=90then clickCard=7if(mx>=110)and(mx<=190)then clickCard=8if(mx>=210)and(mx<=290)then clickCard=9if(mx>=310)and(mx<=390)then clickCard=10if(mx>=410)and(mx<=490)then clickCard=11if(mx>=510)and(mx<=590)then clickCard=12case(my>=42)and(my<62)'third rowif mx<=90then clickCard=13if(mx>=110)and(mx<=190)then clickCard=14if(mx>=210)and(mx<=290)then clickCard=15if(mx>=310)and(mx<=390)then clickCard=16if(mx>=410)and(mx<=490)then clickCard=17if(mx>=510)and(mx<=590)then clickCard=18case(my>=62)and(my<162)'fourth row, full card heightif mx<=90then clickCard=19if(mx>=110)and(mx<=190)then clickCard=20if(mx>=210)and(mx<=290)then clickCard=21if(mx>=310)and(mx<=390)then clickCard=22if(mx>=410)and(mx<=490)then clickCard=23if(mx>=510)and(mx<=590)then clickCard=24caseelse
clickCard=0endselectreturn[fillCardArray]'fill card array'cards 1 to 52 are in the first deck'cards 53 to 104 are in the second deck'use cards Jack through King in each suit, first deck
card(1)=11'jack of clubs
card(2)=12'queen
card(3)=13'king
card(4)=24'jack of diamonds
card(5)=25'queen
card(6)=26'king
card(7)=37'jack of hearts
card(8)=38'queen
card(9)=39'king
card(10)=50'jack of spades
card(11)=51'queen
card(12)=52'king'now use second deck, to fill second half of arrayfor i =1to12
card(i+12)=card(i)+52nextRETURN[shuffleCards]playwave"shuffle.wav",async
'now shuffle cardsfor i =1to24
newIndex=int(rnd(0)*24)+1
tempCard=card(i)'temp var to allow switching values
card(i)=card(newIndex)'this index now contains value from random index
card(newIndex)=tempCard 'random index now contains value from other index'now card(i) has switched values with a random card in the arraynextplaywave"shuffle.wav",sync
RETURN[quit]close#qc:close#1:end'''''''''''''''''''''subs and functions:Sub Pause ms
'pause ms number of millisecondscalldll#kernel32,"Sleep",_
ms aslong, re asvoidEndSubSub SetOffSet offset
calldll#qc,"SetOffSet",offset aslong,_
re asvoidendsubSub AdjustCardBlocked nC, bValue
calldll#qc,"AdjustCardBlocked",_
nC aslong, bValue aslong, re asvoidendsubFunction GetCardBlocked(nC)calldll#qc,"GetCardBlocked",nC aslong,_
GetCardBlocked aslongendfunctionFunction InitDrag(hndle, x, y)calldll#qc,"InitDrag",_
hndle asulong, x aslong, y aslong,_
InitDrag aslongendfunctionSub DoDrag hndle,x,y
calldll#qc,"DoDrag",hndle asulong,_
x aslong, y aslong, r asvoidendsubFunction EndDrag(hndle,x,y)calldll#qc,"EndDrag",hndle asulong,_
x aslong, y aslong, EndDrag aslongendfunctionSub InitializeDeck hndle
calldll#qc,"InitializeDeck",_
hndle asulong,r aslongEndSubSub DealCard hndle,nC,x,y
'places card on window whose handle is hndle at x,y'nC is number of card - 1-52 in first deck and'53-104 in second deck, if usedcalldll#qc,"DealCard",hndle asulong,nC aslong,_
x aslong,y aslong,r asvoidEndSubSub SetDefaultValues
'reset all card properties back to their default values.calldll#qc,"SetDefaultValues",r asvoidEndSub
QCard DLL Lesson 10
Lesson 9 Lesson 11-
QCard DLL Lesson 10 | More on Card Dragging | Blocked Cards | Blocking Some Cards | Preventing the Drag of Blocked Cards | Offset Value | DEMO
See Lesson 1 for QCard DLL and WAV files needed for the demo code.
More on Card Dragging
In the previous demo we allowed the user to right-click on a card and drag it around. When the user released the button the card was dropped in a new position. We paid no attention to the card's status or to the location it was dropped. In a real game we'd need to monitor all such activity.
Blocked Cards
QCard has two functions to set and query the blocked status of a card. A card can be marked as blocked if it is covered or partially covered by another card. QCard lets us set the "blocked" status with AdjustCardBlocked. We can query the "blocked" status of a card with GetCardBlocked. The API calls look like this:
Blocking Some Cards
In our demo, we'll change the display so that the rows of cards overlap. Each row is 20 pixels below the previous row. When we deal the first three rows we'll also set the "blocked" status of each card in that row to "blocked". We will leave the cards in the fourth row with the default "unblocked" status. We've introduced a variable called "row" to keep track of the row being dealt.
Preventing the Drag of Blocked Cards
When the user right-clicks the mouse we'll use the coordinates from the deal to discover which card in our card array was clicked by comparing MouseX and MouseY positions with the locations in the grid. Once we find the array index we can get the value of the card at that index and query its blocked status.
If the card is blocked we document that at the bottom of the display and halt the routine. If it is not blocked, we begin the dragging procedure, as we did in the last lesson.
In a real game you'd want to set the blocked status of the newly uncovered card to 0. You'd also need to set the card that was just covered by the dragged card to "blocked". We did not complicate the code with these routines in order to keep the demo program short and understandable.
Offset Value
QCard has a default offset value of 16 pixels. The cards are 70x95 pixels and if one card is dealt atop another, but 16 pixels lower both card values will be visible. This offset is important in block dragging operations. We are dragging a single card, so it is not critical to our demo.
Even though it is not necessary for our demo, we've used this opportunity to show how to change the offset with SetOffSet.
DEMO
See Lesson 1 for QCard DLL and WAV files needed for the demo code.The demo deals the cards in four overlapped rows. It sets the blocked status of cards in the first three rows. It checks the blocked status of the card clicked by the user. It allows the user to drag the card if the designated card status is "unblocked".
QCard DLL Lesson 10 | More on Card Dragging | Blocked Cards | Blocking Some Cards | Preventing the Drag of Blocked Cards | Offset Value | DEMO
Lesson 9 Lesson 11