We are going to try to build the game pong, which is fundamentally a game that is built upon collisions (although if you think about almost all games are based on collisions). But first we got to figure out how to get objects to respond to keyboard commands:
This is a work sheet that then takes you through coding collisions between objects.
Now here is the basis for pong:
This next bit of code is to work with cuboid objects with spherical objects:
#determines whether a sphere and box intersect or not
#returns boolean
def collisionSphereAndBox(sphereObj, boxObj):
if((sphereObj.pos.x-sphereObj.radius<boxObj.pos.x+boxObj.length/2 and sphereObj.pos.x+sphereObj.radius>boxObj.pos.x-boxObj.length/2) and (sphereObj.pos.y-sphereObj.radius<boxObj.pos.y+boxObj.height/2 and sphereObj.pos.y+sphereObj.radius>boxObj.pos.y-boxObj.height/2)):
result=True
else:
result=False
return result