diff options
Diffstat (limited to 'bridge.py')
-rw-r--r-- | bridge.py | 72 |
1 files changed, 53 insertions, 19 deletions
@@ -1,4 +1,6 @@ import pygame +import Box2D +import _Box2D class Bridge: def __init__(self, game): @@ -20,14 +22,27 @@ class Bridge: self.level_completed = False self.train_was_created = False + # just an alias for convert_coord - for the sake of typing + def cc(self,ow,oh): + return self.convert_coord(ow,oh) + + def convert_coord(self,old_width,old_height): + width, height = self.screen.get_size() + # The percentage height/width + p_width = float(old_width) / 1200 + p_height = float(old_height) / 900 + new_width = p_width * float(width) + new_height = p_height * float(height) + return (int(new_width), int(new_height)) + def create_world(self): self.world.set_color((100,150,50)) - rect = pygame.Rect((-400,800), (750, -250)) + rect = pygame.Rect(self.cc(-400,900), self.cc(750, -250)) rect.normalize() pygame.draw.rect(self.screen, (100,180,255), rect, 3) self.world.add.rect(rect.center, rect.width / 2, rect.height / 2, dynamic=False) - rect = pygame.Rect((1600,800), (-750, -250)) + rect = pygame.Rect(self.cc(1600,900), self.cc(-750, -250)) rect.normalize() pygame.draw.rect(self.screen, (100,180,255), rect, 3) self.world.add.rect(rect.center, rect.width / 2, rect.height / 2, @@ -54,14 +69,30 @@ class Bridge: def box_deleted(self): self.add_cost(-10) + def jointMotor(self,b1,b2,p1,torque=900,speed=-10): + p1 = self.world.add.to_b2vec(p1) + jointDef = Box2D.b2RevoluteJointDef() + jointDef.Initialize(b1, b2, p1) + jointDef.maxMotorTorque = torque + jointDef.motorSpeed = speed + jointDef.enableMotor = True + self.world.add.parent.world.CreateJoint(jointDef) + + def distanceJoint(self,b1,b2,p1,p2): + # Distance Joint + p1 = self.world.add.to_b2vec(p1) + p2 = self.world.add.to_b2vec(p2) + jointDef = Box2D.b2DistanceJointDef() + jointDef.Initialize(b1, b2, p1, p2) + jointDef.collideConnected = True + self.world.add.parent.world.CreateJoint(jointDef) + def for_each_frame(self): self.stress = 0 - joint = self.world.world.GetJointList() - going = True - while going: + for joint in self.world.world.GetJointList(): if joint.GetType() == 1: if joint.asRevoluteJoint().IsMotorEnabled() == False: - force = joint.GetReactionForce().Length() + force = _Box2D.b2Joint_GetReactionForce(joint, 10).Length() self.stress += force if force > 500: @@ -70,14 +101,10 @@ class Bridge: self.capacity -= 500 else: vec = joint.GetAnchor1() - coord = int(self.world.meter_to_screen(vec.x)),int(780 - self.world.meter_to_screen(vec.y)) + coord = self.cc(int(self.world.meter_to_screen(vec.x)),int(780 - self.world.meter_to_screen(vec.y))) pygame.draw.circle(self.screen, (int(force/2),255-int(force/2),0), coord, 4) - if joint.GetNext(): - joint = joint.GetNext() - else: - going = False pos = self.first_train.GetPosition() - if pos.x > 14.0: + if pos.x > 9.0: if not self.level_completed: self.level_completed = True self.sounds['wooo'].play() @@ -87,7 +114,14 @@ class Bridge: print "TRAIN FELL OFF!", pos.x self.train_off_screen = True - def create_train(self, worldpoint = (-100,490), train = (100, 50), wheelrad = 20, cars = 3, force = False): + def create_train(self, worldpoint = (-110,490), train = (100, 50), wheelrad = 20, cars = 3, force = False): + if worldpoint == (-110,490): + worldpoint = self.cc(worldpoint[0], worldpoint[1]) + if train == (100, 50): + train = self.cc(train[0], train[1]) + if wheelrad == 20: + x, y = self.cc(wheelrad, wheelrad) + wheelrad = x if not force and self.train_was_created: return self.sounds['startup'].play() @@ -120,17 +154,17 @@ class Bridge: rearaxle = self.world.get_bodies_at_pos(rearwheel) frontaxle = self.world.get_bodies_at_pos(frontwheel) if len(rearaxle) == 2: - self.world.add.jointMotor(rearaxle[0],rearaxle[1],rearwheel) + self.jointMotor(rearaxle[0],rearaxle[1],rearwheel) if len(frontaxle) == 2: - self.world.add.jointMotor(frontaxle[0],frontaxle[1],frontwheel) + self.jointMotor(frontaxle[0],frontaxle[1],frontwheel) for i in range(1,len(points)): - backlink = (points[i][0]+train[0]-1,points[i][1]+train[1]-1) - frontlink = (points[i-1][0]+1,points[i-1][1]+train[1]-1) + backlink = (points[i][0]+train[0]-1,points[i][1]+train[1]-2) + frontlink = (points[i-1][0]+1,points[i-1][1]+train[1]-2) btrain = self.world.get_bodies_at_pos(backlink) ftrain = self.world.get_bodies_at_pos(frontlink) - if len(ftrain) and len(btrain):
- self.world.add.distanceJoint(btrain[0], ftrain[0], backlink, frontlink) + if len(ftrain) and len(btrain): + self.distanceJoint(btrain[0], ftrain[0], backlink, frontlink) # function for loading sounds (mostly borrowed from Pete Shinners pygame tutorial) def loadSound(name): |