Re: Line Generator

new topic     » goto parent     » topic index » view thread      » older message » newer message

On Thu, 7 Sep 2000 19:02:46 -0700, Thomas wrote:

>What I'm REALLY working on is:

<snipped program to save space>

>oh yeah, is there a way to play a procedure more than once besides writing
>it twice?

Absolutely, that's the whole purpose of using procedures and functions...
it's re-usable code.

As for your program, I think that Euphoria's 'prompt_number' function fits
well in a program like this.  Below I give further suggestions in my
comments.  I wasn't sure what you wanted to do with 'grolin' (changed
to 'groline' because I kept adding an 'e' to the end of it) so I provided
two versions.  Just change the 'groline1' to 'groline2' in the MAIN PROGRAM
section at the bottom to see the other version (what I thought you might
have wanted based on the name of the procedure).  I hope I'm not taking the
fun out of experimenting by 'giving my version', I'm just hoping to give
you ideas...  (you can take it anywhere you'd like to from here)


-- grolin.ex --
include get.e
include graphics.e

-- use constants to define the screen resolution, so if you
--  change the resolution, you only have to change these
--  two numbers at the top of your program
constant
  Max_X = 320,
  Max_Y = 200

integer desc

integer gmode
--gmode = graphics_mode(19)

-- try to set graphics mode to 320 x 200, 256 color

-- set graphics mode, exit if we've failed to do so
if graphics_mode(19) then
  puts( 1, "unable to set graphics mode...\n" )
  abort(1)  -- exit program with error code = 1
end if

-- Define your procedures and functions before the main program
--  just like you would with constants and variables
-- Procedures and functions are re-usable, this is the reason for
--  their existance
procedure groline1( integer n )
  --atom ul,ll,ur,lr,lcol
  integer lcolor
  sequence point1, point2

  -- start by clearing screen
  clear_screen()

-- NOTE: this looks like a job for 'prompt_number' (see above)
  --desc=gets(0)
  --desc=desc[1..2]   -- I'd like the numbers entered, not the ENTER key

  --while desc>0 do
  for i = 1 to n do -- easiest to use a 'for' loop in this case
    lcolor = rand(14)+1  -- add one since color 0 = black (can't see it)
    --ul=rand(100)
    --ll=rand(100)
    -- NOTE: you can put these in a sequence to pass in draw_line
    --  use your constants so that the end points are contained on screen
    point1 = { rand(Max_X), rand(Max_Y) }
    --ur=rand(100)
    --lr=rand(100)
    point2 = { rand(Max_X), rand(Max_Y) }
    --draw_line(lcol,{{ul,ll},{ur,lr}})
    draw_line( lcolor, {point1, point2} )
    --desc-=1
  end for

  -- wait for user input before we put text over the lines
  if wait_key() then end if  -- an empty if/then gets me out of assigning
                             --  the result of wait_key() to a variable
end procedure

procedure groline2( integer n )
  integer lcolor
  sequence lastpt, nextpt

  clear_screen()

  lastpt = { rand(Max_X), rand(Max_Y) }
  for i = 1 to n do
    nextpt = { rand(Max_X), rand(Max_Y) }
    lcolor = rand(14)+1
    draw_line( lcolor, {lastpt, nextpt} )
    lastpt = nextpt
  end for

  if wait_key() then end if

end procedure

-- MAIN PROGRAM STARTS HERE --
--puts(1,"            Enter a Number from one to nine.")

while 1 do
  desc = prompt_number( "Enter a number from one to fifty...\n" &
                        "(enter zero to exit program): ", {0,50} )
  if desc then -- if desc != 0 then run procedure 'groline'
    groline1( desc )  -- 'desc' gets passed to groline()
  else                -- (which is variable 'n' in groline)
    gmode = graphics_mode(-1) -- otherwise restore graphics mode
    exit                      -- and exit while loop
  end if
end while
-- END MAIN PROGRAM --

-- have fun!!!
-- Brian

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu