Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Converted tabs to spaces.

  • Loading branch information...
commit d2168f9da47fb2d1b820fc92b1fa1cfd0b5b2993 1 parent 012f5cf
Al Sweigart authored August 07, 2012

Showing 1 changed file with 499 additions and 499 deletions. Show diff stats Hide diff stats

  1. 998  square-shooter/square-shooter_makeover.py
998  square-shooter/square-shooter_makeover.py
@@ -51,489 +51,489 @@
51 51
 
52 52
 def scale_and_round(x, y):
53 53
     """Returns x and y coordinates from 0.0 to 1.0 scaled to 0 to MAP_WIDTH or MAP_HEIGHT."""
54  
-	return int(round(x * MAP_WIDTH)), int(round(y * MAP_HEIGHT))
  54
+    return int(round(x * MAP_WIDTH)), int(round(y * MAP_HEIGHT))
55 55
 
56 56
 
57 57
 class Vector2D:
58  
-	def __init__(self, x, y):
59  
-		self.x = x
60  
-		self.y = y
  58
+    def __init__(self, x, y):
  59
+        self.x = x
  60
+        self.y = y
61 61
 
62  
-	def __iadd__(self, vector):
63  
-		self.x += vector.x
64  
-		self.y += vector.y
65  
-		return self
  62
+    def __iadd__(self, vector):
  63
+        self.x += vector.x
  64
+        self.y += vector.y
  65
+        return self
66 66
 
67  
-	def __isub__(self, vector):
68  
-		self.x -= vector.x
69  
-		self.y -= vector.y
70  
-		return self
  67
+    def __isub__(self, vector):
  68
+        self.x -= vector.x
  69
+        self.y -= vector.y
  70
+        return self
71 71
 
72  
-	def copy(self, vector):
73  
-		self.x = vector.x
74  
-		self.y = vector.y
  72
+    def copy(self, vector):
  73
+        self.x = vector.x
  74
+        self.y = vector.y
75 75
 
76 76
 class Bubble2D:
77  
-	def __init__(self, radius):
78  
-		self.position = Vector2D(0, 0)
79  
-		self.radius = radius;
80  
-		self.speed = Vector2D(0, 0)
81  
-
82  
-	def update(self, delta_t):
83  
-		self.position.x += self.speed.x * delta_t
84  
-		self.position.y += self.speed.y * delta_t
85  
-
86  
-	def wrap_around(self):
87  
-		pos = self.position
88  
-		if pos.x < 0: pos.x = 1
89  
-		if pos.y < 0: pos.y = 1
90  
-		if pos.x > 1: pos.x = 0
91  
-		if pos.y > 1: pos.y = 0
92  
-
93  
-	def is_out(self):
94  
-		pos = self.position;
95  
-		return pos.x < 0 or pos.y < 0 or pos.x > 1 or pos.y > 1
96  
-
97  
-	def collides_with(self, bubble):
98  
-		a = abs(self.position.x - bubble.position.x)
99  
-		b = abs(self.position.y - bubble.position.y)
100  
-		distance = math.sqrt(a * a + b * b)
101  
-		return distance < (self.radius + bubble.radius)
  77
+    def __init__(self, radius):
  78
+        self.position = Vector2D(0, 0)
  79
+        self.radius = radius;
  80
+        self.speed = Vector2D(0, 0)
  81
+
  82
+    def update(self, delta_t):
  83
+        self.position.x += self.speed.x * delta_t
  84
+        self.position.y += self.speed.y * delta_t
  85
+
  86
+    def wrap_around(self):
  87
+        pos = self.position
  88
+        if pos.x < 0: pos.x = 1
  89
+        if pos.y < 0: pos.y = 1
  90
+        if pos.x > 1: pos.x = 0
  91
+        if pos.y > 1: pos.y = 0
  92
+
  93
+    def is_out(self):
  94
+        pos = self.position;
  95
+        return pos.x < 0 or pos.y < 0 or pos.x > 1 or pos.y > 1
  96
+
  97
+    def collides_with(self, bubble):
  98
+        a = abs(self.position.x - bubble.position.x)
  99
+        b = abs(self.position.y - bubble.position.y)
  100
+        distance = math.sqrt(a * a + b * b)
  101
+        return distance < (self.radius + bubble.radius)
102 102
 
103 103
 def random_position():
104  
-	return (random.random() - 0.5) * 3 + 0.5;
  104
+    return (random.random() - 0.5) * 3 + 0.5;
105 105
 
106 106
 def random_speed(magnitude):
107  
-	return (random.random() * magnitude * 2 - magnitude)
  107
+    return (random.random() * magnitude * 2 - magnitude)
108 108
 
109 109
 def make_bubble(kind):
110  
-	if kind == "big":
111  
-		size = 0.1
112  
-		speed = 0.1
113  
-	elif kind == "medium":
114  
-		size = 0.075
115  
-		speed = 0.15
116  
-	elif kind == "small":
117  
-		size = 0.05
118  
-		speed = 0.25
119  
-
120  
-	new_bubble = Bubble2D(size)
121  
-	new_bubble.position = Vector2D(
122  
-		random_position(),
123  
-		random_position())
124  
-	new_bubble.speed = Vector2D(
125  
-		random_speed(speed),
126  
-		random_speed(speed))
127  
-	new_bubble.kind = kind
128  
-	return new_bubble
  110
+    if kind == "big":
  111
+        size = 0.1
  112
+        speed = 0.1
  113
+    elif kind == "medium":
  114
+        size = 0.075
  115
+        speed = 0.15
  116
+    elif kind == "small":
  117
+        size = 0.05
  118
+        speed = 0.25
  119
+
  120
+    new_bubble = Bubble2D(size)
  121
+    new_bubble.position = Vector2D(
  122
+        random_position(),
  123
+        random_position())
  124
+    new_bubble.speed = Vector2D(
  125
+        random_speed(speed),
  126
+        random_speed(speed))
  127
+    new_bubble.kind = kind
  128
+    return new_bubble
129 129
 
130 130
 class GameWorld:
131  
-	bubbles = []
132  
-	explosions = []
133  
-	powerups = []
134  
-	bullet = None
135  
-	ship = None
136  
-
137  
-	accel_x = 0
138  
-	accel_y = 0
139  
-
140  
-	move_timer = 0
141  
-	death_timer = 0
142  
-	finish_timer = 0
143  
-	ship_shield_timer = 0
144  
-	bullet_shield_timer = 0
145  
-	freeze_timer = 0
146  
-
147  
-	level = 0
148  
-	score = 0
149  
-	max_level = 0
150  
-	high_score = 0
151  
-	lives = 0
152  
-
153  
-	def init_level(self, level):
154  
-		self.level = level
155  
-		if (level > self.max_level): self.max_level = level
156  
-		if self.ship == None:
157  
-			self.ship = Bubble2D(1.0 / 25)
158  
-		self.ship.position = Vector2D(0.5, 0.5)
159  
-		self.ship.speed = Vector2D(0, 0)
160  
-		self.bullet = None;
161  
-		self.move_timer = 0
162  
-		self.death_timer = 0
163  
-		self.finish_timer = 0
164  
-
165  
-		self.ship_shield_timer = 6;
166  
-		self.bullet_shield_timer = 0;
167  
-		self.freeze_timer = 0;
168  
-
169  
-		del self.bubbles[:]
170  
-		del self.explosions[:]
171  
-		del self.powerups[:]
172  
-		for i in range(level):
173  
-			self.bubbles.append(make_bubble("big"))
174  
-
175  
-	def update(self, delta_t):
176  
-		self.handle_collisions(delta_t)
177  
-
178  
-		if len(self.explosions) > 0:
179  
-			if self.explosions[0].radius > 0.5:
180  
-				self.explosions.pop(0)
181  
-		for i in self.explosions:
182  
-			i.radius += delta_t
183  
-
184  
-		if len(self.powerups) > 0:
185  
-			if self.powerups[0].age > 9:
186  
-				self.powerups.pop(0)
187  
-		for i in self.powerups:
188  
-			i.age += delta_t
189  
-		if self.ship_shield_timer > 0:
190  
-			self.ship_shield_timer -= delta_t
191  
-		if self.bullet_shield_timer > 0:
192  
-			self.bullet_shield_timer -= delta_t
193  
-		if self.freeze_timer > 0:
194  
-			self.freeze_timer -= delta_t
195  
-
196  
-		if len(self.bubbles) == 0:
197  
-			if self.finish_timer > 0:
198  
-				self.finish_timer -= delta_t;
199  
-			else:
200  
-				self.level += 1
201  
-				self.lives += 1
202  
-				self.init_level(self.level)
203  
-				return
204  
-		elif self.freeze_timer <= 0:
205  
-			for i in self.bubbles:
206  
-				i.update(delta_t)
207  
-				i.wrap_around()
208  
-
209  
-		if self.bullet != None:
210  
-			self.bullet.update(delta_t)
211  
-			if self.bullet.is_out():
212  
-				self.bullet = None
213  
-
214  
-		if self.ship == None:
215  
-			if self.death_timer > 0:
216  
-				self.death_timer -= delta_t
217  
-			elif self.lives > 0:
218  
-				self.ship = Bubble2D(1.0 / 25)
219  
-				self.ship.position = Vector2D(0.5, 0.5)
220  
-				self.ship_shield_timer = 6;
221  
-			else:
222  
-				self.level = 0 # Game over
223  
-			return
224  
-
225  
-		self.ship.speed.x += self.accel_x
226  
-		self.ship.speed.y += self.accel_y
227  
-		self.ship.speed.x *= 0.99
228  
-		self.ship.speed.y *= 0.99
229  
-		self.ship.update(delta_t)
230  
-		self.ship.wrap_around()
231  
-
232  
-	def handle_collisions(self, delta_t):
233  
-		for b in self.bubbles:
234  
-			if self.bullet != None and b.collides_with(self.bullet):
235  
-				self.bubbles.remove(b)
236  
-				if self.bullet_shield_timer <= 0:
237  
-					self.bullet = None
238  
-				else:
239  
-					# Push it along or it will just
240  
-					# destroy the newly formed bubbles.
241  
-					self.bullet.update(delta_t * 5)
242  
-				self.spawn_bubbles(b)
243  
-				self.spawn_explosion(b)
244  
-				self.mark_score(b)
245  
-				if len(self.bubbles) == 0:
246  
-					self.finish_timer = 3
247  
-				break
248  
-			elif self.ship != None:
249  
-				if not b.collides_with(self.ship):
250  
-					continue
251  
-				if self.ship_shield_timer > 0:
252  
-					continue
253  
-				self.spawn_explosion(self.ship)
254  
-				self.ship = None
255  
-				self.lives -= 1
256  
-				self.death_timer = 3;
257  
-				break
258  
-
259  
-		if self.ship == None: return
260  
-
261  
-		for p in self.powerups[:]:
262  
-			if p.collides_with(self.ship):
263  
-				self.apply_powerup(p)
264  
-				self.powerups.remove(p)
265  
-
266  
-	def spawn_bubbles(self, parent):
267  
-		if parent.kind == "small":
268  
-			if random.random() < 0.25:
269  
-				self.spawn_powerup(parent)
270  
-		else:
271  
-			if parent.kind == "big":
272  
-				new_type = "medium"
273  
-			elif parent.kind == "medium":
274  
-				new_type = "small"
275  
-			b = make_bubble(new_type)
276  
-			b.position.copy(parent.position)
277  
-			self.bubbles.append(b)
278  
-			b = make_bubble(new_type)
279  
-			b.position.copy(parent.position)
280  
-			self.bubbles.append(b)
281  
-
282  
-	def spawn_explosion(self, bubble):
283  
-		explosion = Bubble2D(0)
284  
-		explosion.position.copy(bubble.position)
285  
-		self.explosions.append(explosion)
286  
-
287  
-	def spawn_powerup(self, bubble):
288  
-		powerup = Bubble2D(0.03)
289  
-		powerup.position.copy(bubble.position)
290  
-		powerup.kind = random.choice(("shield", "bullet", "freeze"))
291  
-		powerup.age = 0
292  
-		self.powerups.append(powerup)
293  
-
294  
-	def mark_score(self, bubble):
295  
-		if bubble.kind == "small":
296  
-			self.score += 5
297  
-		elif bubble.kind == "medium":
298  
-			self.score += 2
299  
-		elif bubble.kind == "big":
300  
-			self.score += 1
301  
-
302  
-		if self.score > self.high_score:
303  
-			self.high_score = self.score
304  
-
305  
-	def apply_powerup(self, powerup):
306  
-		if powerup.kind == "shield":
307  
-			self.ship_shield_timer += 6
308  
-		elif powerup.kind == "bullet":
309  
-			self.bullet_shield_timer += 6
310  
-		elif powerup.kind == "freeze":
311  
-			self.freeze_timer += 6
312  
-		else:
313  
-			raise "Bad powerup type"
314  
-		self.score += self.level * 10
315  
-
316  
-		if self.score > self.high_score:
317  
-			self.high_score = self.score
318  
-
319  
-	def shoot_at(self, x, y):
320  
-		if self.bullet != None or self.ship == None:
321  
-			return
322  
-
323  
-		x -= self.ship.position.x;
324  
-		y -= self.ship.position.y;
325  
-
326  
-		b = Bubble2D(0.01)
327  
-		b.position.copy(self.ship.position);
328  
-		b.speed.x = x * 3
329  
-		b.speed.y = y * 3
330  
-
331  
-		# Help out the poor sods who click on their
332  
-		# own ship and get stuck with a non-moving
333  
-		# bullet. (2009-11-14)
334  
-		absx = abs(x)
335  
-		absy = abs(y)
336  
-		if absx < 0.1 and absy < 0.1:
337  
-			b.speed.x *= 30
338  
-			b.speed.y *= 30
339  
-
340  
-		self.bullet = b
341  
-
342  
-	def thrust_at(self, x, y):
343  
-		if self.ship == None:
344  
-			return
345  
-
346  
-		x -= self.ship.position.x;
347  
-		y -= self.ship.position.y;
348  
-
349  
-		self.accel_x += x * 0.03;
350  
-		self.accel_y += y * 0.03;
351  
-
352  
-	def stop_thrust(self):
353  
-		self.accel_x = 0
354  
-		self.accel_y = 0
  131
+    bubbles = []
  132
+    explosions = []
  133
+    powerups = []
  134
+    bullet = None
  135
+    ship = None
  136
+
  137
+    accel_x = 0
  138
+    accel_y = 0
  139
+
  140
+    move_timer = 0
  141
+    death_timer = 0
  142
+    finish_timer = 0
  143
+    ship_shield_timer = 0
  144
+    bullet_shield_timer = 0
  145
+    freeze_timer = 0
  146
+
  147
+    level = 0
  148
+    score = 0
  149
+    max_level = 0
  150
+    high_score = 0
  151
+    lives = 0
  152
+
  153
+    def init_level(self, level):
  154
+        self.level = level
  155
+        if (level > self.max_level): self.max_level = level
  156
+        if self.ship == None:
  157
+            self.ship = Bubble2D(1.0 / 25)
  158
+        self.ship.position = Vector2D(0.5, 0.5)
  159
+        self.ship.speed = Vector2D(0, 0)
  160
+        self.bullet = None;
  161
+        self.move_timer = 0
  162
+        self.death_timer = 0
  163
+        self.finish_timer = 0
  164
+
  165
+        self.ship_shield_timer = 6;
  166
+        self.bullet_shield_timer = 0;
  167
+        self.freeze_timer = 0;
  168
+
  169
+        del self.bubbles[:]
  170
+        del self.explosions[:]
  171
+        del self.powerups[:]
  172
+        for i in range(level):
  173
+            self.bubbles.append(make_bubble("big"))
  174
+
  175
+    def update(self, delta_t):
  176
+        self.handle_collisions(delta_t)
  177
+
  178
+        if len(self.explosions) > 0:
  179
+            if self.explosions[0].radius > 0.5:
  180
+                self.explosions.pop(0)
  181
+        for i in self.explosions:
  182
+            i.radius += delta_t
  183
+
  184
+        if len(self.powerups) > 0:
  185
+            if self.powerups[0].age > 9:
  186
+                self.powerups.pop(0)
  187
+        for i in self.powerups:
  188
+            i.age += delta_t
  189
+        if self.ship_shield_timer > 0:
  190
+            self.ship_shield_timer -= delta_t
  191
+        if self.bullet_shield_timer > 0:
  192
+            self.bullet_shield_timer -= delta_t
  193
+        if self.freeze_timer > 0:
  194
+            self.freeze_timer -= delta_t
  195
+
  196
+        if len(self.bubbles) == 0:
  197
+            if self.finish_timer > 0:
  198
+                self.finish_timer -= delta_t;
  199
+            else:
  200
+                self.level += 1
  201
+                self.lives += 1
  202
+                self.init_level(self.level)
  203
+                return
  204
+        elif self.freeze_timer <= 0:
  205
+            for i in self.bubbles:
  206
+                i.update(delta_t)
  207
+                i.wrap_around()
  208
+
  209
+        if self.bullet != None:
  210
+            self.bullet.update(delta_t)
  211
+            if self.bullet.is_out():
  212
+                self.bullet = None
  213
+
  214
+        if self.ship == None:
  215
+            if self.death_timer > 0:
  216
+                self.death_timer -= delta_t
  217
+            elif self.lives > 0:
  218
+                self.ship = Bubble2D(1.0 / 25)
  219
+                self.ship.position = Vector2D(0.5, 0.5)
  220
+                self.ship_shield_timer = 6;
  221
+            else:
  222
+                self.level = 0 # Game over
  223
+            return
  224
+
  225
+        self.ship.speed.x += self.accel_x
  226
+        self.ship.speed.y += self.accel_y
  227
+        self.ship.speed.x *= 0.99
  228
+        self.ship.speed.y *= 0.99
  229
+        self.ship.update(delta_t)
  230
+        self.ship.wrap_around()
  231
+
  232
+    def handle_collisions(self, delta_t):
  233
+        for b in self.bubbles:
  234
+            if self.bullet != None and b.collides_with(self.bullet):
  235
+                self.bubbles.remove(b)
  236
+                if self.bullet_shield_timer <= 0:
  237
+                    self.bullet = None
  238
+                else:
  239
+                    # Push it along or it will just
  240
+                    # destroy the newly formed bubbles.
  241
+                    self.bullet.update(delta_t * 5)
  242
+                self.spawn_bubbles(b)
  243
+                self.spawn_explosion(b)
  244
+                self.mark_score(b)
  245
+                if len(self.bubbles) == 0:
  246
+                    self.finish_timer = 3
  247
+                break
  248
+            elif self.ship != None:
  249
+                if not b.collides_with(self.ship):
  250
+                    continue
  251
+                if self.ship_shield_timer > 0:
  252
+                    continue
  253
+                self.spawn_explosion(self.ship)
  254
+                self.ship = None
  255
+                self.lives -= 1
  256
+                self.death_timer = 3;
  257
+                break
  258
+
  259
+        if self.ship == None: return
  260
+
  261
+        for p in self.powerups[:]:
  262
+            if p.collides_with(self.ship):
  263
+                self.apply_powerup(p)
  264
+                self.powerups.remove(p)
  265
+
  266
+    def spawn_bubbles(self, parent):
  267
+        if parent.kind == "small":
  268
+            if random.random() < 0.25:
  269
+                self.spawn_powerup(parent)
  270
+        else:
  271
+            if parent.kind == "big":
  272
+                new_type = "medium"
  273
+            elif parent.kind == "medium":
  274
+                new_type = "small"
  275
+            b = make_bubble(new_type)
  276
+            b.position.copy(parent.position)
  277
+            self.bubbles.append(b)
  278
+            b = make_bubble(new_type)
  279
+            b.position.copy(parent.position)
  280
+            self.bubbles.append(b)
  281
+
  282
+    def spawn_explosion(self, bubble):
  283
+        explosion = Bubble2D(0)
  284
+        explosion.position.copy(bubble.position)
  285
+        self.explosions.append(explosion)
  286
+
  287
+    def spawn_powerup(self, bubble):
  288
+        powerup = Bubble2D(0.03)
  289
+        powerup.position.copy(bubble.position)
  290
+        powerup.kind = random.choice(("shield", "bullet", "freeze"))
  291
+        powerup.age = 0
  292
+        self.powerups.append(powerup)
  293
+
  294
+    def mark_score(self, bubble):
  295
+        if bubble.kind == "small":
  296
+            self.score += 5
  297
+        elif bubble.kind == "medium":
  298
+            self.score += 2
  299
+        elif bubble.kind == "big":
  300
+            self.score += 1
  301
+
  302
+        if self.score > self.high_score:
  303
+            self.high_score = self.score
  304
+
  305
+    def apply_powerup(self, powerup):
  306
+        if powerup.kind == "shield":
  307
+            self.ship_shield_timer += 6
  308
+        elif powerup.kind == "bullet":
  309
+            self.bullet_shield_timer += 6
  310
+        elif powerup.kind == "freeze":
  311
+            self.freeze_timer += 6
  312
+        else:
  313
+            raise "Bad powerup type"
  314
+        self.score += self.level * 10
  315
+
  316
+        if self.score > self.high_score:
  317
+            self.high_score = self.score
  318
+
  319
+    def shoot_at(self, x, y):
  320
+        if self.bullet != None or self.ship == None:
  321
+            return
  322
+
  323
+        x -= self.ship.position.x;
  324
+        y -= self.ship.position.y;
  325
+
  326
+        b = Bubble2D(0.01)
  327
+        b.position.copy(self.ship.position);
  328
+        b.speed.x = x * 3
  329
+        b.speed.y = y * 3
  330
+
  331
+        # Help out the poor sods who click on their
  332
+        # own ship and get stuck with a non-moving
  333
+        # bullet. (2009-11-14)
  334
+        absx = abs(x)
  335
+        absy = abs(y)
  336
+        if absx < 0.1 and absy < 0.1:
  337
+            b.speed.x *= 30
  338
+            b.speed.y *= 30
  339
+
  340
+        self.bullet = b
  341
+
  342
+    def thrust_at(self, x, y):
  343
+        if self.ship == None:
  344
+            return
  345
+
  346
+        x -= self.ship.position.x;
  347
+        y -= self.ship.position.y;
  348
+
  349
+        self.accel_x += x * 0.03;
  350
+        self.accel_y += y * 0.03;
  351
+
  352
+    def stop_thrust(self):
  353
+        self.accel_x = 0
  354
+        self.accel_y = 0
355 355
 
356 356
 class GameScreen:
357  
-	def __init__(self, model, screen):
358  
-		self.model = model
359  
-
360  
-		self.screen = screen
361  
-		self.width, self.height = screen.get_size()
362  
-		self.bglayer = pygame.Surface(screen.get_size())
363  
-
364  
-		font_name = pygame.font.get_default_font()
365  
-		self.hud_font =	pygame.font.SysFont(
366  
-			font_name, self.height / 10)
367  
-		self.msg_font = pygame.font.SysFont(
368  
-			font_name, self.height / 20)
369  
-
370  
-		self.bubble_colors = ["#ffffcc", "#ffccff", "#ccffff",
371  
-			"#ffdddd", "#ddffdd", "#ddddff"]
372  
-		for i in range(6):
373  
-			self.bubble_colors[i] = pygame.Color(
374  
-				self.bubble_colors[i])
375  
-
376  
-		self.game_paused = False
377  
-
378  
-		self.render_backround()
379  
-
380  
-	def render(self):
381  
-		m = self.model
382  
-
383  
-		self.screen.blit(self.bglayer, (0, 0))
384  
-
385  
-		if m.level == 0:
386  
-			self.render_title_screen()
387  
-			# Hide the [P]ause text.
388  
-			self.screen.fill(GREEN, (500, 424, 140, 24))
389  
-		else:
390  
-			self.render_game_world()
391  
-			# Hide the [P]lay text.
392  
-			self.screen.fill(GREEN, (500, 400, 140, 24))
393  
-			if self.game_paused: self.render_pause_text()
394  
-
395  
-		text = self.hud_font.render(str(m.level), False, BLACK)
396  
-		self.screen.blit(text, (500, 48))
397  
-		text = self.hud_font.render(str(m.lives), False, BLACK)
398  
-		self.screen.blit(text, (500, 48 * 3))
399  
-		text = self.hud_font.render(str(m.score), False, BLACK)
400  
-		self.screen.blit(text, (500, 48 * 5))
401  
-
402  
-		#fps_text = self.msg_font.render(str(self.fps), False, GREEN)
403  
-		#self.screen.blit(fps_text, (0, 0))
404  
-
405  
-		pygame.display.flip()
406  
-
407  
-	def render_backround(self):
408  
-		self.bglayer.fill(BLACK)
409  
-		self.bglayer.fill(GREEN, (MAP_WIDTH, 0, 160, MAP_HEIGHT))
410  
-
411  
-		msg = ["Level", "Lives", "Score"]
412  
-		for i in range(3):
413  
-			text = self.hud_font.render(msg[i], False, BLACK)
414  
-			self.bglayer.blit(text, (500, i * 96))
415  
-
416  
-		msg = ["[Q]uit", "[P]ause", "[P]lay"]
417  
-		for i in range(3):
418  
-			text = self.msg_font.render(msg[i], False, WHITE)
419  
-			self.bglayer.blit(text, (500, 448 - i * 24))
420  
-
421  
-	def render_title_screen(self):
422  
-		text = self.hud_font.render("SQUARE", False, GREEN)
423  
-		self.screen.blit(text, text.get_rect(midbottom = (MAP_HALF_WIDTH, MAP_HALF_HEIGHT)))
424  
-		text = self.hud_font.render("SHOOTER", False, GREEN)
425  
-		self.screen.blit(text, text.get_rect(midtop = (MAP_HALF_WIDTH, MAP_HALF_HEIGHT)))
426  
-
427  
-		text = self.msg_font.render("FCP", False, GREEN)
428  
-		self.screen.blit(text, text.get_rect(midbottom = (MAP_HALF_WIDTH, MAP_QUARTER_HEIGHT)))
429  
-		text = self.msg_font.render("presents", False, GREEN)
430  
-		self.screen.blit(text, text.get_rect(midtop = (MAP_HALF_WIDTH, MAP_QUARTER_HEIGHT)))
431  
-
432  
-		high_score = "High score: " + str(self.model.high_score)
433  
-		text = self.msg_font.render(high_score, False, GREEN)
434  
-		self.screen.blit(text, text.get_rect(midbottom = (MAP_HALF_WIDTH, MAP_THREE_QUARTER_HEIGHT)))
435  
-
436  
-		max_level = "Max level: " + str(self.model.max_level)
437  
-		text = self.msg_font.render(max_level, False, GREEN)
438  
-		self.screen.blit(text, text.get_rect(midtop = (MAP_HALF_WIDTH, MAP_THREE_QUARTER_HEIGHT)))
439  
-
440  
-	def render_game_world(self):
441  
-		m = self.model
442  
-
443  
-		self.screen.set_clip((0, 0, MAP_WIDTH, MAP_HEIGHT))
444  
-
445  
-		if m.ship != None: self.render_ship()
446  
-		if m.bullet != None: self.render_bullet()
447  
-
448  
-		for bubble in m.bubbles:
449  
-			if not hasattr(bubble, "color"):
450  
-				bubble.color = random.choice(
451  
-					self.bubble_colors)
452  
-			pos = bubble.position
453  
-			pygame.draw.circle(
454  
-				self.screen,
455  
-				bubble.color,
456  
-				scale_and_round(pos.x, pos.y),
457  
-				int(round(bubble.radius * MAP_SIZE)),
458  
-				1)
459  
-		for explosion in m.explosions:
460  
-			pos = explosion.position
461  
-			pygame.draw.circle(
462  
-				self.screen,
463  
-				RED,
464  
-				scale_and_round(pos.x, pos.y),
465  
-				int(round(explosion.radius * MAP_SIZE)),
466  
-				1)
467  
-		for i in m.powerups:
468  
-			self.render_powerup(i)
469  
-
470  
-		self.screen.set_clip(None)
471  
-
472  
-	def render_ship(self):
473  
-		ship = self.model.ship
474  
-		pos = ship.position
475  
-		bbox = pygame.draw.circle(
476  
-			self.screen,
477  
-			SILVER,
478  
-			scale_and_round(pos.x, pos.y),
479  
-			int(round(ship.radius * MAP_SIZE)))
480  
-		pygame.draw.circle(
481  
-			self.screen,
482  
-			BLACK,
483  
-			scale_and_round(pos.x, pos.y),
484  
-			int(round(ship.radius * 0.5 * MAP_SIZE)),
485  
-			1)
486  
-		if self.model.ship_shield_timer > 0:
487  
-			pygame.draw.rect(self.screen, SILVER, bbox, 1)
488  
-
489  
-	def render_bullet(self):
490  
-		bullet = self.model.bullet
491  
-		pos = bullet.position
492  
-		bbox = pygame.draw.circle(
493  
-			self.screen,
494  
-			RED,
495  
-			scale_and_round(pos.x, pos.y),
496  
-			int(round(bullet.radius * MAP_SIZE)))
497  
-		if self.model.bullet_shield_timer > 0:
498  
-			pygame.draw.rect(self.screen, RED, bbox, 1)
499  
-
500  
-	def render_powerup(self, powerup):
501  
-		pos = powerup.position
502  
-		radius = powerup.radius * MAP_SIZE
503  
-		if powerup.kind	== "shield":
504  
-			bbox = pygame.draw.circle(
505  
-				self.screen,
506  
-				WHITE,
507  
-				scale_and_round(pos.x, pos.y),
508  
-				int(round(radius)),
509  
-				1)
510  
-			pygame.draw.rect(self.screen, WHITE, bbox, 1)
511  
-		elif powerup.kind == "bullet":
512  
-			pygame.draw.circle(
513  
-				self.screen,
514  
-				WHITE,
515  
-				scale_and_round(pos.x, pos.y),
516  
-				int(round(radius * 0.3)),
517  
-				1)
518  
-			bbox = pygame.Rect(0, 0, radius * 2, radius * 2)
519  
-			bbox.center = (pos.x * MAP_WIDTH, pos.y * MAP_HEIGHT)
520  
-			pygame.draw.rect(self.screen, WHITE, bbox, 1)
521  
-		elif powerup.kind == "freeze":
522  
-			bbox = pygame.Rect(0, 0, radius * 2, radius * 2)
523  
-			bbox.center = (pos.x * MAP_WIDTH, pos.y * MAP_HEIGHT)
524  
-			pygame.draw.rect(self.screen, WHITE, bbox, 1)
525  
-			bbox.inflate_ip(-radius, -radius)
526  
-			pygame.draw.rect(self.screen, WHITE, bbox, 1)
527  
-			bbox.inflate_ip(-radius * 0.5, -radius * 0.5)
528  
-			pygame.draw.rect(self.screen, WHITE, bbox, 1)
529  
-		else:
530  
-			raise "Bad power-up kind: " + powerup.kind
531  
-
532  
-	def render_pause_text(self):
533  
-		pause_text = self.msg_font.render("Game paused", False, GREEN)
534  
-		self.screen.blit(
535  
-			pause_text,
536  
-			pause_text.get_rect(midbottom = (MAP_HALF_WIDTH, MAP_HEIGHT)))
  357
+    def __init__(self, model, screen):
  358
+        self.model = model
  359
+
  360
+        self.screen = screen
  361
+        self.width, self.height = screen.get_size()
  362
+        self.bglayer = pygame.Surface(screen.get_size())
  363
+
  364
+        font_name = pygame.font.get_default_font()
  365
+        self.hud_font =    pygame.font.SysFont(
  366
+            font_name, self.height / 10)
  367
+        self.msg_font = pygame.font.SysFont(
  368
+            font_name, self.height / 20)
  369
+
  370
+        self.bubble_colors = ["#ffffcc", "#ffccff", "#ccffff",
  371
+            "#ffdddd", "#ddffdd", "#ddddff"]
  372
+        for i in range(6):
  373
+            self.bubble_colors[i] = pygame.Color(
  374
+                self.bubble_colors[i])
  375
+
  376
+        self.game_paused = False
  377
+
  378
+        self.render_backround()
  379
+
  380
+    def render(self):
  381
+        m = self.model
  382
+
  383
+        self.screen.blit(self.bglayer, (0, 0))
  384
+
  385
+        if m.level == 0:
  386
+            self.render_title_screen()
  387
+            # Hide the [P]ause text.
  388
+            self.screen.fill(GREEN, (500, 424, 140, 24))
  389
+        else:
  390
+            self.render_game_world()
  391
+            # Hide the [P]lay text.
  392
+            self.screen.fill(GREEN, (500, 400, 140, 24))
  393
+            if self.game_paused: self.render_pause_text()
  394
+
  395
+        text = self.hud_font.render(str(m.level), False, BLACK)
  396
+        self.screen.blit(text, (500, 48))
  397
+        text = self.hud_font.render(str(m.lives), False, BLACK)
  398
+        self.screen.blit(text, (500, 48 * 3))
  399
+        text = self.hud_font.render(str(m.score), False, BLACK)
  400
+        self.screen.blit(text, (500, 48 * 5))
  401
+
  402
+        #fps_text = self.msg_font.render(str(self.fps), False, GREEN)
  403
+        #self.screen.blit(fps_text, (0, 0))
  404
+
  405
+        pygame.display.flip()
  406
+
  407
+    def render_backround(self):
  408
+        self.bglayer.fill(BLACK)
  409
+        self.bglayer.fill(GREEN, (MAP_WIDTH, 0, 160, MAP_HEIGHT))
  410
+
  411
+        msg = ["Level", "Lives", "Score"]
  412
+        for i in range(3):
  413
+            text = self.hud_font.render(msg[i], False, BLACK)
  414
+            self.bglayer.blit(text, (500, i * 96))
  415
+
  416
+        msg = ["[Q]uit", "[P]ause", "[P]lay"]
  417
+        for i in range(3):
  418
+            text = self.msg_font.render(msg[i], False, WHITE)
  419
+            self.bglayer.blit(text, (500, 448 - i * 24))
  420
+
  421
+    def render_title_screen(self):
  422
+        text = self.hud_font.render("SQUARE", False, GREEN)
  423
+        self.screen.blit(text, text.get_rect(midbottom = (MAP_HALF_WIDTH, MAP_HALF_HEIGHT)))
  424
+        text = self.hud_font.render("SHOOTER", False, GREEN)
  425
+        self.screen.blit(text, text.get_rect(midtop = (MAP_HALF_WIDTH, MAP_HALF_HEIGHT)))
  426
+
  427
+        text = self.msg_font.render("FCP", False, GREEN)
  428
+        self.screen.blit(text, text.get_rect(midbottom = (MAP_HALF_WIDTH, MAP_QUARTER_HEIGHT)))
  429
+        text = self.msg_font.render("presents", False, GREEN)
  430
+        self.screen.blit(text, text.get_rect(midtop = (MAP_HALF_WIDTH, MAP_QUARTER_HEIGHT)))
  431
+
  432
+        high_score = "High score: " + str(self.model.high_score)
  433
+        text = self.msg_font.render(high_score, False, GREEN)
  434
+        self.screen.blit(text, text.get_rect(midbottom = (MAP_HALF_WIDTH, MAP_THREE_QUARTER_HEIGHT)))
  435
+
  436
+        max_level = "Max level: " + str(self.model.max_level)
  437
+        text = self.msg_font.render(max_level, False, GREEN)
  438
+        self.screen.blit(text, text.get_rect(midtop = (MAP_HALF_WIDTH, MAP_THREE_QUARTER_HEIGHT)))
  439
+
  440
+    def render_game_world(self):
  441
+        m = self.model
  442
+
  443
+        self.screen.set_clip((0, 0, MAP_WIDTH, MAP_HEIGHT))
  444
+
  445
+        if m.ship != None: self.render_ship()
  446
+        if m.bullet != None: self.render_bullet()
  447
+
  448
+        for bubble in m.bubbles:
  449
+            if not hasattr(bubble, "color"):
  450
+                bubble.color = random.choice(
  451
+                    self.bubble_colors)
  452
+            pos = bubble.position
  453
+            pygame.draw.circle(
  454
+                self.screen,
  455
+                bubble.color,
  456
+                scale_and_round(pos.x, pos.y),
  457
+                int(round(bubble.radius * MAP_SIZE)),
  458
+                1)
  459
+        for explosion in m.explosions:
  460
+            pos = explosion.position
  461
+            pygame.draw.circle(
  462
+                self.screen,
  463
+                RED,
  464
+                scale_and_round(pos.x, pos.y),
  465
+                int(round(explosion.radius * MAP_SIZE)),
  466
+                1)
  467
+        for i in m.powerups:
  468
+            self.render_powerup(i)
  469
+
  470
+        self.screen.set_clip(None)
  471
+
  472
+    def render_ship(self):
  473
+        ship = self.model.ship
  474
+        pos = ship.position
  475
+        bbox = pygame.draw.circle(
  476
+            self.screen,
  477
+            SILVER,
  478
+            scale_and_round(pos.x, pos.y),
  479
+            int(round(ship.radius * MAP_SIZE)))
  480
+        pygame.draw.circle(
  481
+            self.screen,
  482
+            BLACK,
  483
+            scale_and_round(pos.x, pos.y),
  484
+            int(round(ship.radius * 0.5 * MAP_SIZE)),
  485
+            1)
  486
+        if self.model.ship_shield_timer > 0:
  487
+            pygame.draw.rect(self.screen, SILVER, bbox, 1)
  488
+
  489
+    def render_bullet(self):
  490
+        bullet = self.model.bullet
  491
+        pos = bullet.position
  492
+        bbox = pygame.draw.circle(
  493
+            self.screen,
  494
+            RED,
  495
+            scale_and_round(pos.x, pos.y),
  496
+            int(round(bullet.radius * MAP_SIZE)))
  497
+        if self.model.bullet_shield_timer > 0:
  498
+            pygame.draw.rect(self.screen, RED, bbox, 1)
  499
+
  500
+    def render_powerup(self, powerup):
  501
+        pos = powerup.position
  502
+        radius = powerup.radius * MAP_SIZE
  503
+        if powerup.kind    == "shield":
  504
+            bbox = pygame.draw.circle(
  505
+                self.screen,
  506
+                WHITE,
  507
+                scale_and_round(pos.x, pos.y),
  508
+                int(round(radius)),
  509
+                1)
  510
+            pygame.draw.rect(self.screen, WHITE, bbox, 1)
  511
+        elif powerup.kind == "bullet":
  512
+            pygame.draw.circle(
  513
+                self.screen,
  514
+                WHITE,
  515
+                scale_and_round(pos.x, pos.y),
  516
+                int(round(radius * 0.3)),
  517
+                1)
  518
+            bbox = pygame.Rect(0, 0, radius * 2, radius * 2)
  519
+            bbox.center = (pos.x * MAP_WIDTH, pos.y * MAP_HEIGHT)
  520
+            pygame.draw.rect(self.screen, WHITE, bbox, 1)
  521
+        elif powerup.kind == "freeze":
  522
+            bbox = pygame.Rect(0, 0, radius * 2, radius * 2)
  523
+            bbox.center = (pos.x * MAP_WIDTH, pos.y * MAP_HEIGHT)
  524
+            pygame.draw.rect(self.screen, WHITE, bbox, 1)
  525
+            bbox.inflate_ip(-radius, -radius)
  526
+            pygame.draw.rect(self.screen, WHITE, bbox, 1)
  527
+            bbox.inflate_ip(-radius * 0.5, -radius * 0.5)
  528
+            pygame.draw.rect(self.screen, WHITE, bbox, 1)
  529
+        else:
  530
+            raise "Bad power-up kind: " + powerup.kind
  531
+
  532
+    def render_pause_text(self):
  533
+        pause_text = self.msg_font.render("Game paused", False, GREEN)
  534
+        self.screen.blit(
  535
+            pause_text,
  536
+            pause_text.get_rect(midbottom = (MAP_HALF_WIDTH, MAP_HEIGHT)))
537 537
 
538 538
 pygame.init()
539 539
 
@@ -547,37 +547,37 @@ def render_pause_text(self):
547 547
 
548 548
 running = True
549 549
 while running:
550  
-	delta_t = clock.tick(60)
551  
-	#renderer.fps = int(round(clock.get_fps()))
552  
-
553  
-	ev = pygame.event.poll()
554  
-	if ev.type == pygame.QUIT:
555  
-		running = False
556  
-	elif ev.type == pygame.KEYUP:
557  
-		if ev.key == pygame.K_ESCAPE:
558  
-			running = False
559  
-		elif ev.key == pygame.K_q:
560  
-			if model.level > 0:
561  
-				model.level = 0
562  
-			else:
563  
-				running = False
564  
-		elif ev.key == pygame.K_p:
565  
-			if model.level == 0:
566  
-				model.score = 0
567  
-				model.lives = 1
568  
-				model.init_level(1)
569  
-			else:
570  
-				renderer.game_paused = not renderer.game_paused
571  
-	elif ev.type == pygame.MOUSEBUTTONDOWN:
572  
-		if model.level > 0 and not renderer.game_paused:
573  
-			x, y = ev.pos
574  
-			model.shoot_at(x / float(MAP_WIDTH), y / float(MAP_HEIGHT))
575  
-			model.thrust_at(x / float(MAP_WIDTH), y / float(MAP_HEIGHT))
576  
-	elif ev.type == pygame.MOUSEBUTTONUP:
577  
-		if model.level > 0:
578  
-			model.stop_thrust()
579  
-
580  
-	# Simulations need the time in seconds, dammit!
581  
-	if model.level > 0 and not renderer.game_paused:
582  
-		model.update(delta_t * 0.001)
583  
-	renderer.render()
  550
+    delta_t = clock.tick(60)
  551
+    #renderer.fps = int(round(clock.get_fps()))
  552
+
  553
+    ev = pygame.event.poll()
  554
+    if ev.type == pygame.QUIT:
  555
+        running = False
  556
+    elif ev.type == pygame.KEYUP:
  557
+        if ev.key == pygame.K_ESCAPE:
  558
+            running = False
  559
+        elif ev.key == pygame.K_q:
  560
+            if model.level > 0:
  561
+                model.level = 0
  562
+            else:
  563
+                running = False
  564
+        elif ev.key == pygame.K_p:
  565
+            if model.level == 0:
  566
+                model.score = 0
  567
+                model.lives = 1
  568
+                model.init_level(1)
  569
+            else:
  570
+                renderer.game_paused = not renderer.game_paused
  571
+    elif ev.type == pygame.MOUSEBUTTONDOWN:
  572
+        if model.level > 0 and not renderer.game_paused:
  573
+            x, y = ev.pos
  574
+            model.shoot_at(x / float(MAP_WIDTH), y / float(MAP_HEIGHT))
  575
+            model.thrust_at(x / float(MAP_WIDTH), y / float(MAP_HEIGHT))
  576
+    elif ev.type == pygame.MOUSEBUTTONUP:
  577
+        if model.level > 0:
  578
+            model.stop_thrust()
  579
+
  580
+    # Simulations need the time in seconds, dammit!
  581
+    if model.level > 0 and not renderer.game_paused:
  582
+        model.update(delta_t * 0.001)
  583
+    renderer.render()

0 notes on commit d2168f9

Please sign in to comment.
Something went wrong with that request. Please try again.