Weekly Qbasic and Qb64 Lesson Topics
March 29, 2024, 10:13:27 am
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Want to see a specific Tutorial? ASK!
 
  Home Help Search Arcade Links Staff List Login Register  

RndRange() -- one of my all-time favorite routines

Pages: [1]
  Print  
Author Topic: RndRange() -- one of my all-time favorite routines  (Read 345 times)
Quark
Post Demos
*
Posts: 14


« on: June 15, 2011, 10:42:49 pm »

OldDosLover suggested that RndRange could use an example.  I could do a standard dice throw using two dice: Print "You have rolled the dice and come up with " +str$(RndRange(1,6)) + "and " str$(RndRange(1,6)).

However (heh), I just finished a little self-learning exercise that was fun for me, who isn't much of a graphics person and it uses RndRange a lot.  Have a look at:

' Trapped square
SCREEN _NEWIMAGE(640, 480, 256)
DIM w, h, x1, y1, x2, y2, dx, dy AS INTEGER
w = _WIDTH(0): h = _HEIGHT(0)
x1 = w / 2 - 50: y1 = h / 2 - 50
x2 = x1 + 100: y2 = y1 + 100: dx = 1: dy = 1
RANDOMIZE TIMER
DO
  LINE (x1, y1)-(x2, y2), _RGB(&H00, &H00, &H00), BF
  IF x1 < 1 THEN dx = RndRange(1, 3)
  IF y1 < 1 THEN dy = RndRange(1, 3)
  IF x2 >= w - 1 THEN dx = -RndRange(1, 3)
  IF y2 >= h - 1 THEN dy = -RndRange(1, 3)
  x1 = x1 + dx: y1 = y1 + dy: x2 = x2 + dx: y2 = y2 + dy
  LINE (x1, y1)-(x2, y2), _RGB(&HFF, &H40, &H40), BF
  _DELAY (.005)
  IF INKEY$ <> "" THEN EXIT DO
LOOP
END
FUNCTION RndRange% (lo AS INTEGER, hi AS INTEGER)
RndRange = INT(RND * (hi - lo + 1) + lo)
END FUNCTION

This reminded me of the old Qix game somehow...

--Quark
Report Spam   Logged

Pages: [1]
  Print  
 
Jump to:  

Powered by EzPortal
Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum


Powered by SMF | SMF © 2016, Simple Machines
Privacy Policy