Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame as pg
- from pygame import Vector2 as vec
- WIDTH, HEIGHT = SIZE = (500, 500)
- SCREEN = pg.display.set_mode(SIZE)
- class envObject:
- """
- All values and calculations for objects will be done\n
- in this class. Actual displaying of objects are in\n
- ObjectDisplay.\n
- Data Fields:\n
- Position:
- -> Type : Vector2
- -> Stores current x and y location
- of the object.
- If object is a rectangle the top left corner is used.
- If object is a circle center point is used.\n
- Mass:
- -> Type : Float
- -> Stores mass of the object\n
- """
- def __init__(self, x, y, mass):
- self.position = None
- self.setPostion(x, y)
- self.mass = mass
- def setPostion(self, x, y):
- """
- setPosition is called when the position
- for the envObject are initialized
- or reinitialized
- """
- self.position = vec(x, y)
- def changePosition(self, x=0, y=0):
- """
- changePosition is called when the position
- data field needs to be altered. To subtract
- from x or y pass a negative value
- """
- self.position = vec(self.position.x + x, self.position.y + y)
- def collide(self, envObj):
- """
- collide will return true if the object called upon
- is colliding with the envObject passed in as a
- parameter. Otherwise, collide returns false
- Collision types supported:
- -> Rectangle <-> Rectangle
- -> Circle <-> Circle
- -> Rectangle <-> Circle
- """
- if isinstance(self, envRectangle):
- if isinstance(envObj, envRectangle):
- return self.rectCollide(envObj)
- def display(self):
- tmp_color = (255, 52, 12)
- if isinstance(self, envRectangle):
- pg.draw.rect(SCREEN, tmp_color, (self.position.x, self.position.y, self.width, self.height))
- elif isinstance(self, envCircle):
- pg.draw.circle(SCREEN, tmp_color, (int(self.position.x), int(self.position.y)), self.radius)
- class envRectangle(envObject):
- """
- Subclass of envObject.\n Handles all collisions
- and transformations of rectangular objects\n
- Data Fields:\n
- Position:
- -> Type : Vector2
- -> Stores current x and y location
- of the object.
- If object is a rectangle the top left corner is used.
- If object is a circle center point is used.\n
- Mass:
- -> Type : Float
- -> Stores mass of the object\n
- Width:
- -> Type : Integer
- -> Stores width of object\n
- Height:
- -> Type : Integer
- -> Stores height of object\n
- """
- def __init__(self, x, y, mass, width, height):
- envObject.__init__(self, x, y, mass)
- self.width = width
- self.height = height
- def rectCollide(self, envObj):
- """
- Returns true on collision of two
- rectangles (self, envObj)
- """
- left = self.position.x
- right = self.position.x + self.width
- top = self.position.y
- bottom = self.position.y + self.height
- o_left = envObj.position.x
- o_right = envObj.position.x + envObj.width
- o_top = envObj.position.y
- o_bottom = envObj.position.y + envObj.height
- if left <= o_right and left >= o_left or\
- right >= o_left and right <= o_right:
- if bottom >= o_top and bottom <= o_bottom or\
- top <= o_bottom and top >= o_top:
- return True
- def circleCollide(self, envObj):
- """
- Returns true on collision of
- a rectangle and a circle\n
- (self, envObj)
- """
- print("circle collision")
- def triangleCollide(self, envObj):
- """
- Returns true on collision of
- a rectangle and a triangle\n
- (self, envObj)
- """
- print("triangle collision")
- class envCircle(envObject):
- """
- Subclass of envObject.\n Handles all collisions
- and transformations of circular objects\n
- Data Fields:\n
- Position:
- -> Type : Vector2
- -> Stores current x and y location
- of the object.
- If object is a rectangle the top left corner is used.
- If object is a circle center point is used.\n
- Mass:
- -> Type : Float
- -> Stores mass of the object\n
- Radius:
- -> Type : Float
- -> Stores radius of an object
- """
- def __init__(self, x, y, mass, radius):
- envObject.__init__(self, x, y, mass)
- self.radius = radius
- self.diameter = self.radius * 2
- # rect1 = envRectangle(5, 5, 5, 5, 5)
- # rect2 = envRectangle(3, 3, 3, 3, 3)
- # circle1 = envCircle(3, 3, 3, 3)
- # rect1.collide(rect2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement