Corona Slot Machine is a simple example code that can be used to create a Slot Machine or Casino style app. The example features a fully functional slot machine that cycles through seven images on three slides to create a slot machine feel. This is just the skeleton of the code that can be changed to have a different look or extra features, or be used as is.
You set a default bid used for each spin as well as a default starting amount the user has to play with. After each play the code will check to see if the slides match up to any of the winning combinations and if so, the user will see a message of how much they and an updated total of how much money they have.
The slides graphics are taken from google images so if anyone intends on releasing an app using this code you will have to change the graphics but can keep any of the code or all of the code the same.
This code also uses the new Corona storyboard feature for scene changing and management.
Corona Slot Machine was made starting out from Peach's "Rolling The Dice" tutorial.
main.lua
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | ---------------------------------------------------------------------------------- -- -- Slot Machine Template -- -- Created by "Ertzel" -- -- To publish the code below, you must change the slide.png file to your own work. -- You may use any of the code as you wish. -- ---------------------------------------------------------------------------------- display.setStatusBar( display.HiddenStatusBar ) local storyboard = require "storyboard" require "sprite" storyboard.gotoScene( "menu" ) |
menu.lua
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 62 63 64 65 66 67 68 69 70 | ---------------------------------------------------------------------------------- -- -- Slot Machine Template -- -- Created by "Ertzel" -- -- To publish the code below, you must change the slide.png file to your own work. -- You may use any of the code as you wish. -- ---------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local Title, goTo local function onGoTo( self, event ) if event.phase == "began" then storyboard.gotoScene( "slots", "fade", 300 ) return true end end function scene:createScene( event ) local group = self.view title = display.newText("Corona Slots", 0, 35, native.systemFont, 35) title:setTextColor(255) title.rotation = -25 group:insert(title) goTo = display.newText("Play", 200, 130, native.systemFont, 55) goTo:setTextColor(255) group:insert(goTo) goTo.touch = onGoTo end function scene:enterScene( event ) local group = self.view storyboard.purgeScene( "slots" ) -- Start all listeners goTo:addEventListener( "touch", goTo ) end function scene:exitScene( event ) local group = self.view -- Remove all listeners goTo:removeEventListener( "touch", goTo ) end function scene:destroyScene( event ) local group = self.view end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene |
slots.lua
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | ---------------------------------------------------------------------------------- -- -- Slot Machine Template -- -- Created by "Ertzel" -- -- To publish the code below, you must change the slide.png file to your own work. -- You may use any of the code as you wish. -- ---------------------------------------------------------------------------------- local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local slideTotal = 0 local canSpin = true local total = 100 -- Starting money local bet = 5 -- Cost per play local w,h = display.contentWidth, display.contentHeight local slideSheet, slideSet, slide, slide2, slide3, shade, shade2, shade3, moneyTxt, winTxt local function textFadeIn(params) winTxt.text = "+ $"..params -- Fade in win text transition.to( winTxt, { time=500, alpha=1, x=(w/2), y=(h/2) } ) -- Fade back out win text transition.to( winTxt, { time=500, delay=1000, alpha=0, x=380, y=10 } ) end local function endRoll() -- Set default winnings for spin local winnings = 0 -- Stop slides from moving slide:pause() slide2:pause() slide3:pause() -- Check for matches if (slide.currentFrame == 7 and slide2.currentFrame == 7 and slide3.currentFrame == 7) then -- All three are 7's winnings = 800 elseif (slide.currentFrame == 2 and slide2.currentFrame == 2 and slide3.currentFrame == 2) then -- All three are Bar's winnings = 80 elseif (slide.currentFrame == 3 and slide2.currentFrame == 3 and slide3.currentFrame == 3) then -- All three are Bell's winnings = 40 elseif (slide.currentFrame == 6 and slide2.currentFrame == 6 and slide3.currentFrame == 6) then -- All three are Watermelon's winnings = 25 elseif ( (slide.currentFrame == 1 or slide.currentFrame == 4 or slide.currentFrame == 5) and (slide2.currentFrame == 1 or slide2.currentFrame == 4 or slide2.currentFrame == 5) and (slide3.currentFrame == 1 or slide3.currentFrame == 4 or slide3.currentFrame == 5) ) then -- All three are Cherry's winnings = 15 elseif ( ((slide.currentFrame == 1 or slide.currentFrame == 4 or slide.currentFrame == 5) and (slide2.currentFrame == 1 or slide2.currentFrame == 4 or slide2.currentFrame == 5) and (slide3.currentFrame == 2 or slide3.currentFrame == 3 or slide3.currentFrame == 6 or slide3.currentFrame == 7)) or ( (slide.currentFrame == 1 or slide.currentFrame == 4 or slide.currentFrame == 5) and (slide2.currentFrame == 2 or slide2.currentFrame == 3 or slide2.currentFrame == 6 or slide2.currentFrame == 7) and (slide3.currentFrame == 1 or slide3.currentFrame == 4 or slide3.currentFrame == 5) ) or ((slide.currentFrame == 2 or slide.currentFrame == 3 or slide.currentFrame == 6 or slide.currentFrame == 7) and (slide2.currentFrame == 1 or slide2.currentFrame == 4 or slide2.currentFrame == 5) and (slide3.currentFrame == 1 or slide3.currentFrame == 4 or slide3.currentFrame == 5)) ) then -- Any two are Cherry's winnings = 5 elseif (slide.currentFrame == 1 or slide.currentFrame == 4 or slide.currentFrame == 5 or slide2.currentFrame == 1 or slide2.currentFrame == 4 or slide2.currentFrame == 5 or slide3.currentFrame == 1 or slide3.currentFrame == 4 or slide3.currentFrame == 5) then -- Any one are Cherry's winnings = 2 end if winnings > 0 then textFadeIn(winnings) end -- Set and display new total total = total - bet + winnings moneyTxt.text = "Money: $" .. total -- Let user spin again canSpin = true end local function rollslide() if canSpin == true and total > bet then -- Turn off spin option canSpin = false -- Start spinning all three slides slide:play() slide2:play() slide3:play() -- Random spin time randomTime = math.random(1500, 3500) timer.performWithDelay(randomTime, endRoll, 1) end end function scene:createScene( event ) local group = self.view slideSheet = sprite.newSpriteSheet( "slide.png", 155, 452) slideSet = sprite.newSpriteSet( slideSheet, 1, 6 ) sprite.add( slideSet, "slide", 1, 7, 160, 0) sprite.add( slideSet, "slide2", 1, 7, 100, 0) sprite.add( slideSet, "slide3", 1, 7, 230, 0) slide = sprite.newSprite( slideSet ) slide.x = 75 slide.y = 150 slide:prepare("slide") group:insert(slide) shade = display.newImage("shade.png", true) shade.x = 75 shade.y = 165 group:insert(shade) slide2 = sprite.newSprite( slideSet ) slide2.x = 245 slide2.y = 150 slide2:prepare("slide2") group:insert(slide2) shade2 = display.newImage("shade.png", true) shade2.x = 245 shade2.y = 165 group:insert(shade2) slide3 = sprite.newSprite( slideSet ) slide3.x = 415 slide3.y = 150 slide3:prepare("slide3") group:insert(slide3) shade3 = display.newImage("shade.png", true) shade3.x = 415 shade3.y = 165 group:insert(shade3) moneyTxt = display.newText("Money: $" .. total, 10, 10, native.systemFont, 35) moneyTxt:setTextColor(120, 250, 50) moneyTxt:setReferencePoint( display.CenterReferencePoint ) group:insert(moneyTxt) winTxt = display.newText("+2", 380, 10, native.systemFont, 55) winTxt:setTextColor(50, 100, 250) winTxt:setReferencePoint( display.CenterReferencePoint ) winTxt.alpha = 0 end function scene:enterScene( event ) local group = self.view -- Start all listeners used to spin slides slide:addEventListener("tap", rollslide) slide2:addEventListener("tap", rollslide) slide3:addEventListener("tap", rollslide) end function scene:exitScene( event ) local group = self.view -- Remove all listeners slide:removeEventListener("tap", rollslide ) slide2:removeEventListener("tap", rollslide ) slide3:removeEventListener("tap", rollslide ) end function scene:destroyScene( event ) local group = self.view end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene |
Love this sample code! Thanks for posting! I really like the approach to the slot machine by using sprite sheets. Really good idea :)
Regards,
Jordan Schuetz
Ninja Pig Studios