diff options
author | WSU CS401 <wsu.cs401@gmail.com> | 2010-12-15 10:33:45 (GMT) |
---|---|---|
committer | WSU CS401 <wsu.cs401@gmail.com> | 2010-12-15 10:33:45 (GMT) |
commit | 689f46ea2f95e129284944a313dd06d87ee0df50 (patch) | |
tree | ad7da93d32eb1fd06e870706f842065020ec576d | |
parent | 8e64a836d204bb4e6934a5bb898648627cc89ff2 (diff) |
-Change graphics to use relative positioning instead of absolute positioning. All resolutions should now work, not just XO's 1200x900
-rw-r--r-- | bridge.py | 72 | ||||
-rw-r--r-- | physics.py | 3 | ||||
-rw-r--r-- | tools.py | 4 |
3 files changed, 57 insertions, 22 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): @@ -110,7 +110,8 @@ def main(): pygame.init() pygame.display.init() x,y = pygame.display.list_modes()[0] - screen = pygame.display.set_mode((x,y-toolbarheight-tabheight)) + #screen = pygame.display.set_mode((x,y-toolbarheight-tabheight)) + screen = pygame.display.set_mode((x,y-toolbarheight)) # create an instance of the game game = PhysicsGame(screen) # start the main loop @@ -4,7 +4,7 @@ # By Alex Levenson #================================================================== import pygame -from elements import box2d +import Box2D from pygame.locals import * from helpers import * from inspect import getmro @@ -260,7 +260,7 @@ class BridgeJointTool(Tool): if not bodies or len(bodies) > 2: return - jointDef = box2d.b2RevoluteJointDef() + jointDef = Box2D.b2RevoluteJointDef() if len(bodies) == 1: if not bodies[0].IsStatic(): if event.pos[1] > 550 and (event.pos[0] < 350 or event.pos[0] > 850): |