Qbasicnews.com

Full Version: pixel piling?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I figured out how to let pixels falling from the sky. Their cordinates are stored in two arrays, one for the x-axis the other for the y-axis. All oordinates are randomly chosen.

Now I want to let the pixels land on the ground.

(screen 13)
...
for i = 1 to numberofpixels
if y%(i) > 240 then y%(i) = 240
next i
...

This works so far.

Then i want to make a pixel piling.
Code:
|-----------------------------------|
|...................................|
|..................Pile of pixels...|
|..........................v........|
|...................................|
|..........................o........|
|..........................o........|
|....o.<-Single pixel......o........|
|-----------------------------------|
Does anyone how to figure this out >_>?
Perhaps a.. GET-Command?

Please help me.
Before you move a pixel, just check and see if there's already a pixel in the new location. If there is, then don't move that pixel.
to find out if there's a pixel below it, use point.
Be careful with that! If multiple pixels falling with various speeds it may happen that one reaches a slower below and then it will stuck on the sky "thinking" that it reached the ground. This can be fixed by holding the moving pixels in an array, and only let them where they are when they could not move for 2 frames. This way real snow effect can be made by checking if after falling on a pixel there is a hole on either side and countinue rolling there. So pixels will finally form "hills", not just lines next to each other.
Here's some code and documentation to follow...

[syntax="QBASIC"]'THIS PROGRAM MAKES GRAINS OF SAND FALL FROM THE TOP OF THE SCREEN UNTIL THEY
'COME TO REST AT THE BOTTOM. USER CAN ELECT TO FORM VERTICAL STACKS OR PILES
'OF SAND BY CHANGING THE Slide% CONSTANT. ALSO, USER CAN DRAW LINES, CIRCLES
'AND OTHER OBSTACLES FOR THE SAND TO GATHER ATOP.
' written 01/03/2005 by mb.

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Constant declarations
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
CONST Slide% = 1 'set this to 0 for vertical stack, or 1 for sliding pile

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Seed the RND() function and change to graphics mode [320x200]
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
RANDOMIZE TIMER
SCREEN 13
CLS

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Draw some stuff on the screen. Feel free to put whatever you want, here.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
LINE (30, 140)-(50, 139)
CIRCLE (100, 100), 14, 15
PAINT (100, 100), 15
CIRCLE (180, 100), 6, 15
PAINT (180, 100), 15

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'The main program loop.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DO

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Create a new grain of sand at the top of the screen, randomly placed
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
x% = INT(RND * 320)
y% = 0

DO
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Erase the grain of sand
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
PSET (x%, y%), 0

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'If it's at the bottom of the screen, it can't fall anymore
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IF y% = 199 THEN EXIT DO

IF POINT(x%, y% + 1) = 15 THEN
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'The pixel directly under it is occupied.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IF Slide% = 0 THEN
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'We want a vertical stack, so stop moving the grain.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
EXIT DO
ELSE
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'We want a pile of sand, so see if it can fall diagonally
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IF POINT(x% - 1, y% + 1) = 15 THEN
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'The pixel down and to the left is occupied...
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IF POINT(x% + 1, y% + 1) = 15 THEN
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'...as is the one down and to the right! Stop.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
EXIT DO
ELSE
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'...but the one down and to the right is free!
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
y% = y% + 1
x% = x% + 1
END IF
ELSE
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'The pixel down and to the left is free...
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IF POINT(x% + 1, y% + 1) = 15 THEN
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'...as is the one down and to the right, so we
'just pick one of the two randomly to move to.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
y% = y% + 1
x% = x% + -1 ^ (INT(RND * 2))
ELSE
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'...but the one down and to the right is taken!
'Therefore, we move down and to the left.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
y% = y% + 1
x% = x% - 1
END IF
END IF
END IF
ELSE
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'The pixel directly under it is not occupied. Make it fall.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
y% = y% + 1
END IF

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Draw the grain while it's falling.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
PSET (x%, y%), 15
LOOP

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Draw the grain of sand in its final resting place. Time for a new one.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
PSET (x%, y%), 15

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Keep going until a key is pressed.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
LOOP UNTIL INKEY$ <> ""

SYSTEM
[/syntax]

*peace*

Meg.
Here's an interesting variation, 'cause I was bored:

[syntax="QBASIC"]'THIS IS A VARIATION ON THE FALLING SAND PROGRAM. GRAINS OF MULTICOLORED
'SAND FALL FROM THE TOP OF THE SCREEN AND DESCEND UNTIL THEY ENCOUNTER THE
'BOTTOM OF THE SCREEN OR A 'HEAVIER' (I.E. LOWER COLOR NUMBER) GRAIN OF SAND.
'USER CAN SET Chance!, WHICH IS THE CHANCE OF A HEAVIER GRAIN FALLING THROUGH
'A LIGHTER GRAIN OF SAND, THEREBY SWAPPING THEIR POSITIONS.
'
' written 01/03/2005 by mb.

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'This variable controls the percentage of the time that "heavier" sand will
'pass through "lighter" sand.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
CONST Chance! = .999

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Seed the RND() function and change to graphics mode [320x200]
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
RANDOMIZE TIMER
SCREEN 13

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Define our grain of sand.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
TYPE GrainType
x AS INTEGER 'X-coordinate
y AS INTEGER 'Y-coordinate
c AS INTEGER 'Color
END TYPE
DIM grain AS GrainType

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Main program loop, exits when a key is pressed
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DO
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Make a new grain of sand atop the screen. Random X-coord and color
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
grain.x = INT(RND * 320)
grain.y = 0
grain.c = INT(RND * 255) + 1

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Set an Exit Flag to 0. When it equals 1, the grain is no longer falling
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ExitLoop% = 0

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Make the grain fall.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DO
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Erase the grain.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
PSET (grain.x, grain.y), 0

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Determine the color of the pixel underneath the grain.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
NextGrain = POINT(grain.x, grain.y + 1)

IF NextGrain = 0 THEN
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'The next pixel is unoccupied, so the grain falls down.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
grain.y = grain.y + 1
PSET (grain.x, grain.y), grain.c
ELSEIF NextGrain < grain.c THEN
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'The next grain is heavier than the current one. Stop falling.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
PSET (grain.x, grain.y), grain.c
ExitLoop% = 1
ELSE
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'The next grain is lighter, so the grain MIGHT pass through...
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IF RND < Chance! THEN
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'...and it does! Swap them and keep falling.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
PSET (grain.x, grain.y), NextGrain
grain.y = grain.y + 1
PSET (grain.x, grain.y), grain.c
ELSE
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'...but it doesn't! Make the grain stop falling.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
PSET (grain.x, grain.y), grain.c
ExitLoop% = 1
END IF
END IF
LOOP UNTIL grain.y = 199 OR ExitLoop% = 1
LOOP UNTIL INKEY$ <> ""

SYSTEM
[/syntax]

*peace*

Meg.