Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Replaced the magic numbers for the map and window dimensions with con…

…stant variables.
  • Loading branch information...
commit e1834e155712e02300297b979769ce010d673f4b 1 parent 0d0a7e9
Al Sweigart authored August 07, 2012

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

  1. 62  square-shooter/square-shooter_makeover.py
62  square-shooter/square-shooter_makeover.py
@@ -32,11 +32,26 @@
32 32
 SILVER = (204, 204, 204)
33 33
 WHITE  = (255, 255, 255)
34 34
 
  35
+WINDOW_WIDTH = 640
  36
+WINDOW_HEIGHT = 480
  37
+
  38
+MAP_WIDTH = 480
  39
+MAP_HEIGHT = 480
  40
+
  41
+MAP_SIZE = max(MAP_WIDTH, MAP_HEIGHT)
  42
+
  43
+MAP_QUARTER_WIDTH       = int(MAP_WIDTH / 4.0)
  44
+MAP_HALF_WIDTH          = int(MAP_WIDTH / 2.0)
  45
+MAP_THREE_QUARTER_WIDTH = int(3 * MAP_WIDTH / 4.0)
  46
+
  47
+MAP_QUARTER_HEIGHT       = int(MAP_HEIGHT / 4.0)
  48
+MAP_HALF_HEIGHT          = int(MAP_HEIGHT / 2.0)
  49
+MAP_THREE_QUARTER_HEIGHT = int(3 * MAP_HEIGHT / 4.0)
  50
+
35 51
 
36 52
 def scale_and_round(x, y):
37  
-	x = int(round(x * 480))
38  
-	y = int(round(y * 480))
39  
-	return x, y
  53
+	return int(round(x * MAP_WIDTH)), int(round(y * MAP_HEIGHT))
  54
+
40 55
 
41 56
 class Vector2D:
42 57
 	def __init__(self, x, y):
@@ -390,7 +405,7 @@ def render(self):
390 405
 
391 406
 	def render_backround(self):
392 407
 		self.bglayer.fill(BLACK)
393  
-		self.bglayer.fill(GREEN, (480, 0, 160, 480))
  408
+		self.bglayer.fill(GREEN, (MAP_WIDTH, 0, 160, MAP_HEIGHT))
394 409
 
395 410
 		msg = ["Level", "Lives", "Score"]
396 411
 		for i in range(3):
@@ -404,26 +419,27 @@ def render_backround(self):
404 419
 
405 420
 	def render_title_screen(self):
406 421
 		text = self.hud_font.render("SQUARE", False, GREEN)
407  
-		self.screen.blit(text, text.get_rect(midbottom = (240, 240)))
  422
+		self.screen.blit(text, text.get_rect(midbottom = (MAP_HALF_WIDTH, MAP_HALF_HEIGHT)))
408 423
 		text = self.hud_font.render("SHOOTER", False, GREEN)
409  
-		self.screen.blit(text, text.get_rect(midtop = (240, 240)))
  424
+		self.screen.blit(text, text.get_rect(midtop = (MAP_HALF_WIDTH, MAP_HALF_HEIGHT)))
410 425
 
411 426
 		text = self.msg_font.render("FCP", False, GREEN)
412  
-		self.screen.blit(text, text.get_rect(midbottom = (240, 120)))
  427
+		self.screen.blit(text, text.get_rect(midbottom = (MAP_HALF_WIDTH, MAP_QUARTER_HEIGHT)))
413 428
 		text = self.msg_font.render("presents", False, GREEN)
414  
-		self.screen.blit(text, text.get_rect(midtop = (240, 120)))
  429
+		self.screen.blit(text, text.get_rect(midtop = (MAP_HALF_WIDTH, MAP_QUARTER_HEIGHT)))
415 430
 
416 431
 		high_score = "High score: " + str(self.model.high_score)
417 432
 		text = self.msg_font.render(high_score, False, GREEN)
418  
-		self.screen.blit(text, text.get_rect(midbottom = (240, 360)))
  433
+		self.screen.blit(text, text.get_rect(midbottom = (MAP_HALF_WIDTH, MAP_THREE_QUARTER_HEIGHT)))
  434
+
419 435
 		max_level = "Max level: " + str(self.model.max_level)
420 436
 		text = self.msg_font.render(max_level, False, GREEN)
421  
-		self.screen.blit(text, text.get_rect(midtop = (240, 360)))
  437
+		self.screen.blit(text, text.get_rect(midtop = (MAP_HALF_WIDTH, MAP_THREE_QUARTER_HEIGHT)))
422 438
 
423 439
 	def render_game_world(self):
424 440
 		m = self.model
425 441
 
426  
-		self.screen.set_clip((0, 0, 480, 480))
  442
+		self.screen.set_clip((0, 0, MAP_WIDTH, MAP_HEIGHT))
427 443
 
428 444
 		if m.ship != None: self.render_ship()
429 445
 		if m.bullet != None: self.render_bullet()
@@ -437,7 +453,7 @@ def render_game_world(self):
437 453
 				self.screen,
438 454
 				bubble.color,
439 455
 				scale_and_round(pos.x, pos.y),
440  
-				int(round(bubble.radius * 480)),
  456
+				int(round(bubble.radius * MAP_SIZE)),
441 457
 				1)
442 458
 		for explosion in m.explosions:
443 459
 			pos = explosion.position
@@ -445,7 +461,7 @@ def render_game_world(self):
445 461
 				self.screen,
446 462
 				RED,
447 463
 				scale_and_round(pos.x, pos.y),
448  
-				int(round(explosion.radius * 480)),
  464
+				int(round(explosion.radius * MAP_SIZE)),
449 465
 				1)
450 466
 		for i in m.powerups:
451 467
 			self.render_powerup(i)
@@ -459,12 +475,12 @@ def render_ship(self):
459 475
 			self.screen,
460 476
 			SILVER,
461 477
 			scale_and_round(pos.x, pos.y),
462  
-			int(round(ship.radius * 480)))
  478
+			int(round(ship.radius * MAP_SIZE)))
463 479
 		pygame.draw.circle(
464 480
 			self.screen,
465 481
 			BLACK,
466 482
 			scale_and_round(pos.x, pos.y),
467  
-			int(round(ship.radius * 0.5 * 480)),
  483
+			int(round(ship.radius * 0.5 * MAP_SIZE)),
468 484
 			1)
469 485
 		if self.model.ship_shield_timer > 0:
470 486
 			pygame.draw.rect(self.screen, SILVER, bbox, 1)
@@ -476,13 +492,13 @@ def render_bullet(self):
476 492
 			self.screen,
477 493
 			RED,
478 494
 			scale_and_round(pos.x, pos.y),
479  
-			int(round(bullet.radius * 480)))
  495
+			int(round(bullet.radius * MAP_SIZE)))
480 496
 		if self.model.bullet_shield_timer > 0:
481 497
 			pygame.draw.rect(self.screen, RED, bbox, 1)
482 498
 
483 499
 	def render_powerup(self, powerup):
484 500
 		pos = powerup.position
485  
-		radius = powerup.radius * 480
  501
+		radius = powerup.radius * MAP_SIZE
486 502
 		if powerup.kind	== "shield":
487 503
 			bbox = pygame.draw.circle(
488 504
 				self.screen,
@@ -499,11 +515,11 @@ def render_powerup(self, powerup):
499 515
 				int(round(radius * 0.3)),
500 516
 				1)
501 517
 			bbox = pygame.Rect(0, 0, radius * 2, radius * 2)
502  
-			bbox.center = (pos.x * 480, pos.y * 480)
  518
+			bbox.center = (pos.x * MAP_WIDTH, pos.y * MAP_HEIGHT)
503 519
 			pygame.draw.rect(self.screen, WHITE, bbox, 1)
504 520
 		elif powerup.kind == "freeze":
505 521
 			bbox = pygame.Rect(0, 0, radius * 2, radius * 2)
506  
-			bbox.center = (pos.x * 480, pos.y * 480)
  522
+			bbox.center = (pos.x * MAP_WIDTH, pos.y * MAP_HEIGHT)
507 523
 			pygame.draw.rect(self.screen, WHITE, bbox, 1)
508 524
 			bbox.inflate_ip(-radius, -radius)
509 525
 			pygame.draw.rect(self.screen, WHITE, bbox, 1)
@@ -516,11 +532,11 @@ def render_pause_text(self):
516 532
 		pause_text = self.msg_font.render("Game paused", False, GREEN)
517 533
 		self.screen.blit(
518 534
 			pause_text,
519  
-			pause_text.get_rect(midbottom = (240, 480)))
  535
+			pause_text.get_rect(midbottom = (MAP_HALF_WIDTH, MAP_HEIGHT)))
520 536
 
521 537
 pygame.init()
522 538
 
523  
-screen = pygame.display.set_mode((640, 480))
  539
+screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
524 540
 clock = pygame.time.Clock()
525 541
 model = GameWorld()
526 542
 renderer = GameScreen(model, screen)
@@ -554,8 +570,8 @@ def render_pause_text(self):
554 570
 	elif ev.type == pygame.MOUSEBUTTONDOWN:
555 571
 		if model.level > 0 and not renderer.game_paused:
556 572
 			x, y = ev.pos
557  
-			model.shoot_at(x / 480.0, y / 480.0)
558  
-			model.thrust_at(x / 480.0, y / 480.0)
  573
+			model.shoot_at(x / float(MAP_WIDTH), y / float(MAP_HEIGHT))
  574
+			model.thrust_at(x / float(MAP_WIDTH), y / float(MAP_HEIGHT))
559 575
 	elif ev.type == pygame.MOUSEBUTTONUP:
560 576
 		if model.level > 0:
561 577
 			model.stop_thrust()

0 notes on commit e1834e1

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