1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
import pygame
import Box2D
import _Box2D
class Bridge:
def __init__(self, game):
self.game = game
self.screen = game.screen
self.world = game.world
self.cost = 0
self.stress = 0
self.capacity = 1
self.first_train = None
self.train_off_screen = False
self.train_was_created = False
self.level_completed = False
self.sounds = {"wooo":loadSound("sounds/wooo.wav"), "death":loadSound("sounds/death.wav"), "startup":loadSound("sounds/startup.wav")}
def restart(self):
self.world.run_physics = False
self.train_off_screen = False
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(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(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,
dynamic=False)
self.world.reset_color()
def add_cost(self, value):
self.cost = self.cost + value
print "cost now", value
def joint_added(self, joint):
print "joint added!"
self.add_cost(100)
self.capacity += 500
def joint_deleted(self, joint):
print "joint deleting!"
self.add_cost(-100)
self.capacity -= 500
def box_added(self):
self.add_cost(10)
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
for joint in self.world.world.GetJointList():
if joint.GetType() == 1:
if joint.asRevoluteJoint().IsMotorEnabled() == False:
force = _Box2D.b2Joint_GetReactionForce(joint, 10).Length()
self.stress += force
if force > 500:
print "destroy joint!"
self.world.world.DestroyJoint(joint)
self.capacity -= 500
else:
vec = joint.GetAnchor1()
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)
pos = self.first_train.GetPosition()
if pos.x > 9.0:
if not self.level_completed:
self.level_completed = True
self.sounds['wooo'].play()
elif pos.y < 0.0:
if not self.train_off_screen:
self.sounds['death'].play()
print "TRAIN FELL OFF!", pos.x
self.train_off_screen = True
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()
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])
points.append(startpoint)
rect = pygame.Rect(startpoint, train)
rect.normalize()
pygame.draw.rect(self.screen, (200, 50, 100), rect, 3)
rect = self.world.add.rect(rect.center, rect.width / 2, rect.height / 2,
dynamic = True, density=10.0, restitution=0.16, friction=0.5)
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,
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,
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.jointMotor(rearaxle[0],rearaxle[1],rearwheel)
if len(frontaxle) == 2:
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]-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.distanceJoint(btrain[0], ftrain[0], backlink, frontlink)
# function for loading sounds (mostly borrowed from Pete Shinners pygame tutorial)
def loadSound(name):
# if the mixer didn't load, then create an empy class that has an empty play method
# this way the program will run if the mixer isn't present (sans sound)
class NoneSound:
def play(self): pass
def set_volume(self): pass
if not pygame.mixer:
return NoneSound()
try:
sound = pygame.mixer.Sound(name)
except:
print "error with sound: " + name
return NoneSound()
return sound
|