PROG6.MOD 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. MODULE prog6;
  2. (* This program draws patterns on the screen *)
  3. FROM Lib IMPORT RANDOM,RANDOMIZE;
  4. FROM Graph IMPORT Circle,Disc,GraphMode,TextMode,Width,Depth;
  5. IMPORT IO;
  6. TYPE
  7. FunnyType = RECORD
  8. q : INTEGER; (* value *)
  9. d : INTEGER; (* rate of change *)
  10. min : INTEGER;
  11. max : INTEGER;
  12. END;
  13. PROCEDURE init(VAR v:FunnyType);
  14. BEGIN
  15. v.q := v.min;
  16. v.d := INTEGER(RANDOM(v.max-v.min));
  17. END init;
  18. PROCEDURE bounce(VAR v:FunnyType);
  19. (* Update v.q, 'bouncing' when it goes out of range *)
  20. BEGIN
  21. WITH v DO
  22. q := q + d;
  23. IF q < min THEN
  24. q := 2*min-q;
  25. d := -d;
  26. END;
  27. IF q > max THEN
  28. q := 2*max-q;
  29. d := -d;
  30. END;
  31. END;
  32. END bounce;
  33. VAR
  34. x,y : FunnyType;
  35. color,radius : CARDINAL;
  36. BEGIN
  37. RANDOMIZE;
  38. x.max := Width-1; x.min := 0;
  39. y.max := Depth-1; y.min := 0;
  40. IO.WrStr('Press space to continue, any other key to stop');
  41. IO.WrLn;
  42. WHILE NOT IO.KeyPressed() DO
  43. END;
  44. GraphMode;
  45. WHILE IO.RdKey() = ' ' DO
  46. init(x);
  47. init(y);
  48. REPEAT
  49. color := RANDOM(16);
  50. UNTIL color MOD 5 <> 0; (* fill using 2 colors *)
  51. radius := 2 + RANDOM(5);
  52. WHILE NOT IO.KeyPressed() DO
  53. bounce(x);
  54. bounce(y);
  55. Disc(x.q, y.q, radius, color);
  56. Circle(x.q, y.q, radius, color MOD 4);
  57. END;
  58. Disc(0,0,Width+Depth, 0); (* clear screen *)
  59. END;
  60. TextMode;
  61. END prog6.
  62.