Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/bridge.py
blob: 82a39b8d117c5017a4f25eba5423b9575e489dab (plain)
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
import pygame

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

    def create_world(self):
        self.world.set_color((100, 150, 50))
        rect = pygame.Rect((-400, 825), (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, 825), (-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 for_each_frame(self):
        self.stress = 0
        joint = self.world.world.GetJointList()
        for j in joint:
            try:
                if j.IsMotorEnabled() == False:
                    force = j.GetReactionForce(30).Length()
                    self.stress += force

                    if force > 500:
                        print "destroy joint!"
                        self.world.world.DestroyJoint(j)
                        self.capacity -= 500
                    else:
                        vec = j.GetAnchor1()
                        coord = int(self.world.meter_to_screen(vec.x)), \
                                int(self.screen.get_height() -
                                            self.world.meter_to_screen(vec.y))
                        pygame.draw.circle(self.screen,
                                    (int(force / 2), 255 - int(force / 2), 0),
                                                                     coord, 6)
            except AttributeError:
                pass
        pos = self.first_train.GetPosition()
        if pos.x > 14.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 = (-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])
            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.world.add.jointMotor(rearaxle[0], rearaxle[1], rearwheel)
            if len(frontaxle) == 2:
                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)
            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)

# 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