Skip to content

Commit

Permalink
Refactor reset n lives
Browse files Browse the repository at this point in the history
* Create main text once on init
* Resetting once on key up
* Lives wo counter (lifeX.alive() n kill())
  • Loading branch information
TiggerKitty committed Jan 13, 2019
1 parent b6bec5b commit 1387c2c
Showing 1 changed file with 44 additions and 66 deletions.
110 changes: 44 additions & 66 deletions spaceinvaders.py
Expand Up @@ -312,7 +312,7 @@ def __init__(self, xpos, ypos):
self.image = transform.scale(self.image, (23, 23))
self.rect = self.image.get_rect(topleft=(xpos, ypos))

def update(self, keys, *args):
def update(self, *args):
game.screen.blit(self.image, self.rect)


Expand All @@ -332,6 +332,7 @@ def __init__(self):
# ALSA lib pcm.c:7963:(snd_pcm_recover) underrun occurred
mixer.pre_init(44100, -16, 1, 4096)
init()
self.clock = time.Clock()
self.caption = display.set_caption('Space Invaders')
self.screen = SCREEN
self.background = image.load(IMAGE_PATH + 'background.jpg').convert()
Expand All @@ -340,32 +341,41 @@ def __init__(self):
self.gameOver = False
# Counter for enemy starting position (increased each new round)
self.enemyPosition = ENEMY_DEFAULT_POSITION
self.titleText = Text(FONT, 50, 'Space Invaders', WHITE, 164, 155)
self.titleText2 = Text(FONT, 25, 'Press any key to continue', WHITE,
201, 225)
self.gameOverText = Text(FONT, 50, 'Game Over', WHITE, 250, 270)
self.nextRoundText = Text(FONT, 50, 'Next Round', WHITE, 240, 270)
self.enemy1Text = Text(FONT, 25, ' = 10 pts', GREEN, 368, 270)
self.enemy2Text = Text(FONT, 25, ' = 20 pts', BLUE, 368, 320)
self.enemy3Text = Text(FONT, 25, ' = 30 pts', PURPLE, 368, 370)
self.enemy4Text = Text(FONT, 25, ' = ?????', RED, 368, 420)
self.scoreText = Text(FONT, 20, 'Score', WHITE, 5, 5)
self.livesText = Text(FONT, 20, 'Lives ', WHITE, 640, 5)

self.life1 = Life(715, 3)
self.life2 = Life(742, 3)
self.life3 = Life(769, 3)
self.livesGroup = sprite.Group(self.life1, self.life2, self.life3)

def reset(self, score, lives, newGame=False):
def reset(self, score):
self.player = Ship()
self.playerGroup = sprite.Group(self.player)
self.explosionsGroup = sprite.Group()
self.bullets = sprite.Group()
self.mysteryShip = Mystery()
self.mysteryGroup = sprite.Group(self.mysteryShip)
self.enemyBullets = sprite.Group()
self.reset_lives(lives)
self.make_enemies()
# Only create blockers on a new game, not a new round
if newGame:
self.allBlockers = sprite.Group(self.make_blockers(0),
self.make_blockers(1),
self.make_blockers(2),
self.make_blockers(3))
self.allSprites = sprite.Group(self.player, self.enemies,
self.livesGroup, self.mysteryShip)
self.keys = key.get_pressed()
self.clock = time.Clock()

self.timer = time.get_ticks()
self.noteTimer = time.get_ticks()
self.shipTimer = time.get_ticks()
self.score = score
self.lives = lives
self.create_audio()
self.create_text()
self.makeNewShip = False
self.shipAlive = True

Expand All @@ -379,22 +389,6 @@ def make_blockers(self, number):
blockerGroup.add(blocker)
return blockerGroup

def reset_lives_sprites(self):
self.life1 = Life(715, 3)
self.life2 = Life(742, 3)
self.life3 = Life(769, 3)

if self.lives == 3:
self.livesGroup = sprite.Group(self.life1, self.life2, self.life3)
elif self.lives == 2:
self.livesGroup = sprite.Group(self.life1, self.life2)
elif self.lives == 1:
self.livesGroup = sprite.Group(self.life1)

def reset_lives(self, lives):
self.lives = lives
self.reset_lives_sprites()

def create_audio(self):
self.sounds = {}
for sound_name in ['shoot', 'shoot2', 'invaderkilled', 'mysterykilled',
Expand All @@ -421,19 +415,6 @@ def play_main_music(self, currentTime):
self.note.play()
self.noteTimer += self.enemies.moveTime

def create_text(self):
self.titleText = Text(FONT, 50, 'Space Invaders', WHITE, 164, 155)
self.titleText2 = Text(FONT, 25, 'Press any key to continue', WHITE,
201, 225)
self.gameOverText = Text(FONT, 50, 'Game Over', WHITE, 250, 270)
self.nextRoundText = Text(FONT, 50, 'Next Round', WHITE, 240, 270)
self.enemy1Text = Text(FONT, 25, ' = 10 pts', GREEN, 368, 270)
self.enemy2Text = Text(FONT, 25, ' = 20 pts', BLUE, 368, 320)
self.enemy3Text = Text(FONT, 25, ' = 30 pts', PURPLE, 368, 370)
self.enemy4Text = Text(FONT, 25, ' = ?????', RED, 368, 420)
self.scoreText = Text(FONT, 20, 'Score', WHITE, 5, 5)
self.livesText = Text(FONT, 20, 'Lives ', WHITE, 640, 5)

@staticmethod
def should_exit(evt):
# type: (pygame.event.EventType) -> bool
Expand Down Expand Up @@ -476,8 +457,6 @@ def make_enemies(self):
enemies.add(enemy)

self.enemies = enemies
self.allSprites = sprite.Group(self.player, self.enemies,
self.livesGroup, self.mysteryShip)

def make_enemies_shoot(self):
if (time.get_ticks() - self.timer) > 700 and self.enemies:
Expand Down Expand Up @@ -515,13 +494,6 @@ def create_main_menu(self):
self.screen.blit(self.enemy3, (318, 370))
self.screen.blit(self.enemy4, (299, 420))

for e in event.get():
if self.should_exit(e):
sys.exit()
if e.type == KEYUP:
self.startGame = True
self.mainScreen = False

def check_collisions(self):
sprite.groupcollide(self.bullets, self.enemyBullets, True, True)

Expand All @@ -544,19 +516,13 @@ def check_collisions(self):

for player in sprite.groupcollide(self.playerGroup, self.enemyBullets,
True, True).keys():
if self.lives == 3:
self.lives -= 1
self.livesGroup.remove(self.life3)
self.allSprites.remove(self.life3)
elif self.lives == 2:
self.lives -= 1
self.livesGroup.remove(self.life2)
self.allSprites.remove(self.life2)
elif self.lives == 1:
self.lives -= 1
self.livesGroup.remove(self.life1)
self.allSprites.remove(self.life1)
elif self.lives == 0:
if self.life3.alive():
self.life3.kill()
elif self.life2.alive():
self.life2.kill()
elif self.life1.alive():
self.life1.kill()
else:
self.gameOver = True
self.startGame = False
self.sounds['shipexplosion'].play()
Expand Down Expand Up @@ -605,7 +571,6 @@ def create_game_over(self, currentTime):
def main(self):
while True:
if self.mainScreen:
self.reset(0, 3, True)
self.screen.blit(self.background, (0, 0))
self.titleText.draw(self.screen)
self.titleText2.draw(self.screen)
Expand All @@ -614,6 +579,19 @@ def main(self):
self.enemy3Text.draw(self.screen)
self.enemy4Text.draw(self.screen)
self.create_main_menu()
for e in event.get():
if self.should_exit(e):
sys.exit()
if e.type == KEYUP:
# Only create blockers on a new game, not a new round
self.allBlockers = sprite.Group(self.make_blockers(0),
self.make_blockers(1),
self.make_blockers(2),
self.make_blockers(3))
self.livesGroup.add(self.life1, self.life2, self.life3)
self.reset(0)
self.startGame = True
self.mainScreen = False

elif self.startGame:
if not self.enemies and not self.explosionsGroup:
Expand All @@ -626,12 +604,12 @@ def main(self):
self.scoreText2.draw(self.screen)
self.nextRoundText.draw(self.screen)
self.livesText.draw(self.screen)
self.livesGroup.update(self.keys)
self.livesGroup.update()
self.check_input()
if currentTime - self.gameTimer > 3000:
# Move enemies closer to bottom
self.enemyPosition += ENEMY_MOVE_DOWN
self.reset(self.score, self.lives)
self.reset(self.score)
self.gameTimer += 3000
else:
currentTime = time.get_ticks()
Expand Down

0 comments on commit 1387c2c

Please sign in to comment.