Skip to content

Instantly share code, notes, and snippets.

@dazsim
Created July 25, 2016 08:17
Show Gist options
  • Save dazsim/e5eb8136e9cdac2cfe0bb6ff22843a83 to your computer and use it in GitHub Desktop.
Save dazsim/e5eb8136e9cdac2cfe0bb6ff22843a83 to your computer and use it in GitHub Desktop.
This is my collision box class for a simple game. >.<
class CollisionBox:
"""this class deals with collision boxes like the rocketman and
walls/floors"""
x1,y1 = 0 #This is the position of the box
x2,y2 = 0 #This is the size of the box
def CollisionBox(self,x,y):
self.x1 = 0
self.y1 = 0
self.x2 = x
self.y2 = y
def UpdatePos(self,x,y):
self.x1 = x
self.y1 = y
#This function should be called twice with self and target swapped to
#running into avoid edge cases where self is entirely big enough to engulf
#target
def Collide(self,target):
if self.x1>target.x1:
if self.x1<(target.x1+target.x2):
#handle top left corner
if self.y1>target.y1:
if self.y1<(target.y1+target.y2):
return True#top left corner is inside box
#handle bottom left corner
if (self.y1+self.y2)>target.y1:
if (self.y1+self.y2)<(target.y1+target.y2):
return True
if (self.x1+self.x2)>target.x1:
if (self.x1+self.x2)<(target.x1+target.x2):
#handle top left corner
if self.y1>target.y1:
if self.y1<(target.y1+target.y2):
return True#top left corner is inside box
#handle bottom left corner
if (self.y1+self.y2)>target.y1:
if (self.y1+self.y2)<(target.y1+target.y2):
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment