Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bridge.py52
-rw-r--r--helpers.py78
-rw-r--r--physics.py22
-rw-r--r--tools.py39
4 files changed, 105 insertions, 86 deletions
diff --git a/bridge.py b/bridge.py
index bca7527..1c4d15d 100644
--- a/bridge.py
+++ b/bridge.py
@@ -21,15 +21,15 @@ class Bridge:
self.train_was_created = False
def create_world(self):
- self.world.set_color((100,150,50))
- rect = pygame.Rect((-400,800), (750, -250))
+ self.world.set_color((100, 150, 50))
+ rect = pygame.Rect((-400, 800), (750, -250))
rect.normalize()
- pygame.draw.rect(self.screen, (100,180,255), rect, 3)
+ 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((1600, 800), (-750, -250))
rect.normalize()
- pygame.draw.rect(self.screen, (100,180,255), rect, 3)
+ pygame.draw.rect(self.screen, (100, 180, 255), rect, 3)
self.world.add.rect(rect.center, rect.width / 2, rect.height / 2,
dynamic=False)
self.world.reset_color()
@@ -71,8 +71,8 @@ class Bridge:
vec = j.GetAnchor1()
coord = 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)
+ pygame.draw.circle(self.screen, (int(force / 2),
+ 255-int(force / 2), 0), coord, 4)
except AttributeError:
pass
pos = self.first_train.GetPosition()
@@ -86,15 +86,19 @@ 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 = (-100, 490),
+ train = (100, 50),
+ wheelrad = 20,
+ cars = 3,
+ force = False):
if not force and self.train_was_created:
return
self.sounds['startup'].play()
self.train_was_created = True
points = []
self.train_off_screen = False
- for i in range(0,cars):
- startpoint = (worldpoint[0]-(train[0]+7)*i, worldpoint[1])
+ for i in range(0, cars):
+ startpoint = (worldpoint[0] - (train[0] + 7) * i, worldpoint[1])
points.append(startpoint)
rect = pygame.Rect(startpoint, train)
rect.normalize()
@@ -104,31 +108,33 @@ class Bridge:
if i == 0:
self.first_train = rect
- self.world.set_color((0,0,0))
- rearwheel = (startpoint[0]+wheelrad,startpoint[1]+train[1]-wheelrad/2)
- pygame.draw.circle(self.screen, (0,0,0), rearwheel, wheelrad, 3)
- self.world.add.ball(rearwheel,wheelrad, dynamic=True, density=10.0,
+ self.world.set_color((0, 0, 0))
+ rearwheel = (startpoint[0] + wheelrad,startpoint[1] +
+ train[1] - wheelrad / 2)
+ pygame.draw.circle(self.screen, (0, 0, 0), rearwheel, wheelrad, 3)
+ self.world.add.ball(rearwheel, wheelrad, dynamic=True, density=10.0,
restitution=0.16, friction=0.5)
- frontwheel = (startpoint[0]+train[0]-wheelrad,startpoint[1]+train[1]-wheelrad/2)
- pygame.draw.circle(self.screen, (0,0,0), frontwheel, wheelrad, 3)
- self.world.add.ball(frontwheel,wheelrad, dynamic=True, density=10.0,
+ frontwheel = (startpoint[0] + train[0] - wheelrad, startpoint[1] +
+ train[1] - wheelrad / 2)
+ pygame.draw.circle(self.screen, (0, 0, 0), frontwheel, wheelrad, 3)
+ self.world.add.ball(frontwheel, wheelrad, dynamic=True, density=10.0,
restitution=0.16, friction=0.5)
self.world.reset_color()
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.world.add.jointMotor(rearaxle[0], rearaxle[1], rearwheel)
if len(frontaxle) == 2:
- self.world.add.jointMotor(frontaxle[0],frontaxle[1],frontwheel)
+ self.world.add.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)
+ 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)
btrain = self.world.get_bodies_at_pos(backlink)
ftrain = self.world.get_bodies_at_pos(frontlink)
- if len(ftrain) and len(btrain):
+ if len(ftrain) and len(btrain):
self.world.add.distanceJoint(btrain[0], ftrain[0], backlink, frontlink)
# function for loading sounds (mostly borrowed from Pete Shinners pygame tutorial)
diff --git a/helpers.py b/helpers.py
index b0ac16e..a50c5c6 100644
--- a/helpers.py
+++ b/helpers.py
@@ -6,37 +6,39 @@
import math
# distance calculator, pt1 and pt2 are ordred pairs
def distance(pt1, pt2):
- return math.sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] -pt2[1]) ** 2)
+ return math.sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) ** 2)
-# returns the angle between the line segment from pt1 --> pt2 and the x axis, from -pi to pi
-def getAngle(pt1,pt2):
+# returns the angle between the line segment from
+# pt1 --> pt2 and the x axis, from -pi to pi
+def getAngle(pt1, pt2):
xcomp = pt2[0] - pt1[0]
ycomp = pt1[1] - pt2[1]
- return math.atan2(ycomp,xcomp)
+ return math.atan2(ycomp, xcomp)
-# returns a list of ordered pairs that describe an equilteral triangle around the segment from pt1 --> pt2
-def constructTriangleFromLine(p1,p2):
- halfHeightVector = (0.57735*(p2[1] - p1[1]), 0.57735*(p2[0] - p1[0]))
+# returns a list of ordered pairs that describe an
+# equilteral triangle around the segment from pt1 --> pt2
+def constructTriangleFromLine(p1, p2):
+ halfHeightVector = (0.57735 * (p2[1] - p1[1]), 0.57735 * (p2[0] - p1[0]))
p3 = (p1[0] + halfHeightVector[0], p1[1] - halfHeightVector[1])
p4 = (p1[0] - halfHeightVector[0], p1[1] + halfHeightVector[1])
- return [p2,p3,p4]
+ return [p2, p3, p4]
# returns the area of a polygon
def polyArea(vertices):
n = len(vertices)
A = 0
- p=n-1
- q=0
+ p = n - 1
+ q = 0
while q<n:
- A+=vertices[p][0]*vertices[q][1] - vertices[q][0]*vertices[p][1]
- p=q
+ A += vertices[p][0] * vertices[q][1] - vertices[q][0] * vertices[p][1]
+ p = q
q += 1
- return A/2.0
+ return A / 2.0
#Some polygon magic, thanks to John W. Ratcliff on www.flipcode.com
# returns true if pt is in triangle
-def insideTriangle(pt,triangle):
+def insideTriangle(pt, triangle):
ax = triangle[2][0] - triangle[1][0]
ay = triangle[2][1] - triangle[1][1]
@@ -44,19 +46,19 @@ def insideTriangle(pt,triangle):
by = triangle[0][1] - triangle[2][1]
cx = triangle[1][0] - triangle[0][0]
cy = triangle[1][1] - triangle[0][1]
- apx= pt[0] - triangle[0][0]
- apy= pt[1] - triangle[0][1]
- bpx= pt[0] - triangle[1][0]
- bpy= pt[1] - triangle[1][1]
- cpx= pt[0] - triangle[2][0]
- cpy= pt[1] - triangle[2][1]
+ apx = pt[0] - triangle[0][0]
+ apy = pt[1] - triangle[0][1]
+ bpx = pt[0] - triangle[1][0]
+ bpy = pt[1] - triangle[1][1]
+ cpx = pt[0] - triangle[2][0]
+ cpy = pt[1] - triangle[2][1]
- aCROSSbp = ax*bpy - ay*bpx
- cCROSSap = cx*apy - cy*apx
- bCROSScp = bx*cpy - by*cpx
+ aCROSSbp = ax * bpy - ay * bpx
+ cCROSSap = cx * apy - cy * apx
+ bCROSScp = bx * cpy - by * cpx
return aCROSSbp >= 0.0 and bCROSScp >= 0.0 and cCROSSap >= 0.0
-def polySnip(vertices,u,v,w,n):
+def polySnip(vertices, u, v, w, n):
EPSILON = 0.0000000001
Ax = vertices[u][0]
@@ -68,13 +70,13 @@ def polySnip(vertices,u,v,w,n):
Cx = vertices[w][0]
Cy = vertices[w][1]
- if EPSILON > (((Bx-Ax)*(Cy-Ay)) - ((By-Ay)*(Cx-Ax))): return False
+ if EPSILON > (((Bx - Ax) * (Cy - Ay)) - ((By - Ay) * (Cx - Ax))): return False
- for p in range(0,n):
+ for p in range(0, n):
if p == u or p == v or p == w: continue
Px = vertices[p][0];
Py = vertices[p][1];
- if insideTriangle((Px,Py),((Ax,Ay),(Bx,By),(Cx,Cy))): return False;
+ if insideTriangle((Px, Py), ((Ax, Ay), (Bx, By), (Cx, Cy))): return False;
return True;
@@ -92,32 +94,32 @@ def decomposePoly(vertices):
# remove nv-2 vertices, creating 1 triangle every time
nv = n
- count = 2*nv # error detection
- m=0
- v=nv-1
- while nv>2:
+ count = 2 * nv # error detection
+ m = 0
+ v = nv - 1
+ while nv > 2:
count -= 1
- if 0>= count:
+ if 0 >= count:
return [] # Error -- probably bad polygon
# three consecutive vertices
u = v
- if nv<=u: u = 0 # previous
+ if nv <= u: u = 0 # previous
v = u+1
- if nv<=v: v = 0 # new v
+ if nv <= v: v = 0 # new v
w = v+1
- if nv<=w: w = 0 # next
+ if nv <= w: w = 0 # next
- if(polySnip(vertices,u,v,w,nv)):
+ if(polySnip(vertices, u, v, w, nv)):
# record this triangle
- result.append((vertices[u],vertices[v],vertices[w]))
+ result.append((vertices[u], vertices[v], vertices[w]))
m+=1
# remove v from remaining polygon
vertices.pop(v)
nv -= 1
# reset error detection
- count = 2*nv
+ count = 2 * nv
return result
diff --git a/physics.py b/physics.py
index 68e5ed0..25164c4 100644
--- a/physics.py
+++ b/physics.py
@@ -72,7 +72,7 @@ class PhysicsGame:
for event in pygame.event.get():
self.currentTool.handleEvents(event)
# Clear Display
- self.screen.fill((80,160,240)) #255 for white
+ self.screen.fill((80, 160, 240)) #255 for white
# Update & Draw World
self.world.update()
@@ -84,21 +84,21 @@ class PhysicsGame:
self.currentTool.draw()
#Print all the text on the screen
- text = self.font.render(_("Total Cost: %d") % self.bridge.cost, True, (0,0,0))
- textpos = text.get_rect(left=100,top=7)
+ text = self.font.render(_("Total Cost: %d") % self.bridge.cost, True, (0, 0, 0))
+ textpos = text.get_rect(left=100, top=7)
self.screen.blit(text,textpos)
- ratio = self.bridge.stress*100/self.bridge.capacity
- text = self.font.render(_("Stress: %d%%") % ratio, True, (0,0,0))
- textpos = text.get_rect(left=100,top=25)
+ ratio = self.bridge.stress * 100 / self.bridge.capacity
+ text = self.font.render(_("Stress: %d%%") % ratio, True, (0, 0, 0))
+ textpos = text.get_rect(left=100, top=25)
self.screen.blit(text,textpos)
if self.bridge.train_off_screen:
- text = self.font.render(_("Train fell off the screen, press R to try again!"), True, (0,0,0))
+ text = self.font.render(_("Train fell off the screen, press R to try again!"), True, (0, 0, 0))
elif self.bridge.level_completed:
- text = self.font.render(_("Level completed, well done!! Press T to send another train."), True, (0,0,0))
+ text = self.font.render(_("Level completed, well done!! Press T to send another train."), True, (0, 0, 0))
else:
- text = self.font.render(_("Press the Spacebar to start/pause."), True, (0,0,0))
- textpos = text.get_rect(left=100,top=43)
+ text = self.font.render(_("Press the Spacebar to start/pause."), True, (0, 0, 0))
+ textpos = text.get_rect(left=100, top=43)
self.screen.blit(text,textpos)
# Flip Display
@@ -117,7 +117,7 @@ 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))
# create an instance of the game
game = PhysicsGame(screen)
# start the main loop
diff --git a/tools.py b/tools.py
index d4fd228..80b9a69 100644
--- a/tools.py
+++ b/tools.py
@@ -102,8 +102,10 @@ class CircleTool(Tool):
thick = 3
else:
thick = 0
- pygame.draw.circle(self.game.screen, (100,180,255),self.pt1,int(self.radius),thick)
- pygame.draw.line(self.game.screen,(100,180,255),self.pt1,mouse_pos,1)
+ pygame.draw.circle(self.game.screen, (100, 180, 255), self.pt1,
+ int(self.radius), thick)
+ pygame.draw.line(self.game.screen, (100, 180, 255), self.pt1,
+ mouse_pos, 1)
def cancel(self):
self.pt1 = None
self.radius = None
@@ -148,7 +150,13 @@ class GirderTool(Tool):
elif self.red < 20:
self.colordiff *= -1
print self.theta, math.degrees(self.theta)
- self.game.world.add.rect(((self.pt1[0]+self.pt2[0])/2,(self.pt1[1]+self.pt2[1])/2), distance(self.pt1, self.pt2)/2, self.thickness/2, angle=math.degrees(self.theta), dynamic=True, density=1.0, restitution=0.16, friction=0.5)
+ self.game.world.add.rect(((self.pt1[0] + self.pt2[0]) / 2,
+ (self.pt1[1] + self.pt2[1]) / 2),
+ distance(self.pt1, self.pt2) / 2,
+ self.thickness / 2,
+ angle=math.degrees(self.theta),
+ dynamic=True, density=1.0,
+ restitution=0.16, friction=0.5)
self.game.bridge.box_added()
self.game.world.reset_color()
self.pt1 = None
@@ -157,14 +165,16 @@ class GirderTool(Tool):
# draw a box from pt1 to mouse
if self.pt1 != None:
self.pt2 = pygame.mouse.get_pos()
- self.theta = getAngle(self.pt1,self.pt2)
- if distance2(self.pt1,self.pt2,self.min):
+ self.theta = getAngle(self.pt1, self.pt2)
+ if distance2(self.pt1,self.pt2, self.min):
# too small! force length
- self.pt2 = (self.pt1[0]+self.min * math.cos(self.theta),self.pt1[1]-self.min*math.sin(self.theta))
+ self.pt2 = (self.pt1[0] + self.min * math.cos(self.theta),
+ self.pt1[1] - self.min*math.sin(self.theta))
elif not distance2(self.pt1,self.pt2,self.max):
# too big
- self.pt2 = (self.pt1[0]+(self.max * math.cos(self.theta)),self.pt1[1]-(self.max * math.sin(self.theta)))
- pygame.draw.line(self.game.screen, (255,255,255), self.pt1, self.pt2, 30)
+ self.pt2 = (self.pt1[0] + (self.max * math.cos(self.theta)),
+ self.pt1[1] - (self.max * math.sin(self.theta)))
+ pygame.draw.line(self.game.screen, (255, 255, 255), self.pt1, self.pt2, 30)
def cancel(self):
self.pt1 = None
self.rect = None
@@ -184,7 +194,8 @@ class GrabTool(Tool):
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
# grab the first object at the mouse pointer
- bodylist = self.game.world.get_bodies_at_pos(event.pos, include_static=False)
+ bodylist = self.game.world.get_bodies_at_pos(event.pos,
+ include_static=False)
if bodylist and len(bodylist) > 0:
self.game.world.add.mouseJoint(bodylist[0], event.pos)
elif event.type == MOUSEBUTTONUP:
@@ -234,7 +245,7 @@ class DestroyTool(Tool):
# draw the trail
if self.vertices:
if len(self.vertices) > 1:
- pygame.draw.lines(self.game.screen,(255,0,0),False,self.vertices,3)
+ pygame.draw.lines(self.game.screen,(255, 0, 0), False, self.vertices, 3)
def cancel(self):
self.vertices = None
@@ -257,7 +268,7 @@ class BridgeJointTool(Tool):
return
bodies = self.game.world.get_bodies_at_pos(event.pos,
- include_static=True)
+ include_static=True)
if not bodies or len(bodies) > 2:
return
@@ -266,7 +277,7 @@ class BridgeJointTool(Tool):
if not bodies[0].IsStatic():
if event.pos[1] > 550 and (event.pos[0] < 350 or event.pos[0] > 850):
jointDef.Initialize(self.game.world.world.GetGroundBody(),
- bodies[0], self.to_b2vec(event.pos))
+ bodies[0], self.to_b2vec(event.pos))
else:
return
else:
@@ -274,10 +285,10 @@ class BridgeJointTool(Tool):
elif len(bodies) == 2:
if bodies[0].IsStatic():
jointDef.Initialize(self.game.world.world.GetGroundBody(),
- bodies[1], self.to_b2vec(event.pos))
+ bodies[1], self.to_b2vec(event.pos))
elif bodies[1].IsStatic():
jointDef.Initialize(self.game.world.world.GetGroundBody(),
- bodies[0], self.to_b2vec(event.pos))
+ bodies[0], self.to_b2vec(event.pos))
else:
jointDef.Initialize(bodies[0], bodies[1], self.to_b2vec(event.pos))
joint = self.game.world.world.CreateJoint(jointDef)