Newbie Slot Machine Project

What the heck, here’s what I have made today. I’m proud of it even though I know you guys could do it 1000x better and neater. I’m happy with my days work. I understand far far more after only 8-9 hours working on this and online research than I did before. Comments are welcome as long as it’s not too harsh :wink:

-- noobie code

-- Use this function to perform your initial setup
function setup()
w=WIDTH/2 -- halfway across screen
h=HEIGHT/2 -- halfway up screen

reel1="XOXOXOXOXXXOXXOXXOXO" -- reel 1 setup
reel2="XXOXOOXOXXOOXOOXXOOX" -- reel 2 setup
reel3="OXXOXOOXXOXOXXOXXOXX" -- reel 3 setup

re1=0 -- read reel string position
re2=0 -- read reel string position
re3=0 -- read reel string position

ld=1 -- loop delay to slow down reels
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
     background(40, 40, 50) -- clear the screen

    -- This sets the line thickness
    strokeWidth(5)
    fill(255)
    fontSize(50)   -- make the letters large
    
if ld==30 then -- loop to slow down the display
    
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

text(r1,w-40,h) -- draw reel 1 character
text(r2,w,h) -- draw reel 2 character
text(r3,w+40,h) -- draw reel 3 character

re1=re1+1
re2=re2+1
re3=re3+1


if r1=="X" and r2 =="X" and r3=="X" then -- experimental code to test for winning line
    text("WINNER!",w,h-200)
    end
    
ld=1 -- reset the delay

if re1==20 then -- restart reel position if at end
    re1=0 -- back to start of reel
end
if re2==20 then  -- restart reel position if at end
    re2=0  -- back to start of reel
end
if re3==20 then  -- restart reel position if at end
    re3=0  -- back to start of reel
    end
end

ld = ld + 1 -- delay loop

end

@Ignatz But if you have a goal that you’re working towards, that might be enough of an incentive to keep trying. That’s why I said it may take months to get somewhere. Writing small bits of code and constantly adding them to the main program is a good way to learn. For some, looking at someone else’s code isn’t fun, even if it will teach them something. There has to be a balance in there somewhere. Like what’s been said above, learn how to draw something on the screen. Add code to make it move. Keep trying to write small things that you will need in your game. I agree that they have to know the language, but jumping to the example I gave at the start, there’s a lot of code that can be written just knowing how to use “if”, “for”, “tables”, and “variables”. And it shouldn’t take too long to understand how those 4 things work.

Wow! Nicely done for your first program. And you even got the code to display right in the forum. Well done I say

=D>

Nice! :slight_smile:

@Tyson, I’m impressed, especially for your first program. Nice work. =D>

I have mainly one suggestion: formatting. Personally I find that having ‘proper’ indentation and spacing is a must as it helps make the code a lot easier to read, especially as it grows in size.

I have done the indentation and spacing on your above program for you to give you an example, I suggest you continue doing so but you may wish to keep it as above. To each his own. (Side-note, I also moved the text out of the loop so that it wouldn’t flicker)

-- noobie code

-- Use this function to perform your initial setup
function setup()
    w = WIDTH/2 -- halfway across screen
    h = HEIGHT/2 -- halfway up screen
    
    reel1 = "XOXOXOXOXXXOXXOXXOXO" -- reel 1 setup
    reel2 = "XXOXOOXOXXOOXOOXXOOX" -- reel 2 setup
    reel3 = "OXXOXOOXXOXOXXOXXOXX" -- reel 3 setup
    
    re1 = 0 -- read reel string position
    re2 = 0 -- read reel string position
    re3 = 0 -- read reel string position
    
    r1 = ""
    r2 = ""
    r3 = ""
    
    ld = 1 -- loop delay to slow down reels
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50) -- clear the screen

    -- This sets the line thickness
    strokeWidth(5)
    fill(255)
    fontSize(100)   -- make the letters large
    
    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

    if ld == 30 then -- loop to slow down the display
        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
    
        re1 = re1 + 1
        re2 = re2 + 1
        re3 = re3 + 1
        
        
        if r1 == "X" and r2 == "X" and r3 == "X" then -- experimental code to test for winning line
            text("WINNER!", w, h - 200)
        end
        
        ld = 1 -- reset the delay
        
        if re1 == 20 then -- restart reel position if at end
            re1 = 0 -- back to start of reel
        end
        if re2 == 20 then -- restart reel position if at end
            re2 = 0 -- back to start of reel
        end
        if re3 == 20 then -- restart reel position if at end
            re3 = 0 -- back to start of reel
        end
    end
        
    ld = ld + 1 -- delay loop
end

Welcome on board @Tyson ! You’ve made your first step!

Thanks all and thanks JakAttak!

I’m really having fun working on this, as you can imagine I spend 90% of the time searching online and reading guides for the language and sure ‘most’ things I try don’t work or give errors but I feel I’m learning lots and it’s always better for me to learn when I’m interested in the subject and making a slot machine is interesting to me.

Right now I have figured out how to ‘spin’ the reels when the screen is touched which is pretty awesome!! And now I am trying to research how to only spin the reels once and stop after the touch. It might take me hours, days, weeks to learn and to progress by one little step but it’s fun and I’m enjoying it so far, I guess it’ll get frustrating at some point when I get totally and utterly stuck for weeks etc but I’ll do my best to stick at it.

Sorry if I’m spamming the board too much.

@Tyson - As long as you make a reasonable effort first, nobody minds helping.

Can we suggest you keep all your questions about this project, on this thread, to keep things tidy? You might want to edit the subject name in that case to “Slot machine project” or something similar (just go to your original message and press Edit, and you’ll be able to do that). Then there can be absolutely no complaints about spamming!

I will do that Ignatz, thanks.

@Tyson, I am in a similar position to you. I started my journey a few months back, working on some ideas in the evenings and weekends. I’ve learnt a bit, produced not much at all, but really enjoyed the process of learning; I have a found a creative outlet that I had been looking for for a while.

I am learning by example, too, searching for solutions and trying to implement them into projects that grow from an initial idea. I make many failed attempts before anything works, then more failed attempts to change or enhance things, but I am very slowly getting things to work quicker. The excellent guides from Ignatz can’t be recommended highly enough, and the other regulars who help have impressive knowledge and keen willingness to help. I’m amazed at how quickly they code up examples to people’s questions!!

You have made a very quick start, and I’m sure you’ll be gaining knowledge and ability at an ever increasing rate as you spend more time on your coding. It appears you have a natural aptitude for coding, so go for it and enjoy the ride!

@time_trial Thanks for the very kind comments.

I guess I got very lucky yesterday as today I’ve been working on this about 4-5 hours and I’ve made no progress at all, nothing worked but it’s not a problem, only to be expected when this is totally new to me. I’ll keep reading and trying code :). Thanks for the guide tip, I grabbed it and put it on iBooks on my ipad so it’s to hand along with a few other guides too :slight_smile:

@Tyson - when starting something new, I have learned to

  • start with the simplest example and slowly build up
  • make very small changes and test them before making more
  • when I get something working, leave it, copy the code, and begin a new version so that when I make more changes and mess things up, I can always go back to the old one (see $ below)
  • sprinkle print statements through your code so you can see what is happening (parameter.watch is also a good way of watching how variables change)
  • avoid putting all your code in one function. If you can break it into smaller functions, each of which does something specific, then you can test each function thoroughly on its own (see $$ below). The advantage is that this makes it much easier to test, and to find bugs
  • when you’re are trying to master something unfamiliar, try to find examples of where someone else has used it, and create small stand alone examples of your own to play with

$ sometimes I just copy all the code in a tab to a new one, drag it to the right of the original tab so Codea will use it instead of the original (because when it compiles - left to right - and finds duplicate functions, it overwrites the first with the second) and then start changing the new one. This saves having to make copies of the whole project
.
$$ following on from the previous suggestion, to test chunks of code, such as a new function I’ve just written, I’ll add a temporary tab on the right, and put a setup and draw function in there. Then I’ll use the setup function to test my code, eg try it with a bunch of different values. So if I was testing a function Max to give the greater of two values (which math.max does for you, of course) I might test like this

function setup()
    print(Max(1,2)==2) --should return true in each case
    print(Max(-5,-1)==-1)
    --and so on until you're sure it works
end

function Max(a,b)
    if a>b then return a else return b end
end

--this is only here to replace your normal draw function 
--and prevent it from running
function draw() 
end

When you’re done, you delete or comment out this tab and go back to your program.

Thanks Ignatz! :slight_smile:

I have been working every spare minute on codea. It’s quite addictive isn’t it!!
My code has not moved on a huge amount but I’m still learning although it has grown a little, also the reels now start from random positions and the code has been shuffled around into functions.

I spent the better part of 2 hours this morning trying to get the reels to spin for about 2-3 seconds after you touched the screen then stop. But no joy yet! It wasn’t wasted time as it was fun and I learnt more in the process, sometimes strangely I learn more from making mistakes than not. Haha

Anyway I suspect that I’ll need some kind of timer code to make the reels spin for 2-3 secs, I am looking into it and perhaps I’ll get it working in the next few days.

Here is my code currently.

-- Project 1 - 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
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
    
   if r1 == "X" and r2 == "X" and r3 == "X" then -- experimental code to test for winning line
     winner()
   end 
end

function spinreels() -- spin the reels
        re1 = re1 + 1
        re2 = re2 + 1
        re3 = re3 + 1
        
        print("r1:",r1) -- debugging/testing 
        print("r2:",r2)
        print("r3:",r3)

   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
    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
end

function winner() -- Announce a winning line
    text("Winner!",w,h/2)
end

@Tyson try tween.delay

Set a timer to start with, outside of the draw function

timer=3 --seconds to spin

Then increment it in draw by the time since the last draw, and when it reaches the time you want, stop spinning

--this goes in the draw function
if timer then --only do this next bit if timer is live
    timer=timer-DeltaTime -- subtract time since last draw
    if timer<0 then 
        timer=nil --kill timer
        --do whatever you want here when timer stops, eg read the results
        --to keep things tidy, best is to write a separate function, eg
        ReadResults()
    end
end

Thanks to both of you.

Before anyone says it though, I’d just like to point out I posted my code to show the progress I’d made on it, not to beg for help.

But I will take those ideas and run with them anyway, my thanks.

@Tyson That just goes to show you that everyone here wants to help, even if you don’t ask for it. Maybe you can say “Just posting, no help wanted” when you just want to show the progress of your code…

@dave1707 yes, that’s really great to know you guys are around :slight_smile:

I don’t mind the help, it’s actually very helpful and I’ll learn more but I just don’t want to be accused of begging for help all the time, forgive me I get excited about what I have done and want to show it lol! Anyway JakAttak and Ignatz have given me new lines of research and discovery!

Awesome fun

This is an awesome forum and you are as far fom begging as it gets. Welcome.