Forum breadcrumbs - You are here:WeLoveGod RallysPublic Forums: futurebasicGameplayKit Card Shuffler
You need to log in to create posts and topics.
GameplayKit Card Shuffler
77,940 Posts
#1 · March 11, 2022, 4:23 am
Quote from Forum Archives on March 11, 2022, 4:23 amPosted by: kashmidheiser <kashmidheiser@...>
This demo uses CocoaUI GameplayKit's:fn GKRandomSourceArrayByShufflingObjectsInArrayfunction to simulate shuffling a deck of cards and drawing five random cards. The code can easily be adapted to dealing any kind of card hands.When the app is launched, the code parses a 2560x1160 open source Wikipedia image of a card deck found at:and stores an array of ImageRefs for each parsed card as an app property. (The Wikimedia Commons image has an ugly green background which I easily removed in Affinity Photo.) I've attached a copy of my modified image, but it may not come through at the correct resolution. If you need a copy of the modified image, contact me back channel. Note: The image is renamed "card_deck.png" in the demo.An interesting thing about the GamplayKit function, is that its use is not necessarily limited to games. It's useful any time an array needs to be randomly assorted.Hope you enjoy.Kenoutput file "GameplayKit Card Shuffler"include "Tlbx GameplayKit.incl"include resources "card_deck.png"_window = 1begin enum output 1_card1_card2_card3_card4_card5_shuffleBtnend enumvoid local fn BuildWindow'~'1CGRect rNSUInteger ir = fn CGRectMake( 0, 0, 730, 240 )window _window, @"GameplayKit Card Shuffler", r, NSWindowStyleMaskTitled + NSWindowStyleMaskClosable + NSWindowStyleMaskMiniaturizabler = fn CGRectMake( 20, 60, 130, 160 )for i = _card1 to _card5imageview i, YES, ,r, NSImageScaleAxesIndependently, NSImageAlignCenter, NSImageFrameGrayBezel, _windowr = fn CGRectOffset( r, 140, 0 )nextr = fn CGRectMake( 585, 15, 130, 26 )button _shuffleBtn,,, @"Shuffle cards", rend fnlocal fn CropImage( image as ImageRef, moveRight as CGFloat, moveUp as CGFloat, croppedRect as CGRect ) as ImageRef'~'1ImageRef croppedImageCGRect originalImageRectoriginalImageRect = fn CGRectMake( moveRight, moveUp, 411, 295 )croppedImage = fn ImageWithSize( fn CGSizeMake( 212, 295 ) )ImageLockFocus( croppedImage )ImageDrawInRectFromRect( image, croppedRect, originalImageRect, NSCompositeCopy, 1.0 )ImageUnlockFocus( croppedImage )end fn = croppedImage// Load array of card ImageRefs on launchvoid local fn ProcessImagesFromMaster'~'1ImageRef image, croppedImageNSUInteger i, cardCounterCGFloat moveRight, moveUpCFMutableArrayRef cardArr_rightIncrement = 196_upIncrement = 287cardArr = fn MutableArrayWithCapacity( 0 )image = fn ImageNamed( @"card_deck.png" )moveRight = 0 : moveUp = 0 : cardCounter = 1for i = 1 to 52if ( cardCounter == 14 )cardCounter = 1moveRight = 0moveUp = moveUp + _upIncrementend ifcroppedImage = fn CropImage( image, moveRight, moveUp, fn CGRectMake( -1, -1, 427, 305 ) )moveRight = moveRight + _rightIncrementcardCounter++MutableArrayAddObject( cardArr, (CFTypeRef)croppedImage )nextAppSetProperty( @"Cards", cardArr )end fnvoid local fn ShuffleCards'~'1CFArrayRef shuffledCardsArrNSUInteger iImageRef imageshuffledCardsArr = fn GKRandomSourceArrayByShufflingObjectsInArray( fn GKRandomSourceInit, fn AppProperty( @"Cards" ) )for i = 1 to 5image = fn ArrayObjectAtIndex( shuffledCardsArr, i )ImageViewSetImage( i, image )nextend fnvoid local fn DoDialog( ev as long, tag as long, wnd as long )'~'1select ( ev )case _btnClickselect ( tag )case _shuffleBtn : fn ShuffleCardsend selectend selectend fnon dialog fn DoDialogfn ProcessImagesFromMasterfn BuildWindowfn ShuffleCardsHandleEvents--
To unsubscribe, send ANY message to: futurebasic-unsubscribe@freegroups.net To access the list archives, go to: http://freegroups.net/groups/futurebasic/
Posted by: kashmidheiser <kashmidheiser@...>
This demo uses CocoaUI GameplayKit's:
fn GKRandomSourceArrayByShufflingObjectsInArray
function to simulate shuffling a deck of cards and drawing five random cards. The code can easily be adapted to dealing any kind of card hands.
When the app is launched, the code parses a 2560x1160 open source Wikipedia image of a card deck found at:
and stores an array of ImageRefs for each parsed card as an app property. (The Wikimedia Commons image has an ugly green background which I easily removed in Affinity Photo.) I've attached a copy of my modified image, but it may not come through at the correct resolution. If you need a copy of the modified image, contact me back channel. Note: The image is renamed "card_deck.png" in the demo.
An interesting thing about the GamplayKit function, is that its use is not necessarily limited to games. It's useful any time an array needs to be randomly assorted.
Hope you enjoy.
Ken
output file "GameplayKit Card Shuffler"
include "Tlbx GameplayKit.incl"
include resources "card_deck.png"
_window = 1
begin enum output 1
_card1
_card2
_card3
_card4
_card5
_shuffleBtn
end enum
void local fn BuildWindow
'~'1
CGRect r
NSUInteger i
r = fn CGRectMake( 0, 0, 730, 240 )
window _window, @"GameplayKit Card Shuffler", r, NSWindowStyleMaskTitled + NSWindowStyleMaskClosable + NSWindowStyleMaskMiniaturizable
r = fn CGRectMake( 20, 60, 130, 160 )
for i = _card1 to _card5
imageview i, YES, ,r, NSImageScaleAxesIndependently, NSImageAlignCenter, NSImageFrameGrayBezel, _window
r = fn CGRectOffset( r, 140, 0 )
next
r = fn CGRectMake( 585, 15, 130, 26 )
button _shuffleBtn,,, @"Shuffle cards", r
end fn
local fn CropImage( image as ImageRef, moveRight as CGFloat, moveUp as CGFloat, croppedRect as CGRect ) as ImageRef
'~'1
ImageRef croppedImage
CGRect originalImageRect
originalImageRect = fn CGRectMake( moveRight, moveUp, 411, 295 )
croppedImage = fn ImageWithSize( fn CGSizeMake( 212, 295 ) )
ImageLockFocus( croppedImage )
ImageDrawInRectFromRect( image, croppedRect, originalImageRect, NSCompositeCopy, 1.0 )
ImageUnlockFocus( croppedImage )
end fn = croppedImage
// Load array of card ImageRefs on launch
void local fn ProcessImagesFromMaster
'~'1
ImageRef image, croppedImage
NSUInteger i, cardCounter
CGFloat moveRight, moveUp
CFMutableArrayRef cardArr
_rightIncrement = 196
_upIncrement = 287
cardArr = fn MutableArrayWithCapacity( 0 )
image = fn ImageNamed( @"card_deck.png" )
moveRight = 0 : moveUp = 0 : cardCounter = 1
for i = 1 to 52
if ( cardCounter == 14 )
cardCounter = 1
moveRight = 0
moveUp = moveUp + _upIncrement
end if
croppedImage = fn CropImage( image, moveRight, moveUp, fn CGRectMake( -1, -1, 427, 305 ) )
moveRight = moveRight + _rightIncrement
cardCounter++
MutableArrayAddObject( cardArr, (CFTypeRef)croppedImage )
next
AppSetProperty( @"Cards", cardArr )
end fn
void local fn ShuffleCards
'~'1
CFArrayRef shuffledCardsArr
NSUInteger i
ImageRef image
shuffledCardsArr = fn GKRandomSourceArrayByShufflingObjectsInArray( fn GKRandomSourceInit, fn AppProperty( @"Cards" ) )
for i = 1 to 5
image = fn ArrayObjectAtIndex( shuffledCardsArr, i )
ImageViewSetImage( i, image )
next
end fn
void local fn DoDialog( ev as long, tag as long, wnd as long )
'~'1
select ( ev )
case _btnClick
select ( tag )
case _shuffleBtn : fn ShuffleCards
end select
end select
end fn
on dialog fn DoDialog
fn ProcessImagesFromMaster
fn BuildWindow
fn ShuffleCards
HandleEvents

--
To unsubscribe, send ANY message to: futurebasic-unsubscribe@freegroups.net To access the list archives, go to: http://freegroups.net/groups/futurebasic/
Click for thumbs down.0Click for thumbs up.0