def startNewGame(self): self.playerShip.reset() self.timeLeft = self.max_time self.score = 0 self.enemies = [] self.enemyShotList = [] self.showText1 = True self.text1 = FlashText(self.screen, "3", 40, self.blueColor) self.hasExitedGame = False self.gameMusic.playRandomSong() self.allsprites = pygame.sprite.RenderPlain((self.playerShip)) self.flashTexts = [] self.currentBoss = 0 self.currentPhase = 0 self.isOnBoss = False self.playerName = "" self.acceptKeys = False print "Starting new game"
def draw(self): self.playerShip.draw() removeList = [] for star in self.backgroundBoulders: star.draw() if star.y > self.screenHeight + 50: removeList.append(star) for star in removeList: self.backgroundBoulders.remove(star) if self.showText1: if self.text1.isValid(): self.text1.draw() else: self.text2 = FlashText(self.screen, "2", 40, self.blueColor) self.showText2 = True self.showText1 = False elif self.showText2: if self.text2.isValid(): self.text2.draw() else: self.text3 = FlashText(self.screen, "1", 40, self.blueColor) self.showText3 = True self.showText2 = False elif self.showText3: if self.text3.isValid(): self.text3.draw() else: self.text4 = FlashText(self.screen, "Go!", 40, self.blueColor) self.showText4 = True self.showText3 = False elif self.showText4: if self.text4.isValid(): self.text4.draw() else: self.showText4 = False self.gameRunning = True self.nextPhase() else: if self.gameRunning and not self.hasExitedGame and not self.isOnBoss: if random.randint(0, 120) == 0: self.addBoulder() if random.randint(0, 150) == 0: self.addWeakEnemy() if random.randint(0, 300) == 0: self.addDoubleWeakEnemy() if random.randint(0, 340) == 0: self.addTripleSeekingEnemy() if random.randint(0, 240) == 0: self.addSeekingEnemy() self.screen.blit( self.healthProgress.render(self.playerShip.health), (self.screenWidth - 300, self.screenHeight - 100)) removeList = [] for enemy in self.enemies: enemy.draw() #If enemy is collided with the player if enemy.isPositionInside(self.playerShip.x, self.playerShip.y): self.flashTexts.append( FlashText(self.screen, "BAM!!!", 20, self.blueColor)) removeList.append(enemy) enemy.health = 0 self.playerShip.health -= 10 self.addScore() elif enemy.isPositionInside( self.playerShip.x + self.playerShip.width, self.playerShip.y): self.flashTexts.append( FlashText(self.screen, "BAM!!!", 20, self.blueColor)) removeList.append(enemy) self.playerShip.health -= 10 enemy.health = 0 self.addScore() elif enemy.isPositionInside( self.playerShip.x, self.playerShip.y + self.playerShip.height): self.flashTexts.append( FlashText(self.screen, "BAM!!!", 20, self.blueColor)) removeList.append(enemy) self.playerShip.health -= 10 enemy.health = 0 self.addScore() elif enemy.isPositionInside( self.playerShip.x + self.playerShip.width, self.playerShip.y + self.playerShip.height): self.flashTexts.append( FlashText(self.screen, "BAM!!!", 20, self.blueColor)) removeList.append(enemy) self.playerShip.health -= 10 enemy.health = 0 self.addScore() #If enemy is hit by any player shot. elif self.playerShip.isHitByWeapon(enemy): removeList.append(enemy) enemy.health = 0 self.allsprites.remove(enemy) self.addScore() elif enemy.y > self.screenHeight + 50: removeList.append(enemy) enemy.health = 0 self.allsprites.remove(enemy) for enemy in removeList: self.allsprites.remove(enemy) enemy.health = 0 self.enemies.remove(enemy) shotRemoveList = [] for shot in self.enemyShotList: shot.draw() if self.playerShip.isHitByShot(shot): shotRemoveList.append(shot) for shot in shotRemoveList: self.enemyShotList.remove(shot) if self.playerShip.health < 0: self.onGameFinished() timeLeftLabel = self.scoreFont.render("Next boss in: 0", 1, (255, 255, 255)) if self.isOnBoss: if self.boss.isDead(): self.bossKilled() self.allsprites.remove(self.boss) else: self.boss.draw() if self.playerShip.checkBossHit(self.boss): self.boss.setIsHitByWeapon(1) if self.timeLeft != 0: self.timeLeft = self.timeLeft - 1 timeLeftLabel = self.scoreFont.render( "Time left: " + str(self.timeLeft / 60), 1, (255, 255, 255)) self.screen.blit(timeLeftLabel, (self.screenWidth - 200, 80)) elif self.timeLeft <= 0 and not self.isOnBoss: self.startBossEvent() timeLeftLabel = self.scoreFont.render("Kill boss!", 1, (255, 255, 255)) self.screen.blit(timeLeftLabel, (self.screenWidth - 200, 80)) self.allsprites.draw(self.screen) self.allsprites.update() if random.randint(0, 12) == 0: self.addBackgroundStar() self.scoreLabel = self.scoreFont.render("Score: " + str(self.score), 1, (255, 255, 255)) self.screen.blit(self.scoreLabel, (self.screenWidth - 175, 30)) if self.acceptKeys: enterNameLabel = self.enterNameFont.render( "You entered the highscore! Name: " + str(self.playerName) + "_", 1, (255, 255, 255)) self.screen.blit(enterNameLabel, (self.screenWidth / 2 - 300, 600)) for text in self.flashTexts: if text.isValid(): text.draw() else: self.flashTexts.remove(text)