Skip to content

Commit

Permalink
- add in logging support
Browse files Browse the repository at this point in the history
 - add a utils file (useful stuff goes here)
 - moved ball entity into entity.py, added box entity
 - refactored entities a bit, DRY!!!

(yeah, too much stuff in one commit, tut tut tut)
  • Loading branch information
bavardage committed Jul 18, 2010
1 parent 65bd03e commit 1591337
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 33 deletions.
79 changes: 77 additions & 2 deletions entity.py
@@ -1,9 +1,37 @@
import log, logging
import Box2D as box2d
import pygame

from utils import *

entity_log = logging.getLogger('entity')

class Entity:
_body_def = None
_shape_def = None

def __init__(self, position, properties={}):
entity_log.debug("in Entity __init__ %s" % self)
self._position = position #TODO: property for actual position?
self.properties = dict(properties)

def construct(self, world):
entity_log.debug("constructing %s" % self)
self.body = world.world.CreateBody(self.body_def)
self.body.CreateShape(self.shape_def)
if not self.properties.get('static', False):
self.body.SetMassFromShapes()
entity_log.debug("done constructing %s" % self)

@property
def body_def(self):
if self._body_def is None:
self._body_def = box2d.b2BodyDef()
self._body_def.position = self._position
return self._body_def

@property
def shape_def(self):
raise NotImplementedError

def destroy(self, world):
Expand All @@ -13,8 +41,55 @@ def draw(self, camera):
raise NotImplementedError

def step(self, dt):
print "step entity"
pass

def apply_properties_to_def(self, d):
for k,v in self.properties.items():
setattr(d, k, v)
if hasattr(d, k):
setattr(d, k, v)


########################################
# Some default entities
########################################

class BallEntity(Entity):
def draw(self, camera):
pygame.draw.circle(camera.surface,
self.properties.get('color', (0,0,0,255)),
camera.vector_to_screen(self.body.position.tuple()),
camera.scalar_to_screen(self.properties['radius']),
self.properties.get('line-width', 1))

@property
def shape_def(self):
if self._shape_def is None:
self._shape_def = box2d.b2CircleDef()
self.apply_properties_to_def(self.shape_def)
return self._shape_def


class BoxEntity(Entity):
def draw(self, camera):
w,h = [self.properties[k] for k in ('width', 'height')]
vertices = [camera.vector_to_screen(v)
for v in [self._position,
vadd(self._position, (w,0)),
vadd(self._position, (w,h)),
vadd(self._position, (0,h))]]
pygame.draw.aalines(camera.surface,
self.properties.get('color', (0,0,0,255)),
True,
vertices)

# self.properties.get('line-width', 1))

@property
def shape_def(self):
if self._shape_def is None:
self._shape_def = box2d.b2PolygonDef()
self._shape_def.SetAsBox(self.properties['width'],
self.properties['height'])
return self._shape_def


14 changes: 14 additions & 0 deletions log.py
@@ -0,0 +1,14 @@
import logging

logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='/tmp/myapp.log',
filemode='w')

console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console) #add to root logger

40 changes: 9 additions & 31 deletions test1.py
@@ -1,39 +1,14 @@
import pygame
import Box2D as box2d

from world import World
from entity import Entity
from entity import *
from camera import Camera
import log

hz = 30
dt = 1.0/hz

class BallEntity(Entity):
def __init__(self, position, properties):
Entity.__init__(self, position, properties)

def construct(self, world):
print "constructing"
self.body_def = box2d.b2BodyDef()
self.body_def.position = self._position
self.body = world.world.CreateBody(self.body_def)

self.shape_def = box2d.b2CircleDef()
self.apply_properties_to_def(self.shape_def)

self.body.CreateShape(self.shape_def)
self.body.SetMassFromShapes()

def draw(self, camera):
print "draw, pos is? %s" % self.body.position
pygame.draw.circle(camera.surface,
(255,0,0,255),
camera.vector_to_screen(self.body.position.tuple()),
camera.scalar_to_screen(self.properties['radius']),
1)

def step(self, dt):
print "step ball"



def run():
Expand All @@ -48,16 +23,19 @@ def run():
c = Camera(screen, (0,0), 3.0, HEIGHT)
c.set_world(w)

b = BallEntity((100,100), {'radius': 10, 'density': 10, 'restitution': 0.1})
b = BallEntity((100,100), {'radius': 10, 'density': 10, 'restitution': 0.1,
'static': True, 'line-width': 5})
w.add_entity(b)

b2 = BallEntity((100,60), {'radius': 20, 'density': 100})
b2 = BallEntity((100,60), {'radius': 20, 'density': 100, 'restitution': 1.0})
w.add_entity(b2)
b2.body.SetLinearVelocity((0,40))

base = BoxEntity((20,20), {'width': 100, 'height': 10})#, 'static': True})
w.add_entity(base)

while running:
screen.fill((255,255,255))
print b.body.position
c.draw()
pygame.display.flip()

Expand Down
4 changes: 4 additions & 0 deletions utils.py
@@ -0,0 +1,4 @@

import operator
def vadd(*args):
return map(operator.add, *args)

0 comments on commit 1591337

Please sign in to comment.