Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.97 KB | None | 0 0
  1. import pygame as pg
  2. from pygame import Vector2 as vec
  3.  
  4. WIDTH, HEIGHT = SIZE = (500, 500)
  5. SCREEN = pg.display.set_mode(SIZE)
  6.  
  7. class envObject:
  8.     """
  9.    All values and calculations for objects will be done\n
  10.    in this class. Actual displaying of objects are in\n
  11.    ObjectDisplay.\n
  12.  
  13.    Data Fields:\n
  14.        Position:
  15.            -> Type : Vector2
  16.            -> Stores current x and y location
  17.               of the object.
  18.               If object is a rectangle the top left corner is used.
  19.               If object is a circle center point is used.\n
  20.        Mass:
  21.            -> Type : Float
  22.            -> Stores mass of the object\n
  23.    """
  24.  
  25.     def __init__(self, x, y, mass):
  26.         self.position = None
  27.         self.setPostion(x, y)
  28.         self.mass = mass
  29.  
  30.     def setPostion(self, x, y):
  31.         """
  32.        setPosition is called when the position
  33.        for the envObject are initialized
  34.        or reinitialized
  35.        """
  36.         self.position = vec(x, y)
  37.  
  38.     def changePosition(self, x=0, y=0):
  39.         """
  40.        changePosition is called when the position
  41.        data field needs to be altered. To subtract
  42.        from x or y pass a negative value
  43.        """
  44.         self.position = vec(self.position.x + x, self.position.y + y)
  45.  
  46.     def collide(self, envObj):
  47.         """
  48.        collide will return true if the object called upon
  49.        is colliding with the envObject passed in as a
  50.        parameter. Otherwise, collide returns false
  51.  
  52.        Collision types supported:
  53.            -> Rectangle <-> Rectangle
  54.            -> Circle <-> Circle
  55.            -> Rectangle <-> Circle
  56.        """
  57.  
  58.         if isinstance(self, envRectangle):
  59.             if isinstance(envObj, envRectangle):
  60.                 return self.rectCollide(envObj)
  61.    
  62.     def display(self):
  63.         tmp_color = (255, 52, 12)
  64.         if isinstance(self, envRectangle):
  65.             pg.draw.rect(SCREEN, tmp_color, (self.position.x, self.position.y, self.width, self.height))
  66.         elif isinstance(self, envCircle):
  67.             pg.draw.circle(SCREEN, tmp_color, (int(self.position.x), int(self.position.y)), self.radius)
  68. class envRectangle(envObject):
  69.     """
  70.    Subclass of envObject.\n Handles all collisions
  71.    and transformations of rectangular objects\n
  72.  
  73.    Data Fields:\n
  74.        Position:
  75.            -> Type : Vector2
  76.            -> Stores current x and y location
  77.               of the object.
  78.               If object is a rectangle the top left corner is used.
  79.               If object is a circle center point is used.\n
  80.        Mass:
  81.            -> Type : Float
  82.            -> Stores mass of the object\n
  83.        Width:
  84.            -> Type : Integer
  85.            -> Stores width of object\n
  86.        Height:
  87.            -> Type : Integer
  88.            -> Stores height of object\n
  89.  
  90.    """
  91.     def __init__(self, x, y, mass, width, height):
  92.         envObject.__init__(self, x, y, mass)
  93.         self.width = width
  94.         self.height = height
  95.  
  96.     def rectCollide(self, envObj):
  97.         """
  98.        Returns true on collision of two
  99.        rectangles (self, envObj)
  100.        """
  101.         left = self.position.x
  102.         right = self.position.x + self.width
  103.         top = self.position.y
  104.         bottom = self.position.y + self.height
  105.  
  106.         o_left = envObj.position.x
  107.         o_right = envObj.position.x + envObj.width
  108.         o_top = envObj.position.y
  109.         o_bottom = envObj.position.y + envObj.height
  110.  
  111.         if left <= o_right and left >= o_left or\
  112.         right >= o_left and right <= o_right:
  113.             if bottom >= o_top and bottom <= o_bottom or\
  114.             top <= o_bottom and top >= o_top:
  115.                 return True
  116.  
  117.     def circleCollide(self, envObj):
  118.         """
  119.        Returns true on collision of
  120.        a rectangle and a circle\n
  121.        (self, envObj)
  122.        """
  123.         print("circle collision")
  124.  
  125.     def triangleCollide(self, envObj):
  126.         """
  127.        Returns true on collision of
  128.        a rectangle and a triangle\n
  129.        (self, envObj)
  130.        """
  131.         print("triangle collision")
  132.  
  133. class envCircle(envObject):
  134.     """
  135.    Subclass of envObject.\n Handles all collisions
  136.    and transformations of circular objects\n
  137.  
  138.    Data Fields:\n
  139.        Position:
  140.            -> Type : Vector2
  141.            -> Stores current x and y location
  142.               of the object.
  143.               If object is a rectangle the top left corner is used.
  144.               If object is a circle center point is used.\n
  145.        Mass:
  146.            -> Type : Float
  147.            -> Stores mass of the object\n
  148.        Radius:
  149.            -> Type : Float
  150.            -> Stores radius of an object
  151.    """
  152.     def __init__(self, x, y, mass, radius):
  153.         envObject.__init__(self, x, y, mass)
  154.         self.radius = radius
  155.         self.diameter = self.radius * 2
  156.  
  157.  
  158. # rect1 = envRectangle(5, 5, 5, 5, 5)
  159. # rect2 = envRectangle(3, 3, 3, 3, 3)
  160.  
  161. # circle1 = envCircle(3, 3, 3, 3)
  162. # rect1.collide(rect2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement