Snowfall

Type
Graphics
Category
Falling-snow simulation
Author
Fred Key
Published in
Antic, April 1988 (Vol. 6, No. 12), Tech Tips; first published in Page 6 magazine (England), January/February 1986
Year
1988
Language
Atari BASIC
Requirements
Any Atari 8-bit, 16K RAM, Atari BASIC
A chunky GRAPHICS 5 winter scene: a huge white snowdrift piled over the ground and against a green T-shaped post and a red house with a stepped roof, with loose flakes in the dark blue sky

About the program

Antic's April 1988 Tech Tips column borrowed a small seasonal demo from England's Page 6 magazine, where Fred Key's Snowfall had appeared in the January/February 1986 issue. Antic described it as a demonstration of what a crosswind does to falling snow. A simple winter scene, with flat ground, a T-shaped post and a house with a sloping roof, is drawn in chunky full-screen GRAPHICS 5. Then fifteen snowflakes start blowing across it. They don't just fall through things. They land, slide off edges, pile up against obstacles and gradually build drifts. The REM header calls it "SNOWFALL 2".

Typing it in

It's 60 lines of plain text with no special characters. The # signs in lines 1–5 are only a decorative REM box. Every TRAP line matters, because the program deliberately uses errors (see below), so don't leave any out.

How to use it

RUN it and watch the snow blow in diagonally from the left. Leave it long enough and the drift buries almost the whole scene. Two keys change the weather:

  • T: the wind drops. The flakes still in the air finish their journey, and then the snow falls straight down, piling up on the roof, the post and the ground.
  • S: the wind blows again.

Press BREAK to stop.

How it works

  • Fifteen flakes in an array. FL(I,0) and FL(I,1) hold each flake's X and Y. Each pass through the loop erases a flake, moves it and replots it with COLOR 1.
  • The screen is the physics. Before a flake moves, LOCATE reads the pixel it's heading for. If that pixel is lit, it's snow or scenery. The routine at line 300 looks at the cells diagonally below. If one side is free, the flake slides that way, and if both are blocked it stays put and becomes part of the drift. The calm-weather routine at 400 does the same for straight falls, and the routine at 500 lets flakes roll along the ground row until they hit something.
  • Using errors on purpose. A flake blown off the right or bottom edge would make LOCATE or PLOT fail with "cursor out of range". Instead of testing the boundaries, the program sets TRAP 75, TRAP 80, TRAP 190 and so on before each risky statement, so the error simply jumps to the code that starts a new flake. It's a cheap way to skip a lot of IF statements. POP is used several times to leave a subroutine early and GOTO back into the main loop without unbalancing the stack.
  • Keys without INPUT. The program PEEKs location 764 for the last key's internal code, where 45 is T and 62 is S, so the snow never pauses to wait for the keyboard. POKE 77,0 stops attract mode from changing the colours during a long snowfall.

Variations to try

  • More flakes: raise the 14s in DIM FL(14,1) and the FOR I=0 TO 14 loops (and the FLC=15 test in line 100).
  • Change the scenery in lines 1000–1040, perhaps adding a second house or a tree, and watch where the drifts form.
  • Make the wind blow from the right: in lines 77 and 80, start flakes at the right edge and subtract 1 from X instead of adding it.

Listing

1 REM ###############################
2 REM # SNOWFALL 2  - by Fred Key   #
3 REM # PAGE 6 MAGAZINE - ENGLAND   #
5 REM ###############################
10 DIM FL(14,1):GRAPHICS 5+16
15 SETCOLOR 4,8,4:SETCOLOR 0,0,14:SETCOLOR 1,12,8:SETCOLOR 2,3,8
20 GOSUB 1000
30 GOSUB 2000:FLC=0:K=1
35 COLOR 1:FOR I=0 TO 14:FL(I,1)=FL(I,1)-47:NEXT I
40 FOR I=0 TO 14:IF FL(I,1)<0 THEN FL(I,1)=FL(I,1)+1:GOTO 100
50 TRAP 80:LOCATE FL(I,0)+1,FL(I,1)+1,Z
60 IF Z=0 THEN 80
70 GOSUB 300:FL(I,1)=0:IF PEEK(764)=45 THEN FL(I,1)=-47:FLC=FLC+1:GOTO 100
75 Y=INT(RND(0)*46):X=INT(RND(0)*76):Z=INT(RND(0)*3+1):ON Z GOTO 76,77,76
76 FL(I,0)=X:FL(I,1)=0:GOTO 90
77 FL(I,0)=0:FL(I,1)=Y:GOTO 90
80 COLOR 0:PLOT FL(I,0),FL(I,1):FL(I,0)=FL(I,0)+1:FL(I,1)=FL(I,1)+1
90 TRAP 75:COLOR 1:PLOT FL(I,0),FL(I,1)
100 NEXT I:POKE 77,0:IF FLC=15 THEN 120
110 GOTO 40
120 POKE 764,255:GOSUB 2000
140 FOR I=0 TO 14
145 IF FL(I,1)=47 THEN GOSUB 500
150 TRAP 190:LOCATE FL(I,0),FL(I,1),Z:IF Z=0 THEN 190
160 IF Z<>1 THEN 180
170 COLOR 0:PLOT FL(I,0),FL(I,1)
175 GOSUB 400
180 FL(I,1)=0:FL(I,0)=INT(RND(0)*80):GOTO 200
190 FL(I,1)=FL(I,1)+1
200 NEXT I:POKE 77,0:IF PEEK(764)<>62 THEN 140
210 POKE 764,255:GOTO 30
300 TRAP 350:LOCATE FL(I,0),FL(I,1)+1,X:LOCATE FL(I,0)+2,FL(I,1)+1,Y
310 IF X<>0 AND Y<>0 THEN RETURN
320 J=1:IF X=0 THEN J=-1
330 COLOR 0:PLOT FL(I,0),FL(I,1)
340 COLOR 1:FL(I,0)=FL(I,0)+1+J:FL(I,1)=FL(I,1)+1:POP :GOTO 90
350 RETURN
400 TRAP 450:LOCATE FL(I,0)-1,FL(I,1)-1,X:LOCATE FL(I,0)+1,FL(I,1)-1,Y
410 IF X=0 AND Y=0 THEN RETURN
430 IF X=1 THEN J=-1:GOTO 440
435 IF Y=1 THEN J=1:GOTO 440
436 RETURN
440 FL(I,0)=FL(I,0)+J:FL(I,1)=FL(I,1)-10:POP :GOTO 200
450 RETURN
500 K=-K:TRAP 550
510 LOCATE FL(I,0)+K,46,X
520 IF X=0 THEN FL(I,0)=FL(I,0)+K:GOTO 510
530 IF X<>1 THEN POP :GOTO 180
540 FL(I,0)=FL(I,0)+K:FL(I,1)=37:RETURN
550 POP :GOTO 180
1000 COLOR 1:PLOT 0,47:DRAWTO 79,47
1010 COLOR 2:PLOT 10,25:DRAWTO 20,25
1020 PLOT 15,26:DRAWTO 15,47
1030 COLOR 3:J=70:FOR I=10 TO 20:PLOT J,I:DRAWTO 79,I:J=J-1.5:NEXT I
1040 FOR I=19 TO 47:PLOT 60,I:DRAWTO 79,I:NEXT I
1100 RETURN
2000 FOR I=0 TO 14
2010 FL(I,0)=INT(RND(0)*80):FL(I,1)=INT(RND(0)*47)
2020 LOCATE FL(I,0),FL(I,1),X:IF X<>0 THEN 2010
2030 NEXT I:RETURN

Download

The original program from the Antic disk, to LOAD or ENTER instead of typing it: SNOWFALL.BAS.

Credits