March 28, 2024, 09:02:04 am
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email?
1 Hour
1 Day
Forever
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
Weekly Qbasic and Qb64 Lesson Topics
>
Forum
>
DEMOS
>
Function + SUB Club
>
RndRange() -- one of my all-time favorite routines
Pages: [
1
]
« previous
next »
Print
Author
Topic: RndRange() -- one of my all-time favorite routines (Read 343 times)
Quark
Post Demos
Posts: 14
RndRange() -- one of my all-time favorite routines
«
on:
June 15, 2011, 10:02:51 am »
I use this in many programs I write, so handy:
Function RndRange%(lo As Integer,hi As Integer)
' Returns a number within a range, e.g. RndRange(1,6) gives 1 or 2 or 3 or 4 or 5 or 6
Return INT(RND*(hi-lo+1)+lo)
End Function
--Quark
Report Spam
Logged
Quark
Post Demos
Posts: 14
Re: RndRange() -- one of my all-time favorite routines
«
Reply #1
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
OlDosLover
Guest
Re: RndRange() -- one of my all-time favorite routines
«
Reply #2
on:
June 15, 2011, 10:59:10 pm »
Hi all,
A fine example of a really good and useful function. I took the liberty of rewriting it slightly. Hope you like my version.
Code:
' Trapped square
DEFINT A-Z
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
_LIMIT 100
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
_DISPLAY
LOOP WHILE INKEY$ = ""
END
FUNCTION RndRange% (lo AS INTEGER, hi AS INTEGER)
RndRange = INT(RND * (hi - lo + 1) + lo)
END FUNCTION
OlDosLover.
Report Spam
Logged
Pages: [
1
]
Print
« previous
next »
Jump to:
Please select a destination:
-----------------------------
General Category
-----------------------------
=> Useful Information ,Rules,FAQs,LINKS,etc.
===> INTRODUCE Yourself
=> General QB64 Forum
=> MS DOS, FREE DOS, ANY DOS
-----------------------------
QB64 lessons and tutorials,Games
-----------------------------
=> Lesson Disussion
=> Weekly Lesson
=> Games
-----------------------------
TUTORIALS
-----------------------------
=> QB64 Tutorials
=> Text Adventure
=> QBasic Tutorials
=> Other Tutorials
-----------------------------
UNSEENS GDK+SFML Librarys
-----------------------------
=> Discussions on GDK+SFML
=> GDK+SFML Demos, and projects
-----------------------------
DEMOS
-----------------------------
=> Working QB64 Demos
=> QB64 Games
=> QB64 Programs You Are Proud Of.
=> Function + SUB Club
-----------------------------
OFF TOPIC,and Off The wall
-----------------------------
=> OffTopic and Off the Wall
Powered by
EzPortal
Loading...