I agree, this is an amazing forum, and Codea itself is an amazing tool! One thing that makes you think about how much time you’re spending on Codea is when you start to write your Language Arts final in code… Happened to me today, true story!
When I used to do something similar when 3d modeling in college (the 6mhz 286 with overdrive was the best chip you could get at the time). I would look at things and break then into primitives.
I have to admit to running into what seems to be a brick wall, as I have made no progress at all in the last few days. I’m not screaming for help just yet but just giving an update.
I cannot seem to get the reels to spin for x time before stopping, even using the code above kindly given by Ignatz. I am thinking it may need a total code rewrite in order for it to function but I’m working hard and even though I’m making no progress, I still feel I’m learning a bit more.
@Tyson you have to be prepared to make complete rewrites of your code several times, as you progress in abstraction level with your program. This will make eventually your code more expandable and easier to evolve, but it is always painfull to rewite because it feel like a regression, for days or week… Good luck!
I’m having a bit of trouble with this piece of code, this is responsible for the error that appears.
for x=1, 3 do
r1[x] = string.sub(reel1,re1+x,re1+x) -- get the next character from reel 1
end
Trying to work out how to fix it from messing up.
Hi again, several days more of playing with this. It doesn’t look like I’ve done much but most of my time has been spent trying code that never really worked and reading up on tutorials.
I still cannot understand how to have the reels spinning for x amount of time, even with Ignatz code I cannot do it. Maybe it’s a blind spot I have, I dunno. But I’m trying my best and still working and learning.
Here is my code atm
-- Project 1 - iPad Text Slot Machine
-- Use this function to perform your initial setup
function setup()
w = WIDTH/2 -- halfway across screen
h = HEIGHT/2 -- halfway up screen
font("Courier-Bold") -- select font type
strokeWidth(5)
fill(255) -- make characters filled white
fontSize(100) -- make the letters large
reel1 = "XOXOXOXOXXXOXXOXXOXO" -- reel 1 setup
reel2 = "XXOXOOXOXXOOXOOXXOOX" -- reel 2 setup
reel3 = "OXXOXOOXXOXOXXOXXOXX" -- reel 3 setup
re1=math.random(1,20) -- get random reel 1 string position
re2=math.random(1,20) -- get random reel 2 string position
re3=math.random(1,20) -- get random reel 3 string position
r1 = "" -- reel 1 character setup
r2 = "" -- reel 2 character setup
r3 = "" -- reel 3 character setup
money=100 -- starting money for new game
win=0 -- Winner text flag
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50) -- clear the screen
text(r1, w - fontSize(), h) -- draw reel 1 character
text(r2, w, h) -- draw reel 2 character
text(r3, w + fontSize(), h) -- draw reel 3 character
pushStyle()
font("Arial-BoldMT") -- select font type
strokeWidth(5)
fill(255) -- make characters filled white
fontSize(50) -- make the letters large
text("Money £",w-75,h+300) -- ipad screen dimensions
text(money,w+75,h+300) -- ipad screen dimensions
text("£1 per spin, XXX wins you £10",w*1,h*1.5)
if win==1 then
text("Winner!",w,h/2)
end
popStyle()
end
function spinreels() -- spin the reels
re1 = re1 + 1
re2 = re2 + 1
re3 = re3 + 1
print("r1:",r1) -- debugging/testing
print("r2:",r2) -- sometimes reel 1 or 3 doesnt display and the variable is empty
print("r3:",r3) -- need to work out what causes it
if re1 == 20 then -- restart reel position if at end
re1 = 1 -- back to start of reel
end
if re2 == 20 then -- restart reel position if at end
re2 = 1 -- back to start of reel
end
if re3 == 20 then -- restart reel position if at end
re3 = 1 -- back to start of reel
end
end
function touched(t) -- detect screen touch
if CurrentTouch.state==BEGAN then
spinonce() -- call the function to spin once (possibly unneeded?)
spinreels() -- call the function to spin the reels
money = money - 1
end
end
function spinonce() -- attempt to only spin once, this may be pointless and removed later
r1 = string.sub(reel1,re1,re1) -- get the next character from reel 1
r2 = string.sub(reel2,re2,re2) -- get the next character from reel 2
r3 = string.sub(reel3,re3,re3) -- get the next character from reel 3
win=0
if r1 == "X" and r2 == "X" and r3 == "X" then -- experimental code to test for winning line
money = money + 10
win=1
end
end
@Tyson, try this
-- Project 1 - iPad Text Slot Machine
-- Use this function to perform your initial setup
function setup()
w = WIDTH/2 -- halfway across screen
h = HEIGHT/2 -- halfway up screen
font("Courier-Bold") -- select font type
strokeWidth(5)
fill(255) -- make characters filled white
fontSize(100) -- make the letters large
--table works best
reels = {"XOXOXOXOXXXOXXOXXOXO","XXOXOOXOXXOOXOOXXOOX","OXXOXOOXXOXOXXOXXOXX"}
money=100 -- starting money for new game
win=0 -- Winner text flag
state="Ready"
end
function draw()
background(40, 40, 50) -- clear the screen
--see how clear the code is when you break it into blocks
--the state variable totally controls what is drawn
if state=="Ready" then DrawScreen("Press any key to spin")
elseif state=="Spin" then DrawSpin()
---note \
is a line feed..
elseif state=="Win" then DrawScreen(numbers .."\
\
You won! \
\
Press any key to spin")
elseif state=="Lose" then DrawScreen(numbers.."\
\
You lost! \
\
Press any key to spin")
end
end
--see how this all purpose text function draws the screen for all the states
function DrawScreen(txt)
pushStyle()
font("Arial-BoldMT") -- select font type
strokeWidth(5)
fill(255) -- make characters filled white
fontSize(50) -- make the letters large
text("Money £",w-75,h+300) -- ipad screen dimensions
text(money,w+75,h+300) -- ipad screen dimensions
text("£1 per spin, XXX wins you £10",w*1,h*1.5)
if txt then
fill(255,255,0) -- make characters filled white
fontSize(25) -- make the letters large
text(txt,w,h)
end
popStyle()
end
function DrawSpin()
delayTimer=delayTimer+DeltaTime --see timer descriptions at bottom
totalTimer=totalTimer+DeltaTime
if totalTimer>totalSpinTime then --stop spin, see what we have
--I'll leave this code to you
--you figure out if they won or lost, my choice is random, just to test this works
GetNumbers() --puts final numbers in numbers variable
if math.random()<.5 then state="Win" else state="Lose" end
elseif delayTimer>spinDelay then --rotate text if it's time
for i=1,3 do
re[i]=re[i]+1
if re[i]>string.len(reels[i]) then re[i]=1 end
end
delayTimer=0 --reset delay timer
end
GetNumbers()
DrawScreen(numbers) --draw the default screen
end
--put this in its own function because we need to calculate it in two places
--try never to repeat code!
function GetNumbers()
numbers=""
for i=1,3 do
numbers=numbers .." " .. string.sub(reels[i],re[i],re[i])
end
end
function touched(t) -- detect screen touch
if CurrentTouch.state==BEGAN then
--spinonce() -- call the function to spin once (possibly unneeded?)
--spinreels() -- call the function to spin the reels
SpinReels()
money = money - 1
end
end
function SpinReels()
state="Spin"
spinDelay=0.1 --deay between rotating each letter (too fast otherwise)
totalSpinTime=3 --total spin time
delayTimer,totalTimer=0,0 --timers
re={math.random(1,20),math.random(1,20),math.random(1,20)}
end
@Tyson, I know you aren’t asking for help, but why stay stuck when you can get help, right?
Try this:
function spinreels(time) -- spin the reels
re1 = re1 + 1
re2 = re2 + 1
re3 = re3 + 1
print("r1:",r1) -- debugging/testing
print("r2:",r2) -- sometimes reel 1 or 3 doesnt display and the variable is empty
print("r3:",r3) -- need to work out what causes it
if re1 == 20 then -- restart reel position if at end
re1 = 1 -- back to start of reel
end
if re2 == 20 then -- restart reel position if at end
re2 = 1 -- back to start of reel
end
if re3 == 20 then -- restart reel position if at end
re3 = 1 -- back to start of reel
end
r1 = string.sub(reel1,re1,re1) -- get the next character from reel 1
r2 = string.sub(reel2,re2,re2) -- get the next character from reel 2
r3 = string.sub(reel3,re3,re3) -- get the next character from reel 3
local timeToNextSpin = 0.1
if time > timeToNextSpin then -- Don't spin again if time is out or already won
tween.delay(timeToNextSpin, function() spinreels(time - timeToNextSpin) end)
else -- Done spinning, time is up, check for winner
if r1 == "X" and r2 == "X" and r3 == "X" then -- experimental code to test for winning line
money = money + 10
won = true
end
end
end
function touched(t) -- detect screen touch
if CurrentTouch.state == ENDED then
won = false -- Didn't win, just spun
spinreels(1) -- call the function to spin the reels
money = money - 1
end
end
How does it work? When you call spinreels you pass it how long you want them to spin for (1 second in this example). Then, once the function spins the reel it checks if the time is greater than the delay between spins. If not, it is done spinning. If it is though, it waits (tween.delay) and then calls the spinreels function again, passing it the time it had minus the time it is waiting. Hope that makes sense.
@Tyson What you need to do is take the check for “XXX” out of spinonce(). Each time you touch the screen, set a count to 60. In draw(), if that count is greater than 0, call the spinonce(), spinreels() routines, subtract 1 from the count. When the count is 0, check for the “XXX”. Because the call to spinonce() is in draw(), it will change the values in r1,r2,r3 and display them. That should give the spin effect. Since draw() is called 60 times per second, they will appear to spin for 1 sec. You should also comment out the print statements when you’re done.
EDIT: I modified your code and it works. I know you want to do it yourself, so I’m not going to show you unless you ask.
EDIT: Instead of adding 1 to re1,re2,re3, you should use math.random to just pick a number from 1 to 20. By adding 1, you’re going to get the same results to keep repeating.
Thanks for the advice and the code guys.
@ignatz & @JakAttak thank you for the code, I hope you won’t be offended but I’m trying to do it myself after reading Dave’s hints.
@Tyson - that’s ok, but don’t waste our work. See if you can follow what we did, and the techniques we used.
I got it working, it’s much better than the reels just appearing
@Dave1707 regarding re1,2,3 I know they rotate the reels one step at a time but that’s what I wanted, in an arcade the reels are fixed and rotate? I figured random reels would not be authentic, maybe silly but it’s how my brain works.
My original plan was to do this in text then swap the text for sprites, add a nice front end and end up with a cool little game. After some research I think my original idea was flawed, I don’t think it would be that easy to swap the text for sprites, also making a routine to actually animating the reels rotating would be next to impossible for someone with my limited experience so I guess I will settle with keeping it text but I will add some more things: ie a run out of money screen, possibly holding a reel and other small ideas
Here’s my now spinning reels code
-- Project 1 - iPad Text Slot Machine
-- Use this function to perform your initial setup
function setup()
w = WIDTH/2 -- halfway across screen
h = HEIGHT/2 -- halfway up screen
font("Courier-Bold") -- select font type
strokeWidth(5)
fill(255) -- make characters filled white
fontSize(100) -- make the letters large
reel1 = "XOXOXOXOXXXOXXOXXOXO" -- reel 1 setup
reel2 = "XXOXOOXOXXOOXOOXXOOX" -- reel 2 setup
reel3 = "OXXOXOOXXOXOXXOXXOXX" -- reel 3 setup
re1=math.random(1,20) -- get random reel 1 string position
re2=math.random(1,20) -- get random reel 2 string position
re3=math.random(1,20) -- get random reel 3 string position
r1 = "" -- reel 1 character setup
r2 = "" -- reel 2 character setup
r3 = "" -- reel 3 character setup
money=100 -- starting money for new game
win=0 -- Winner text flag
count=0 -- reel spinning counter
start=1 -- flag to stop you lost text showing at beginning of game
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50) -- clear the screen
text(r1, w - fontSize(), h) -- draw reel 1 character
text(r2, w, h) -- draw reel 2 character
text(r3, w + fontSize(), h) -- draw reel 3 character
pushStyle()
font("Arial-BoldMT") -- select font type
strokeWidth(5)
fill(255) -- make characters filled white
fontSize(50) -- make the letters large
text("Money £",w-75,h+300) -- ipad screen dimensions
text(money,w+75,h+300) -- ipad screen dimensions
text("£1 per spin, XXX wins you £10",w*1,h*1.5)
if win==1 then
text("Winner!",w,h/2) else
end
if win==0 and count<=0 and start==0 then
text("Sorry, you lost!",w,h/2)
end
popStyle()
if count > 0 then
spinonce() -- call the function to spin once (possibly unneeded?)
spinreels() -- call the function to spin the reels
end
if count == 0 then
if r1 == "X" and r2 == "X" and r3 == "X" then -- experimental code to test for winning line
money = money + 10
win=1
end
end
count = count - 1 -- reduce reel spin time
end
function spinreels() -- spin the reels
re1 = re1 + 1
re2 = re2 + 1
re3 = re3 + 1
print("r1:",r1) -- debugging/testing
print("r2:",r2) -- sometimes reel 1 or 3 doesnt display and the variable is empty
print("r3:",r3) -- need to work out what causes it
if re1 == 20 then -- restart reel position if at end
re1 = 1 -- back to start of reel
end
if re2 == 20 then -- restart reel position if at end
re2 = 1 -- back to start of reel
end
if re3 == 20 then -- restart reel position if at end
re3 = 1 -- back to start of reel
end
end
function touched(t) -- detect screen touch
if CurrentTouch.state==BEGAN then
money = money - 1 -- pay to spin reels
count=30 -- reset reel spin count
start=0
end
end
function spinonce() -- attempt to only spin once, this may be pointless and removed later
r1 = string.sub(reel1,re1,re1) -- get the next character from reel 1
r2 = string.sub(reel2,re2,re2) -- get the next character from reel 2
r3 = string.sub(reel3,re3,re3) -- get the next character from reel 3
win=0
end
@Tyson One thing you might try for the spin is, increase your count to 90 ( 3 seconds of spin) and in the spinreels() routine, you can add code so that if the count is greater than 60, add 1 to re1. If count is greater than 30, add 1 to re2. if count is greater than 0, add 1 to re3. When the screen is touched, reel1 will stop after 1 second, reel2 will stop after 2 seconds, and reel3 will stop after 3 seconds. Another thing I noticed was there was a time when only 2 of the 3 reels showed.
@Tyson The problem with one of your reels missing is because the value of re1, re2, or re3 is getting greater than 20. Change your “if” statements in spinreels() from “if re1==20” to “if re1>=20”. Same for re2 and re3. That missing reel happens when re1, re2, or re3 starts out at 20.
@Dave1707 oh wow thank you for that. Really helpful
Sorry for taking so much of your time but it’s much appreciated
@Tyson I’m retired, so I have plenty of time. Especially when it’s only 5 degrees F outside. It’s great that you only want suggestions instead of code. That’s a great way to learn. You’d rather dig for the answers then have them shown to you.
Thanks Dave
I’m playing with sprites now and found it actually very easy to use sprites instead of the text, but I have a question. If I add custom sprites to my game, how can I then give the sprites to anyone looking at my code when I paste my code in here?
@Tyson You would have to put your sprites on a public site (Dropbox) or some other one. Then your code would have a link to your sprites to read them in. There was an example of this in another post that I was trying to find.
Thanks, I’ll search for that.
I’m really pleased with my progress at the moment, I know it’s early days and I’m working on a simple game but it’s great fun, especially when you fix a problem or figure out how to do something.
Today I had various problems, the reels would spin continuously and your money would drop if you tapped the screen mid-spin, I’ve fixed that now. Got sprites working for the reel graphics which is really cool, had huge problems trying to get a ‘game over’ screen working, now that’s working and a tap resets the game.
Getting a little more adventurous now and I’m working out how to implement a hold system. Nudges are above me right now but baby steps. I really love codea and you get such a buzz when you get something to work or fix a problem.
@tyson - I’ve posted about that here
http://coolcodea.wordpress.com/2013/05/23/62-3d-downloading-images-on-demand/