import pygame
import pygame.locals
import displayable
import math
pygame.init()
class Object(displayable.Displayable):
ObjectList = []
def __init__(self, xStart, yStart, screen,keyup,keydown,keyleft,keyright, sprite_file):
displayable.Displayable.__init__(self,sprite_file, screen)
self.position.x = xStart
self.position.y = yStart
self.velocity.x = 0
self.velocity.y = 0
self.acceleration.x = 0
self.acceleration.y = 0
self.jerk = .25
self.friction = .025
self.drag = .05
self.keyup = keyup
self.keydown = keydown
self.keyright = keyright
self.keyleft = keyleft
Object.ObjectList.append(self)
def updateState(self,event):
if event.type == pygame.locals.KEYDOWN:
if event.key == self.keyleft:
self.acceleration.x += -self.jerk
elif event.key == self.keyright:
self.acceleration.x += +self.jerk
elif event.key == self.keyup:
self.acceleration.y += -self.jerk
elif event.key == self.keydown:
self.acceleration.y+= self.jerk
def computeFriction(self, dx, dt, effect):
if (dx > 0.0):
dx -= effect*dt
if dx < 0.0:
dx = 0
if (dx < 0.0):
dx += effect*dt
if dx > 0.0:
dx = 0
return dx
def updatePhysics(self, dt):
self.acceleration.x = self.computeFriction(self.acceleration.x,dt, self.friction)
self.acceleration.y = self.computeFriction(self.acceleration.y,dt, self.friction)
self.velocity.x += self.acceleration.x*dt
self.velocity.y += self.acceleration.y*dt
self.velocity.x = self.computeFriction(self.velocity.x,dt, self.drag)
self.velocity.y = self.computeFriction(self.velocity.y,dt, self.drag)
self.position.x += self.velocity.x*dt
self.position.y += self.velocity.y*dt
self.Boundary()
for pObject in Object.ObjectList:
if pObject == self:
continue
fDX = self.position.x-pObject.position.x
fDY = self.position.y-pObject.position.y
fDist = math.sqrt(fDX**2 + fDY**2)
fRadius = math.sqrt(self.half_width**2+self.half_height**2)
if fDist < fRadius:
self.collide(pObject)
def collide(self, pObject):
fDeltaVx = (self.velocity.x+pObject.velocity.x)/2.0
fDeltaVy = (self.velocity.y+pObject.velocity.y)/2.0
fSelfVx = self.velocity.x - fDeltaVx
fSelfVy = self.velocity.y - fDeltaVy
fObjectVx = pObject.velocity.x - fDeltaVx
fObjectVy = pObject.velocity.y - fDeltaVy
# reflect!
fSelfVx = -fSelfVx
fSelfVy = -fSelfVy
fObjectVx = -fObjectVx
fObjectVy = -fObjectVy
self.velocity.x = fSelfVx + fDeltaVx
self.velocity.y = fSelfVy + fDeltaVy
pObject.velocity.x = fObjectVx + fDeltaVx
pObject.velocity.y = fObjectVy + fDeltaVy
def Boundary(self):
if (self.position.x-self.half_width-.5 < 0):
self.velocity.x = self.velocity.x*-1
self.position.x = 0+self.half_width
elif (self.position.x+self.half_width+.5 > self.screen.get_width()):
self.velocity.x = self.velocity.x*-1
self.position.x = self.screen.get_width()-self.half_width
if (self.position.y-self.half_height-.5 < 0):
self.velocity.y = self.velocity.y*-1
self.position.y = 0+self.half_height
elif (self.position.y+self.half_height+.5 > self.screen.get_height()):
self.velocity.y = self.velocity.y*-1
self.position.y = self.screen.get_height()-self.half_height
def display(self):
displayable.Displayable.display(self)