From my Merlin's Mystical Magic series - Puzzle 1

Started by geecee, March 12, 2009, 02:58:49 AM

Previous topic - Next topic

geecee

This is one of the puzzles from my Merlin's Wide and Wonderful World of Mystical Magic series.

It is necessary that you download and unzip the attached Media.zip file to the same folder as the one where you download this programme in order for it to run properly.

PlayBASIC Code: [Select]
remstart
=======================================================
Puzzles from my Merlin's Wide and Wonderful World
of Mystical Magic
*************************************************
Author: geecee
Originally written for Dark Basic - October 2007
Rewritten for Play Basic - March 2009
*************************************************
Puzzle Number 1
=======================================================
remend


`Start of programme
start:

` Null message
message$=""

` Assure random is always different
randomize timer()

` Set text style and size
loadfont "arial bold",1,20,0

` Determine height of font
fontheight=gettextheight("arial bold")

` Declare array/s
dim message$(2):dim a$(84)

` Tell PB to include the input support library
#include "Input"

` Set new ink colour
ink rgb(255,217,128)

` This command will hide the mouse pointer
mouse off

` Load an image to file
loadimage "base.bmp",1

` Display the image
drawImage 1,0,0,false

` Define the message/s (a space is needed at the end of each line
` to write the last word)
message$=message$+"|Hello! My name is Merlin. "
message$=message$+"||Welcome to my Wide and Wonderful World of Mystical Magic. "

` Determine text width of longest message line and where to write text to screen.
tw=gettextwidth("Welcome to my Wide and Wonderful World of Mystical Magic. ")/2
across=300-tw:down=315-fontheight*1/2:wide=across+0
wide=wide+tw*2

` A short wait before proceeding
sync
wait 3000

` Call the function to write words to screen
writewords(across,down,message$,tw,wide)

` A short wait before proceeding
for times=1 to 4
sync
wait 300
next times

` Set ink colour and draw a box to hide previous text at location
ink rgb(255,0,0)
box 2,315,580,315+fontheight*4,1

` Set new ink colour
ink rgb(255,217,128)

` ---------------------------------------------------
` Select a number from 10 to 99
` ---------------------------------------------------
` Write message/s to screen
message$="Left click mouse to select a number from 10 to 99"
centertext 300,266,message$

` A short wait before proceeding
sync
wait 300

` Set across and down coordinates
across=300:down=287

` Until mouse is clicked
repeat

` Set ink colour and draw a box to hide previous text at location
ink rgb(255,0,0)
box 290,287,310,307,1

` Set new ink colour
ink rgb(255,217,128)

` If mouse is clicked
mb=mousebutton()

` Determine random number
number=rnd(89)+10
chosennumber$=str$(number)

` Write chosen number to screen and exit loop
centertext across,down,chosennumber$

` A short wait before proceeding
sync
wait 30

until mb=1

` Write message/s to screen
message$="You have chosen number "+chosennumber$
centertext 300,266+fontheight*3,message$

` A short wait before proceeding
sync
wait 3000

` ---------------------------------------------------
` Add two digits of number and subtract from original
` ---------------------------------------------------
` Write message/s to screen
message$(1)="Add the two digits of the number together and subtract your answer"
message$(2)="from "+chosennumber$+". Remember your new answer and click mouse"
centertext 300,266+fontheight*5,message$(1)

` Determine text width of longest message line and where to write text to screen.
tw=gettextwidth("Add the two digits of the number together and subtract your answer")/2
across=300-tw
text across,266+fontheight*6,message$(2)

` A short wait before proceeding
sync
wait 300

` Wait for mouseclick
repeat

` If mouse is clicked
mb=mousebutton()

until mb=1 or mb=2

Login required to view complete source code


As always, comments constructive or otherwise appreciated.

:)
Enjoy
geecee

Others from my Merlin's Wide and Wonderful World of Mystical Magic series

Merlin's Mystical Magic series - Puzzle 2
Merlin's Mystical Magic series - Puzzle 3
Merlin's Mystical Magic series - Magical card Predictor
LANG MEY YER LUM REEK

A smile costs less than electricity and gives more light :)

micky4fun

hi geecee

very nicely put together and well presented , works evey time even if i think of my own number and not the one generated ,

some loverly magic trick programs you have written and given a good insight in how to display text

good work ,
mick :)

kevin

#2
  Looks pretty good. But  rather than posting the 'snippet' and media separately.. Why not zip up the project folder post it. So everything is together and ready to run.

  code wise,  there's a few unnecessary bits and bobs and couple of dodgy practices..

* Randomizing isn't  required.  The runtime automatically calls Randomize prior to starting a program.

* Most drawing commands have two versions,  one to use the INK colour, and coloured version.  IE. BOX (ises iNK) and BoxC (user supplied ink).  The latter won't change the INK colour.


` Set ink colour and draw a box to hide previous text at location
ink rgb(255,0,0)
box 2,315,580,315+fontheight*4,1


  becomes


` Set ink colour and draw a box to hide previous text at location
boxc 2,315,580,315+fontheight*4,1,rgb(255,0,0)


  Which means the you don't have to change the INK colour back to what it was prior to the draw.


* Print/Text can display Constant strings directly, so there's no need for the message variable here


` Write message/s to screen
message$="Left click mouse to select a number from 10 to 99"
centertext 300,266,message$



centertext 300,266,"Left click mouse to select a number from 10 to 99"





*  Use WaitMouse

  So this


  ` Wait for mouseclick
  repeat

    ` If mouse is clicked
    mb=mousebutton()
   
  until mb=1 or mb=2


could be


   Waitmouse false,(1+2)
   mb=mousebutton()



* In the play again Y/N  section.  Rather than test for every case combination,  you can force the case which will get rid of this possibly.

so this


    ` Decide if to try another
    answer$=staticinput("Try another [Y] or [N] > ")

  until answer$="Y" or answer$="y" or answer$="N" or answer$="n" 



Could be,

    ` Decide if to try another and force the answer to be upper case
    answer$=upper$(staticinput("Try another [Y] or [N] > "))

  until answer$="Y" or answer$="N"





* Gosub Start  -  Your usage of the GOSUB operation here is really a NO NO. This should be GOTO. The reason for this is that each time you call a Gosub, the programs current position  is dumped onto the stack.   Because the nature of the gosub operation assumes at some point, you're going to call RETURN to pop that information from the stack and return execution back to the point of the GOSUB statement.   Since that can't occur.   What this program is doing is slowly leaking memory.   Given the type of program this is, it's unlikely anybody would ever re-run the program enough times to make it crash.  But if they did, then  eventually PB will halt it's execution from a stack overflow error.   



* Media management -  This is more a heads up on tweaking the programs design.   What i'm referring to here, is the loading of media (images/fonts) in the middle of the programs executions.   Granted in a small example like this,  we can get away with some memory thrashing, but if we've any concerns over time this process being time critical, then pre-loading media is far better approach.

 



 



geecee

Thanks for your reply micky4fun.

Your comments are appreciated but I have a few things still to learn regarding programme writing as can be seen from kevin's comprehensive reply.

:)
geecee
LANG MEY YER LUM REEK

A smile costs less than electricity and gives more light :)

geecee

#4
Thanks for your comprehensive reply kevin.

My PlayBasic programming skills are on a continual learner level and to post in these forums and learn different ways of writing something to get the same result is certainly paying off for me.

You are most considerate in all of your replies, and that you took the time to go through the whole programme pointing out more efficient procedures is most appreciated.

In this one programme alone you have shown a number of improvements which I will now have to take on board when writing programmes in future.

One thing in particular I must realise is that not everyone has the same fonts installed as I have ...... I see you made a change from "Arial Black" to "Arial".


:)
Thanks again.
geecee
LANG MEY YER LUM REEK

A smile costs less than electricity and gives more light :)