a442

Simple Collisions

Sep 29th, 2013
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. import pygame
  2. from pygame.locals import *
  3.  
  4. # Originally common.py
  5. objs = []
  6.  
  7. # Originally configs.py
  8. h = 500
  9. w = 300
  10. color_depth = 24
  11. obj_width = 1
  12.  
  13. # Originally constants.py
  14. # Colors
  15. WHITE = (255, 255, 255)
  16. BLACK = (0, 0, 0)
  17. RED = (255, 0, 0)
  18. GRAY = (186, 186, 186)
  19. # Mouse
  20. MAIN_BUTTON = 1
  21. MIDDLE_BUTTON = 2
  22. SECONDARY_BUTTON = 3
  23. DRAG_AND_DROP = 2
  24.  
  25. # Originally contoller.py
  26. def mouse(event):
  27.     """Processes mouse events"""
  28.    
  29.     global clickPos
  30.     global releasePos
  31.     if event.type == MOUSEBUTTONUP:
  32.         releasePos = pygame.mouse.get_pos()
  33.         if event.button == MAIN_BUTTON:
  34.             # Notice we are not handling drag and drops and considering
  35.             # everything as a simple click.
  36.             obj = collideAll(releasePos)
  37.             if obj:
  38.                 obj.text = 'Hello World' if not obj.text else obj.text
  39.             return MAIN_BUTTON
  40.         elif event.button == MIDDLE_BUTTON:
  41.             Circle(list(releasePos), 50)
  42.            
  43.     else:
  44.         return False
  45.  
  46. clock = pygame.time.Clock()
  47.  
  48. def mainLoop():
  49.     clock.tick(10)
  50.     screen.fill(WHITE)
  51.     for obj in objs:
  52.         obj.draw()
  53.     pygame.display.update()
  54.     for event in pygame.event.get():   
  55.         mouse(event)
  56.         if event.type == pygame.QUIT:
  57.             pygame.quit()
  58.             return None
  59.     return True
  60.  
  61. # Originally objects.py
  62. class Object:
  63.     """Superclass for all objects.
  64.    
  65.     position - tuple
  66.         Pair of screen coordinates.
  67.    
  68.     dimensions - list or array
  69.         Pair of floats or integers representing width and height.  
  70.     """
  71.    
  72.    
  73.     global objs
  74.    
  75.     def __init__(self, position, dimensions, color=BLACK):
  76.        
  77.         self.dimensions = dimensions
  78.         self.position = position
  79.         objs.append(self)      
  80.    
  81.     def draw(self):
  82.         """Should be defined in the subclass."""
  83.        
  84.         raise RuntimeError('draw() not defined for '+str(self))
  85.    
  86. class Circle(Object):
  87.     """
  88.     radius - integer
  89.    
  90.     density - float or integer
  91.     """
  92.    
  93.    
  94.     def __init__(self, position, radius, density=1, color=BLACK, text=None):
  95.        
  96.         Object.__init__(self, position, [radius, radius], color)
  97.         self.color = color
  98.         self.radius = radius
  99.         self.text = text
  100.         self.draw()
  101.        
  102.     def draw(self):
  103.        
  104.         pygame.draw.circle(
  105.             screen,
  106.             self.color,
  107.             self.position,
  108.             self.radius,
  109.             obj_width
  110.         )
  111.         sendMsg(self.text, (self.position[0]-self.dimensions[0], self.position[1]-15))
  112.        
  113. # Originally graphics.py
  114. pygame.init()
  115. # The 0 below stands for "no flags"
  116. screen = pygame.display.set_mode((w, h), 0, color_depth)
  117.  
  118. def sendMsg(msg, pos, font='monospace', fontsize=15, color=BLACK):
  119.    
  120.     myfont = pygame.font.SysFont(font, fontsize)
  121.     label = myfont.render(msg, 1, color)
  122.     screen.blit(label, pos)
  123.     pygame.display.update()
  124.  
  125. def collideObject(position, obj):
  126.    
  127.     if ((position[0] >= obj.position[0]-obj.dimensions[0]) and (
  128.             position[0] <= obj.position[0]+obj.dimensions[0])) and (
  129.             (position[1] >= obj.position[1]-obj.dimensions[1]) and (
  130.             position[1] <= obj.position[1]+obj.dimensions[1])):
  131.         return obj
  132.     else:
  133.         return None
  134.  
  135. def collideAll(position, except_=None):
  136.    
  137.     for obj in objs:
  138.         if not obj == except_:
  139.             returnValue = collideObject(position, obj)
  140.             if returnValue:
  141.                 return returnValue
  142.     return None
  143.    
  144. screen.fill(WHITE)
  145.  
  146. # Originally __init__.py
  147. try:
  148.     while mainLoop():
  149.         pass
  150. except (KeyboardInterrupt, SystemExit):
  151.     pass
  152.  
  153. raise SystemExit
Advertisement
Add Comment
Please, Sign In to add comment