Dice game

Creating Games? Developing something for fun?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
catalinanesia
Posts: 83
Joined: Sun Feb 16, 2014 10:08 pm

Dice game

Post by catalinanesia » Tue Nov 04, 2014 5:03 pm

Hi everyone,
I am working on a "Dice" roll game, a bit about the game mechanics:
Throw the dice (a button is generating a random number <1-6> and place it on a field),
move the player on the grid to his new location. Player2 throw the Dice and moves the player
to his new location. The player who reaches the last grid position wins the game.

What I try to accomplish:
If the player1 location is grid5 and the Dice roll is 5 I want to change the grid6 to grid10 collor to Red,
when the player1 reached his new location grid10 the grid6 to grid10 will change color back to Yellow.
(player1 is a button which checks intersection with button grid1 to grid 30) - i accomplished this with this:

Code: Select all

 
on pIntGrid --playerIntersectGrid
   repeat with x=1 to 30
   if intersect (button "player1", button ("grid"&x), "pixels") then
     set the backgroundColor of button ("grid"&x) to "Red"
else
   set the backgroundColor of button ("grid"&x) to "Yellow"
end if
end repeat
end pIntGrid
What I am trying to do for the next step and I am blocked:
I try to put into a "global" variable the current location of player1
(sort of say: hey I am player1 and my current location is grid5).
Here is what I have for that:

Code: Select all

on player1Loc
   repeat with x=1 to 30
   if intersect (button "player1", button ("grid"&x), "pixels") then
      do ("put true into" && (player1LocG&x))
   end if
end repeat
end player1Loc
Here is how I understand variables:
A "box" where I put something (tru/false, numbers, text)
So now I can say:
if the "box" contains player1 location and the "Dice" roll = 5
set the color for grid(player1 location + 5) to color "Red"

Any guidance/help is welcomed.
Thanks,
Catalin
Attachments
DiceGame.zip
(3.33 KiB) Downloaded 366 times

catalinanesia
Posts: 83
Joined: Sun Feb 16, 2014 10:08 pm

Re: Dice game

Post by catalinanesia » Tue Nov 04, 2014 8:31 pm

I think I will take a different approach instead of using Variables will use Field
to add the player1 location based on intersect.
Now I have the below code, and I want on the line: put the last character of ("grid"&x) into field "fieldGridLoc"
to say last 2 characters (in this moment when the player1 intersects the button grid 10 I get in the field 0 (which is correct
but I need the last 2 characters to get the 10 not only the 0, when it intersects the grid12 it takes only the 2 instead of the 12)

So what should I use instead of "last character" ? to get the last digits ...
Example:
If player1 intersects button grid5 then on the field "fieldGridLoc" i will have 5
If player1 intersects button grid15 then on the field "fieldGridLoc" i will have 15

How do I achieve this? (I checked Offset in dictionary but I am not able to use it)
maybe is something else I should check ...

Code: Select all

on pIntGrid
   repeat with x=1 to 30
   if intersect (button "player1", button ("grid"&x), "pixels") then
     set the backgroundColor of button ("grid"&x) to "Red"
   put the last character of ("grid"&x) into field "fieldGridLoc"
else
   set the backgroundColor of button ("grid"&x) to "Yellow"
end if
end repeat
end pIntGrid
Thanks,
Cata

catalinanesia
Posts: 83
Joined: Sun Feb 16, 2014 10:08 pm

Re: Dice game

Post by catalinanesia » Tue Nov 04, 2014 10:29 pm

I am getting closer,
I replaced this line:

Code: Select all

put the last character of ("grid"&x) into field "fieldGridLoc"
with this line:

Code: Select all

put x into field "fieldGridLoc"
now the field "fieldGridLoc" it shows the right values.

My previous question still remain unsolved...

So what should I use instead of "last character" ? to get the last digits ...
Example:
If player1 intersects button grid5 then on the field "fieldGridLoc" i will have 5
If player1 intersects button grid15 then on the field "fieldGridLoc" i will have 15

How do I achieve this? (I checked Offset in dictionary but I am not able to use it)

Regards!

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Dice game

Post by Simon » Tue Nov 04, 2014 10:35 pm

Hi catalinanesia,
I haven't read all your posting but for the last 2 characters
put char -2 to -1 of thisThing into mVar
I think that is what you are after.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

catalinanesia
Posts: 83
Joined: Sun Feb 16, 2014 10:08 pm

Re: Dice game

Post by catalinanesia » Wed Nov 05, 2014 5:06 am

Thank you Simon!

What would be the experienced programmer way of writing this: ?

Code: Select all

on hiliteGrid
   repeat with x = field "fieldGridLoc" to field "diceNumber" + field "fieldGridLoc"
   if intersect (button "player1", button "grid2", "pixels") and field "diceNumber" <= 6 then
   set the backgroundColor of button ("grid"&x) to "Red"
   else
   if intersect (button "player1", button "grid3", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
         else
   if intersect (button "player1", button "grid4", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
         else
   if intersect (button "player1", button "grid5", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
         else
   if intersect (button "player1", button "grid6", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
         else
   if intersect (button "player1", button "grid7", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
         else
   if intersect (button "player1", button "grid8", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
         else
   if intersect (button "player1", button "grid9", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
         else
   if intersect (button "player1", button "grid10", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
         else
   if intersect (button "player1", button "grid11", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
         else
   if intersect (button "player1", button "grid12", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
         else
   if intersect (button "player1", button "grid13", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
         else
   if intersect (button "player1", button "grid14", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
         else
   if intersect (button "player1", button "grid15", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
         else
   if intersect (button "player1", button "grid16", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
         else
   if intersect (button "player1", button "grid17", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
         else
   if intersect (button "player1", button "grid18", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
         else
   if intersect (button "player1", button "grid19", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
         else
   if intersect (button "player1", button "grid20", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
               else
   if intersect (button "player1", button "grid21", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
               else
   if intersect (button "player1", button "grid22", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
               else
   if intersect (button "player1", button "grid23", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
               else
   if intersect (button "player1", button "grid24", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
               else
   if intersect (button "player1", button "grid25", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
               else
   if intersect (button "player1", button "grid26", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
               else
   if intersect (button "player1", button "grid27", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
               else
   if intersect (button "player1", button "grid28", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
               else
   if intersect (button "player1", button "grid29", "pixels") and field "diceNumber" <= 6 then
      set the backgroundColor of button ("grid"&x) to "Red"
               else
   if intersect (button "player1", button "grid30", "pixels") and field "diceNumber" <= 6 then
   set the backgroundColor of button ("grid"&x) to "Red"
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end repeat
end hiliteGrid

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Dice game

Post by Simon » Wed Nov 05, 2014 5:21 am

I didn't put too much work into this but switch/case is fun

Code: Select all

on hiliteGrid
   repeat with x = field "fieldGridLoc" to field "diceNumber" + field "fieldGridLoc"
      switch
         case intersect (button "player1", button "grid2", "pixels") and field "diceNumber" <= 6
            set the backgroundColor of button ("grid"&x) to "Red"
            break
         case intersect (button "player1", button "grid3", "pixels") and field "diceNumber" <= 6
            set the backgroundColor of button ("grid"&x) to "Red"
            break
         case intersect (button "player1", button "grid4", "pixels") and field "diceNumber" <= 6
            set the backgroundColor of button ("grid"&x) to "Red"
            break
         case intersect (button "player1", button "grid5", "pixels") and field "diceNumber" <= 6
            set the backgroundColor of button ("grid"&x) to "Red"
            break
      end switch
   end repeat
end hiliteGrid
If I spend some more time on it I bet I could get something simpler.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Dice game

Post by Simon » Wed Nov 05, 2014 5:33 am

How about this;

Code: Select all

on hiliteGrid
   put 1 into y
   repeat with x = field "fieldGridLoc" to field "diceNumber" + field "fieldGridLoc"
      add 1 to y
      if intersect (button "player1", button ("grid"&y), "pixels") and field "diceNumber" <= 6 then
         set the backgroundColor of button ("grid"&x) to "Red"
      end if
   end repeat
end hiliteGrid
I'm probably missing everything.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

catalinanesia
Posts: 83
Joined: Sun Feb 16, 2014 10:08 pm

Re: Dice game

Post by catalinanesia » Wed Nov 05, 2014 9:06 pm

Thanks Simon,
I tried your both methods to replace my long and boring "not so smart" code but
for some reason your methods are not working as I expected.
(I am not complaining at all, your methods are just example to set me on track ...)
The attached stack contains your methods vs mine, if someone wants to have fun with debug :)

Another dead end comes with the following questions:
How can I STOP a "handler" to run in a Loop after a action has been completed ?
and to restart the "handler" again when needed ?
(I have 2 players, after player1 throw the dice and move the player on the grid the action is completed
player2 throw the dice and move the player on the grid and switch to player1 turn and so on so forth...)

Regards,
Catalin
Attachments
DiceG02.zip
(3.5 KiB) Downloaded 350 times

catalinanesia
Posts: 83
Joined: Sun Feb 16, 2014 10:08 pm

Re: Dice game

Post by catalinanesia » Fri Nov 07, 2014 6:53 pm

Another dead end,
I have a substack Assets where I will keep the game graphics, sounds, etc ...
I have 6 dice images imported as control on the Substack Assets and the below script on a button in the main Stack
(credit and Thanks! to who shared this code on the internet, i found it somewhere)

Code: Select all

on mouseUp
   ## Hide all dice images
   repeat with n=1 to 6
      put"diceR" & n & ".png" into diceImage
      hide image diceImage
   end repeat
   
   ##Show each dice image quickly to simulate the dice roll
   repeat with n=1 to 6
      put "diceR" & n &".png" into diceImage
      show image diceImage
      wait 100 millisecs ### delay before hiding the image again
      hide image diceImage
   end repeat
   
   ##Choose a random dice image to show
   put random(6) into diceNumber
   put "diceR" & diceNumber & ".png" into diceShowFace
   show image diceShowFace 
   
   put dicenumber into field "diceNumber1"
   
end mouseUp
How do I tell "reference" my button to use the images (6 dices) from substack Assets and "skin" them
on the main Stack dice button who have the above script. ?

If I make a button on the substack Assets everything works fine.

Thanks,
Catalin

catalinanesia
Posts: 83
Joined: Sun Feb 16, 2014 10:08 pm

Re: Dice game

Post by catalinanesia » Mon Nov 10, 2014 6:55 pm

Well,
I sort of answering my own question, so what I did is this:
created 6 buttons on the card and i mapped "icon" the images of
the dice from 1 to 6 and now everything works like magic ...
(the dice is rolling)

Another question: is it possible to have 2 cards opened at the same time ? (shown on screen)

Regards,
Catalin

Klaus
Posts: 13837
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Dice game

Post by Klaus » Mon Nov 10, 2014 7:59 pm

Hi Catalin,
catalinanesia wrote:Another question: is it possible to have 2 cards opened at the same time ? (shown on screen)
no, only one card at a time!


Best

Klaus

Post Reply

Return to “Games”