Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #!/bin/env python
  2. #
  3. # vines borrowed from xscreensaver
  4. #
  5. """
  6. /*-
  7. * Copyright (c) 1997 by Tracy Camp campt@hurrah.com
  8. *
  9. * Permission to use, copy, modify, and distribute this software and its
  10. * documentation for any purpose and without fee is hereby granted,
  11. * provided that the above copyright notice appear in all copies and that
  12. * both that copyright notice and this permission notice appear in
  13. * supporting documentation.
  14. *
  15. * This file is provided AS IS with no warranties of any kind.  The author
  16. * shall have no liability with respect to the infringement of copyrights,
  17. * trade secrets or any patents by this file or any part thereof.  In no
  18. * event will the author be liable for any lost revenue or profits or
  19. * other special, indirect and consequential damages.
  20. *
  21. * If you make a modification I would of course appreciate a copy.
  22. *
  23. * Revision History:
  24. * 01-Nov-2000: Allocation checks
  25. * 11-Jul-1997: David Hansen <dhansen@metapath.com>
  26. *              Changed names to vines and modified draw loop
  27. *              to honor batchcount so vines can be grown or plotted.
  28. * 10-May-1997: Compatible with xscreensaver
  29. * 21-Mar-1997: David Hansen <dhansen@metapath.com>
  30. *              Updated mode to draw complete patterns on every
  31. *              iteration instead of growing the vine.  Also made
  32. *              adjustments to randomization and changed variable
  33. *              names to make logic easier to follow.
  34. */
  35.  
  36. /*-
  37. * This was modifed from a 'screen saver' that a friend and I
  38. * wrote on our TI-8x calculators in high school physics one day
  39. * Basically another geometric pattern generator, this ones claim
  40. * to fame is a pseudo-fractal looking vine like pattern that creates
  41. * nifty whorls and loops.
  42. */
  43. """
  44.  
  45. import sys
  46. import math
  47. import random
  48. import pygame
  49. if "tinypy" not in sys.version:         # not tinypy
  50.     import pygame.locals
  51.  
  52. SCR_WIDTH   = 800
  53. SCR_HEIGHT  = 600
  54.  
  55. class VineStruct(object):
  56.     a   = 0
  57.     x1  = 0
  58.     y1  = 0
  59.     x2  = 0
  60.     y2  = 0
  61.     i   = 0
  62.     length      = 0
  63.     iterations  = 0
  64.     constant    = 0
  65.     ang         = 0
  66.     centerx     = 0
  67.     centery     = 0
  68.  
  69. class Vines(object):
  70.     def __init__(self):
  71.         self.fp             = VineStruct()
  72.         self.fp.i           = 0
  73.         self.fp.length      = 0
  74.         self.fp.iterations  = 30 + random.randint(0, 100)
  75.        
  76.         pygame.init()
  77.         self.screen         = pygame.display.set_mode((SCR_WIDTH, SCR_HEIGHT))
  78.        
  79.     def __drawLine__(self, x1, y1, x2, y2, color):
  80.        
  81.         # validate the bounds
  82.         if x1 < 0: x1 = 0
  83.         if x1 > SCR_WIDTH: x1 = SCR_WIDTH
  84.         if x2 < 0: x2 = 0
  85.         if x2 > SCR_WIDTH: x2 = SCR_WIDTH
  86.         if y1 < 0: y1 = 0
  87.         if y1 > SCR_HEIGHT: y1 = SCR_HEIGHT
  88.         if y2 < 0: y2 = 0
  89.         if y2 > SCR_HEIGHT: y2 = SCR_HEIGHT
  90.        
  91.         if x1 <= x2:
  92.             sx, sy = x1, y1
  93.             dx, dy = x2, y2
  94.         else:
  95.             sx, sy = x2, y2
  96.             dx, dy = x1, y1
  97.        
  98.         if (abs(x1 - x2) < 1e-4):
  99.             x = sx
  100.             if sy > dy:
  101.                 sy, dy = dy, sy
  102.             y = sy
  103.             while (y < dy):
  104.                 self.screen.set_at((x, y), color)
  105.                 y += 1
  106.         else:
  107.             k = (dy - sy) / (dx - sx)
  108.             x = sx
  109.             while (x < dx):
  110.                 y = sy + k * (x - sx)
  111.                 self.screen.set_at((x, y), color)
  112.                 x += 1
  113.        
  114.         pygame.display.flip()
  115.        
  116.     def draw(self):
  117.         red     = random.randint(0, 255)
  118.         green   = random.randint(0, 255)
  119.         blue    = random.randint(0, 255)
  120.         if (self.fp.i >= self.fp.length):
  121.             self.fp.iterations -= 1
  122.             if (self.fp.iterations == 0):
  123.                 self.__init__(self)
  124.             self.fp.centerx = random.randint(0, SCR_WIDTH);
  125.             self.fp.centery = random.randint(0, SCR_HEIGHT);
  126.            
  127.             self.fp.ang     = 60 + random.randint(0, 720);
  128.             self.fp.length  = 100 + random.randint(0, 3000);
  129.             self.fp.constant= self.fp.length * (10 + random.randint(0, 10))
  130.            
  131.             self.fp.i       = 0;
  132.             self.fp.a       = 0;
  133.             self.fp.x1      = 0;
  134.             self.fp.y1      = 0;
  135.             self.fp.x2      = 1;
  136.             self.fp.y2      = 0;
  137.        
  138.         count = self.fp.i + random.randint(10, 100)
  139.         if (count > self.fp.length):
  140.             count = self.fp.length
  141.        
  142.         while (self.fp.i < count):
  143.             x1 = self.fp.centerx + (self.fp.x1 / self.fp.constant)
  144.             y1 = self.fp.centery - (self.fp.y1 / self.fp.constant)
  145.             x2 = self.fp.centerx + (self.fp.x2 / self.fp.constant)
  146.             y2 = self.fp.centery - (self.fp.y2 / self.fp.constant)
  147.            
  148.             color = (red, green, blue)
  149.             self.__drawLine__(x1, y1, x2, y2, color)
  150.            
  151.             self.fp.a   += (self.fp.ang * self.fp.i)
  152.             self.fp.x1  = self.fp.x2
  153.             self.fp.y1  = self.fp.y2
  154.            
  155.             self.fp.x2  += int((self.fp.i * (math.cos(self.fp.a) * 360.0)) / (2.0 * math.pi))
  156.             self.fp.y2  += int((self.fp.i * (math.sin(self.fp.a) * 360.0)) / (2.0 * math.pi))
  157.             self.fp.i   += 1
  158.  
  159. def main():
  160.     myVine = Vines()
  161.     _quit = False
  162.     while not _quit:
  163.         for e in pygame.event.get():
  164.             if e.type in (pygame.locals.QUIT,pygame.locals.KEYDOWN):
  165.                 _quit = True
  166.         myVine.draw()
  167.  
  168. if __name__ == '__main__':
  169.     main()
  170.     print("#OK")