Example code: Making a slot machine that works

I was playing around with pulp with one of my games and realized that slot machines could be made in pulp.

firefox_MM4RW7dKxB

Example code:
This only uses player code

source code
source JSON file.zip (3.4 KB)

on enter do // what the variable values start as
  winner = 0
	money = 50
	slots1 = 0
	slots2 = 0
	slots3 = 0
	pay = 0
	begin = 1 // variable for starting the slot machine
end

on draw do // draws stuff on the screen, for example, the variables
	label "tokens: {money}" at 0,0 // labels value of your cash
	if begin==1 then
		label "Press A to insert monies " at 0,14 // tells the player to press A
		label "{slots1}" at 11,5 // labels the slot machine var "slots1"
		label "{slots2}" at 12,5 // labels the slot machine var "slots2"
		label "{slots3}" at 13,5 // labels the slot machine var "slots3"
	end
	if begin==2 then
		label "{slots1}" at 11,5 // labels the slot machine var "slots1"
		label "{slots2}" at 12,5 // labels the slot machine var "slots2"
		label "{slots3}" at 13,5 // labels the slot machine var "slots3"
		if begin==3 then
			label "{slots1}" at 11,5 // labels the slot machine var "slots1"
			label "{slots2}" at 12,5 // labels the slot machine var "slots2"
			label "{slots3}" at 13,5 // labels the slot machine var "slots3"
		end
		if winner==1 then // if you actually win something
			label "winner" at 10,10 // shows that you won
		else
		end
		if pay==1 then // if you pay you will be able to crank
			label "   Crank handle to play  " at 0,14
		else
		end
	end
end

on confirm do // your A button
	if begin==1 then // if begin = 1 then pressing A will make being = 2
		tell event.player to // tells the player to call pay (on pay do)
			call "pay"
			begin = 2 // begin variable now = 2
		end
	else // else nothing happens
	end
end

on pay do // if you paid, your money variable goes down by -1
	pay = 1
	money--
end

on crank do // if pay = 1 then you can crank and attempt to win again
	if pay==1 then
		pay = 0
		tell event.player to // calls slots after paying
			call "slots"
		end
	end
end

on slots do
	if begin==2 then // the values of the slot1, slot2, and slot3 variables will randomize from 0,1,2, or 3
		slots1 = random 0,3
		slots2 = random 0,3
		slots3 = random 0,3
		wait 0 then // after randomizing each of the 3 variables once, it will wait 0 seconds then do again in a loop 7 more times
		slots1 = random 0,3
		slots2 = random 0,3
		slots3 = random 0,3
		wait 0 then
		slots1 = random 0,3
		slots2 = random 0,3
		slots3 = random 0,3
		wait 0 then
		slots1 = random 0,3
		slots2 = random 0,3
		slots3 = random 0,3
		wait 0 then
		slots1 = random 0,3
		slots2 = random 0,3
		slots3 = random 0,3
		wait 0 then
		slots1 = random 0,3
		slots2 = random 0,3
		slots3 = random 0,3
		wait 0 then
		slots1 = random 0,3
		slots2 = random 0,3
		slots3 = random 0,3
		wait 0 then
		slots1 = random 0,3
		slots2 = random 0,3
		slots3 = random 0,3
		tell event.player to // tells player to call match0 mmatch1, match2, and match3
			call "match0"
			call "match1"
			call "match2"
			call "match3"
		end
	end
end
end
end
end
end
end
end
end

on match0 do
	begin = 1 //begin var goes back to 1
	if slots1==1 then //if all the 3 variables = 0, your player gains 1 token
		if slots2==1 then
			if slots3==1 then
				money ++ //money you earn
				winner = 0 //text doesn't display that you won
			end
		end
	end
end

on match1 do
	begin = 1 //begin var goes back to 1
	if slots1==1 then //if all the 3 variables = 1, your players money increases
		if slots2==1 then
			if slots3==1 then
				money += 20 //money +20
				winner = 1 //text displays from draw saying that you won
			else
				winner = 0 //text doesn't display that you won
			end
		end
	end
end

on match2 do
	begin = 1 //begin var goes back to 1
	if slots1==2 then //if all the 3 variables = 2, your players money increases
		if slots2==2 then
			if slots3==2 then
				money += 40 //money +50
				winner = 1
			else
				winner = 0
			end
		end
	end
end

on match3 do
	begin = 1 //begin var goes back to 1
	if slots1==3 then //if all the 3 variables = 3, your players money increases
		if slots2==3 then
			if slots3==3 then
				money += 70 //money +70
				winner = 1
			else
				winner = 0
			end
		end
	end
end

You are free to use the code.

I changed the code so the crank will function correctly. The crank will only work if you rotate it in a specific area. There is also buy-ins where if you insert more than 1 credit, you might win or lose more. You can insert 1, 2, 5, or 10 credits total. When you select the buy-in, pressing A will insert that amount in, and let you crank to try and win more credits.

1 Like

Update: I fixed all the bugs and added a custom background. I hope to have a written tutorial this weekend or Labor Day.

Many improvements: The player can use the d-pad to choose how much credits to spend.
I plan on adding reel animations, logo screen, title screen, options (select backgrounds) etc.

firefox_xgMqt1aVkq

The source code and prelease game is available here, but its very incomplete. I should hopefully have it complete this weekend or Labor Day.

1 Like

Already looking a lot more like a slot machine! It'd be great if you could animate the slots spinning.

1 Like

I plan on trying that today or tomorrow. Idea: When a player cranks, the game will cause each reel to play a spinning animation. When the player presses the A button, the first reel will then have a random value (0,3) then stop spinning. After the first reel stops spinning and lands on a random value, the player will be able to do repeat that process for the other 2 reels. Each reel type (X, 7's, watermelons, and gold coins) will have a start and end animation. Start animations where the reel begins spinning and a end animation where the reel stops spinning

Edit: I also will remove the top text. Total cash will be represented with a dollar sign inside the box

1 Like

This looks much better. I also created a title screen with a menu, and will add a options menu where the player can choose which background they want. I have added some of the code, but just need to add a room called options. In the level select event in the player code, if the background is set to 0, when the player starts the game, they will go to a room with a slot machine with the forest background. If the background variable is set to 1 the player will go to a room with a slot machine on a blank background.

I would like to add some more backgrounds, but I'll probably add them later.

1 Like

Some updates to my game. I created more backgrounds (converted and altered cc0 images). The game also has reel spin animations
msedge_Js57jeEB7l

msedge_cY1W8EO9KK

I'm not sure what other backgrounds to add. I tried adding casino related ones, but the reels take up most of the screen. I might play around with animating some of the backgrounds. I need to add title screen art, saving, sounds, and make the source code more readable.