<< Previous Topic | Next Topic >> Return to Index

# Buggy Pixel Rain program

October 15 2007 at 10:48 PM

SCREEN 13 '320 by 200
DIM pixeldead(20) AS INTEGER
DIM pixels(20) AS INTEGER
DIM x(20) AS INTEGER
DIM y(20) AS INTEGER
DIM defy(20) AS INTEGER

FOR i = 1 TO 20
RANDOMIZE TIMER
x(i) = INT(RND * 318 + 1)
y(i) = 1 'setup intial coordinates

SELECT CASE INT(RND * 4 + 1)
CASE 1
defy(i) = .5
CASE 2
defy(i) = 1
CASE 3 'makes pixels fall at different rates
defy(i) = .3
CASE 4
defy(i) = .8
CASE 5
defy(i) = 1.3
END SELECT
NEXT i

'*** Main ***
DO
key\$ = INKEY\$
FOR z = 1 TO 20

IF POINT(x(z), y(z)) = 0 THEN

PSET (x(z), y(z)), 15
PSET (x(z), y(z) - 1), 0
y(z) = y(z) + defy(z)

IF y(z) >= 199 THEN pixeldead(z) = 1 'reset values if pixel > 199

IF pixeldead(z) = 1 THEN
RANDOMIZE TIMER
x(z) = INT(RND * 318 + 1)
y(z) = 1

SELECT CASE INT(RND * 4 + 1)
CASE 1
defy(z) = .5
CASE 2
defy(z) = 1
CASE 3
defy(z) = .3
CASE 4
defy(z) = .8
CASE 5
defy(z) = 1.3
END SELECT

END IF

END IF
NEXT z

WAIT &H3DA, 8, 8
LOOP UNTIL key\$ = CHR\$(27)

 Respond to this message

Forum Owner

# Buggy pixel program 2

October 16 2007, 6:24 AM
 SCREEN 13 '320 by 200DIM pixeldead(20) AS INTEGERDIM pixels(20) AS INTEGERDIM x(20) AS INTEGERDIM y(20) AS INTEGERDIM defy(20) AS INTEGERFOR i = 1 TO 20  RANDOMIZE TIMER  x(i) = INT(RND * 318 + 1)  y(i) = 1 'setup intial coordinates  SELECT CASE INT(RND * 4 + 1)  CASE 1: defy(i) = .5  CASE 2: defy(i) = 1  CASE 3: defy(i) = .3  CASE 4: defy(i) = .8  CASE 5: defy(i) = 1.3  END SELECTNEXT i' *** Main ***DO  FOR z = 1 TO 20    IF POINT(x(z), y(z)) = 0 THEN      PSET (x(z), y(z)), 15      PSET (x(z), y(z) - 1), 0      y(z) = y(z) + defy(z)      IF y(z) >= 199 THEN pixeldead(z) = 1 'reset values if pixel > 199      IF pixeldead(z) = 1 THEN        RANDOMIZE TIMER        x(z) = INT(RND * 318 + 1)        y(z) = 1        SELECT CASE INT(RND * 4 + 1)        CASE 1: defy(z) = .5        CASE 2: defy(z) = 1        CASE 3: defy(z) = .3        CASE 4: defy(z) = .8        CASE 5: defy(z) = 1.3        END SELECT        pixeldead(z) = 0      END IF    END IF  NEXT z  WAIT &H3DA, 8, 8  IF INPUT\$(1) = CHR\$(27) THEN EXIT DO  LOCATE 1, 1: PRINT SPACE\$(240)LOOP UNTIL INPUT\$(1) = CHR\$(27)WHILE INKEY\$ <> "": WENDSYSTEM
 Respond to this message

Forum Owner

# One that works using TYPE

October 16 2007, 9:30 AM
 DECLARE SUB FillBottom ()' http://www.network54.com/Forum/190883/message/1192513695SCREEN 13 '320 by 200RANDOMIZE TIMERTYPE Pixel  x AS INTEGER  y AS INTEGER  yNew AS SINGLE  Delta AS SINGLE  Count AS INTEGEREND TYPE '==========================================================CONST pMax = 20 ' How many pixels in motion at the same time?CONST MaxStack = 5 ' How high is the stack?CONST StackBottom = 100' Where is the bottom of the stack?'========================================================== DIM SHARED P(pMax) AS PixelDIM SHARED Xpos(100) AS INTEGER ' all start locations LOCATE 10, 5: LINE INPUT "Press ENTER to start"; e\$CALL FillBottomLOCATE 10, 1: LINE INPUT "That's all - Press ENTER"; e\$CLSSYSTEM SUB FillBottom ' Determine start for all pMax pixelsDIM i AS INTEGER, j AS INTEGERFOR i = 1 TO pMax: GOSUB InitPixel: NEXT i ' Fill BottomCLSDO  cc = cc + 1: IF cc > 3 THEN cc = 0: WAIT &H3DA, 8: WAIT &H3DA, 8, 8  FOR i = 1 TO pMax    P(i).yNew = P(i).yNew + P(i).Delta    IF INT(P(i).yNew) > P(i).y THEN GOSUB DropPixel  NEXT iLOOP WHILE INKEY\$ = ""SYSTEM DropPixel:IF P(i).Count = MaxStack THEN  FOR j = 1 TO pMax    IF P(j).Count < MaxStack THEN EXIT FOR  NEXT j  IF j < pMax THEN RETURN ELSE EXIT SUBEND IFIF POINT(P(i).x, P(i).y + 1) = 0 AND P(i).y < StackBottom THEN  PSET (P(i).x, P(i).y), 0  P(i).y = P(i).y + 1  PSET (P(i).x, P(i).y), 15ELSE  PSET (P(i).x, P(i).y), 15  IF P(i).Count < MaxStack THEN    P(i).Count = P(i).Count + 1    IF P(i).Count < MaxStack THEN GOSUB StartPixel ELSE GOSUB InitPixel  END IFEND IFRETURN InitPixel:tCount = tCount + 1IF tCount > UBOUND(Xpos) THEN RETURNDO: j = 1 + INT(RND * UBOUND(Xpos)): LOOP UNTIL Xpos(j) = 0Xpos(j) = 1: P(i).x = jP(i).Delta = .1 + RND / 10P(i).Count = 0StartPixel:P(i).y = 1: P(i).yNew = .9RETURN END SUB
 Respond to this message

Forum Owner

# Same without TYPE

October 16 2007, 9:46 AM
 DECLARE SUB FillBottom ()' http://www.network54.com/Forum/190883/message/1192513695SCREEN 13 '320 by 200RANDOMIZE TIMER '==========================================================CONST pMax = 20 ' How many pixels in motion at the same time?CONST MaxStack = 5 ' How high is the stack?CONST StackBottom = 100' Where is the bottom of the stack?'========================================================== DIM SHARED Xpos(100) AS INTEGER ' all start locationsDIM SHARED x(pMax) AS INTEGERDIM SHARED y(pMax) AS INTEGERDIM SHARED yNew(pMax) AS SINGLEDIM SHARED Delta(pMax) AS SINGLEDIM SHARED Count(pMax) AS INTEGER   LOCATE 10, 5: LINE INPUT "Press ENTER to start"; e\$CALL FillBottomLOCATE 10, 1: LINE INPUT "That's all - Press ENTER"; e\$CLSSYSTEM SUB FillBottom ' Determine start for all pMax pixelsDIM i AS INTEGER, j AS INTEGERFOR i = 1 TO pMax: GOSUB InitPixel: NEXT i ' Fill BottomCLSDO  cc = cc + 1: IF cc > 3 THEN cc = 0: WAIT &H3DA, 8: WAIT &H3DA, 8, 8  FOR i = 1 TO pMax    yNew(i) = yNew(i) + Delta(i)    IF INT(yNew(i)) > y(i) THEN GOSUB DropPixel  NEXT iLOOP WHILE INKEY\$ = ""SYSTEM DropPixel:IF Count(i) = MaxStack THEN  FOR j = 1 TO pMax    IF Count(j) < MaxStack THEN EXIT FOR  NEXT j  IF j < pMax THEN RETURN ELSE EXIT SUBEND IFIF POINT(x(i), y(i) + 1) = 0 AND y(i) < StackBottom THEN  PSET (x(i), y(i)), 0  y(i) = y(i) + 1  PSET (x(i), y(i)), 15ELSE  PSET (x(i), y(i)), 15  IF Count(i) < MaxStack THEN    Count(i) = Count(i) + 1    IF Count(i) < MaxStack THEN GOSUB StartPixel ELSE GOSUB InitPixel  END IFEND IFRETURN InitPixel:tCount = tCount + 1IF tCount > UBOUND(Xpos) THEN RETURNDO: j = 1 + INT(RND * UBOUND(Xpos)): LOOP UNTIL Xpos(j) = 0Xpos(j) = 1: x(i) = jDelta(i) = .1 + RND / 10Count(i) = 0StartPixel:y(i) = 1: yNew(i) = .9RETURN END SUB
 Respond to this message
Ty

# Newbie question =(

October 16 2007, 9:15 PM
 I know this is probably simple. But how does DIM and DIM SHARED differ from one another? Please give a few examples if you can Thanks guys for all your help ~Ty
 Respond to this message

Forum Owner

# DIM SHARED

October 17 2007, 4:51 AM
 I separated the function of raining pixels from the main program. If you haven't seen or used SUBs much, you might wonder why. The answer is to make that function  stand-alone so it doesn't clutter the main program. SUBs are like smaller programs. All of the variables are internal. But for some reason unknown to me, it is hard to define an array inside a SUB. So I defined the array outside of the SUB (Put it in MAIN). But now the SUB can't "see" the array as it can only see its own internal variables. So I put SHARED on the DIM. That allows SUBs to ""see" the array. In a program that has no SUBs or FUNCTIONs, you would never used DIM SHARED as it is no different than DIM. Mac
 Respond to this message
chequer

# re Rain programs

October 19 2007, 12:18 AM
 I ran, Buggy pixel programs 1, 2 and ' One that works ' Re program 1 I got error ' illegal outside Sub Function '. I remmed END SUB, then it worked Re program 2 Error free but only puts the 1st. row of drops. Pressing a key toggles between 1 and 2 rows. That's as far as it goes. re ' One that works ' Had to rem END SUB to make it work. Why is it that when a program is copied from the Forum to QBasic the lines of the program are staggered. Printing the program from QB gives a messy Printout. I know that I could just copy it direct from the forum to Word then print it. What I really need is a program with REMS on the lines explaining the functions
 Respond to this message

Forum Owner

# Re: re Rain programs

October 19 2007, 1:36 PM

| re ' One that works '
| Had to rem END SUB to make it work.

NO WAY!! Start all over. The END SUB is required.

| Why is it that when a program is copied from the Forum to QBasic
| the lines of the program are staggered?

I presume you copy from the forum and then use the DOS Edit/Paste function. Well QBasic doesn't know that. It thinks you are entering the program manually.

So suppose you copy this from The QBasic Forum:
IF A = 5 THEN
PRINT "A was 5"
END IF

Now QBasic sees
{IF A = 5 THEN}+{Enter Key}
{     PRINT "A was 5"}+{Enter Key}
{END IF}+{Enter Key}

agreed? OK, after the first two, your screen looks like this
IF A = 5 THEN
PRINT "A was 5"
_
where _ is the cursor. QBasic expects you to start entering the next command indented.

If you were manually entering the input, you would manually backspace at this time. But instead, you are automatically entering the third line. So you will get
IF A = 5 THEN
PRINT "A was 5"
END IF
_ <------ cursor still here

So every line which has indentation will increase the indentation levels.

Never mind all my words above, forget paste from forum. Just play with entering code in the IDE and see for yourself how it works.

=========================================
What to do in order to copy from the forum:

At DOS prompt, enter NOTEPAD xxx.BAS, where xxx is the name you want your new program to have. You should be staring at a blank NOTEPAD screen.

Paste the program you just copied from the Forum and save it.

At DOS prompt, enter QBasic /run xxx

Mac

 This message has been edited by iorr5t on Oct 19, 2007 1:41 PM

 Respond to this message

R

# LOL he copied the entire message with Select all!

October 26 2007, 10:31 PM
 Then he pasted it into the QB Window directly, so there were two END SUB s. When you do that, QB tries to put an END SUB into a SUB immediately. So he had two. Naturally the code would not run with uncommented code. Ted
 Respond to this message

Forum Owner

# Demo with Fast/Slow + Even distribution

October 17 2007, 12:14 PM
 DECLARE SUB FillBottom ()' http://www.network54.com/Forum/190883/message/1192513695SCREEN 13 '320 by 200RANDOMIZE TIMER'==========================================================CONST pMax = 20 ' How many pixels in motion at the same time?CONST MaxStack = 50 ' How high is the stack?CONST StackBottom = 200 ' Where is the bottom of the stack?CONST MaxColumns = 320 ' How many columns?'==========================================================DEFSTR A-Z ' Simulate VB's "Option Explicit"TYPE Pixel  x AS INTEGER  y AS INTEGER  yNew AS SINGLE  Delta AS SINGLE  Born AS INTEGER  Died AS INTEGEREND TYPEDIM SHARED p(pMax) AS Pixel DIM SHARED Count(MaxColumns) AS INTEGERDIM SHARED Falling(MaxColumns) AS INTEGER LOCATE 5, 10: PRINT "During Demo:  F=Faster  S=Slower"LOCATE 7, 15: PRINT "(ESC = Stop demo)"LOCATE 15, 5: LINE INPUT "Press ENTER to start: "; e\$CALL FillBottomLOCATE 10, 1: LINE INPUT "That's all - Press ENTER: "; e\$CLSSYSTEM SUB FillBottomDIM Last AS INTEGER: Last = UBOUND(Count)DIM cc AS INTEGER, Speed AS INTEGER ' Determine start for all pMax pixelsDIM i AS INTEGER, j AS INTEGERFOR i = 1 TO pMax: GOSUB InitPixel: NEXT i ' Fill BottomCLSDO  cc = cc + 1  IF cc > Speed THEN cc = 0: WAIT &H3DA, 8: WAIT &H3DA, 8, 8  FOR i = 1 TO pMax    IF p(i).Born THEN      p(i).yNew = p(i).yNew + p(i).Delta      IF INT(p(i).yNew) > p(i).y THEN GOSUB DropPixel    ELSE      IF RND > .999 THEN p(i).Born = -1    END IF  NEXT i  SELECT CASE UCASE\$(INKEY\$)  CASE "F": Speed = Speed + 1  CASE "S": IF Speed > 0 THEN Speed = Speed - 1  CASE CHR\$(27): EXIT DO  END SELECTLOOPSYSTEM DropPixel:IF p(i).Died THEN RETURNIF POINT(p(i).x, p(i).y + 1) = 0 AND p(i).y < StackBottom THEN  PSET (p(i).x, p(i).y), 0  p(i).y = p(i).y + 1  PSET (p(i).x, p(i).y), 15ELSE  PSET (p(i).x, p(i).y), 15  Count(p(i).x) = Count(p(i).x) + 1  Falling(p(i).x) = 0  GOSUB InitPixelEND IFRETURN InitPixel:DIM GotCandidate AS INTEGER, Finished AS INTEGERGotCandidate = 0: Finished = 0FOR j = 1 TO LastIF Count(j) > MaxStack THEN STOP  IF (Count(j) = MaxStack) THEN    IF (Falling(j) = 0) THEN Finished = Finished + 1  ELSE    IF Falling(j) = 0 THEN GotCandidate = 1  END IFNEXT jIF Finished = Last THEN EXIT SUBIF GotCandidate = 0 THEN p(i).Died = -1: RETURNDO  j = 1 + INT(RND * Last)LOOP UNTIL (Count(j) < MaxStack) AND (Falling(j) = 0)Falling(j) = 1p(i).x = jp(i).Delta = .1 + RND / 10p(i).y = 1: p(i).yNew = .9RETURN END SUB
 Respond to this message
chequer

# Did I get it right

October 21 2007, 5:49 PM
 ' Demo with Fast/Slow + Even distribution (message 1192541058) ' To slow the drops down in the RAINTEST SUB, you changed pMax to a lower number. ie. CONST pMax = 20 In the SUB FILLBOTOM line: {FOR i = 1 to pMax: GOSUB InitPixel:NEXT i } ( is it necessary, in a NEXT program, to have a variable after NEXT. or is it just to help follow the flow of the program? ) The Gosub goes to routine InitPixel, adds 1 and returns. Have I got this right?
 Respond to this message

Forum Owner

# I think so, chequer

October 22 2007, 10:47 AM

> The Gosub goes to routine InitPixel, adds 1 and returns. Have I got this right?

I think so. Just to be sure, here is more explanation.

The line
FOR i = 1 TO pMax: GOSUB InitPixel: NEXT i
does this

1) It sets i to 1

2) It goes to InitPixel.

3) Eventually, InitPixel reaches a RETURN command

4) That gets us to NEXT i.

5) The next command adds 1 to i.

6) If i is less than pMax, it goes to step 2.

7) Otherwise, we are finished with this command.

--------

It is not required to code "i" after NEXT but it is a very good idea.

Consider this code:

FOR i = 1 TO 10
FOR j = 1 TO 10
FOR k = 1 TO 10
PRINT i; j; k
NEXT
NEXT

Say I just coded that, but forgot to put a NEXT after the PRINT command.

If you run that code, it will stop on
FOR i = 1 TO 10
with the 10 highlighted and the error message that NEXT has been omitted. In this short program, it may be easy to see what happened, but in a big one, it is confusing and time consuming. Where do I need the NEXT?

Now if I coded

FOR i = 1 TO 10
FOR j = 1 TO 10
FOR k = 1 TO 10
PRINT i; j; k
NEXT j
NEXT i

The message is still a bit confusing, but not so bad. It will stop on [NEXT j] with the error message "NEXT without FOR". Well, I say to myself, what is he talking about? I have a "FOR j". The answer is to look between the "FOR j" and "NEXT j" to see if there is another FOR. Sure enough, I find it and can see that I need "FOR k".

Mac

 This message has been edited by iorr5t on Oct 22, 2007 10:48 AM

 Respond to this message
 Current Topic - Buggy Pixel Rain program
 << Previous Topic | Next Topic >> Return to Index

Newbies usually go to www.qbasic.com and click on The QBasic Forum
Forum regulars have their own ways, which include The QBasic Community Forums