Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.39 KB | None | 0 0
  1. (*
  2. * Platform: BP7 in dosbox, cycles=fixed 10000
  3. * Controls: <ESC> to exit, anything else to pause/resume
  4. *)
  5. program sky;
  6. uses
  7.     graph, crt;
  8.  
  9. type
  10.     Point = record
  11.         x, y: word;
  12.         color: byte;
  13.         velocity: byte;
  14.     end;
  15.  
  16. const
  17.     N = 1000;
  18.     VELOCITY_MAX = 5;
  19.  
  20.     COLOR_MAX = 7;
  21.     PALETTE: array [1..3*COLOR_MAX] of byte = (
  22.         $92, $B5, $FF,
  23.         $A2, $C0, $FF,
  24.         $D5, $E0, $FF,
  25.         $FF, $FF, $FF,
  26.         $FF, $ED, $E3,
  27.         $FF, $B5, $6C,
  28.         $FF, $DA, $B5
  29.     );
  30.  
  31. var
  32.     gd, gm: integer;
  33.     page, W, H: word;
  34.     stars: array [1..N] of Point;
  35.     ch: char;
  36.  
  37. procedure skyPalette;
  38. var
  39.     i, j: word;
  40. begin
  41.     j := 1;
  42.     for i:=1 to COLOR_MAX do begin
  43.         setPalette(i, i);
  44.         setRGBPalette(i,
  45.             PALETTE[j] shr 2,
  46.             PALETTE[j+1] shr 2,
  47.             PALETTE[j+2] shr 2
  48.         );
  49.  
  50.         inc(j,3);
  51.     end;
  52. end;
  53.  
  54. function randomColor: byte;
  55. var
  56.     x: word;
  57.     c: byte;
  58. begin
  59.     x := random(10000);
  60.  
  61.     if x >= 2355 then
  62.         c := 7
  63.     else if x >=1145 then
  64.         c := 6
  65.     else if x >= 385 then
  66.         c := 5
  67.     else if x >= 85 then
  68.         c := 4
  69.     else if x >= 25 then
  70.         c := 3
  71.     else if x >= 12 then
  72.         c := 2
  73.     else
  74.         c := 1;
  75.  
  76.     randomColor := c;
  77. end;
  78.  
  79. function randomPwr( limit: word): word;
  80. var
  81.     t, f: real;
  82. begin
  83.     t := 2*random - 1;
  84.  
  85.     f := exp(1.6*ln(abs(t)));
  86.     if t < 0 then
  87.         f := -f;
  88.  
  89.     randomPwr := round((f+1)*(limit-1)/2);
  90. end;
  91.  
  92. procedure randomPoint( var p: Point; m: word);
  93. begin
  94.     if m > 0 then
  95.         p.x := W - m + random(m)
  96.     else
  97.         p.x := random(W);
  98.  
  99.     p.y := randomPwr(H);
  100.     p.color := randomColor;
  101.     p.velocity := randomPwr(VELOCITY_MAX) + 1;
  102. end;
  103.  
  104. procedure skyInit;
  105. var
  106.     i: word;
  107. begin
  108.     randomize;
  109.     for i:=1 to N do
  110.         randomPoint(stars[i], 0);
  111. end;
  112.  
  113. procedure skyShift;
  114. var
  115.     i: word;
  116. begin
  117.     for i:=1 to N do begin
  118.         if stars[i].x < stars[i].velocity then
  119.             randomPoint(stars[i], 1)
  120.         else
  121.             dec(stars[i].x, stars[i].velocity);
  122.     end;
  123. end;
  124.  
  125. procedure skyPaint;
  126. var
  127.     i: word;
  128.  
  129. begin
  130.     for i:=1 to N do
  131.         putPixel(stars[i].x, stars[i].y, stars[i].color);
  132. end;
  133.  
  134.  
  135. begin
  136.     gd := VGA;
  137.     gm := VGAMed;
  138.     initGraph(gd, gm, 'c:\bp\bgi');
  139.  
  140.     W := getMaxX + 1;
  141.     H := getMaxY + 1;
  142.  
  143.     skyPalette;
  144.     skyInit;
  145.     skyPaint;
  146.  
  147.     repeat
  148.         repeat
  149.             skyShift;
  150.             page := page xor 1;
  151.             setActivePage(page);
  152.             clearDevice;
  153.             skyPaint;
  154.             setVisualPage(page);
  155.             delay(20);
  156.         until keypressed;
  157.  
  158.         while keypressed do
  159.             ch := readkey;
  160.  
  161.         if ch <> #27 then
  162.             ch := readkey;
  163.     until ch = #27;
  164.  
  165.     closeGraph;
  166. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement