Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Aguiar <alanjas@hotmail.com>2014-01-07 23:42:53 (GMT)
committer Alan Aguiar <alanjas@hotmail.com>2014-01-07 23:42:53 (GMT)
commit81ab3d83e4357d4f562b95644d5a431c6054e25d (patch)
tree6471715b94a37e5922c1325285c0269e1be152cf
parent817c417e60e6a3371b7e7a6bb658e6fcd3f4daef (diff)
add level and score labels
-rwxr-xr-xSpaceWar.py31
1 files changed, 27 insertions, 4 deletions
diff --git a/SpaceWar.py b/SpaceWar.py
index cfb488c..ae0a9bd 100755
--- a/SpaceWar.py
+++ b/SpaceWar.py
@@ -26,12 +26,18 @@ import itertools
import pygame
import random
+from gettext import gettext as _
+
+POINTS_ENEMY = 1000
+POINTS_SHOT = 100
+
class SpaceWar():
"""Top-level application class."""
def __init__(self):
self.count = 0
self.level = 1
+ self.points = 0
def load_all(self):
# Dun dun duuuuuuuun
@@ -42,7 +48,9 @@ class SpaceWar():
if not(self.screen):
info = pygame.display.Info()
size = (info.current_w, info.current_h)
- self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
+ size = (800, 600)
+ self.screen = pygame.display.set_mode(size) #, pygame.FULLSCREEN)
+ pygame.display.set_caption('SpaceWar')
self.rect = self.screen.get_rect()
# Core objects
@@ -57,6 +65,12 @@ class SpaceWar():
pygame.mixer.init()
self.shot_sound = pygame.mixer.Sound('sounds/shot.wav')
+ # game labels
+ pygame.font.init()
+ self._font = pygame.font.Font(None, 40)
+ self.level_msg = self._font.render(_('Level: %s') % 1, 1, (255, 255, 255))
+ self.points_msg = self._font.render(_('Score: %s') % 0, 1, (255, 255, 255))
+
# Game objects
self.ship = Ship(self)
self.ship.rect.midbottom = self.rect.midbottom
@@ -69,7 +83,7 @@ class SpaceWar():
for l in range(level):
enemy = Enemy(self)
x = random.randint(0, self.rect[2])
- y = random.randint(20, self.rect[3] - 100)
+ y = random.randint(50, self.rect[3] - 100)
enemy.rect.center = x, y
enemy.direction = random.choice((-1 , 1))
self.enemies.add(enemy)
@@ -93,6 +107,7 @@ class SpaceWar():
elif evt.key == pygame.K_RIGHT:
self.ship.direction = 1
elif evt.key == pygame.K_SPACE:
+ self.points = self.points - POINTS_SHOT
new_shot = Shot(self)
new_shot.rect.midbottom = self.ship.rect.midtop
self.shots.add(new_shot)
@@ -107,7 +122,10 @@ class SpaceWar():
self.shots.update(delta)
self.enemies.update(delta)
# Look for shot-enemy collisions
- pygame.sprite.groupcollide(self.enemies, self.shots, True, True)
+ d = pygame.sprite.groupcollide(self.enemies, self.shots, True, True)
+
+ for ship in d:
+ self.points = self.points + POINTS_ENEMY
if len(self.enemies) == 0:
self.count = self.count + 1
@@ -115,10 +133,15 @@ class SpaceWar():
if self.count > 10:
self.count = 0
self.level = self.level + 1
+ self.level_msg = self._font.render(_('Level: %s') % self.level, 1, (255, 255, 255))
self.add_enemies(self.level)
+
+ self.points_msg = self._font.render(_('Score: %s') % self.points, 1, (255, 255, 255))
# Display phase
self.screen.fill((0,0,0))
+ self.screen.blit(self.points_msg, (self.rect[2] - 200, 10))
+ self.screen.blit(self.level_msg, (10, 10))
for spr in self.enemies:
self.screen.blit(spr.image, spr.rect, spr.source_rect)
self.shots.draw(self.screen)
@@ -154,7 +177,7 @@ class Shot(pygame.sprite.Sprite):
# Load and play the shot sound
self.sound = parent.shot_sound
- self.sound.play()
+ #self.sound.play()
def update(self, delta):
self.rect.move_ip(0, -1*delta*self.speed)