comparison demos/shooter/scripts/scene.py @ 450:ba6817013343

Added score keeping ability. Some misc cleanup. Fixed the bounding box. Enemies are now removed from the scene when they are destroyed.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Thu, 01 Apr 2010 19:26:56 +0000
parents 1cf56403347a
children f463ab431cc0
comparison
equal deleted inserted replaced
449:1cf56403347a 450:ba6817013343
92 for i in range(rangeL, rangeR): 92 for i in range(rangeL, rangeR):
93 objects.extend(self._nodes[i].spaceobjects) 93 objects.extend(self._nodes[i].spaceobjects)
94 94
95 return objects 95 return objects
96 96
97 def checkCollision(self, instance1, instance2): 97 def removeObjectFromScene(self, obj):
98 pos1 = instnace1.getLocation().getExactLayerCoordinates() 98 for node in self._nodes:
99 pos2 = instnace2.getLocation().getExactLayerCoordinates() 99 if obj in node.spaceobjects:
100 100 node.spaceobjects.remove(obj)
101 101 return
102 102
103 def attachCamera(self, cam): 103 def attachCamera(self, cam):
104 self._camera = cam 104 self._camera = cam
105 self._camera.setLocation(self._player.location) 105 self._camera.setLocation(self._player.location)
106 106
121 topleft = self._camera.toMapCoordinates(fife.ScreenPoint(0,0)) 121 topleft = self._camera.toMapCoordinates(fife.ScreenPoint(0,0))
122 bottomright = self._camera.toMapCoordinates(fife.ScreenPoint(1024,768)) 122 bottomright = self._camera.toMapCoordinates(fife.ScreenPoint(1024,768))
123 123
124 #which scene nodes to use to update objects 124 #which scene nodes to use to update objects
125 leftnode = int(topleft.x) 125 leftnode = int(topleft.x)
126 rightnode = int(bottomright.x) + 2 126 rightnode = int(bottomright.x) + 1
127 127
128 #update other objects on the screen 128 #get a list of objects on the screen
129 if leftnode < 0: 129 if leftnode < 0:
130 leftnode = 0 130 leftnode = 0
131 if rightnode > self._maxnodes: 131 if rightnode > self._maxnodes:
132 rightnode = self._maxnodes 132 rightnode = self._maxnodes
133 screenlist = self.getObjectsInRange(leftnode, rightnode) 133 screenlist = self.getObjectsInRange(leftnode, rightnode)
134 134
135 for cl in screenlist: 135 #update objects on the screen
136 cl.update(timedelta) 136 for obj in screenlist:
137 obj.update(timedelta)
137 138
138 #update the player 139 #update the player
139 self._player.update(timedelta, keystate, self._camera) 140 self._player.update(timedelta, keystate, self._camera)
140 141
141 #update the list of projectiles 142 #update the list of projectiles
142 todelete = list() 143 projtodelete = list()
143 for p in self._projectiles: 144 for p in self._projectiles:
144 p.update(timedelta) 145 p.update(timedelta)
146 #check to see if the projectile hit any object on the screen
145 for o in screenlist: 147 for o in screenlist:
146 if p.boundingbox.intersects(o.boundingbox): 148 if p.boundingbox.intersects(o.boundingbox):
149 self._player.applyScore(100)
147 p.destroy() 150 p.destroy()
148 o.destroy() 151 o.destroy()
152 self.removeObjectFromScene(o)
153
154 #build a list of projectiles to remove (ttl expired)
149 if not p.running: 155 if not p.running:
150 todelete.append(p) 156 projtodelete.append(p)
151 for p in todelete: 157
158 #remove any non running projectiles
159 for p in projtodelete:
152 self._projectiles.remove(p) 160 self._projectiles.remove(p)
153 161
154 #fire the currently selected gun 162 #fire the currently selected gun
155 if keystate['SPACE']: 163 if keystate['SPACE']:
156 prjct = self._player.fire(time) 164 prjct = self._player.fire(time)