Rectangles that randomly appear

Creating Games? Developing something for fun?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

thrwawy
Posts: 8
Joined: Thu Dec 09, 2021 6:06 pm

Rectangles that randomly appear

Post by thrwawy » Thu Dec 09, 2021 6:13 pm

Yo,

I'm making a game that should, in theory, utilise 4 pre-set rectangles I imported onto the screen and, after the click of the 'start' button, should select 3 at random and prompt the user asking for their guesses on its size (not actual size, just a value between 10 - 20). However, my problem is that I'm not sure how I would go about cycling through these images? I'm assuming I'd need an array of some sort but how would I put these images into the array?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9567
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Rectangles that randomly appear

Post by dunbarx » Thu Dec 09, 2021 6:43 pm

Hi.

Nothing wrong with using arrays, especially if you love them, but I would not if you do not need to. You do not need to.

II assume you already have your rectangles (are they images? Graphics?) resident. And hidden. Did you do the right thing and name them properly? Something like:
rect1. rect2, rect3 and rect4
Then think how this works in a button script:

Code: Select all

on mouseUp
   put "1,2,3,4" into rectList
   repeat 3
      put random(the number of items of rectList) into currentChoice
      show img ("rect" & currentChoice) --assume images
      delete item currentChoice of rectList
   end repeat
end mouseUp
This is an old method, to select random elements from an ever decreasing list. Step through the handler if you need to. You can see how the list is managed...

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9567
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Rectangles that randomly appear

Post by dunbarx » Thu Dec 09, 2021 7:04 pm

HOLD EVERY THING..

Error in previous post. Brain problem. Will fix.

Craig

thrwawy
Posts: 8
Joined: Thu Dec 09, 2021 6:06 pm

Re: Rectangles that randomly appear

Post by thrwawy » Thu Dec 09, 2021 7:28 pm

Hi Craig,

After spending some time with your suggestions it appears that it has worked! :D I hope you don't mind me asking further but how would I refer to these when asking for user input? Should I refer to "currentChoice"?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9567
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Rectangles that randomly appear

Post by dunbarx » Thu Dec 09, 2021 7:51 pm

I think if you try a few times you will see that it does not always work. But this has better thinking:

Code: Select all

on mouseUp
   put "1,2,3,4,"  into rectList
   repeat 3
      put any item of rectList into currentChoice
      show img ("F" & currentChoice)
    replace (currentChoice & ",") with "" in rectList
   end repeat
end mouseUp
The use of "any" instead of "random()" is the key.

As for asking the user, what do you want to say? What choice does the user have when the process is initiated?

Craig

thrwawy
Posts: 8
Joined: Thu Dec 09, 2021 6:06 pm

Re: Rectangles that randomly appear

Post by thrwawy » Thu Dec 09, 2021 8:04 pm

It would go along the lines of, "Enter your guess between 10-20" and I'd like it to be able to see if any of the rectangles on the screen contain that value, returning a "Correct" message to the user

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9567
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Rectangles that randomly appear

Post by dunbarx » Thu Dec 09, 2021 9:46 pm

Ah. I see.

So I assume you have that original handful of rectangles that are named "rect 1", "rect2", "rect3 and "rect4". Maybe something like this in a button script:

Code: Select all

on mouseUp
   ask "What Rectangle (between 1 and 10)"
   if it is an integer then put it into userGuess
   put "rect1,rect2,rect3,rect4" into possibles
   
   if ("rect" & userGuess) is among the items of possibles then show img ("rect" & userguess)
end mouseUp
This uses the same controls as before. No reason to make new stuff until you make your final stuff.

But are you following the steps so far? For example, I am creating names by concatenating a literal, "rect", with a number from 1 to 4 that is unknown until a selection is made. The result is designed to match an existing control. Or not.

This is where LiveCode shines; the process is done virtually in plain english and is very readable.

craig
Last edited by dunbarx on Thu Dec 09, 2021 11:52 pm, edited 1 time in total.

thrwawy
Posts: 8
Joined: Thu Dec 09, 2021 6:06 pm

Re: Rectangles that randomly appear

Post by thrwawy » Thu Dec 09, 2021 10:13 pm

Ah yeah I see what you mean, how would I assign an image a value for the final if statement though so it can see if it is correct?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9567
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Rectangles that randomly appear

Post by dunbarx » Fri Dec 10, 2021 12:02 am

To answer both your questions, how about this:

Code: Select all

on mouseUp
   ask "What Rectangle (between 1 and 10)"
   if it is not an integer then
     answer "Must be an integer"
      exit to top
   else
      put it into userGuess
      put "1,2,3,4" into possibles
      if userGuess is among the items of possibles then show img ("rect" & userGuess)
    end if
end mouseUp
This still assumes you have four hidden images named as before.

Can you read this and pretty much see what is going on? You did not answer about how you are doing with learning how this works. Are you stepping though the handlers? You do know how to do that, right?

Homework: (C'mon Jacque, it has been so long).

Add what happens if the user enters a wrong guess.

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9567
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Rectangles that randomly appear

Post by dunbarx » Fri Dec 10, 2021 12:09 am

I changed the form of this handler to make it easier for you to do your homework. This one is identical in function, but more verbose.

Code: Select all

on mouseUp
   ask "What Rectangle (between 1 and 10)"
   if it is not an integer then
     answer "Must be an integer"
      exit to top
  end if
    --continue...
      put it into userGuess
      put "1,2,3,4" into possibles
      if userGuess is among the items of possibles then
        show img ("rect" & userGuess) --valid guess
      else
      --invalid guess
      --your homework here
    end if
end mouseUp
Craig

thrwawy
Posts: 8
Joined: Thu Dec 09, 2021 6:06 pm

Re: Rectangles that randomly appear

Post by thrwawy » Fri Dec 10, 2021 6:18 pm

Hey Craig,

Would this be what the answer would be?

Code: Select all


else
      answer "Incorrect guess, try again"
    end if

Also, after some careful consideration of the program, I would like it to be able to do something like pick a number(s) from an array and then put that alongside the following line:

Code: Select all


 show img ("rect" & currentChoice)

For example, the random choice of number from the array would look something like this in Python:

Code: Select all

currentChoice = [1, 2, 3, 4, 5]

print(random(.)choices(currentChoice))

How would I be able to go about this? I am, unfortunately, tied down and need to be able to do an array.

(Need to put the "(.)" due to it being detected as an external link).

Thank you for all your help so far, however, it has been immense.

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

Re: Rectangles that randomly appear

Post by Klaus » Fri Dec 10, 2021 7:59 pm

Hi thrwawy,

let me see, so you want to put the numbers from 1 to 5 into an Array?
And later fetch a random vaule from that array?

If yes to both, do like this.
Create array:

Code: Select all

...
put Empty into tChoicesArray
repeat with i = 1 to 5
  put i into tChoicesArray[i]
end repeat
...
Fetch a random value:

Code: Select all

...
## Since the keys of the array are identical to their content, we can just do this:
put the keys of tChoicesArray into tKeys
put any line of tKeys into tRandomValue
...
ANY is a synonym for random, but has to be used differently.
This -> any line of tKeys... is short for:

Code: Select all

...
put the num of lines of tChoicesArray into tNumOfKeys
put random(tNumOfKeys) into tRandomValue
...
Hope that helps!


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9567
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Rectangles that randomly appear

Post by dunbarx » Fri Dec 10, 2021 8:38 pm

thrwawy

You keep asking about arrays. Nothing you have mentioned so far indicates that you need arrays of any kind, unless you are simply using that term generically, as some sort of dataset. So I am seeing that you are very new to LC. Are you also new to programming in general?

We are all here to help you along. That help will be never-ending, mostly playful, and very useful to you. But we are not here to write your application for you.

Please tell me how long you have used LC, and what you have done with it on your own. If very little, fine. We all have to start at the beginning. But you then have a bit of work ahead of you. You simply must start with a few lessons and tutorials. Then you must start playing. I always recommend making a simple stack that does something basic, like an address book or calculator. The final result will likely be useless, but the effort to get it going will be priceless. Maybe if the scope of your current project does not grow dramatically, you can start with that. But you need to get some of this done on your own, or you will never advance.

Anyway, about that question above?

Craig

thrwawy
Posts: 8
Joined: Thu Dec 09, 2021 6:06 pm

Re: Rectangles that randomly appear

Post by thrwawy » Fri Dec 10, 2021 9:44 pm

Hey Craig

I have been using LC since 2016 (school) and this is the year we have been asked to make a project of our own, of which little is taught. I have used Python to an extent for a while, but have only used LC for school work (of which is extremely confusing).

Klaus:

I have used this array and it seems to work, thank you :D
Last edited by thrwawy on Fri Dec 10, 2021 11:31 pm, edited 1 time in total.

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

Re: Rectangles that randomly appear

Post by Klaus » Fri Dec 10, 2021 11:24 pm

It only seems?
Too bad... 8)

Locked

Return to “Games”