Subversion Repositories Kolibri OS

Rev

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

  1. """
  2. a simple diversing asteroids simulation
  3. """
  4.  
  5. import sys
  6. import math
  7. import random
  8. import pygame
  9. if "tinypy" not in sys.version:         # not tinypy
  10.     import pygame.locals
  11.  
  12. SCR_WIDTH   = 600
  13. SCR_HEIGHT  = 600
  14. NASTEROIDS  = 320       # number of asteroids
  15. INIT_NASTEROIDS = 80    # initial number of asteroids
  16. OFFSET      = 20        # max initial offset
  17. DIV_FACTOR  = 1.1       # diverse factor
  18.  
  19. class Center(object):
  20.     x = SCR_WIDTH / 2
  21.     y = SCR_HEIGHT / 2
  22.  
  23. class Graphics(object):
  24.     x = 0
  25.     y = 0
  26.     w = SCR_WIDTH
  27.     h = SCR_HEIGHT    
  28.     center = Center()
  29.     BACKGROUND = (0, 0, 0)  # black background
  30.    
  31.     def __init__(self):
  32.         pygame.init()
  33.         self.screen = pygame.display.set_mode((SCR_WIDTH, SCR_HEIGHT))
  34.         self.clearScreen()
  35.     def drawRect(self, sx, sy, w, h, color):
  36.         sx = int(sx)
  37.         sy = int(sy)
  38.         dx = int(sx + w)
  39.         dy = int(sy + h)
  40.         for x in range(sx, dx):
  41.             for y in range(sy, dy):
  42.                 self.screen.set_at((x, y), color)
  43.     def clearScreen(self):
  44.         for x in range(SCR_WIDTH):
  45.             for y in range(SCR_HEIGHT):
  46.                 self.screen.set_at((x, y), self.BACKGROUND)
  47.     def flipDisplay(self):
  48.         pygame.display.flip()
  49.  
  50. class Asteroid(object):
  51.     graphics = Graphics()
  52.     def __init__(self):
  53.         self.x          = 0    # x and y, set to invalid position
  54.         self.y          = 0
  55.         self.size       = 1
  56.         self.color      = [0, 0, 0]
  57.     def show(self):
  58.         self.graphics.drawRect(self.x, self.y, self.size, self.size, self.color)
  59.     def hide(self):
  60.         self.graphics.drawRect(self.x, self.y, self.size, self.size, self.graphics.BACKGROUND)
  61.     def update(self):
  62.         # update asteroids[i]'s position
  63.         if (self.x <= self.graphics.x or self.x >= self.graphics.w or
  64.             self.y <= self.graphics.y or self.y >= self.graphics.h):
  65.             self.x = self.graphics.center.x - OFFSET + OFFSET * 2 * random.random()
  66.             self.y = self.graphics.center.y - OFFSET + OFFSET * 2 * random.random()
  67.             self.color[0] = random.randint(20, 255)
  68.             self.color[1] = random.randint(20, 255)
  69.             self.color[2] = random.randint(20, 255)
  70.         else:
  71.             gx = self.graphics.center.x + (self.x - self.graphics.center.x) * DIV_FACTOR
  72.             if (math.fabs(self.x - self.graphics.center.x) < 1e-6):
  73.                 gy = self.graphics.center.y + (self.y - self.graphics.center.y) * DIV_FACTOR
  74.             else:
  75.                 k  = (gx - self.graphics.center.x) / (self.x - self.graphics.center.x)
  76.                 gy = self.graphics.center.y + (self.y - self.graphics.center.y) * k
  77.             self.x = gx
  78.             self.y = gy
  79.            
  80.         # update self's size
  81.         self.size = int(5 * ((math.fabs(self.x - self.graphics.center.x) * 2) / self.graphics.w))
  82.         if self.size <= 1:
  83.             self.size = 1
  84.        
  85. def main():
  86.     asteroids = []
  87.     for i in range(INIT_NASTEROIDS):
  88.         asteroid = Asteroid()
  89.         asteroid.update()
  90.         asteroids.append(asteroid)
  91.    
  92.     _quit = False
  93.     while not _quit:
  94.         for e in pygame.event.get():
  95.             if e.type in (pygame.locals.QUIT, pygame.locals.KEYDOWN):
  96.                 _quit = True        
  97.        
  98.         if (len(asteroids) < NASTEROIDS):
  99.             asteroid = Asteroid()
  100.             asteroid.update()
  101.             asteroids.append(asteroid)
  102.        
  103.         for i in range(len(asteroids)):
  104.             # hide asteroids[i]
  105.             asteroids[i].hide()
  106.            
  107.             # update asteroids[i]
  108.             asteroids[i].update()
  109.            
  110.             # show asteroids[i]
  111.             asteroids[i].show()
  112.        
  113.         # swap display content actually
  114.         Asteroid.graphics.flipDisplay()
  115.        
  116. if __name__ == '__main__':
  117.     main()
  118.     print("#OK")
  119.